View | Details | Raw Unified | Return to bug 29355
Collapse All | Expand All

(-)bin/cp/utils.c (-6 / +6 lines)
Lines 211-217 Link Here
211
		warn("symlink: %s", llink);
211
		warn("symlink: %s", llink);
212
		return (1);
212
		return (1);
213
	}
213
	}
214
	return (0);
214
	return (pflag ? setfile(p->fts_statp, 0) : 0);
215
}
215
}
216
216
217
int
217
int
Lines 256-266 Link Here
256
256
257
	TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
257
	TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
258
	TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
258
	TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
259
	if (utimes(to.p_path, tv)) {
259
	if (lutimes(to.p_path, tv)) {
260
		warn("utimes: %s", to.p_path);
260
		warn("utimes: %s", to.p_path);
261
		rval = 1;
261
		rval = 1;
262
	}
262
	}
263
	if (fd ? fstat(fd, &ts) : stat(to.p_path, &ts))
263
	if (fd ? fstat(fd, &ts) : lstat(to.p_path, &ts))
264
		gotstat = 0;
264
		gotstat = 0;
265
	else {
265
	else {
266
		gotstat = 1;
266
		gotstat = 1;
Lines 275-281 Link Here
275
	 */
275
	 */
276
	if (!gotstat || fs->st_uid != ts.st_uid || fs->st_gid != ts.st_gid)
276
	if (!gotstat || fs->st_uid != ts.st_uid || fs->st_gid != ts.st_gid)
277
		if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
277
		if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
278
		    chown(to.p_path, fs->st_uid, fs->st_gid)) {
278
		    lchown(to.p_path, fs->st_uid, fs->st_gid)) {
279
			if (errno != EPERM) {
279
			if (errno != EPERM) {
280
				warn("chown: %s", to.p_path);
280
				warn("chown: %s", to.p_path);
281
				rval = 1;
281
				rval = 1;
Lines 284-297 Link Here
284
		}
284
		}
285
285
286
	if (!gotstat || fs->st_mode != ts.st_mode)
286
	if (!gotstat || fs->st_mode != ts.st_mode)
287
		if (fd ? fchmod(fd, fs->st_mode) : chmod(to.p_path, fs->st_mode)) {
287
		if (fd ? fchmod(fd, fs->st_mode) : lchmod(to.p_path, fs->st_mode)) {
288
			warn("chmod: %s", to.p_path);
288
			warn("chmod: %s", to.p_path);
289
			rval = 1;
289
			rval = 1;
290
		}
290
		}
291
291
292
	if (!gotstat || fs->st_flags != ts.st_flags)
292
	if (!gotstat || fs->st_flags != ts.st_flags)
293
		if (fd ?
293
		if (fd ?
294
		    fchflags(fd, fs->st_flags) : chflags(to.p_path, fs->st_flags)) {
294
		    fchflags(fd, fs->st_flags) : lchflags(to.p_path, fs->st_flags)) {
295
			warn("chflags: %s", to.p_path);
295
			warn("chflags: %s", to.p_path);
296
			rval = 1;
296
			rval = 1;
297
		}
297
		}
(-)bin/rm/rm.c (-2 / +2 lines)
Lines 239-245 Link Here
239
		if (!uid &&
239
		if (!uid &&
240
		    (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
240
		    (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
241
		    !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)))
241
		    !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)))
242
			rval = chflags(p->fts_accpath,
242
			rval = lchflags(p->fts_accpath,
243
				       p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
243
				       p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
244
		if (rval == 0) {
244
		if (rval == 0) {
245
			/*
245
			/*
Lines 329-335 Link Here
329
		if (!uid &&
329
		if (!uid &&
330
		    (sb.st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
330
		    (sb.st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
331
		    !(sb.st_flags & (SF_APPEND|SF_IMMUTABLE)))
331
		    !(sb.st_flags & (SF_APPEND|SF_IMMUTABLE)))
332
			rval = chflags(f, sb.st_flags & ~(UF_APPEND|UF_IMMUTABLE));
332
			rval = lchflags(f, sb.st_flags & ~(UF_APPEND|UF_IMMUTABLE));
333
		if (rval == 0) {
333
		if (rval == 0) {
334
			if (S_ISWHT(sb.st_mode))
334
			if (S_ISWHT(sb.st_mode))
335
				rval = undelete(f);
335
				rval = undelete(f);
(-)lib/libc/sys/chflags.2 (-2 / +6 lines)
Lines 48-56 Link Here
48
.Ft int
48
.Ft int
49
.Fn chflags "const char *path"  "u_long flags"
49
.Fn chflags "const char *path"  "u_long flags"
50
.Ft int
50
.Ft int
51
.Fn lchflags "const char *path" "int flags"
52
.Ft int
53
.Fn fchflags "int fd" "u_long flags"
51
.Fn fchflags "int fd" "u_long flags"
52
.Ft int
53
.Fn lchflags "const char *path"  "u_long flags"
54
.Sh DESCRIPTION
54
.Sh DESCRIPTION
55
The file whose name
55
The file whose name
56
is given by
56
is given by
Lines 186-188 Link Here
186
.Nm fchflags
186
.Nm fchflags
187
functions first appeared in
187
functions first appeared in
188
.Bx 4.4 .
188
.Bx 4.4 .
189
The
190
.Fn lchflags
191
function first appeared in
192
.Nx 1.5 .
(-)sbin/restore/tape.c (+1 lines)
Lines 574-579 Link Here
574
			(void) lchmod(name, mode);
574
			(void) lchmod(name, mode);
575
			(void) lutimes(name, ctimep);
575
			(void) lutimes(name, ctimep);
576
			(void) lutimes(name, mtimep);
576
			(void) lutimes(name, mtimep);
577
			(void) lchflags(name, flags);
577
			return (GOOD);
578
			return (GOOD);
578
		}
579
		}
579
		return (FAIL);
580
		return (FAIL);
(-)sys/kern/vfs_syscalls.c (-2 / +9 lines)
Lines 1908-1914 Link Here
1908
}
1908
}
1909
1909
1910
/*
1910
/*
1911
 * Common implementation code for chflags() and fchflags().
1911
 * Common implementation code for chflags(), lchflags and fchflags().
1912
 */
1912
 */
1913
static int
1913
static int
1914
setfflags(td, vp, flags)
1914
setfflags(td, vp, flags)
Lines 1982-1989 Link Here
1982
}
1982
}
1983
1983
1984
/*
1984
/*
1985
 * Same as chflags() but doesn't follow symlinks.
1985
 * Change flags of a file given a path name (don't follow links).
1986
 */
1986
 */
1987
#ifndef _SYS_SYSPROTO_H_
1988
struct lchflags_args {
1989
	char	*path;
1990
	int	flags;
1991
};
1992
#endif
1993
/* ARGSUSED */
1987
int
1994
int
1988
lchflags(td, uap)
1995
lchflags(td, uap)
1989
	struct thread *td;
1996
	struct thread *td;
(-)sys/sys/stat.h (-1 / +1 lines)
Lines 273-279 Link Here
273
273
274
#ifndef _POSIX_SOURCE
274
#ifndef _POSIX_SOURCE
275
int	chflags(const char *, unsigned long);
275
int	chflags(const char *, unsigned long);
276
int	lchflags(const char *, int);
276
int	lchflags(const char *, unsigned long);
277
int	fchflags(int, unsigned long);
277
int	fchflags(int, unsigned long);
278
int	fchmod(int, mode_t);
278
int	fchmod(int, mode_t);
279
int	lchmod(const char *, mode_t);
279
int	lchmod(const char *, mode_t);
(-)usr.bin/chflags/chflags.1 (-7 / +7 lines)
Lines 47-52 Link Here
47
.Fl R
47
.Fl R
48
.Op Fl H | Fl L | Fl P
48
.Op Fl H | Fl L | Fl P
49
.Oc
49
.Oc
50
.Op Fl h 
50
.Ar flags
51
.Ar flags
51
.Ar
52
.Ar
52
.Sh DESCRIPTION
53
.Sh DESCRIPTION
Lines 76-81 Link Here
76
.It Fl R
77
.It Fl R
77
Change the file flags for the file hierarchies rooted
78
Change the file flags for the file hierarchies rooted
78
in the files instead of just the files themselves.
79
in the files instead of just the files themselves.
80
.It Fl h 
81
If the 
82
.Ar file 
83
or a file encountered during directory traversal is a symbolic link, 
84
the file flags of the link itself are changed. 
79
.El
85
.El
80
.Pp
86
.Pp
81
The flags are specified as an octal number or a comma separated list
87
The flags are specified as an octal number or a comma separated list
Lines 117-129 Link Here
117
the immutable bit should be cleared
123
the immutable bit should be cleared
118
.El
124
.El
119
.Pp
125
.Pp
120
Symbolic links do not have flags, so unless the
121
.Fl H
122
or
123
.Fl L
124
option is set,
125
.Nm
126
on a symbolic link always succeeds and has no effect.
127
The
126
The
128
.Fl H ,
127
.Fl H ,
129
.Fl L
128
.Fl L
Lines 141-146 Link Here
141
.Sh SEE ALSO
140
.Sh SEE ALSO
142
.Xr ls 1 ,
141
.Xr ls 1 ,
143
.Xr chflags 2 ,
142
.Xr chflags 2 ,
143
.Xr lchflags 2 ,
144
.Xr stat 2 ,
144
.Xr stat 2 ,
145
.Xr fts 3 ,
145
.Xr fts 3 ,
146
.Xr symlink 7
146
.Xr symlink 7
(-)usr.bin/chflags/chflags.c (-19 / +40 lines)
Lines 64-76 Link Here
64
{
64
{
65
	FTS *ftsp;
65
	FTS *ftsp;
66
	FTSENT *p;
66
	FTSENT *p;
67
	u_long clear, set;
67
	u_long clear, set, newflags;
68
	long val;
68
	long val;
69
	int Hflag, Lflag, Pflag, Rflag, ch, fts_options, oct, rval;
69
	int Hflag, Lflag, Pflag, Rflag, hflag, ch, fts_options, oct, rval;
70
	char *flags, *ep;
70
	char *flags, *ep;
71
	int (*change_flags) __P((const char *, u_long));
71
72
72
	Hflag = Lflag = Pflag = Rflag = 0;
73
	Hflag = Lflag = Pflag = Rflag = hflag = 0;
73
	while ((ch = getopt(argc, argv, "HLPR")) != -1)
74
	while ((ch = getopt(argc, argv, "HLPRh")) != -1)
74
		switch (ch) {
75
		switch (ch) {
75
		case 'H':
76
		case 'H':
76
			Hflag = 1;
77
			Hflag = 1;
Lines 87-92 Link Here
87
		case 'R':
88
		case 'R':
88
			Rflag = 1;
89
			Rflag = 1;
89
			break;
90
			break;
91
		case 'h':
92
			hflag = 1;
93
			break;
90
		case '?':
94
		case '?':
91
		default:
95
		default:
92
			usage();
96
			usage();
Lines 106-112 Link Here
106
			fts_options |= FTS_LOGICAL;
110
			fts_options |= FTS_LOGICAL;
107
		}
111
		}
108
	} else
112
	} else
109
		fts_options = FTS_LOGICAL;
113
		fts_options = hflag ? FTS_PHYSICAL : FTS_LOGICAL;
110
114
111
	flags = *argv;
115
	flags = *argv;
112
	if (*flags >= '0' && *flags <= '7') {
116
	if (*flags >= '0' && *flags <= '7') {
Lines 131-136 Link Here
131
		err(1, NULL);
135
		err(1, NULL);
132
136
133
	for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
137
	for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
138
		change_flags = chflags;
134
		switch (p->fts_info) {
139
		switch (p->fts_info) {
135
		case FTS_D:
140
		case FTS_D:
136
			if (Rflag)		/* Change it at FTS_DP. */
141
			if (Rflag)		/* Change it at FTS_DP. */
Lines 146-173 Link Here
146
			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
151
			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
147
			rval = 1;
152
			rval = 1;
148
			continue;
153
			continue;
149
		case FTS_SL:			/* Ignore. */
154
		case FTS_SL:			/* Ignore unless -h. */ 
155
			/* 
156
			 * All symlinks we found while doing a physical 
157
			 * walk end up here. 
158
			 */ 
159
			if (!hflag) 
160
				continue; 
161
			/* 
162
			 * Note that if we follow a symlink, fts_info is 
163
			 * not FTS_SL but FTS_F or whatever.  And we should 
164
			 * use lchflags only for FTS_SL and should use chflags 
165
			 * for others. 
166
			 */ 
167
			change_flags = lchflags; 
168
			break; 
169
150
		case FTS_SLNONE:
170
		case FTS_SLNONE:
151
			/*
171
			/*
152
			 * The only symlinks that end up here are ones that
172
			 * The only symlinks that end up here are ones that
153
			 * don't point to anything and ones that we found
173
			 * don't point to anything. Note that if we are 
154
			 * doing a physical walk.
174
			 * doing a physical walk, we never reach here unless 
175
			 * we asked to follow explicitly (with -H or -L).
155
			 */
176
			 */
156
			continue;
177
			continue;
157
		default:
178
		default:
158
			break;
179
			break;
159
		}
180
		}
160
		if (oct) {
181
		if (oct)
161
			if (!chflags(p->fts_accpath, set))
182
			newflags = set;
162
				continue;
183
		else {
163
		} else {
184
			newflags = p->fts_statp->st_flags;
164
			p->fts_statp->st_flags |= set;
185
			newflags |= set;
165
			p->fts_statp->st_flags &= clear;
186
			newflags &= clear;
166
			if (!chflags(p->fts_accpath, (u_long)p->fts_statp->st_flags))
187
		}
167
				continue;
188
		if ((*change_flags)(p->fts_accpath, newflags)) {
189
			warn("%s", p->fts_path);
190
			rval = 1;
168
		}
191
		}
169
		warn("%s", p->fts_path);
170
		rval = 1;
171
	}
192
	}
172
	if (errno)
193
	if (errno)
173
		err(1, "fts_read");
194
		err(1, "fts_read");
Lines 178-183 Link Here
178
usage(void)
199
usage(void)
179
{
200
{
180
	(void)fprintf(stderr,
201
	(void)fprintf(stderr,
181
	    "usage: chflags [-R [-H | -L | -P]] flags file ...\n");
202
	    "usage: chflags [-R [-H | -L | -P]] [-h] flags file ...\n");
182
	exit(1);
203
	exit(1);
183
}
204
}
(-)usr.bin/find/function.c (-1 / +1 lines)
Lines 403-409 Link Here
403
	if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
403
	if ((entry->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
404
	    !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
404
	    !(entry->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
405
	    geteuid() == 0)
405
	    geteuid() == 0)
406
		chflags(entry->fts_accpath,
406
		lchflags(entry->fts_accpath,
407
		       entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
407
		       entry->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
408
408
409
	/* rmdir directories, unlink everything else */
409
	/* rmdir directories, unlink everything else */
(-)usr.sbin/mtree/compare.c (-5 / +5 lines)
Lines 61-67 Link Here
61
#include "mtree.h"
61
#include "mtree.h"
62
#include "extern.h"
62
#include "extern.h"
63
63
64
extern int uflag;
64
extern int Lflag, uflag;
65
extern int lineno;
65
extern int lineno;
66
66
67
static const char *ftype(u_int);
67
static const char *ftype(u_int);
Lines 127-133 Link Here
127
		(void)printf("%suser expected %lu found %lu",
127
		(void)printf("%suser expected %lu found %lu",
128
		    tab, (u_long)s->st_uid, (u_long)p->fts_statp->st_uid);
128
		    tab, (u_long)s->st_uid, (u_long)p->fts_statp->st_uid);
129
		if (uflag)
129
		if (uflag)
130
			if (chown(p->fts_accpath, s->st_uid, -1))
130
			if ((Lflag ? chown : lchown)(p->fts_accpath, s->st_uid, -1))
131
				(void)printf(" not modified: %s\n",
131
				(void)printf(" not modified: %s\n",
132
				    strerror(errno));
132
				    strerror(errno));
133
			else
133
			else
Lines 141-147 Link Here
141
		(void)printf("%sgid expected %lu found %lu",
141
		(void)printf("%sgid expected %lu found %lu",
142
		    tab, (u_long)s->st_gid, (u_long)p->fts_statp->st_gid);
142
		    tab, (u_long)s->st_gid, (u_long)p->fts_statp->st_gid);
143
		if (uflag)
143
		if (uflag)
144
			if (chown(p->fts_accpath, -1, s->st_gid))
144
			if ((Lflag ? chown : lchown)(p->fts_accpath, -1, s->st_gid))
145
				(void)printf(" not modified: %s\n",
145
				(void)printf(" not modified: %s\n",
146
				    strerror(errno));
146
				    strerror(errno));
147
			else
147
			else
Lines 233-239 Link Here
233
		free(fflags);
233
		free(fflags);
234
234
235
		if (uflag)
235
		if (uflag)
236
			if (chflags(p->fts_accpath, s->st_flags))
236
			if ((Lflag ? chflags : lchflags)(p->fts_accpath, s->st_flags))
237
				(void)printf(" not modified: %s\n",
237
				(void)printf(" not modified: %s\n",
238
				    strerror(errno));
238
				    strerror(errno));
239
			else
239
			else
Lines 301-307 Link Here
301
	    strcmp(cp = rlink(p->fts_accpath), s->slink)) {
301
	    strcmp(cp = rlink(p->fts_accpath), s->slink)) {
302
		LABEL;
302
		LABEL;
303
		(void)printf("%slink_ref expected %s found %s\n",
303
		(void)printf("%slink_ref expected %s found %s\n",
304
		      tab, cp, s->slink);
304
		      tab, s->slink, cp);
305
	}
305
	}
306
	return (label);
306
	return (label);
307
}
307
}
(-)usr.sbin/mtree/mtree.8 (-1 / +1 lines)
Lines 76-82 Link Here
76
Don't follow symbolic links in the file hierarchy, instead consider
76
Don't follow symbolic links in the file hierarchy, instead consider
77
the symbolic link itself in any comparisons. This is the default.
77
the symbolic link itself in any comparisons. This is the default.
78
.It Fl U
78
.It Fl U
79
Modify the owner, group and permissions of existing files to match
79
Modify the owner, group, flags and permissions of existing files to match
80
the specification and create any missing directories or symbolic links.
80
the specification and create any missing directories or symbolic links.
81
User, group and permissions must all be specified for missing directories
81
User, group and permissions must all be specified for missing directories
82
to be created.
82
to be created.
(-)usr.sbin/mtree/mtree.c (-1 / +2 lines)
Lines 58-64 Link Here
58
extern long int crc_total;
58
extern long int crc_total;
59
59
60
int ftsoptions = FTS_PHYSICAL;
60
int ftsoptions = FTS_PHYSICAL;
61
int cflag, dflag, eflag, iflag, nflag, qflag, rflag, sflag, uflag, Uflag;
61
int cflag, dflag, eflag, iflag, Lflag, nflag, qflag, rflag, sflag, uflag, Uflag;
62
u_int keys;
62
u_int keys;
63
char fullpath[MAXPATHLEN];
63
char fullpath[MAXPATHLEN];
64
64
Lines 105-110 Link Here
105
					keys |= parsekey(p, NULL);
105
					keys |= parsekey(p, NULL);
106
			break;
106
			break;
107
		case 'L':
107
		case 'L':
108
			Lflag = 1;
108
			ftsoptions &= ~FTS_PHYSICAL;
109
			ftsoptions &= ~FTS_PHYSICAL;
109
			ftsoptions |= FTS_LOGICAL;
110
			ftsoptions |= FTS_LOGICAL;
110
			break;
111
			break;
(-)usr.sbin/mtree/verify.c (+4 lines)
Lines 206-211 Link Here
206
				if (lchown(path, p->st_uid, p->st_gid))
206
				if (lchown(path, p->st_uid, p->st_gid))
207
					(void)printf("%s: user/group not modified: %s\n",
207
					(void)printf("%s: user/group not modified: %s\n",
208
					    path, strerror(errno));
208
					    path, strerror(errno));
209
				if ((p->flags & F_FLAGS) && p->st_flags &&
210
					lchflags(path, p->st_flags))
211
					(void)printf("%s: symlink flags not set: %s\n",
212
					    path, strerror(errno));
209
				continue;
213
				continue;
210
			} else if (!(p->flags & F_MODE))
214
			} else if (!(p->flags & F_MODE))
211
			    (void)printf(" (directory not created: mode not specified)");
215
			    (void)printf(" (directory not created: mode not specified)");
(-)usr.sbin/pkg_install/add/extract.c (-2 / +2 lines)
Lines 62-68 Link Here
62
	if (q->type == PLIST_FILE) {
62
	if (q->type == PLIST_FILE) {
63
	    snprintf(try, FILENAME_MAX, "%s/%s", dir, q->name);
63
	    snprintf(try, FILENAME_MAX, "%s/%s", dir, q->name);
64
	    if (make_preserve_name(bup, FILENAME_MAX, name, try) && fexists(bup)) {
64
	    if (make_preserve_name(bup, FILENAME_MAX, name, try) && fexists(bup)) {
65
		(void)chflags(try, 0);
65
		(void)lchflags(try, 0);
66
		(void)unlink(try);
66
		(void)unlink(try);
67
		if (rename(bup, try))
67
		if (rename(bup, try))
68
		    warnx("rollback: unable to rename %s back to %s", bup, try);
68
		    warnx("rollback: unable to rename %s back to %s", bup, try);
Lines 138-144 Link Here
138
		/* first try to rename it into place */
138
		/* first try to rename it into place */
139
		snprintf(try, FILENAME_MAX, "%s/%s", Directory, p->name);
139
		snprintf(try, FILENAME_MAX, "%s/%s", Directory, p->name);
140
		if (fexists(try)) {
140
		if (fexists(try)) {
141
		    (void)chflags(try, 0);	/* XXX hack - if truly immutable, rename fails */
141
		    (void)lchflags(try, 0);	/* XXX hack - if truly immutable, rename fails */
142
		    if (preserve && PkgName) {
142
		    if (preserve && PkgName) {
143
			char pf[FILENAME_MAX];
143
			char pf[FILENAME_MAX];
144
144

Return to bug 29355