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