blob: 167d16e86faa361da512aa364cc2e80e21bced8f [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * fsetflags.c - Set 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/*
39 * Deal with lame glibc's that define this function without actually
40 * implementing it. Can you say "attractive nuisance", boys and girls?
41 * I knew you could!
42 */
43#ifdef __linux__
44#undef HAVE_CHFLAGS
45#endif
46
47#ifdef O_LARGEFILE
48#define OPEN_FLAGS (O_RDONLY|O_NONBLOCK|O_LARGEFILE)
49#else
50#define OPEN_FLAGS (O_RDONLY|O_NONBLOCK)
51#endif
52
53int fsetflags (const char * name, unsigned long flags)
54{
55#if HAVE_CHFLAGS && !(APPLE_DARWIN && HAVE_EXT2_IOCTLS)
56 unsigned long bsd_flags = 0;
57
58#ifdef UF_IMMUTABLE
59 if (flags & EXT2_IMMUTABLE_FL)
60 bsd_flags |= UF_IMMUTABLE;
61#endif
62#ifdef UF_APPEND
63 if (flags & EXT2_APPEND_FL)
64 bsd_flags |= UF_APPEND;
65#endif
66#ifdef UF_NODUMP
67 if (flags & EXT2_NODUMP_FL)
68 bsd_flags |= UF_NODUMP;
69#endif
70
71 return chflags (name, bsd_flags);
72#else /* !HAVE_CHFLAGS || (APPLE_DARWIN && HAVE_EXT2_IOCTLS) */
73#if HAVE_EXT2_IOCTLS
74 int fd, r, f, save_errno = 0;
75 struct stat buf;
76
77 if (!lstat(name, &buf) &&
78 !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode)) {
79 goto notsupp;
80 }
81#if !APPLE_DARWIN
82 fd = open (name, OPEN_FLAGS);
83 if (fd == -1)
84 return -1;
85 f = (int) flags;
86 r = ioctl (fd, EXT2_IOC_SETFLAGS, &f);
87 if (r == -1)
88 save_errno = errno;
89 close (fd);
90 if (save_errno)
91 errno = save_errno;
92#else /* APPLE_DARWIN */
93 f = (int) flags;
94 return syscall(SYS_fsctl, name, EXT2_IOC_SETFLAGS, &f, 0);
95#endif /* !APPLE_DARWIN */
96 return r;
97
98notsupp:
99#endif /* HAVE_EXT2_IOCTLS */
100#endif
101 errno = EOPNOTSUPP;
102 return -1;
103}