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