Home | History | Annotate | Download | only in squashfs_utils
      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 #define ERROR(x...)   KLOG_ERROR("squashfs_utils", x)
     29 
     30 int squashfs_parse_sb(char *blk_device, struct squashfs_info *info) {
     31     int ret = 0;
     32     struct squashfs_super_block sb;
     33     int data_device;
     34 
     35     data_device = TEMP_FAILURE_RETRY(open(blk_device, O_RDONLY | O_CLOEXEC));
     36     if (data_device == -1) {
     37         ERROR("Error opening block device (%s)\n", strerror(errno));
     38         return -1;
     39     }
     40 
     41     if (TEMP_FAILURE_RETRY(read(data_device, &sb, sizeof(sb)))
     42             != sizeof(sb)) {
     43         ERROR("Error reading superblock\n");
     44         ret = -1;
     45         goto cleanup;
     46     }
     47     if (sb.s_magic != SQUASHFS_MAGIC) {
     48         ERROR("Not a valid squashfs filesystem\n");
     49         ret = -1;
     50         goto cleanup;
     51     }
     52 
     53     info->block_size = sb.block_size;
     54     info->inodes = sb.inodes;
     55     info->bytes_used = sb.bytes_used;
     56     // by default mksquashfs pads the filesystem to 4K blocks
     57     info->bytes_used_4K_padded =
     58         sb.bytes_used + (4096 - (sb.bytes_used & (4096 - 1)));
     59 
     60 cleanup:
     61     close(data_device);
     62     return ret;
     63 }
     64