blob: 86bf65d5027eefcbc9c867f78ab97a1fa8bb2a72 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysingerd89e6292005-04-24 05:07:59 +00002/*
3 * fgetflags.c - Get a file flags on an ext2 file system
Mike Frysinger548ffd42005-05-07 07:14:41 +00004 * fsetflags.c - Set a file flags on an ext2 file system
Mike Frysingerd89e6292005-04-24 05:07:59 +00005 *
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 Frysinger548ffd42005-05-07 07:14:41 +000040int fgetsetflags (const char * name, unsigned long * get_flags, unsigned long set_flags)
Mike Frysingerd89e6292005-04-24 05:07:59 +000041{
Mike Frysingerd89e6292005-04-24 05:07:59 +000042#if HAVE_EXT2_IOCTLS
Mike Frysinger548ffd42005-05-07 07:14:41 +000043 struct stat buf;
Mike Frysingerd89e6292005-04-24 05:07:59 +000044 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 Frysingerd89e6292005-04-24 05:07:59 +000050 fd = open (name, OPEN_FLAGS);
51 if (fd == -1)
52 return -1;
Mike Frysinger548ffd42005-05-07 07:14:41 +000053 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 Frysingerd89e6292005-04-24 05:07:59 +000060 if (r == -1)
61 save_errno = errno;
Mike Frysingerd89e6292005-04-24 05:07:59 +000062 close (fd);
63 if (save_errno)
64 errno = save_errno;
65 return r;
Mike Frysingerd89e6292005-04-24 05:07:59 +000066#endif /* HAVE_EXT2_IOCTLS */
Mike Frysingerd89e6292005-04-24 05:07:59 +000067notsupp:
68 errno = EOPNOTSUPP;
69 return -1;
70}