-
Notifications
You must be signed in to change notification settings - Fork 5
/
unattr.c
170 lines (162 loc) · 4.72 KB
/
unattr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/***************************************************************************/
/* Copyright (C) 2006-2009 Brice Arnould. */
/* */
/* This file is part of ShaKe. */
/* */
/* ShaKe is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License, or */
/* (at your option) any later version. */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/***************************************************************************/
/*** unattr: quick and dirty hack to remove unwanted Xattrs ****/
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <error.h>
#include <string.h>
#include <attr/attributes.h> // flistxattr
#include <sys/types.h> // open()
#include <sys/stat.h> // open()
#include <sys/xattr.h> // fremovexattr()
#include <fcntl.h> // open()
#include <unistd.h> // close()
#include "linux.h" // getopt()
#include "config.h"
#include "executive.h"
void strip (char *name, char **attrs);
void
show_help (void)
{
puts ("\
Usage: unattr [OPTION]... <FILE|DIRECTORY>...\n\
Remove choosen xattr from files, recursively.\n\
\n\
-a, --attr specify the name of a new attribute\n\
-h, --help help\n\
-V, --version show version number and copyright\n\
Report bugs at https://github.com/unbrice/shake/issues\
");
}
/* If name refer to a regular file, call strip() on it.
* If name refer to a directory, call itself on dir entries.
* Else, or if open failed, do nothing.
*/
void
look (char *name, char **attr)
{
assert (name && attr);
struct stat st;
/* stat */
if (-1 == lstat (name, &st))
error (0, errno, "%s: stat() failed", name);
else if (S_ISREG (st.st_mode)) // regular file
strip (name, attr);
else if (S_ISDIR (st.st_mode)) // directory
{
/* list it */
char **flist = list_dir (name, false);
if (!flist)
{
error (0, 0, "%s: list_dir() failed", name);
return;
}
/* Go through the list */
for (char **name = flist; *name; name++)
{
look (*name, attr);
free (*name);
}
free (flist);
}
}
void
show_version (void)
{
puts ("\
Unattr " VERSION "\n\
Copyright (C) 2006-2008 Brice Arnould.\n\
Unattr comes with ABSOLUTELY NO WARRANTY. You may redistribute copies of Unattr\n\
under the terms of the GNU General Public License. For more information about\n\
these matters, see the file named GPL.txt.\
");
}
/* This function remove attributes attr[0], attr[1]... attr[n]
* with attr[n] == NULL, from the file named name
*/
void
strip (char *name, char **attr)
{
assert (name), assert (attr);
int fd = open (name, O_WRONLY);
if (0 > fd)
{
error (0, errno, "%s: open() failed", name);
return;
}
for (char **attr = attr; *attr; attr++)
fremovexattr (fd, *attr);
close (fd);
}
int
main (int argc, char **argv)
{
char **attr = NULL;
/* Parse opts */
{
const uint BUFSTEP = 32;
uint pos = 0;
while (1)
{
int c;
static const struct option long_options[] = {
{"attr", required_argument, NULL, 'a'},
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'V'},
{0, 0, 0, 0}
};
c = getopt_long (argc, argv, "a:hV", long_options, NULL);
if (c == -1)
break;
switch (c)
{
case 'a':
if (0 == pos % BUFSTEP)
attr = realloc (attr, sizeof (*attr) * (pos + BUFSTEP + 1));
if (!attr)
error (1, errno, "alloc() failed");
attr[pos] = strdup (optarg);
if (!attr[pos++])
error (1, errno, "alloc() failed");
break;
case 'h':
show_help ();
return 0;
case 'V':
show_version ();
return 0;
case 0:
case '?':
default:
error (1, 0, "invalid args, aborting");
}
}
if (!attr)
return 0;
attr[pos] = NULL;
}
/* For each name */
if (attr)
for (; optind < argc; optind++)
look (argv[optind], attr);
/* free */
close_list (attr);
return 0;
}