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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
/*
* Copyright (c) 2002 by David I. Bell
* Permission is granted to use, distribute, or modify this source,
* provided that this copyright notice remains intact.
*
* The "chattr" and "lsattr" built-in commands.
* These commands are optionally built into sash.
* They manipulate the important ext2 file system file attribute flags.
*/
#if HAVE_LINUX_ATTR
#include <sys/ioctl.h>
#include <sys/types.h>
#include <ext2fs/ext2_fs.h>
#include "sash.h"
/*
* The chattr command.
* This can turn on or off the immutable and append-only ext2 flags.
*/
void
do_chattr(int argc, const char ** argv)
{
const char * fileName;
const char * options;
int * flagPointer;
int offFlags;
int onFlags;
int oldFlags;
int newFlags;
int fd;
argc--;
argv++;
/*
* Parse the options.
*/
onFlags = 0;
offFlags = 0;
while ((**argv == '-') || (**argv == '+'))
{
options = *argv++;
argc--;
/*
* Point at the proper flags to be modified.
*/
if (*options++ == '+')
flagPointer = &onFlags;
else
flagPointer = &offFlags;
/*
* Parse the flag characters.
*/
while (*options)
{
switch (*options++)
{
case 'i':
*flagPointer |= EXT2_IMMUTABLE_FL;
break;
case 'a':
*flagPointer |= EXT2_APPEND_FL;
break;
default:
fprintf(stderr, "Unknown flag '%c'\n",
options[-1]);
return;
}
}
}
/*
* Make sure that the attributes are reasonable.
*/
if ((onFlags == 0) && (offFlags == 0))
{
fprintf(stderr, "No attributes specified\n");
return;
}
if ((onFlags & offFlags) != 0)
{
fprintf(stderr, "Inconsistent attributes specified\n");
return;
}
/*
* Make sure there are some files to affect.
*/
if (argc <= 0)
{
fprintf(stderr, "No files specified for setting attributes\n");
return;
}
/*
* Iterate over all of the file names.
*/
while (argc-- > 0)
{
fileName = *argv++;
/*
* Open the file name.
*/
fd = open(fileName, O_RDONLY | O_NONBLOCK);
if (fd < 0)
{
perror(fileName);
continue;
}
/*
* Read the current ext2 flag bits.
*/
if (ioctl(fd, EXT2_IOC_GETFLAGS, &oldFlags) < 0)
{
perror(fileName);
(void) close(fd);
continue;
}
/*
* Modify the flag bits as specified.
*/
newFlags = oldFlags;
newFlags |= onFlags;
newFlags &= ~offFlags;
/*
* If the flags aren't being changed, then close this
* file and continue.
*/
if (newFlags == oldFlags)
{
(void) close(fd);
continue;
}
/*
* Set the new ext2 flag bits.
*/
if (ioctl(fd, EXT2_IOC_SETFLAGS, &newFlags) < 0)
{
perror(fileName);
(void) close(fd);
continue;
}
/*
* Close the file.
*/
(void) close(fd);
}
}
/*
* The lsattr command.
* This lists the immutable and append-only ext2 flags.
*/
void
do_lsattr(int argc, const char ** argv)
{
const char * fileName;
int fd;
int status;
int flags;
char string[4];
argc--;
argv++;
/*
* Iterate over all of the file names.
*/
while (argc-- > 0)
{
fileName = *argv++;
/*
* Open the file name.
*/
fd = open(fileName, O_RDONLY | O_NONBLOCK);
if (fd < 0)
{
perror(fileName);
continue;
}
/*
* Read the ext2 flag bits.
*/
status = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
/*
* Close the file and check the status.
*/
(void) close(fd);
if (status < 0)
{
perror(fileName);
continue;
}
/*
* Build up the string according to the flags.
* This is 'i' for immutable, and 'a' for append only.
*/
string[0] = ((flags & EXT2_IMMUTABLE_FL) ? 'i' : '-');
string[1] = ((flags & EXT2_APPEND_FL) ? 'a' : '-');
string[2] = '\0';
/*
* Print the flags and the file name.
*/
printf("%s %s\n", string, fileName);
}
}
#endif
/* END CODE */
|