xref: /xnu-11215.41.3/bsd/man/man2/setattrlist.2 (revision 33de042d024d46de5ff4e89f2471de6608e37fa4)
1.\" Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
2.\"
3.\" The contents of this file constitute Original Code as defined in and
4.\" are subject to the Apple Public Source License Version 1.1 (the
5.\" "License").  You may not use this file except in compliance with the
6.\" License.  Please obtain a copy of the License at
7.\" http://www.apple.com/publicsource and read it before using this file.
8.\"
9.\" This Original Code and all software distributed under the License are
10.\" distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
11.\" EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
12.\" INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
13.\" FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
14.\" License for the specific language governing rights and limitations
15.\" under the License.
16.\"
17.\"     @(#)setattrlist.2
18.
19.Dd December 15, 2003
20.Dt SETATTRLIST 2
21.Os Darwin
22.Sh NAME
23.Nm setattrlist ,
24.Nm fsetattrlist ,
25.Nm setattrlistat
26.Nd set file system attributes
27.Sh SYNOPSIS
28.Fd #include <sys/attr.h>
29.Fd #include <unistd.h>
30.Ft int
31.Fn setattrlist "const char * path" "struct attrlist * attrList" "void * attrBuf" "size_t attrBufSize" "unsigned long options"
32.Ft int
33.Fn fsetattrlist "int fd" "struct attrlist * attrList" "void * attrBuf" "size_t attrBufSize" "unsigned long options"
34.Ft int
35.Fn setattrlistat "int dir_fd" "const char * path" "struct attrlist * attrList" "void * attrBuf" "size_t attrBufSize" "uint32_t options"
36.
37.Sh DESCRIPTION
38The
39.Fn setattrlist
40and
41.Fn fsetattrlist
42functions set attributes (that is, metadata) of file system objects.
43They are the logical opposite of
44.Xr getattrlist 2 .
45The
46.Fn setattrlist
47function sets attributes about the file system object specified by
48.Fa path
49from the values in the buffer specified by
50.Fa attrBuf
51and
52.Fa attrBufSize ;
53the
54.Fn fsetattrlist
55function does the same for the
56.Fa fd
57file descriptor.
58The
59.Fa attrList
60parameter determines what attributes are set.
61The
62.Fa options
63parameter lets you control specific aspects of the function's behaviour.
64.Pp
65The
66.Fn setattrlistat
67system call is equivalent to
68.Fn setattrlist
69except in the case where
70.Fa path
71specifies a relative path.
72In this case the attributes are set for the file system object named by
73path relative to the directory associated with the file descriptor
74.Fa fd
75instead of the current working directory.
76If
77.Fn setattrlistat
78is passed the special value
79.Dv AT_FDCWD
80in the
81.Fa fd
82parameter, the current working directory is used and the behavior is
83identical to a call to
84.Fn setattrlist .
85.Pp
86.
87The
88functions are only supported by certain volume format implementations.
89For maximum compatibility, client programs should use high-level APIs
90(such as the Carbon File Manager) to access file system attributes.
91These high-level APIs include logic to emulate file system attributes
92on volumes that don't support
93.Fn setattrlist
94and
95.Fn fsetattrlist .
96.Pp
97.
98.\" path parameter
99.
100The
101.Fa path
102parameter for
103.Fn setattrlist
104must reference a valid file system object.
105All directories listed in the path name leading to the object
106must be searchable.
107The
108.Fa fd
109parameter for
110.Fn fsetattrlist
111must be a valid file descriptor for the calling process.
112.
113The list of potentially settable attributes via
114.Fn setattrlist
115is different than the list of attributes that are accessible via
116.Fn getattrlist
117In particular, only the following attributes are modifiable via
118.Fn setattrlist
119and not all of them may be supported on all filesystems.
120.Pp
121.
122.Bl -item -compact
123.It
124ATTR_CMN_SCRIPT
125.It
126ATTR_CMN_CRTIME
127.It
128ATTR_CMN_MODTIME
129.It
130ATTR_CMN_CHGTIME
131.It
132ATTR_CMN_ACCTIME
133.It
134ATTR_CMN_BKUPTIME
135.It
136ATTR_CMN_FNDRINFO
137.It
138ATTR_CMN_OWNERID
139.It
140ATTR_CMN_GRPID
141.It
142ATTR_CMN_ACCESSMASK
143.It
144ATTR_CMN_FLAGS
145.It
146ATTR_CMN_EXTENDED_SECURITY
147.It
148ATTR_CMN_GRPUUID
149.It
150ATTR_CMN_ADDEDTIME
151.Pp
152.It
153ATTR_VOL_NAME
154.It
155ATTR_VOL_INFO
156.Pp
157.It
158ATTR_FILE_DEVTYPE
159.El
160.Pp
161.
162.
163You must own the file system object in order to set any of the
164following attributes:
165.Pp
166.
167.Bl -item -compact
168.It
169ATTR_CMN_GRPID
170.It
171ATTR_CMN_ACCESSMASK
172.It
173ATTR_CMN_FLAGS
174.It
175ATTR_CMN_CRTIME
176.It
177ATTR_CMN_MODTIME
178.It
179ATTR_CMN_ACCTIME
180.It
181ATTR_CMN_ADDEDTIME
182.Pp
183ATTR_CMN_CHGTIME
184.Fa cannot be set programmatically. Any attempt to set change time is ignored.
185.El
186.Pp
187.
188.Pp
189If ATTR_CMN_MODTIME is set to a time before ATTR_CMN_CRTIME, the value of
190ATTR_CMN_CRTIME is set to the same value as ATTR_CMN_MODTIME.
191.El
192.Pp
193.
194You must be root (that is, your process's effective UID must be 0) in order to change the
195.Dv ATTR_CMN_OWNERID
196attribute
197Setting other attributes requires that you have write access to the object.
198.Pp
199.
200.\" attrList parameter
201.
202The
203.Fa attrList
204parameter is a pointer to an
205.Vt attrlist
206structure.
207You are responsible for filling out all fields of this structure before calling the function.
208See the discussion of the
209.Xr getattrlist 2
210function for a detailed description of this structure.
211To set an attribute you must set the corresponding bit in the appropriate
212.Vt attrgroup_t
213field of the
214.Vt attrlist
215structure.
216.Pp
217.
218.\" attrBuf and attrBufSize parameters
219.
220The
221.Fa attrBuf
222and
223.Fa attrBufSize
224parameters specify a buffer that contains the attribute values to set.
225Attributes are packed in exactly the same way as they are returned from
226.Xr getattrlist 2
227except that, when setting attributes, the buffer does not include the leading
228.Vt u_int32_t
229length value.
230.Pp
231.
232.\" option parameter
233.
234The
235.Fa options
236parameter is a bit set that controls the behaviour of
237.Fn setattrlist .
238The following option bits are defined.
239.
240.Bl -tag -width XXXbitmapcount
241.
242.It FSOPT_NOFOLLOW
243If this bit is set,
244.Fn setattrlist
245will not follow a symlink if it occurs as
246the last component of
247.Fa path .
248.
249.It FSOPT_NOFOLLOW_ANY
250If this bit is set,
251.Fn setattrlist
252will not follow a symlink if it occurs as
253the last component of
254.Fa path .
255In addition, if a symbolic link is encountered
256before the final component, an error is
257returned
258.
259.El
260.
261.Sh RETURN VALUES
262Upon successful completion a value of 0 is returned.
263Otherwise, a value of -1 is returned and
264.Va errno
265is set to indicate the error.
266.
267.Sh COMPATIBILITY
268Not all volumes support
269.Fn setattrlist .
270However, if a volume supports
271.Xr getattrlist 2 ,
272it must also support
273.Fn setattrlist .
274See the documentation for
275.Xr getattrlist 2
276for details on how to tell whether a volume supports it.
277.Pp
278.
279The
280.Fn setattrlist
281function has been undocumented for more than two years.
282In that time a number of volume format implementations have been created without
283a proper specification for the behaviour of this routine.
284You may encounter volume format implementations with slightly different
285behaviour than what is described here.
286Your program is expected to be tolerant of this variant behaviour.
287.Pp
288.
289If you're implementing a volume format that supports
290.Fn setattrlist ,
291you should be careful to support the behaviour specified by this document.
292.
293.Sh ERRORS
294.Fn setattrlist
295and
296.Fn fsetattrlist
297will fail if:
298.Bl -tag -width Er
299.
300.It Bq Er ENOTSUP
301The call is not supported by the volume.
302.
303.It Bq Er ENOTDIR
304A component of the path for
305.Fn setattrlist
306prefix is not a directory.
307.
308.It Bq Er ENAMETOOLONG
309A component of a path name for
310.Fn setattrlist
311exceeded
312.Dv NAME_MAX
313characters, or an entire path name exceeded
314.Dv PATH_MAX
315characters.
316.
317.It Bq Er ENOENT
318The file system object for
319.Fn setattrlist
320does not exist.
321.
322.It Bq Er EBADF
323The file descriptor argument for
324.Fn fsetattrlist
325is not a valid file descriptor.
326.
327.It Bq Er EROFS
328The volume is read-only.
329.
330.It Bq Er EACCES
331Search permission is denied for a component of the path prefix for
332.Fn setattrlist .
333.
334.It Bq Er ELOOP
335Too many symbolic links were encountered in translating the pathname for
336.Fn setattrlist .
337.
338.It Bq Er ELOOP
339FSOPT_NOFOLLOW_ANY was passed and a symbolic link was encountered in
340translating the pathname for
341.Fn setattrlist .
342.
343.It Bq Er EFAULT
344.Fa path ,
345.Fa attrList
346or
347.Em attrBuf
348points to an invalid address.
349.
350.It Bq Er EINVAL
351The
352.Fa bitmapcount
353field of
354.Fa attrList
355is not
356.Dv ATTR_BIT_MAP_COUNT .
357.
358.It Bq Er EINVAL
359You try to set an invalid attribute.
360.
361.It Bq Er EINVAL
362You try to set an attribute that is read-only.
363.
364.It Bq Er EINVAL
365You try to set volume attributes and directory or file attributes at the same time.
366.
367.It Bq Er EINVAL
368You try to set volume attributes but
369.Fa path
370does not reference the root of the volume.
371.
372.It Bq Er EPERM
373You try to set an attribute that can only be set by the owner.
374.
375.It Bq Er EACCES
376You try to set an attribute that's only settable if you have write permission,
377and you do not have write permission.
378.
379.It Bq Er EINVAL
380The buffer size you specified in
381.Fa attrBufSize
382is too small to hold all the attributes that you are trying to set.
383.
384.It Bq Er EIO
385An I/O error occurred while reading from or writing to the file system.
386.El
387.Pp
388.Pp
389In addition to the errors returned by the
390.Fn setattrlist ,
391the
392.Fn setattrlistat
393function may fail if:
394.Bl -tag -width Er
395.It Bq Er EBADF
396The
397.Fa path
398argument does not specify an absolute path and the
399.Fa fd
400argument is neither
401.Dv AT_FDCWD
402nor a valid file descriptor open for searching.
403.It Bq Er ENOTDIR
404The
405.Fa path
406argument is not an absolute path and
407.Fa fd
408is neither
409.Dv AT_FDCWD
410nor a file descriptor associated with a directory.
411.El
412.Pp
413.
414.Sh CAVEATS
415.
416If you try to set any volume attributes, you must set
417.Dv ATTR_VOL_INFO
418in the
419.Fa volattr
420field, even though it consumes no data from the attribute buffer.
421.Pp
422.
423For more caveats, see also the compatibility notes above.
424.
425.Sh EXAMPLES
426.
427The following code shows how to set the file type and creator of
428a file by getting the
429.Dv ATTR_CMN_FNDRINFO
430attribute using
431.Xr getattrlist 2 ,
432modifying the appropriate fields of the 32-byte Finder information structure,
433and then setting the attribute back using
434.Fn setattrlist .
435This assumes that the target volume supports the required attributes
436.
437.Bd -literal
438#include <assert.h>
439#include <stdio.h>
440#include <stddef.h>
441#include <string.h>
442#include <sys/attr.h>
443#include <sys/errno.h>
444#include <unistd.h>
445#include <sys/vnode.h>
446.Pp
447.
448typedef struct attrlist attrlist_t;
449.Pp
450.
451struct FInfoAttrBuf {
452    u_int32_t       length;
453    fsobj_type_t    objType;
454    char            finderInfo[32];
455};
456typedef struct FInfoAttrBuf FInfoAttrBuf;
457.Pp
458.
459static int FInfoDemo(
460    const char *path,
461    const char *type,
462    const char *creator
463)
464{
465    int             err;
466    attrlist_t      attrList;
467    FInfoAttrBuf    attrBuf;
468.Pp
469
470    assert( strlen(type)    == 4 );
471    assert( strlen(creator) == 4 );
472.Pp
473.
474    memset(&attrList, 0, sizeof(attrList));
475    attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
476    attrList.commonattr  = ATTR_CMN_OBJTYPE | ATTR_CMN_FNDRINFO;
477.Pp
478
479    err = getattrlist(path, &attrList, &attrBuf, sizeof(attrBuf), 0);
480    if (err != 0) {
481        err = errno;
482    }
483.Pp
484
485    if ( (err == 0) && (attrBuf.objType != VREG) ) {
486        fprintf(stderr, "Not a standard file.\en");
487        err = EINVAL;
488    } else {
489        memcpy( &attrBuf.finderInfo[0], type,    4 );
490        memcpy( &attrBuf.finderInfo[4], creator, 4 );
491
492        attrList.commonattr = ATTR_CMN_FNDRINFO;
493        err = setattrlist(
494            path,
495            &attrList,
496            attrBuf.finderInfo,
497            sizeof(attrBuf.finderInfo),
498            0
499        );
500    }
501.Pp
502    return err;
503}
504.Ed
505.Pp
506.
507.Sh SEE ALSO
508.
509.Xr chflags 2 ,
510.Xr chmod 2 ,
511.Xr chown 2 ,
512.Xr getattrlist 2 ,
513.Xr getdirentriesattr 2 ,
514.Xr searchfs 2 ,
515.Xr utimes 2
516.
517.Sh HISTORY
518A
519.Fn setattrlist
520function call appeared in Darwin 1.3.1 (Mac OS X version 10.0). The setatrlistat function call first
521appeared in macOS version 10.13.
522.
523