Snap for 8139020 from 480ad2a56bd9780e697ce68160aeaf1f43f7de79 to tm-release
Change-Id: Iaeb695073572e9cbc0375f98bc5ed03745cc2298
diff --git a/configure.ac b/configure.ac
index 6fdb0e4..6f7e271 100644
--- a/configure.ac
+++ b/configure.ac
@@ -183,7 +183,8 @@
strerror
strrchr
strtoull
- tmpfile64]))
+ tmpfile64
+ utimensat]))
# Configure debug mode
AS_IF([test "x$enable_debug" != "xno"], [], [
diff --git a/dump/main.c b/dump/main.c
index 71b44b4..bb1bd7f 100644
--- a/dump/main.c
+++ b/dump/main.c
@@ -161,8 +161,10 @@
}
}
- if (optind >= argc)
+ if (optind >= argc) {
+ erofs_err("missing argument: IMAGE");
return -EINVAL;
+ }
cfg.c_img_path = strdup(argv[optind++]);
if (!cfg.c_img_path)
diff --git a/fsck/main.c b/fsck/main.c
index aefa881..e669b44 100644
--- a/fsck/main.c
+++ b/fsck/main.c
@@ -6,39 +6,85 @@
#include <stdlib.h>
#include <getopt.h>
#include <time.h>
+#include <utime.h>
+#include <unistd.h>
+#include <sys/stat.h>
#include "erofs/print.h"
#include "erofs/io.h"
+#include "erofs/compress.h"
#include "erofs/decompress.h"
+#include "erofs/dir.h"
-static void erofs_check_inode(erofs_nid_t pnid, erofs_nid_t nid);
+static int erofsfsck_check_inode(erofs_nid_t pnid, erofs_nid_t nid);
struct erofsfsck_cfg {
+ u64 physical_blocks;
+ u64 logical_blocks;
+ char *extract_path;
+ size_t extract_pos;
+ mode_t umask;
+ bool superuser;
bool corrupted;
bool print_comp_ratio;
bool check_decomp;
- u64 physical_blocks;
- u64 logical_blocks;
+ bool force;
+ bool overwrite;
+ bool preserve_owner;
+ bool preserve_perms;
};
static struct erofsfsck_cfg fsckcfg;
static struct option long_options[] = {
{"help", no_argument, 0, 1},
- {"extract", no_argument, 0, 2},
+ {"extract", optional_argument, 0, 2},
{"device", required_argument, 0, 3},
+ {"force", no_argument, 0, 4},
+ {"overwrite", no_argument, 0, 5},
+ {"preserve", no_argument, 0, 6},
+ {"preserve-owner", no_argument, 0, 7},
+ {"preserve-perms", no_argument, 0, 8},
+ {"no-preserve", no_argument, 0, 9},
+ {"no-preserve-owner", no_argument, 0, 10},
+ {"no-preserve-perms", no_argument, 0, 11},
{0, 0, 0, 0},
};
+static void print_available_decompressors(FILE *f, const char *delim)
+{
+ unsigned int i = 0;
+ const char *s;
+
+ while ((s = z_erofs_list_available_compressors(i)) != NULL) {
+ if (i++)
+ fputs(delim, f);
+ fputs(s, f);
+ }
+ fputc('\n', f);
+}
+
static void usage(void)
{
fputs("usage: [options] IMAGE\n\n"
- "Check erofs filesystem integrity of IMAGE, and [options] are:\n"
- " -V print the version number of fsck.erofs and exit.\n"
- " -d# set output message level to # (maximum 9)\n"
- " -p print total compression ratio of all files\n"
- " --device=X specify an extra device to be used together\n"
- " --extract check if all files are well encoded\n"
- " --help display this help and exit.\n",
- stderr);
+ "Check erofs filesystem compatibility and integrity of IMAGE, and [options] are:\n"
+ " -V print the version number of fsck.erofs and exit\n"
+ " -d# set output message level to # (maximum 9)\n"
+ " -p print total compression ratio of all files\n"
+ " --device=X specify an extra device to be used together\n"
+ " --extract[=X] check if all files are well encoded, optionally extract to X\n"
+ " --help display this help and exit\n"
+ "\nExtraction options (--extract=X is required):\n"
+ " --force allow extracting to root\n"
+ " --overwrite overwrite files that already exist\n"
+ " --preserve extract with the same ownership and permissions as on the filesystem\n"
+ " (default for superuser)\n"
+ " --preserve-owner extract with the same ownership as on the filesystem\n"
+ " --preserve-perms extract with the same permissions as on the filesystem\n"
+ " --no-preserve extract as yourself and apply user's umask on permissions\n"
+ " (default for ordinary users)\n"
+ " --no-preserve-owner extract as yourself\n"
+ " --no-preserve-perms apply user's umask when extracting permissions\n"
+ "\nSupported algorithms are: ", stderr);
+ print_available_decompressors(stderr, ", ");
}
static void erofsfsck_print_version(void)
@@ -49,6 +95,7 @@
static int erofsfsck_parse_options_cfg(int argc, char **argv)
{
int opt, ret;
+ bool has_opt_preserve = false;
while ((opt = getopt_long(argc, argv, "Vd:p",
long_options, NULL)) != -1) {
@@ -72,6 +119,28 @@
exit(0);
case 2:
fsckcfg.check_decomp = true;
+ if (optarg) {
+ size_t len = strlen(optarg);
+
+ if (len == 0) {
+ erofs_err("empty value given for --extract=X");
+ return -EINVAL;
+ }
+
+ /* remove trailing slashes except root */
+ while (len > 1 && optarg[len - 1] == '/')
+ len--;
+
+ fsckcfg.extract_path = malloc(PATH_MAX);
+ if (!fsckcfg.extract_path)
+ return -ENOMEM;
+ strncpy(fsckcfg.extract_path, optarg, len);
+ fsckcfg.extract_path[len] = '\0';
+ /* if path is root, start writing from position 0 */
+ if (len == 1 && fsckcfg.extract_path[0] == '/')
+ len = 0;
+ fsckcfg.extract_pos = len;
+ }
break;
case 3:
ret = blob_open_ro(optarg);
@@ -79,13 +148,65 @@
return ret;
++sbi.extra_devices;
break;
+ case 4:
+ fsckcfg.force = true;
+ break;
+ case 5:
+ fsckcfg.overwrite = true;
+ break;
+ case 6:
+ fsckcfg.preserve_owner = fsckcfg.preserve_perms = true;
+ has_opt_preserve = true;
+ break;
+ case 7:
+ fsckcfg.preserve_owner = true;
+ has_opt_preserve = true;
+ break;
+ case 8:
+ fsckcfg.preserve_perms = true;
+ has_opt_preserve = true;
+ break;
+ case 9:
+ fsckcfg.preserve_owner = fsckcfg.preserve_perms = false;
+ has_opt_preserve = true;
+ break;
+ case 10:
+ fsckcfg.preserve_owner = false;
+ has_opt_preserve = true;
+ break;
+ case 11:
+ fsckcfg.preserve_perms = false;
+ has_opt_preserve = true;
+ break;
default:
return -EINVAL;
}
}
- if (optind >= argc)
+ if (fsckcfg.extract_path) {
+ if (!fsckcfg.extract_pos && !fsckcfg.force) {
+ erofs_err("--extract=/ must be used together with --force");
+ return -EINVAL;
+ }
+ } else {
+ if (fsckcfg.force) {
+ erofs_err("--force must be used together with --extract=X");
+ return -EINVAL;
+ }
+ if (fsckcfg.overwrite) {
+ erofs_err("--overwrite must be used together with --extract=X");
+ return -EINVAL;
+ }
+ if (has_opt_preserve) {
+ erofs_err("--[no-]preserve[-owner/-perms] must be used together with --extract=X");
+ return -EINVAL;
+ }
+ }
+
+ if (optind >= argc) {
+ erofs_err("missing argument: IMAGE");
return -EINVAL;
+ }
cfg.c_img_path = strdup(argv[optind++]);
if (!cfg.c_img_path)
@@ -98,6 +219,43 @@
return 0;
}
+static void erofsfsck_set_attributes(struct erofs_inode *inode, char *path)
+{
+ int ret;
+
+ /* don't apply attributes when fsck is used without extraction */
+ if (!fsckcfg.extract_path)
+ return;
+
+#ifdef HAVE_UTIMENSAT
+ if (utimensat(AT_FDCWD, path, (struct timespec []) {
+ [0] = { .tv_sec = inode->i_ctime,
+ .tv_nsec = inode->i_ctime_nsec },
+ [1] = { .tv_sec = inode->i_ctime,
+ .tv_nsec = inode->i_ctime_nsec },
+ }, AT_SYMLINK_NOFOLLOW) < 0)
+#else
+ if (utime(path, &((struct utimbuf){.actime = inode->i_ctime,
+ .modtime = inode->i_ctime})) < 0)
+#endif
+ erofs_warn("failed to set times: %s", path);
+
+ if (!S_ISLNK(inode->i_mode)) {
+ if (fsckcfg.preserve_perms)
+ ret = chmod(path, inode->i_mode);
+ else
+ ret = chmod(path, inode->i_mode & ~fsckcfg.umask);
+ if (ret < 0)
+ erofs_warn("failed to set permissions: %s", path);
+ }
+
+ if (fsckcfg.preserve_owner) {
+ ret = lchown(path, inode->i_uid, inode->i_gid);
+ if (ret < 0)
+ erofs_warn("failed to change ownership: %s", path);
+ }
+}
+
static int erofs_check_sb_chksum(void)
{
int ret;
@@ -125,252 +283,6 @@
return 0;
}
-static bool check_special_dentry(struct erofs_dirent *de,
- unsigned int de_namelen, erofs_nid_t nid,
- erofs_nid_t pnid)
-{
- if (de_namelen == 2 && de->nid != pnid) {
- erofs_err("wrong parent dir nid(%llu): pnid(%llu) @ nid(%llu)",
- de->nid | 0ULL, pnid | 0ULL, nid | 0ULL);
- return false;
- }
-
- if (de_namelen == 1 && de->nid != nid) {
- erofs_err("wrong current dir nid(%llu) @ nid(%llu)",
- de->nid | 0ULL, nid | 0ULL);
- return false;
- }
- return true;
-}
-
-static int traverse_dirents(erofs_nid_t pnid, erofs_nid_t nid,
- void *dentry_blk, erofs_blk_t block,
- unsigned int next_nameoff, unsigned int maxsize)
-{
- struct erofs_dirent *de = dentry_blk;
- const struct erofs_dirent *end = dentry_blk + next_nameoff;
- unsigned int idx = 0;
- char *prev_name = NULL, *cur_name = NULL;
- int ret = 0;
-
- erofs_dbg("traversing pnid(%llu), nid(%llu)", pnid | 0ULL, nid | 0ULL);
-
- if (!block && (next_nameoff < 2 * sizeof(struct erofs_dirent))) {
- erofs_err("too small dirents of size(%d) in nid(%llu)",
- next_nameoff, nid | 0ULL);
- return -EFSCORRUPTED;
- }
-
- while (de < end) {
- const char *de_name;
- unsigned int de_namelen;
- unsigned int nameoff;
-
- nameoff = le16_to_cpu(de->nameoff);
- de_name = (char *)dentry_blk + nameoff;
-
- /* the last dirent check */
- if (de + 1 >= end)
- de_namelen = strnlen(de_name, maxsize - nameoff);
- else
- de_namelen = le16_to_cpu(de[1].nameoff) - nameoff;
-
- if (prev_name)
- free(prev_name);
- prev_name = cur_name;
- cur_name = strndup(de_name, de_namelen);
- if (!cur_name) {
- ret = -ENOMEM;
- goto out;
- }
-
- erofs_dbg("traversed filename(%s)", cur_name);
-
- /* corrupted entry check */
- if (nameoff != next_nameoff) {
- erofs_err("bogus dirent with nameoff(%u): expected(%d) @ nid %llu, block %u, idx %u",
- nameoff, next_nameoff, nid | 0ULL,
- block, idx);
- ret = -EFSCORRUPTED;
- goto out;
- }
-
- if (nameoff + de_namelen > maxsize ||
- de_namelen > EROFS_NAME_LEN) {
- erofs_err("bogus dirent with namelen(%u) @ nid %llu, block %u, idx %u",
- de_namelen, nid | 0ULL, block, idx);
- ret = -EFSCORRUPTED;
- goto out;
- }
-
- if (prev_name && (strcmp(prev_name, cur_name) >= 0)) {
- erofs_err("wrong dirent name order @ nid %llu block %u idx %u: prev(%s), cur(%s)",
- nid | 0ULL, block, idx,
- prev_name, cur_name);
- ret = -EFSCORRUPTED;
- goto out;
- }
-
- if (is_dot_dotdot(cur_name)) {
- if (!check_special_dentry(de, de_namelen, nid, pnid)) {
- ret = -EFSCORRUPTED;
- goto out;
- }
- } else {
- erofs_check_inode(nid, de->nid);
- }
-
- if (fsckcfg.corrupted) {
- ret = -EFSCORRUPTED;
- goto out;
- }
-
- next_nameoff += de_namelen;
- ++de;
- ++idx;
- }
-
-out:
- if (prev_name)
- free(prev_name);
- if (cur_name)
- free(cur_name);
-
- erofs_dbg("traversing ... done nid(%llu)", nid | 0ULL);
- return ret;
-}
-
-static int verify_uncompressed_inode(struct erofs_inode *inode)
-{
- struct erofs_map_blocks map = {
- .index = UINT_MAX,
- };
- int ret;
- erofs_off_t ptr = 0;
- u64 i_blocks = DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ);
-
- while (ptr < inode->i_size) {
- map.m_la = ptr;
- ret = erofs_map_blocks(inode, &map, 0);
- if (ret)
- return ret;
-
- if (map.m_plen != map.m_llen || ptr != map.m_la) {
- erofs_err("broken data chunk layout m_la %" PRIu64 " ptr %" PRIu64 " m_llen %" PRIu64 " m_plen %" PRIu64,
- map.m_la, ptr, map.m_llen, map.m_plen);
- return -EFSCORRUPTED;
- }
-
- if (!(map.m_flags & EROFS_MAP_MAPPED) && !map.m_llen) {
- /* reached EOF */
- ptr = inode->i_size;
- continue;
- }
-
- ptr += map.m_llen;
- }
-
- if (fsckcfg.print_comp_ratio) {
- fsckcfg.logical_blocks += i_blocks;
- fsckcfg.physical_blocks += i_blocks;
- }
-
- return 0;
-}
-
-static int verify_compressed_inode(struct erofs_inode *inode)
-{
- struct erofs_map_blocks map = {
- .index = UINT_MAX,
- };
- struct erofs_map_dev mdev;
- int ret = 0;
- u64 pchunk_len = 0;
- erofs_off_t end = inode->i_size;
- unsigned int raw_size = 0, buffer_size = 0;
- char *raw = NULL, *buffer = NULL;
-
- while (end > 0) {
- map.m_la = end - 1;
-
- ret = z_erofs_map_blocks_iter(inode, &map, 0);
- if (ret)
- goto out;
-
- if (end > map.m_la + map.m_llen) {
- erofs_err("broken compressed chunk layout m_la %" PRIu64 " m_llen %" PRIu64 " end %" PRIu64,
- map.m_la, map.m_llen, end);
- ret = -EFSCORRUPTED;
- goto out;
- }
-
- pchunk_len += map.m_plen;
- end = map.m_la;
-
- if (!fsckcfg.check_decomp || !(map.m_flags & EROFS_MAP_MAPPED))
- continue;
-
- if (map.m_plen > raw_size) {
- raw_size = map.m_plen;
- raw = realloc(raw, raw_size);
- BUG_ON(!raw);
- }
-
- if (map.m_llen > buffer_size) {
- buffer_size = map.m_llen;
- buffer = realloc(buffer, buffer_size);
- BUG_ON(!buffer);
- }
-
- mdev = (struct erofs_map_dev) {
- .m_deviceid = map.m_deviceid,
- .m_pa = map.m_pa,
- };
- ret = erofs_map_dev(&sbi, &mdev);
- if (ret) {
- erofs_err("failed to map device of m_pa %" PRIu64 ", m_deviceid %u @ nid %llu: %d",
- map.m_pa, map.m_deviceid, inode->nid | 0ULL, ret);
- goto out;
- }
-
- ret = dev_read(mdev.m_deviceid, raw, mdev.m_pa, map.m_plen);
- if (ret < 0) {
- erofs_err("failed to read compressed data of m_pa %" PRIu64 ", m_plen %" PRIu64 " @ nid %llu: %d",
- mdev.m_pa, map.m_plen, inode->nid | 0ULL, ret);
- goto out;
- }
-
- ret = z_erofs_decompress(&(struct z_erofs_decompress_req) {
- .in = raw,
- .out = buffer,
- .decodedskip = 0,
- .inputsize = map.m_plen,
- .decodedlength = map.m_llen,
- .alg = map.m_algorithmformat,
- .partial_decoding = 0
- });
-
- if (ret < 0) {
- erofs_err("failed to decompress data of m_pa %" PRIu64 ", m_plen %" PRIu64 " @ nid %llu: %d",
- mdev.m_pa, map.m_plen, inode->nid | 0ULL, ret);
- goto out;
- }
- }
-
- if (fsckcfg.print_comp_ratio) {
- fsckcfg.logical_blocks +=
- DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ);
- fsckcfg.physical_blocks +=
- DIV_ROUND_UP(pchunk_len, EROFS_BLKSIZ);
- }
-out:
- if (raw)
- free(raw);
- if (buffer)
- free(buffer);
- return ret < 0 ? ret : 0;
-}
-
static int erofs_verify_xattr(struct erofs_inode *inode)
{
unsigned int xattr_hdr_size = sizeof(struct erofs_xattr_ibody_header);
@@ -449,9 +361,18 @@
return ret;
}
-static int erofs_verify_inode_data(struct erofs_inode *inode)
+static int erofs_verify_inode_data(struct erofs_inode *inode, int outfd)
{
- int ret;
+ struct erofs_map_blocks map = {
+ .index = UINT_MAX,
+ };
+ struct erofs_map_dev mdev;
+ int ret = 0;
+ bool compressed;
+ erofs_off_t pos = 0;
+ u64 pchunk_len = 0;
+ unsigned int raw_size = 0, buffer_size = 0;
+ char *raw = NULL, *buffer = NULL;
erofs_dbg("verify data chunk of nid(%llu): type(%d)",
inode->nid | 0ULL, inode->datalayout);
@@ -460,30 +381,295 @@
case EROFS_INODE_FLAT_PLAIN:
case EROFS_INODE_FLAT_INLINE:
case EROFS_INODE_CHUNK_BASED:
- ret = verify_uncompressed_inode(inode);
+ compressed = false;
break;
case EROFS_INODE_FLAT_COMPRESSION_LEGACY:
case EROFS_INODE_FLAT_COMPRESSION:
- ret = verify_compressed_inode(inode);
+ compressed = true;
break;
default:
- ret = -EINVAL;
- break;
+ erofs_err("unknown datalayout");
+ return -EINVAL;
}
- if (ret == -EIO)
- erofs_err("I/O error occurred when verifying data chunk of nid(%llu)",
- inode->nid | 0ULL);
+ while (pos < inode->i_size) {
+ map.m_la = pos;
+ if (compressed)
+ ret = z_erofs_map_blocks_iter(inode, &map,
+ EROFS_GET_BLOCKS_FIEMAP);
+ else
+ ret = erofs_map_blocks(inode, &map,
+ EROFS_GET_BLOCKS_FIEMAP);
+ if (ret)
+ goto out;
+ if (!compressed && map.m_llen != map.m_plen) {
+ erofs_err("broken chunk length m_la %" PRIu64 " m_llen %" PRIu64 " m_plen %" PRIu64,
+ map.m_la, map.m_llen, map.m_plen);
+ ret = -EFSCORRUPTED;
+ goto out;
+ }
+
+ /* the last lcluster can be divided into 3 parts */
+ if (map.m_la + map.m_llen > inode->i_size)
+ map.m_llen = inode->i_size - map.m_la;
+
+ pchunk_len += map.m_plen;
+ pos += map.m_llen;
+
+ /* should skip decomp? */
+ if (!(map.m_flags & EROFS_MAP_MAPPED) || !fsckcfg.check_decomp)
+ continue;
+
+ if (map.m_plen > raw_size) {
+ raw_size = map.m_plen;
+ raw = realloc(raw, raw_size);
+ BUG_ON(!raw);
+ }
+
+ mdev = (struct erofs_map_dev) {
+ .m_deviceid = map.m_deviceid,
+ .m_pa = map.m_pa,
+ };
+ ret = erofs_map_dev(&sbi, &mdev);
+ if (ret) {
+ erofs_err("failed to map device of m_pa %" PRIu64 ", m_deviceid %u @ nid %llu: %d",
+ map.m_pa, map.m_deviceid, inode->nid | 0ULL,
+ ret);
+ goto out;
+ }
+
+ if (compressed && map.m_llen > buffer_size) {
+ buffer_size = map.m_llen;
+ buffer = realloc(buffer, buffer_size);
+ BUG_ON(!buffer);
+ }
+
+ ret = dev_read(mdev.m_deviceid, raw, mdev.m_pa, map.m_plen);
+ if (ret < 0) {
+ erofs_err("failed to read data of m_pa %" PRIu64 ", m_plen %" PRIu64 " @ nid %llu: %d",
+ mdev.m_pa, map.m_plen, inode->nid | 0ULL,
+ ret);
+ goto out;
+ }
+
+ if (compressed) {
+ struct z_erofs_decompress_req rq = {
+ .in = raw,
+ .out = buffer,
+ .decodedskip = 0,
+ .inputsize = map.m_plen,
+ .decodedlength = map.m_llen,
+ .alg = map.m_algorithmformat,
+ .partial_decoding = 0
+ };
+
+ ret = z_erofs_decompress(&rq);
+ if (ret < 0) {
+ erofs_err("failed to decompress data of m_pa %" PRIu64 ", m_plen %" PRIu64 " @ nid %llu: %s",
+ mdev.m_pa, map.m_plen,
+ inode->nid | 0ULL, strerror(-ret));
+ goto out;
+ }
+ }
+
+ if (outfd >= 0 && write(outfd, compressed ? buffer : raw,
+ map.m_llen) < 0) {
+ erofs_err("I/O error occurred when verifying data chunk @ nid %llu",
+ inode->nid | 0ULL);
+ ret = -EIO;
+ goto out;
+ }
+ }
+
+ if (fsckcfg.print_comp_ratio) {
+ fsckcfg.logical_blocks +=
+ DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ);
+ fsckcfg.physical_blocks +=
+ DIV_ROUND_UP(pchunk_len, EROFS_BLKSIZ);
+ }
+out:
+ if (raw)
+ free(raw);
+ if (buffer)
+ free(buffer);
+ return ret < 0 ? ret : 0;
+}
+
+static inline int erofs_extract_dir(struct erofs_inode *inode)
+{
+ int ret;
+
+ erofs_dbg("create directory %s", fsckcfg.extract_path);
+
+ /* verify data chunk layout */
+ ret = erofs_verify_inode_data(inode, -1);
+ if (ret)
+ return ret;
+
+ /*
+ * Make directory with default user rwx permissions rather than
+ * the permissions from the filesystem, as these may not have
+ * write/execute permission. These are fixed up later in
+ * erofsfsck_set_attributes().
+ */
+ if (mkdir(fsckcfg.extract_path, 0700) < 0) {
+ struct stat st;
+
+ if (errno != EEXIST) {
+ erofs_err("failed to create directory: %s (%s)",
+ fsckcfg.extract_path, strerror(errno));
+ return -errno;
+ }
+
+ if (lstat(fsckcfg.extract_path, &st) ||
+ !S_ISDIR(st.st_mode)) {
+ erofs_err("path is not a directory: %s",
+ fsckcfg.extract_path);
+ return -ENOTDIR;
+ }
+
+ /*
+ * Try to change permissions of existing directory so
+ * that we can write to it
+ */
+ if (chmod(fsckcfg.extract_path, 0700) < 0) {
+ erofs_err("failed to set permissions: %s (%s)",
+ fsckcfg.extract_path, strerror(errno));
+ return -errno;
+ }
+ }
+ return 0;
+}
+
+static inline int erofs_extract_file(struct erofs_inode *inode)
+{
+ bool tryagain = true;
+ int ret, fd;
+
+ erofs_dbg("extract file to path: %s", fsckcfg.extract_path);
+
+again:
+ fd = open(fsckcfg.extract_path,
+ O_WRONLY | O_CREAT | O_NOFOLLOW |
+ (fsckcfg.overwrite ? O_TRUNC : O_EXCL), 0700);
+ if (fd < 0) {
+ if (fsckcfg.overwrite && tryagain) {
+ if (errno == EISDIR) {
+ erofs_warn("try to forcely remove directory %s",
+ fsckcfg.extract_path);
+ if (rmdir(fsckcfg.extract_path) < 0) {
+ erofs_err("failed to remove: %s (%s)",
+ fsckcfg.extract_path, strerror(errno));
+ return -EISDIR;
+ }
+ } else if (errno == EACCES &&
+ chmod(fsckcfg.extract_path, 0700) < 0) {
+ erofs_err("failed to set permissions: %s (%s)",
+ fsckcfg.extract_path, strerror(errno));
+ return -errno;
+ }
+ tryagain = false;
+ goto again;
+ }
+ erofs_err("failed to open: %s (%s)", fsckcfg.extract_path,
+ strerror(errno));
+ return -errno;
+ }
+
+ /* verify data chunk layout */
+ ret = erofs_verify_inode_data(inode, fd);
+ if (ret)
+ return ret;
+
+ if (close(fd))
+ return -errno;
return ret;
}
-static void erofs_check_inode(erofs_nid_t pnid, erofs_nid_t nid)
+static inline int erofs_extract_symlink(struct erofs_inode *inode)
+{
+ bool tryagain = true;
+ int ret;
+ char *buf = NULL;
+
+ erofs_dbg("extract symlink to path: %s", fsckcfg.extract_path);
+
+ /* verify data chunk layout */
+ ret = erofs_verify_inode_data(inode, -1);
+ if (ret)
+ return ret;
+
+ buf = malloc(inode->i_size + 1);
+ if (!buf) {
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ ret = erofs_pread(inode, buf, inode->i_size, 0);
+ if (ret) {
+ erofs_err("I/O error occurred when reading symlink @ nid %llu: %d",
+ inode->nid | 0ULL, ret);
+ goto out;
+ }
+
+ buf[inode->i_size] = '\0';
+again:
+ if (symlink(buf, fsckcfg.extract_path) < 0) {
+ if (errno == EEXIST && fsckcfg.overwrite && tryagain) {
+ erofs_warn("try to forcely remove file %s",
+ fsckcfg.extract_path);
+ if (unlink(fsckcfg.extract_path) < 0) {
+ erofs_err("failed to remove: %s",
+ fsckcfg.extract_path);
+ ret = -errno;
+ goto out;
+ }
+ tryagain = false;
+ goto again;
+ }
+ erofs_err("failed to create symlink: %s",
+ fsckcfg.extract_path);
+ ret = -errno;
+ }
+out:
+ if (buf)
+ free(buf);
+ return ret;
+}
+
+static int erofsfsck_dirent_iter(struct erofs_dir_context *ctx)
+{
+ int ret;
+ size_t prev_pos = fsckcfg.extract_pos;
+
+ if (ctx->dot_dotdot)
+ return 0;
+
+ if (fsckcfg.extract_path) {
+ size_t curr_pos = prev_pos;
+
+ fsckcfg.extract_path[curr_pos++] = '/';
+ strncpy(fsckcfg.extract_path + curr_pos, ctx->dname,
+ ctx->de_namelen);
+ curr_pos += ctx->de_namelen;
+ fsckcfg.extract_path[curr_pos] = '\0';
+ fsckcfg.extract_pos = curr_pos;
+ }
+
+ ret = erofsfsck_check_inode(ctx->dir->nid, ctx->de_nid);
+
+ if (fsckcfg.extract_path) {
+ fsckcfg.extract_path[prev_pos] = '\0';
+ fsckcfg.extract_pos = prev_pos;
+ }
+ return ret;
+}
+
+static int erofsfsck_check_inode(erofs_nid_t pnid, erofs_nid_t nid)
{
int ret;
struct erofs_inode inode;
- char buf[EROFS_BLKSIZ];
- erofs_off_t offset;
erofs_dbg("check inode: nid(%llu)", nid | 0ULL);
@@ -501,49 +687,48 @@
if (ret)
goto out;
- /* verify data chunk layout */
- ret = erofs_verify_inode_data(&inode);
+ if (fsckcfg.extract_path) {
+ switch (inode.i_mode & S_IFMT) {
+ case S_IFDIR:
+ ret = erofs_extract_dir(&inode);
+ break;
+ case S_IFREG:
+ ret = erofs_extract_file(&inode);
+ break;
+ case S_IFLNK:
+ ret = erofs_extract_symlink(&inode);
+ break;
+ default:
+ /* TODO */
+ goto verify;
+ }
+ } else {
+verify:
+ /* verify data chunk layout */
+ ret = erofs_verify_inode_data(&inode, -1);
+ }
if (ret)
goto out;
- if ((inode.i_mode & S_IFMT) != S_IFDIR)
- goto out;
+ /* XXXX: the dir depth should be restricted in order to avoid loops */
+ if (S_ISDIR(inode.i_mode)) {
+ struct erofs_dir_context ctx = {
+ .flags = EROFS_READDIR_VALID_PNID,
+ .pnid = pnid,
+ .dir = &inode,
+ .cb = erofsfsck_dirent_iter,
+ };
- offset = 0;
- while (offset < inode.i_size) {
- erofs_blk_t block = erofs_blknr(offset);
- erofs_off_t maxsize = min_t(erofs_off_t,
- inode.i_size - offset, EROFS_BLKSIZ);
- struct erofs_dirent *de = (void *)buf;
-
- unsigned int nameoff;
-
- ret = erofs_pread(&inode, buf, maxsize, offset);
- if (ret) {
- erofs_err("I/O error occurred when reading dirents @ nid %llu, block %u: %d",
- nid | 0ULL, block, ret);
- goto out;
- }
-
- nameoff = le16_to_cpu(de->nameoff);
- if (nameoff < sizeof(struct erofs_dirent) ||
- nameoff >= PAGE_SIZE) {
- erofs_err("invalid de[0].nameoff %u @ nid %llu block %u: %d",
- nameoff, nid | 0ULL, block, ret);
- ret = -EFSCORRUPTED;
- goto out;
- }
-
- ret = traverse_dirents(pnid, nid, buf, block,
- nameoff, maxsize);
- if (ret)
- goto out;
-
- offset += maxsize;
+ ret = erofs_iterate_dir(&ctx, true);
}
+
+ if (!ret)
+ erofsfsck_set_attributes(&inode, fsckcfg.extract_path);
+
out:
if (ret && ret != -EIO)
fsckcfg.corrupted = true;
+ return ret;
}
int main(int argc, char **argv)
@@ -552,11 +737,19 @@
erofs_init_configure();
+ fsckcfg.physical_blocks = 0;
+ fsckcfg.logical_blocks = 0;
+ fsckcfg.extract_path = NULL;
+ fsckcfg.extract_pos = 0;
+ fsckcfg.umask = umask(0);
+ fsckcfg.superuser = geteuid() == 0;
fsckcfg.corrupted = false;
fsckcfg.print_comp_ratio = false;
fsckcfg.check_decomp = false;
- fsckcfg.logical_blocks = 0;
- fsckcfg.physical_blocks = 0;
+ fsckcfg.force = false;
+ fsckcfg.overwrite = false;
+ fsckcfg.preserve_owner = fsckcfg.superuser;
+ fsckcfg.preserve_perms = fsckcfg.superuser;
err = erofsfsck_parse_options_cfg(argc, argv);
if (err) {
@@ -582,13 +775,19 @@
goto exit_dev_close;
}
- erofs_check_inode(sbi.root_nid, sbi.root_nid);
-
+ err = erofsfsck_check_inode(sbi.root_nid, sbi.root_nid);
if (fsckcfg.corrupted) {
- erofs_err("Found some filesystem corruption");
+ if (!fsckcfg.extract_path)
+ erofs_err("Found some filesystem corruption");
+ else
+ erofs_err("Failed to extract filesystem");
err = -EFSCORRUPTED;
- } else {
- erofs_info("No error found");
+ } else if (!err) {
+ if (!fsckcfg.extract_path)
+ erofs_info("No errors found");
+ else
+ erofs_info("Extracted filesystem successfully");
+
if (fsckcfg.print_comp_ratio) {
double comp_ratio =
(double)fsckcfg.physical_blocks * 100 /
diff --git a/mkerofsimage.sh b/mkerofsimage.sh
index 7ca8110..c449352 100755
--- a/mkerofsimage.sh
+++ b/mkerofsimage.sh
@@ -133,6 +133,8 @@
exit 4
fi
+fsck.erofs --extract $OUTPUT_FILE
+
SPARSE_SUFFIX=".sparse"
if [ "$SPARSE" = true ]; then
img2simg $OUTPUT_FILE $OUTPUT_FILE$SPARSE_SUFFIX
diff --git a/mkfs/main.c b/mkfs/main.c
index 90cedde..3f34450 100644
--- a/mkfs/main.c
+++ b/mkfs/main.c
@@ -375,9 +375,6 @@
}
}
- if (optind >= argc)
- return -EINVAL;
-
if (cfg.c_blobdev_path && cfg.c_chunkbits < LOG_BLOCK_SIZE) {
erofs_err("--blobdev must be used together with --chunksize");
return -EINVAL;
@@ -390,24 +387,29 @@
return -EINVAL;
}
+ if (optind >= argc) {
+ erofs_err("missing argument: FILE");
+ return -EINVAL;
+ }
+
cfg.c_img_path = strdup(argv[optind++]);
if (!cfg.c_img_path)
return -ENOMEM;
if (optind >= argc) {
- erofs_err("Source directory is missing");
+ erofs_err("missing argument: DIRECTORY");
return -EINVAL;
}
cfg.c_src_path = realpath(argv[optind++], NULL);
if (!cfg.c_src_path) {
- erofs_err("Failed to parse source directory: %s",
+ erofs_err("failed to parse source directory: %s",
erofs_strerror(-errno));
return -ENOENT;
}
if (optind < argc) {
- erofs_err("Unexpected argument: %s\n", argv[optind]);
+ erofs_err("unexpected argument: %s\n", argv[optind]);
return -EINVAL;
}
if (quiet)
@@ -450,7 +452,7 @@
buf = calloc(sb_blksize, 1);
if (!buf) {
- erofs_err("Failed to allocate memory for sb: %s",
+ erofs_err("failed to allocate memory for sb: %s",
erofs_strerror(-errno));
return -ENOMEM;
}
@@ -532,7 +534,7 @@
epoch = strtoull(source_date_epoch, &endptr, 10);
if (epoch == -1ULL || *endptr != '\0') {
- erofs_err("Environment variable $SOURCE_DATE_EPOCH %s is invalid",
+ erofs_err("environment variable $SOURCE_DATE_EPOCH %s is invalid",
source_date_epoch);
return -EINVAL;
}
@@ -589,7 +591,7 @@
err = lstat64(cfg.c_src_path, &st);
if (err)
return 1;
- if ((st.st_mode & S_IFMT) != S_IFDIR) {
+ if (!S_ISDIR(st.st_mode)) {
erofs_err("root of the filesystem is not a directory - %s",
cfg.c_src_path);
usage();
@@ -633,34 +635,34 @@
sb_bh = erofs_buffer_init();
if (IS_ERR(sb_bh)) {
err = PTR_ERR(sb_bh);
- erofs_err("Failed to initialize buffers: %s",
+ erofs_err("failed to initialize buffers: %s",
erofs_strerror(err));
goto exit;
}
err = erofs_bh_balloon(sb_bh, EROFS_SUPER_END);
if (err < 0) {
- erofs_err("Failed to balloon erofs_super_block: %s",
+ erofs_err("failed to balloon erofs_super_block: %s",
erofs_strerror(err));
goto exit;
}
err = erofs_load_compress_hints();
if (err) {
- erofs_err("Failed to load compress hints %s",
+ erofs_err("failed to load compress hints %s",
cfg.c_compress_hints_file);
goto exit;
}
err = z_erofs_compress_init(sb_bh);
if (err) {
- erofs_err("Failed to initialize compressor: %s",
+ erofs_err("failed to initialize compressor: %s",
erofs_strerror(err));
goto exit;
}
err = erofs_generate_devtable();
if (err) {
- erofs_err("Failed to generate device table: %s",
+ erofs_err("failed to generate device table: %s",
erofs_strerror(err));
goto exit;
}
@@ -673,7 +675,7 @@
err = erofs_build_shared_xattrs_from_path(cfg.c_src_path);
if (err) {
- erofs_err("Failed to build shared xattrs: %s",
+ erofs_err("failed to build shared xattrs: %s",
erofs_strerror(err));
goto exit;
}