1 /* 2 * fgetflags.c - Get a file flags on an ext2 file system 3 * 4 * Copyright (C) 1993, 1994 Remy Card <card@masi.ibp.fr> 5 * Laboratoire MASI, Institut Blaise Pascal 6 * Universite Pierre et Marie Curie (Paris VI) 7 * 8 * %Begin-Header% 9 * This file may be redistributed under the terms of the GNU Library 10 * General Public License, version 2. 11 * %End-Header% 12 */ 13 14 /* 15 * History: 16 * 93/10/30 - Creation 17 */ 18 19 #ifndef _LARGEFILE_SOURCE 20 #define _LARGEFILE_SOURCE 21 #endif 22 #ifndef _LARGEFILE64_SOURCE 23 #define _LARGEFILE64_SOURCE 24 #endif 25 26 #include "config.h" 27 #if HAVE_ERRNO_H 28 #include <errno.h> 29 #endif 30 #if HAVE_UNISTD_H 31 #include <unistd.h> 32 #endif 33 #include <sys/types.h> 34 #include <sys/stat.h> 35 #if HAVE_EXT2_IOCTLS 36 #include <fcntl.h> 37 #include <sys/ioctl.h> 38 #endif 39 40 #include "e2p.h" 41 42 #ifdef O_LARGEFILE 43 #define OPEN_FLAGS (O_RDONLY|O_NONBLOCK|O_LARGEFILE) 44 #else 45 #define OPEN_FLAGS (O_RDONLY|O_NONBLOCK) 46 #endif 47 48 int fgetflags (const char * name, unsigned long * flags) 49 { 50 struct stat buf; 51 #if HAVE_STAT_FLAGS && !(APPLE_DARWIN && HAVE_EXT2_IOCTLS) 52 53 if (stat (name, &buf) == -1) 54 return -1; 55 56 *flags = 0; 57 #ifdef UF_IMMUTABLE 58 if (buf.st_flags & UF_IMMUTABLE) 59 *flags |= EXT2_IMMUTABLE_FL; 60 #endif 61 #ifdef UF_APPEND 62 if (buf.st_flags & UF_APPEND) 63 *flags |= EXT2_APPEND_FL; 64 #endif 65 #ifdef UF_NODUMP 66 if (buf.st_flags & UF_NODUMP) 67 *flags |= EXT2_NODUMP_FL; 68 #endif 69 70 return 0; 71 #else /* !HAVE_STAT_FLAGS || (APPLE_DARWIN && HAVE_EXT2_IOCTLS) */ 72 #if HAVE_EXT2_IOCTLS 73 int fd, r, f, save_errno = 0; 74 75 if (!lstat(name, &buf) && 76 !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode)) { 77 goto notsupp; 78 } 79 #if !APPLE_DARWIN 80 fd = open (name, OPEN_FLAGS); 81 if (fd == -1) 82 return -1; 83 r = ioctl (fd, EXT2_IOC_GETFLAGS, &f); 84 if (r == -1) 85 save_errno = errno; 86 *flags = f; 87 close (fd); 88 if (save_errno) 89 errno = save_errno; 90 return r; 91 #else /* APPLE_DARWIN */ 92 f = -1; 93 save_errno = syscall(SYS_fsctl, name, EXT2_IOC_GETFLAGS, &f, 0); 94 *flags = f; 95 return (save_errno); 96 #endif /* !APPLE_DARWIN */ 97 notsupp: 98 #endif /* HAVE_EXT2_IOCTLS */ 99 #endif 100 errno = EOPNOTSUPP; 101 return -1; 102 } 103