aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTheodore Ts'o <tytso@mit.edu>2021-02-23 21:25:46 -0500
committerTheodore Ts'o <tytso@mit.edu>2021-02-23 21:25:46 -0500
commit40ea4628ba1b55f8eba311f12399d039698dbeeb (patch)
treeeddfb76d37889a5e99b939df671fc046678c950a
parent7fc56dd147fe7f1a4427f8caed10866e0255eea3 (diff)
downloade2fsprogs-40ea4628ba1b55f8eba311f12399d039698dbeeb.tar.gz
libe2p: skip unneeded lstat(2) in fgetflags() and fsetflags()
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
-rw-r--r--lib/e2p/fgetflags.c40
-rw-r--r--lib/e2p/fsetflags.c35
2 files changed, 41 insertions, 34 deletions
diff --git a/lib/e2p/fgetflags.c b/lib/e2p/fgetflags.c
index 7b93cbae3..b2f7bf9c7 100644
--- a/lib/e2p/fgetflags.c
+++ b/lib/e2p/fgetflags.c
@@ -39,11 +39,14 @@
#include "e2p.h"
-#ifdef O_LARGEFILE
-#define OPEN_FLAGS (O_RDONLY|O_NONBLOCK|O_LARGEFILE)
-#else
-#define OPEN_FLAGS (O_RDONLY|O_NONBLOCK)
+#ifndef O_LARGEFILE
+#define O_LARGEFILE 0
#endif
+#ifndef O_NOFOLLOW
+#define O_NOFOLLOW 0
+#endif
+
+#define OPEN_FLAGS (O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_NOFOLLOW)
int fgetflags (const char * name, unsigned long * flags)
{
@@ -72,29 +75,30 @@ int fgetflags (const char * name, unsigned long * flags)
#if HAVE_EXT2_IOCTLS
int fd, r, f, save_errno = 0;
- if (!lstat(name, &buf) &&
- !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode)) {
- goto notsupp;
- }
#if !APPLE_DARWIN
- fd = open (name, OPEN_FLAGS);
- if (fd == -1)
+ fd = open(name, OPEN_FLAGS);
+ if (fd == -1) {
+ if (errno == ELOOP || errno == ENXIO)
+ errno = EOPNOTSUPP;
return -1;
- r = ioctl (fd, EXT2_IOC_GETFLAGS, &f);
- if (r == -1)
+ }
+ r = ioctl(fd, EXT2_IOC_GETFLAGS, &f);
+ if (r == -1) {
+ if (errno == ENOTTY)
+ errno = EOPNOTSUPP;
save_errno = errno;
+ }
*flags = f;
- close (fd);
+ close(fd);
if (save_errno)
errno = save_errno;
return r;
#else /* APPLE_DARWIN */
- f = -1;
- save_errno = syscall(SYS_fsctl, name, EXT2_IOC_GETFLAGS, &f, 0);
- *flags = f;
- return (save_errno);
+ f = -1;
+ save_errno = syscall(SYS_fsctl, name, EXT2_IOC_GETFLAGS, &f, 0);
+ *flags = f;
+ return (save_errno);
#endif /* !APPLE_DARWIN */
-notsupp:
#endif /* HAVE_EXT2_IOCTLS */
#endif
errno = EOPNOTSUPP;
diff --git a/lib/e2p/fsetflags.c b/lib/e2p/fsetflags.c
index 027834b96..795042baf 100644
--- a/lib/e2p/fsetflags.c
+++ b/lib/e2p/fsetflags.c
@@ -48,11 +48,14 @@
#undef HAVE_CHFLAGS
#endif
-#ifdef O_LARGEFILE
-#define OPEN_FLAGS (O_RDONLY|O_NONBLOCK|O_LARGEFILE)
-#else
-#define OPEN_FLAGS (O_RDONLY|O_NONBLOCK)
+#ifndef O_LARGEFILE
+#define O_LARGEFILE 0
#endif
+#ifndef O_NOFOLLOW
+#define O_NOFOLLOW 0
+#endif
+
+#define OPEN_FLAGS (O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_NOFOLLOW)
int fsetflags (const char * name, unsigned long flags)
{
@@ -78,28 +81,28 @@ int fsetflags (const char * name, unsigned long flags)
int fd, r, f, save_errno = 0;
struct stat buf;
- if (!lstat(name, &buf) &&
- !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode)) {
- goto notsupp;
- }
#if !APPLE_DARWIN
- fd = open (name, OPEN_FLAGS);
- if (fd == -1)
+ fd = open(name, OPEN_FLAGS);
+ if (fd == -1) {
+ if (errno == ELOOP || errno == ENXIO)
+ errno = EOPNOTSUPP;
return -1;
+ }
f = (int) flags;
- r = ioctl (fd, EXT2_IOC_SETFLAGS, &f);
- if (r == -1)
+ r = ioctl(fd, EXT2_IOC_SETFLAGS, &f);
+ if (r == -1) {
+ if (errno == ENOTTY)
+ errno = EOPNOTSUPP;
save_errno = errno;
- close (fd);
+ }
+ close(fd);
if (save_errno)
errno = save_errno;
+ return r;
#else /* APPLE_DARWIN */
f = (int) flags;
return syscall(SYS_fsctl, name, EXT2_IOC_SETFLAGS, &f, 0);
#endif /* !APPLE_DARWIN */
- return r;
-
-notsupp:
#endif /* HAVE_EXT2_IOCTLS */
#endif
errno = EOPNOTSUPP;