btrfs-progs: add helper for copying paths
Check the source path length and do the copy. Signed-off-by: David Sterba <dsterba@suse.cz>
This commit is contained in:
parent
230ab9376c
commit
6bfa3cae05
2 changed files with 20 additions and 0 deletions
19
utils.c
19
utils.c
|
@ -2871,3 +2871,22 @@ int btrfs_check_nodesize(u32 nodesize, u32 sectorsize)
|
|||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Copy a path argument from SRC to DEST and check the SRC length if it's at
|
||||
* most PATH_MAX and fits into DEST. DESTLEN is supposed to be exact size of
|
||||
* the buffer.
|
||||
* The destination buffer is zero terminated.
|
||||
* Return < 0 for error, 0 otherwise.
|
||||
*/
|
||||
int arg_copy_path(char *dest, const char *src, int destlen)
|
||||
{
|
||||
size_t len = strlen(src);
|
||||
|
||||
if (len >= PATH_MAX || len >= destlen)
|
||||
return -ENAMETOOLONG;
|
||||
|
||||
__strncpy__null(dest, src, destlen);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
1
utils.h
1
utils.h
|
@ -135,6 +135,7 @@ int get_mountpt(char *dev, char *mntpt, size_t size);
|
|||
u64 parse_size(char *s);
|
||||
u64 parse_qgroupid(const char *p);
|
||||
u64 arg_strtou64(const char *str);
|
||||
int arg_copy_path(char *dest, const char *src, int destlen);
|
||||
int open_file_or_dir(const char *fname, DIR **dirstream);
|
||||
int open_file_or_dir3(const char *fname, DIR **dirstream, int open_flags);
|
||||
void close_file_or_dir(int fd, DIR *dirstream);
|
||||
|
|
Loading…
Reference in a new issue