Skip to content

Commit

Permalink
Add fallible lcfs_node_try_set_mode()
Browse files Browse the repository at this point in the history
Previously I added code to validate this but it happened much
later. The mode (type) of a file is a very fundamental property
and we should constrain its state as early as possible.

Signed-off-by: Colin Walters <[email protected]>
  • Loading branch information
cgwalters committed Sep 9, 2024
1 parent ab45b87 commit 9da2423
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
9 changes: 9 additions & 0 deletions libcomposefs/lcfs-writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,15 @@ void lcfs_node_set_mode(struct lcfs_node_s *node, uint32_t mode)
node->inode.st_mode = mode;
}

int lcfs_node_try_set_mode(struct lcfs_node_s *node, uint32_t mode)
{
if (lcfs_validate_mode(mode) < 0) {
return -1;
}
node->inode.st_mode = mode;
return 0;
}

uint32_t lcfs_node_get_uid(struct lcfs_node_s *node)
{
return node->inode.st_uid;
Expand Down
1 change: 1 addition & 0 deletions libcomposefs/lcfs-writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ LCFS_EXTERN struct lcfs_node_s *lcfs_node_get_hardlink_target(struct lcfs_node_s
LCFS_EXTERN bool lcfs_node_dirp(struct lcfs_node_s *node);
LCFS_EXTERN uint32_t lcfs_node_get_mode(struct lcfs_node_s *node);
LCFS_EXTERN void lcfs_node_set_mode(struct lcfs_node_s *node, uint32_t mode);
LCFS_EXTERN int lcfs_node_try_set_mode(struct lcfs_node_s *node, uint32_t mode);
LCFS_EXTERN uint32_t lcfs_node_get_uid(struct lcfs_node_s *node);
LCFS_EXTERN void lcfs_node_set_uid(struct lcfs_node_s *node, uint32_t uid);
LCFS_EXTERN uint32_t lcfs_node_get_gid(struct lcfs_node_s *node);
Expand Down
4 changes: 3 additions & 1 deletion tools/mkcomposefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,9 @@ static char *tree_from_dump_line(dump_info *info, const char *line, size_t line_
if (node == NULL) {
oom();
}
lcfs_node_set_mode(node, mode);
if (lcfs_node_try_set_mode(node, mode) < 0) {
return make_error("Invalid mode %o", (unsigned int)mode);
}

err = tree_add_node(info, path, node);
if (err)
Expand Down

0 comments on commit 9da2423

Please sign in to comment.