Skip to content

Commit a363b7c

Browse files
adam900710kdave
authored andcommitted
btrfs-progs: mkfs/rootdir: factor out inode flags validation code into a helper
Currently we do the validation of inode flag parameters inside mkfs/main.c, but considering all things like structure rootdir_inode_flags_entry are all inside rootdir.[ch], it's better to move the validation part into rootdir.[ch]. Extract the validation part into a helper, btrfs_mkfs_validate_inode_flags(), into rootdir.[ch]. Signed-off-by: Qu Wenruo <[email protected]>
1 parent 3a3b787 commit a363b7c

File tree

3 files changed

+43
-34
lines changed

3 files changed

+43
-34
lines changed

mkfs/main.c

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,40 +1531,9 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
15311531
if (ret < 0)
15321532
goto error;
15331533

1534-
list_for_each_entry(rif, &inode_flags_list, list) {
1535-
char path[PATH_MAX];
1536-
struct rootdir_inode_flags_entry *rif2;
1537-
1538-
if (path_cat_out(path, source_dir, rif->inode_path)) {
1539-
ret = -EINVAL;
1540-
error("path invalid: %s", path);
1541-
goto error;
1542-
}
1543-
if (!realpath(path, rif->full_path)) {
1544-
ret = -errno;
1545-
error("could not get canonical path: %s: %m", path);
1546-
goto error;
1547-
}
1548-
if (!path_exists(rif->full_path)) {
1549-
ret = -ENOENT;
1550-
error("inode path does not exist: %s", rif->full_path);
1551-
goto error;
1552-
}
1553-
list_for_each_entry(rif2, &inode_flags_list, list) {
1554-
/*
1555-
* Only compare entries before us. So we won't compare
1556-
* the same pair twice.
1557-
*/
1558-
if (rif2 == rif)
1559-
break;
1560-
if (strcmp(rif2->full_path, rif->full_path) == 0) {
1561-
error("duplicated inode flag entries for %s",
1562-
rif->full_path);
1563-
ret = -EEXIST;
1564-
goto error;
1565-
}
1566-
}
1567-
}
1534+
ret = btrfs_mkfs_validate_inode_flags(source_dir, &inode_flags_list);
1535+
if (ret < 0)
1536+
goto error;
15681537

15691538
if (*fs_uuid) {
15701539
uuid_t dummy_uuid;

mkfs/rootdir.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,45 @@ int btrfs_mkfs_validate_subvols(const char *source_dir, struct list_head *subvol
11231123
return 0;
11241124
}
11251125

1126+
int btrfs_mkfs_validate_inode_flags(const char *source_dir, struct list_head *inode_flags)
1127+
{
1128+
struct rootdir_inode_flags_entry *rif;
1129+
1130+
list_for_each_entry(rif, inode_flags, list) {
1131+
char path[PATH_MAX];
1132+
struct rootdir_inode_flags_entry *rif2;
1133+
int ret;
1134+
1135+
if (path_cat_out(path, source_dir, rif->inode_path)) {
1136+
error("path invalid: %s", path);
1137+
return -EINTR;
1138+
}
1139+
if (!realpath(path, rif->full_path)) {
1140+
ret = -errno;
1141+
error("could not get canonical path: %s: %m", path);
1142+
return ret;
1143+
}
1144+
if (!path_exists(rif->full_path)) {
1145+
error("inode path does not exist: %s", rif->full_path);
1146+
return -ENOENT;
1147+
}
1148+
list_for_each_entry(rif2, inode_flags, list) {
1149+
/*
1150+
* Only compare entries before us. So we won't compare
1151+
* the same pair twice.
1152+
*/
1153+
if (rif2 == rif)
1154+
break;
1155+
if (strcmp(rif2->full_path, rif->full_path) == 0) {
1156+
error("duplicated inode flag entries for %s",
1157+
rif->full_path);
1158+
return -EEXIST;
1159+
}
1160+
}
1161+
}
1162+
return 0;
1163+
}
1164+
11261165
static int add_file_items(struct btrfs_trans_handle *trans,
11271166
struct btrfs_root *root,
11281167
struct btrfs_inode_item *btrfs_inode, u64 objectid,

mkfs/rootdir.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ struct rootdir_inode_flags_entry {
6161
};
6262

6363
int btrfs_mkfs_validate_subvols(const char *source_dir, struct list_head *subvols);
64+
int btrfs_mkfs_validate_inode_flags(const char *source_dir, struct list_head *inode_flags);
6465
int btrfs_mkfs_fill_dir(struct btrfs_trans_handle *trans, const char *source_dir,
6566
struct btrfs_root *root, struct list_head *subvols,
6667
struct list_head *inode_flags_list,

0 commit comments

Comments
 (0)