"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 2 | /* |
| 3 | * fgetflags.c - Get a file flags on an ext2 file system |
Mike Frysinger | 548ffd4 | 2005-05-07 07:14:41 +0000 | [diff] [blame] | 4 | * fsetflags.c - Set a file flags on an ext2 file system |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 5 | * |
| 6 | * Copyright (C) 1993, 1994 Remy Card <card@masi.ibp.fr> |
| 7 | * Laboratoire MASI, Institut Blaise Pascal |
| 8 | * Universite Pierre et Marie Curie (Paris VI) |
| 9 | * |
| 10 | * This file can be redistributed under the terms of the GNU Library General |
| 11 | * Public License |
| 12 | */ |
| 13 | |
| 14 | /* |
| 15 | * History: |
| 16 | * 93/10/30 - Creation |
| 17 | */ |
| 18 | |
| 19 | #if HAVE_ERRNO_H |
| 20 | #include <errno.h> |
| 21 | #endif |
| 22 | #if HAVE_UNISTD_H |
| 23 | #include <unistd.h> |
| 24 | #endif |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/stat.h> |
| 27 | #if HAVE_EXT2_IOCTLS |
| 28 | #include <fcntl.h> |
| 29 | #include <sys/ioctl.h> |
| 30 | #endif |
| 31 | |
| 32 | #include "e2p.h" |
| 33 | |
| 34 | #ifdef O_LARGEFILE |
| 35 | #define OPEN_FLAGS (O_RDONLY|O_NONBLOCK|O_LARGEFILE) |
| 36 | #else |
| 37 | #define OPEN_FLAGS (O_RDONLY|O_NONBLOCK) |
| 38 | #endif |
| 39 | |
Mike Frysinger | 548ffd4 | 2005-05-07 07:14:41 +0000 | [diff] [blame] | 40 | int fgetsetflags (const char * name, unsigned long * get_flags, unsigned long set_flags) |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 41 | { |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 42 | #if HAVE_EXT2_IOCTLS |
Mike Frysinger | 548ffd4 | 2005-05-07 07:14:41 +0000 | [diff] [blame] | 43 | struct stat buf; |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 44 | int fd, r, f, save_errno = 0; |
| 45 | |
| 46 | if (!stat(name, &buf) && |
| 47 | !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode)) { |
| 48 | goto notsupp; |
| 49 | } |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 50 | fd = open (name, OPEN_FLAGS); |
| 51 | if (fd == -1) |
| 52 | return -1; |
Mike Frysinger | 548ffd4 | 2005-05-07 07:14:41 +0000 | [diff] [blame] | 53 | if (!get_flags) { |
| 54 | f = (int) set_flags; |
| 55 | r = ioctl (fd, EXT2_IOC_SETFLAGS, &f); |
| 56 | } else { |
| 57 | r = ioctl (fd, EXT2_IOC_GETFLAGS, &f); |
| 58 | *get_flags = f; |
| 59 | } |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 60 | if (r == -1) |
| 61 | save_errno = errno; |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 62 | close (fd); |
| 63 | if (save_errno) |
| 64 | errno = save_errno; |
| 65 | return r; |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 66 | notsupp: |
Denis Vlasenko | 7d219aa | 2006-10-05 10:17:08 +0000 | [diff] [blame] | 67 | #endif /* HAVE_EXT2_IOCTLS */ |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 68 | errno = EOPNOTSUPP; |
| 69 | return -1; |
| 70 | } |