squashfs_utils.c revision d640b023a6272852551550df15c2de01c335d43d
1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include "squashfs_utils.h" 18 19#include <cutils/klog.h> 20#include <errno.h> 21#include <fcntl.h> 22#include <stdlib.h> 23#include <string.h> 24#include <unistd.h> 25 26#include "squashfs_fs.h" 27 28#ifdef SQUASHFS_NO_KLOG 29#include <stdio.h> 30#define ERROR(x...) fprintf(stderr, x) 31#else 32#define ERROR(x...) KLOG_ERROR("squashfs_utils", x) 33#endif 34 35size_t squashfs_get_sb_size() 36{ 37 return sizeof(struct squashfs_super_block); 38} 39 40int squashfs_parse_sb_buffer(const void *buf, struct squashfs_info *info) 41{ 42 const struct squashfs_super_block *sb = 43 (const struct squashfs_super_block *)buf; 44 45 if (sb->s_magic != SQUASHFS_MAGIC) { 46 return -1; 47 } 48 49 info->block_size = sb->block_size; 50 info->inodes = sb->inodes; 51 info->bytes_used = sb->bytes_used; 52 // by default mksquashfs pads the filesystem to 4K blocks 53 info->bytes_used_4K_padded = 54 sb->bytes_used + (4096 - (sb->bytes_used & (4096 - 1))); 55 56 return 0; 57} 58 59int squashfs_parse_sb(const char *blk_device, struct squashfs_info *info) 60{ 61 int ret = 0; 62 struct squashfs_super_block sb; 63 int data_device; 64 65 data_device = TEMP_FAILURE_RETRY(open(blk_device, O_RDONLY | O_CLOEXEC)); 66 if (data_device == -1) { 67 ERROR("Error opening block device (%s)\n", strerror(errno)); 68 return -1; 69 } 70 71 if (TEMP_FAILURE_RETRY(read(data_device, &sb, sizeof(sb))) 72 != sizeof(sb)) { 73 ERROR("Error reading superblock\n"); 74 ret = -1; 75 goto cleanup; 76 } 77 78 if (squashfs_parse_sb_buffer(&sb, info) == -1) { 79 ERROR("Not a valid squashfs filesystem\n"); 80 ret = -1; 81 goto cleanup; 82 } 83 84cleanup: 85 close(data_device); 86 return ret; 87} 88