1 .\" Copyright (C) 2014, Heinrich Schuchardt <xypron.glpk@gmx.de>
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of
9 .\" this manual under the conditions for verbatim copying, provided that
10 .\" the entire resulting derived work is distributed under the terms of
11 .\" a permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume
15 .\" no responsibility for errors or omissions, or for damages resulting
16 .\" from the use of the information contained herein. The author(s) may
17 .\" not have taken the same level of care in the production of this
18 .\" manual, which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
24 .TH IOCTL_FAT 2 2021-03-22 "Linux" "Linux Programmer's Manual"
26 ioctl_fat \- manipulating the FAT filesystem
29 .BR "#include <linux/msdos_fs.h>" " /* Definition of [" V ] FAT_* " and"
30 .BR " ATTR_* " constants */"
31 .B #include <sys/ioctl.h>
33 .BI "int ioctl(int " fd ", FAT_IOCTL_GET_ATTRIBUTES, uint32_t *" attr );
34 .BI "int ioctl(int " fd ", FAT_IOCTL_SET_ATTRIBUTES, uint32_t *" attr );
35 .BI "int ioctl(int " fd ", FAT_IOCTL_GET_VOLUME_ID, uint32_t *" id );
36 .BI "int ioctl(int " fd ", VFAT_IOCTL_READDIR_BOTH,"
37 .BI " struct __fat_dirent " entry [2]);
38 .BI "int ioctl(int " fd ", VFAT_IOCTL_READDIR_SHORT,"
39 .BI " struct __fat_dirent " entry [2]);
44 system call can be used to read and write metadata of FAT filesystems that
45 are not accessible using other system calls.
46 .SS Reading and setting file attributes
47 Files and directories in the FAT filesystem possess an attribute bit mask that
49 .B FAT_IOCTL_GET_ATTRIBUTES
51 .BR FAT_IOCTL_SET_ATTRIBUTES .
55 argument contains a file descriptor for a file or directory.
56 It is sufficient to create the file descriptor by calling
64 argument contains a pointer to a bit mask.
65 The bits of the bit mask are:
68 This bit specifies that the file or directory is read-only.
71 This bit specifies that the file or directory is hidden.
74 This bit specifies that the file is a system file.
77 This bit specifies that the file is a volume label.
78 This attribute is read-only.
81 This bit specifies that this is a directory.
82 This attribute is read-only.
85 This bit indicates that this file or directory should be archived.
86 It is set when a file is created or modified.
87 It is reset by an archiving system.
91 can be used to indicate that no attribute bit is set.
92 .SS Reading the volume ID
93 FAT filesystems are identified by a volume ID.
94 The volume ID can be read with
95 .BR FAT_IOCTL_GET_VOLUME_ID .
99 argument can be a file descriptor for any file or directory of the
101 It is sufficient to create the file descriptor by calling
109 argument is a pointer to the field that will be filled with the volume ID.
110 Typically the volume ID is displayed to the user as a group of two
115 printf("Volume ID %04x\-%04x\en", id >> 16, id & 0xFFFF);
118 .SS Reading short filenames of a directory
119 A file or directory on a FAT filesystem always has a short filename
120 consisting of up to 8 capital letters, optionally followed by a period
121 and up to 3 capital letters for the file extension.
122 If the actual filename does not fit into this scheme, it is stored
123 as a long filename of up to 255 UTF-16 characters.
125 The short filenames in a directory can be read with
126 .BR VFAT_IOCTL_READDIR_SHORT .
127 .B VFAT_IOCTL_READDIR_BOTH
128 reads both the short and the long filenames.
132 argument must be a file descriptor for a directory.
133 It is sufficient to create the file descriptor by calling
138 The file descriptor can be used only once to iterate over the directory
145 argument is a two-element array of the following structures:
149 struct __fat_dirent {
151 __kernel_off_t d_off;
152 uint32_t short d_reclen;
158 The first entry in the array is for the short filename.
159 The second entry is for the long filename.
165 fields are filled only for long filenames.
168 field holds the inode number of the directory.
171 field holds the offset of the file entry in the directory.
172 As these values are not available for short filenames, the user code should
177 contains the length of the filename in the field
179 To keep backward compatibility, a length of 0 for the short filename signals
180 that the end of the directory has been reached.
181 However, the preferred method for detecting the end of the directory
185 If no long filename exists, field
189 is a character string of length 0 for the long filename.
191 On error, \-1 is returned, and
193 is set to indicate the error.
196 .B VFAT_IOCTL_READDIR_BOTH
198 .B VFAT_IOCTL_READDIR_SHORT
199 a return value of 1 signals that a new directory entry has been read and
200 a return value of 0 signals that the end of the directory has been reached.
204 This error is returned by
205 .B VFAT_IOCTL_READDIR_BOTH
207 .B VFAT_IOCTL_READDIR_SHORT
208 if the file descriptor
210 refers to a removed, but still open directory.
213 This error is returned by
214 .B VFAT_IOCTL_READDIR_BOTH
216 .B VFAT_IOCTL_READDIR_SHORT
217 if the file descriptor
219 does not refer to a directory.
224 does not refer to an object in a FAT filesystem.
226 For further error values, see
229 .BR VFAT_IOCTL_READDIR_BOTH
231 .B VFAT_IOCTL_READDIR_SHORT
232 first appeared in Linux 2.0.
234 .BR FAT_IOCTL_GET_ATTRIBUTES
236 .BR FAT_IOCTL_SET_ATTRIBUTES
238 .\" just before we got Git history
241 .B FAT_IOCTL_GET_VOLUME_ID
242 was introduced in version 3.11
243 .\" commit 6e5b93ee55d401f1619092fb675b57c28c9ed7ec
246 This API is Linux-specific.
248 .SS Toggling the archive flag
249 The following program demonstrates the usage of
251 to manipulate file attributes.
252 The program reads and displays the archive attribute of a file.
253 After inverting the value of the attribute,
254 the program reads and displays the attribute again.
256 The following was recorded when applying the program for the file
261 # ./toggle_fat_archive_flag /mnt/user/foo
263 Toggling archive flag
264 Archive flag is not set
267 .SS Program source (toggle_fat_archive_flag.c)
271 #include <linux/msdos_fs.h>
275 #include <sys/ioctl.h>
279 * Read file attributes of a file on a FAT filesystem.
280 * Output the state of the archive flag.
288 ret = ioctl(fd, FAT_IOCTL_GET_ATTRIBUTES, &attr);
294 if (attr & ATTR_ARCH)
295 printf("Archive flag is set\en");
297 printf("Archive flag is not set\en");
303 main(int argc, char *argv[])
310 printf("Usage: %s FILENAME\en", argv[0]);
314 fd = open(argv[1], O_RDONLY);
321 * Read and display the FAT file attributes.
326 * Invert archive attribute.
328 printf("Toggling archive flag\en");
329 attr \(ha= ATTR_ARCH;
332 * Write the changed FAT file attributes.
334 ret = ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &attr);
341 * Read and display the FAT file attributes.
351 .SS Reading the volume ID
352 The following program demonstrates the use of
354 to display the volume ID of a FAT filesystem.
356 The following output was recorded when applying the program for
362 $ ./display_fat_volume_id /mnt/user
366 .SS Program source (display_fat_volume_id.c)
370 #include <linux/msdos_fs.h>
374 #include <sys/ioctl.h>
378 main(int argc, char *argv[])
385 printf("Usage: %s FILENAME\en", argv[0]);
389 fd = open(argv[1], O_RDONLY);
398 ret = ioctl(fd, FAT_IOCTL_GET_VOLUME_ID, &id);
405 * Format the output as two groups of 16 bits each.
407 printf("Volume ID %04x\-%04x\en", id >> 16, id & 0xFFFF);
415 .SS Listing a directory
416 The following program demonstrates the use of
420 The following was recorded when applying the program to the directory
425 $ \fB./fat_dir /mnt/user\fP
426 \[char46] \-> \(aq\(aq
427 \[char46]. \-> \(aq\(aq
428 ALONGF\(ti1.TXT \-> \(aqa long filename.txt\(aq
429 UPPER.TXT \-> \(aq\(aq
430 LOWER.TXT \-> \(aqlower.txt\(aq
438 #include <linux/msdos_fs.h>
441 #include <sys/ioctl.h>
445 main(int argc, char *argv[])
447 struct __fat_dirent entry[2];
452 printf("Usage: %s DIRECTORY\en", argv[0]);
457 * Open file descriptor for the directory.
459 fd = open(argv[1], O_RDONLY | O_DIRECTORY);
468 * Read next directory entry.
470 ret = ioctl( fd, VFAT_IOCTL_READDIR_BOTH, entry);
473 * If an error occurs, the return value is \-1.
474 * If the end of the directory list has been reached,
475 * the return value is 0.
476 * For backward compatibility the end of the directory
477 * list is also signaled by d_reclen == 0.
483 * Write both the short name and the long name.
485 printf("%s \-> \(aq%s\(aq\en", entry[0].d_name, entry[1].d_name);
489 perror("VFAT_IOCTL_READDIR_BOTH");
494 * Close the file descriptor.