blob: 6db729e08e5083235cadd77503a50f24c5fa09be [file] [log] [blame]
Mike Frysingerd89e6292005-04-24 05:07:59 +00001/*
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 * This file can be redistributed under the terms of the GNU Library General
9 * Public License
10 */
11
12/*
13 * History:
14 * 93/10/30 - Creation
15 */
16
17#if HAVE_ERRNO_H
18#include <errno.h>
19#endif
20#if HAVE_UNISTD_H
21#include <unistd.h>
22#endif
23#include <sys/types.h>
24#include <sys/stat.h>
25#if HAVE_EXT2_IOCTLS
26#include <fcntl.h>
27#include <sys/ioctl.h>
28#endif
29
30#include "e2p.h"
31
32#ifdef O_LARGEFILE
33#define OPEN_FLAGS (O_RDONLY|O_NONBLOCK|O_LARGEFILE)
34#else
35#define OPEN_FLAGS (O_RDONLY|O_NONBLOCK)
36#endif
37
38int fgetflags (const char * name, unsigned long * flags)
39{
40 struct stat buf;
41#if HAVE_STAT_FLAGS && !(APPLE_DARWIN && HAVE_EXT2_IOCTLS)
42
43 if (stat (name, &buf) == -1)
44 return -1;
45
46 *flags = 0;
47#ifdef UF_IMMUTABLE
48 if (buf.st_flags & UF_IMMUTABLE)
49 *flags |= EXT2_IMMUTABLE_FL;
50#endif
51#ifdef UF_APPEND
52 if (buf.st_flags & UF_APPEND)
53 *flags |= EXT2_APPEND_FL;
54#endif
55#ifdef UF_NODUMP
56 if (buf.st_flags & UF_NODUMP)
57 *flags |= EXT2_NODUMP_FL;
58#endif
59
60 return 0;
61#else
62#if HAVE_EXT2_IOCTLS
63 int fd, r, f, save_errno = 0;
64
65 if (!stat(name, &buf) &&
66 !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode)) {
67 goto notsupp;
68 }
69#if !APPLE_DARWIN
70 fd = open (name, OPEN_FLAGS);
71 if (fd == -1)
72 return -1;
73 r = ioctl (fd, EXT2_IOC_GETFLAGS, &f);
74 if (r == -1)
75 save_errno = errno;
76 *flags = f;
77 close (fd);
78 if (save_errno)
79 errno = save_errno;
80 return r;
81#else
82 f = -1;
83 save_errno = syscall(SYS_fsctl, name, EXT2_IOC_GETFLAGS, &f, 0);
84 *flags = f;
85 return (save_errno);
86#endif
87#endif /* HAVE_EXT2_IOCTLS */
88#endif
89notsupp:
90 errno = EOPNOTSUPP;
91 return -1;
92}