blob: 0a9f5359abc6b8d0a14a0b34aa1473e1fcb831ce [file] [log] [blame]
Mike Frysingerd89e6292005-04-24 05:07:59 +00001/*
2 * fgetflags.c - Get a file flags on an ext2 file system
Mike Frysinger548ffd42005-05-07 07:14:41 +00003 * fsetflags.c - Set a file flags on an ext2 file system
Mike Frysingerd89e6292005-04-24 05:07:59 +00004 *
5 * Copyright (C) 1993, 1994 Remy Card <card@masi.ibp.fr>
6 * Laboratoire MASI, Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
8 *
9 * This file can be redistributed under the terms of the GNU Library General
10 * Public License
11 */
12
13/*
14 * History:
15 * 93/10/30 - Creation
16 */
17
18#if HAVE_ERRNO_H
19#include <errno.h>
20#endif
21#if HAVE_UNISTD_H
22#include <unistd.h>
23#endif
24#include <sys/types.h>
25#include <sys/stat.h>
26#if HAVE_EXT2_IOCTLS
27#include <fcntl.h>
28#include <sys/ioctl.h>
29#endif
30
31#include "e2p.h"
32
33#ifdef O_LARGEFILE
34#define OPEN_FLAGS (O_RDONLY|O_NONBLOCK|O_LARGEFILE)
35#else
36#define OPEN_FLAGS (O_RDONLY|O_NONBLOCK)
37#endif
38
Mike Frysinger548ffd42005-05-07 07:14:41 +000039int fgetsetflags (const char * name, unsigned long * get_flags, unsigned long set_flags)
Mike Frysingerd89e6292005-04-24 05:07:59 +000040{
Mike Frysingerd89e6292005-04-24 05:07:59 +000041#if HAVE_EXT2_IOCTLS
Mike Frysinger548ffd42005-05-07 07:14:41 +000042 struct stat buf;
Mike Frysingerd89e6292005-04-24 05:07:59 +000043 int fd, r, f, save_errno = 0;
44
45 if (!stat(name, &buf) &&
46 !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode)) {
47 goto notsupp;
48 }
Mike Frysingerd89e6292005-04-24 05:07:59 +000049 fd = open (name, OPEN_FLAGS);
50 if (fd == -1)
51 return -1;
Mike Frysinger548ffd42005-05-07 07:14:41 +000052 if (!get_flags) {
53 f = (int) set_flags;
54 r = ioctl (fd, EXT2_IOC_SETFLAGS, &f);
55 } else {
56 r = ioctl (fd, EXT2_IOC_GETFLAGS, &f);
57 *get_flags = f;
58 }
Mike Frysingerd89e6292005-04-24 05:07:59 +000059 if (r == -1)
60 save_errno = errno;
Mike Frysingerd89e6292005-04-24 05:07:59 +000061 close (fd);
62 if (save_errno)
63 errno = save_errno;
64 return r;
Mike Frysingerd89e6292005-04-24 05:07:59 +000065#endif /* HAVE_EXT2_IOCTLS */
Mike Frysingerd89e6292005-04-24 05:07:59 +000066notsupp:
67 errno = EOPNOTSUPP;
68 return -1;
69}