From 6ba1bb6d0245ecfca39224a44e2b3af3b2fef3d3 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Thu, 19 May 2022 12:34:34 +0100 Subject: chattr,lsattr: code shrink Drop fgetsetflags() and move its functionality into chattr and lsattr. Raw file attributes are available from struct stat. In chattr don't compile code related to the unused (on Windows) OPT_SET option. --- e2fsprogs/chattr.c | 26 ++++++++++---------------- e2fsprogs/e2fs_lib.c | 30 ------------------------------ e2fsprogs/e2fs_lib.h | 8 ++++---- e2fsprogs/lsattr.c | 7 ++++++- 4 files changed, 20 insertions(+), 51 deletions(-) diff --git a/e2fsprogs/chattr.c b/e2fsprogs/chattr.c index c1e90d13f..e35b9d223 100644 --- a/e2fsprogs/chattr.c +++ b/e2fsprogs/chattr.c @@ -190,8 +190,8 @@ static int FAST_FUNC chattr_dir_proc(const char *dir_name, struct dirent *de, vo static void change_attributes(const char *name, struct globals *gp) { - unsigned fsflags; #if !ENABLE_PLATFORM_MINGW32 + unsigned fsflags; int fd; #endif struct stat st; @@ -261,22 +261,14 @@ static void change_attributes(const char *name, struct globals *gp) close(fd); } #else /* ENABLE_PLATFORM_MINGW32 */ - if (gp->flags & OPT_SET) { - fsflags = gp->af; - } else { - if (fgetflags(name, &fsflags) != 0) { - bb_perror_msg("reading flags on %s", name); - goto skip_setflags; - } - /*if (gp->flags & OPT_REM) - not needed, rf is zero otherwise */ - fsflags &= ~gp->rf; - /*if (gp->flags & OPT_ADD) - not needed, af is zero otherwise */ - fsflags |= gp->af; - } - if (fsetflags(name, fsflags) != 0) + /*if (gp->flags & OPT_REM) - not needed, rf is zero otherwise */ + st.st_attr &= ~gp->rf; + /*if (gp->flags & OPT_ADD) - not needed, af is zero otherwise */ + st.st_attr |= gp->af; + if (!SetFileAttributes(name, st.st_attr & CHATTR_MASK)) { + errno = err_win_to_posix(); bb_perror_msg("setting flags on %s", name); - - skip_setflags: + } #endif if (gp->recursive && S_ISDIR(st.st_mode)) @@ -307,8 +299,10 @@ int chattr_main(int argc UNUSED_PARAM, char **argv) /* note: on loop exit, remaining argv[] is never empty */ /* run sanity checks on all the arguments given us */ +#if !ENABLE_PLATFORM_MINGW32 if ((g.flags & OPT_SET) && (g.flags & (OPT_ADD|OPT_REM))) bb_simple_error_msg_and_die("= is incompatible with - and +"); +#endif if (g.rf & g.af) bb_simple_error_msg_and_die("can't set and unset a flag"); if (!g.flags) diff --git a/e2fsprogs/e2fs_lib.c b/e2fsprogs/e2fs_lib.c index 48c3b68b5..e40702e78 100644 --- a/e2fsprogs/e2fs_lib.c +++ b/e2fsprogs/e2fs_lib.c @@ -8,36 +8,6 @@ #include "libbb.h" #include "e2fs_lib.h" -#if ENABLE_PLATFORM_MINGW32 -/* Only certain attributes can be set using SetFileAttributes() */ -#define CHATTR_MASK (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | \ - FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE | \ - FILE_ATTRIBUTE_TEMPORARY | FILE_ATTRIBUTE_NOT_CONTENT_INDEXED | \ - FILE_ATTRIBUTE_OFFLINE) - -/* Get/set file attributes on a Windows file system */ -int fgetsetflags(const char *name, unsigned *get_flags, unsigned set_flags) -{ - struct stat buf; - - if (stat(name, &buf) == 0 /* stat is ok */ - && !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode) - ) { - errno = EOPNOTSUPP; - return -1; - } - - if (get_flags) { - *get_flags = (unsigned long)buf.st_attr; - } - else if (!SetFileAttributes(name, set_flags & CHATTR_MASK)) { - errno = err_win_to_posix(); - return -1; - } - return 0; -} -#endif - #if !ENABLE_PLATFORM_MINGW32 /* Print file attributes on an ext2 file system */ const uint32_t e2attr_flags_value[] ALIGN4 = { diff --git a/e2fsprogs/e2fs_lib.h b/e2fsprogs/e2fs_lib.h index aa92e63af..fd6948fc7 100644 --- a/e2fsprogs/e2fs_lib.h +++ b/e2fsprogs/e2fs_lib.h @@ -12,10 +12,10 @@ PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN #if ENABLE_PLATFORM_MINGW32 -/* Get/set a file flags */ -int fgetsetflags(const char *name, unsigned *get_flags, unsigned set_flags); -#define fgetflags(name, flags) fgetsetflags(name, flags, 0) -#define fsetflags(name, flags) fgetsetflags(name, NULL, flags) +/* Only certain attributes can be set using SetFileAttributes() */ +#define CHATTR_MASK (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN | \ + FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE | \ + FILE_ATTRIBUTE_TEMPORARY | FILE_ATTRIBUTE_NOT_CONTENT_INDEXED) #endif /* Print file attributes on an ext2 file system */ diff --git a/e2fsprogs/lsattr.c b/e2fsprogs/lsattr.c index d82861e14..461ce39e6 100644 --- a/e2fsprogs/lsattr.c +++ b/e2fsprogs/lsattr.c @@ -89,8 +89,13 @@ static void list_attributes(const char *name) close(fd); #else /* ENABLE_PLATFORM_MINGW32 */ - if (fgetflags(name, &fsflags) != 0) + struct stat st; + + if (lstat(name, &st) == 0 && !(S_ISREG(st.st_mode) || + S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))) goto read_err; + + fsflags = st.st_attr; #endif if (option_mask32 & OPT_PF_LONG) { -- cgit v1.2.3-55-g6feb