1 .\" Copyright (C) 2014, Heinrich Schuchardt <xypron.glpk@gmx.de>
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
4 .TH IOCTL_FAT 2 2021-03-22 "Linux" "Linux Programmer's Manual"
6 ioctl_fat \- manipulating the FAT filesystem
12 .BR "#include <linux/msdos_fs.h>" " /* Definition of [" V ] FAT_* " and"
13 .BR " ATTR_* " constants */"
14 .B #include <sys/ioctl.h>
16 .BI "int ioctl(int " fd ", FAT_IOCTL_GET_ATTRIBUTES, uint32_t *" attr );
17 .BI "int ioctl(int " fd ", FAT_IOCTL_SET_ATTRIBUTES, uint32_t *" attr );
18 .BI "int ioctl(int " fd ", FAT_IOCTL_GET_VOLUME_ID, uint32_t *" id );
19 .BI "int ioctl(int " fd ", VFAT_IOCTL_READDIR_BOTH,"
20 .BI " struct __fat_dirent " entry [2]);
21 .BI "int ioctl(int " fd ", VFAT_IOCTL_READDIR_SHORT,"
22 .BI " struct __fat_dirent " entry [2]);
27 system call can be used to read and write metadata of FAT filesystems that
28 are not accessible using other system calls.
29 .SS Reading and setting file attributes
30 Files and directories in the FAT filesystem possess an attribute bit mask that
32 .B FAT_IOCTL_GET_ATTRIBUTES
34 .BR FAT_IOCTL_SET_ATTRIBUTES .
38 argument contains a file descriptor for a file or directory.
39 It is sufficient to create the file descriptor by calling
47 argument contains a pointer to a bit mask.
48 The bits of the bit mask are:
51 This bit specifies that the file or directory is read-only.
54 This bit specifies that the file or directory is hidden.
57 This bit specifies that the file is a system file.
60 This bit specifies that the file is a volume label.
61 This attribute is read-only.
64 This bit specifies that this is a directory.
65 This attribute is read-only.
68 This bit indicates that this file or directory should be archived.
69 It is set when a file is created or modified.
70 It is reset by an archiving system.
74 can be used to indicate that no attribute bit is set.
75 .SS Reading the volume ID
76 FAT filesystems are identified by a volume ID.
77 The volume ID can be read with
78 .BR FAT_IOCTL_GET_VOLUME_ID .
82 argument can be a file descriptor for any file or directory of the
84 It is sufficient to create the file descriptor by calling
92 argument is a pointer to the field that will be filled with the volume ID.
93 Typically the volume ID is displayed to the user as a group of two
98 printf("Volume ID %04x\-%04x\en", id >> 16, id & 0xFFFF);
101 .SS Reading short filenames of a directory
102 A file or directory on a FAT filesystem always has a short filename
103 consisting of up to 8 capital letters, optionally followed by a period
104 and up to 3 capital letters for the file extension.
105 If the actual filename does not fit into this scheme, it is stored
106 as a long filename of up to 255 UTF-16 characters.
108 The short filenames in a directory can be read with
109 .BR VFAT_IOCTL_READDIR_SHORT .
110 .B VFAT_IOCTL_READDIR_BOTH
111 reads both the short and the long filenames.
115 argument must be a file descriptor for a directory.
116 It is sufficient to create the file descriptor by calling
121 The file descriptor can be used only once to iterate over the directory
128 argument is a two-element array of the following structures:
132 struct __fat_dirent {
134 __kernel_off_t d_off;
135 uint32_t short d_reclen;
141 The first entry in the array is for the short filename.
142 The second entry is for the long filename.
148 fields are filled only for long filenames.
151 field holds the inode number of the directory.
154 field holds the offset of the file entry in the directory.
155 As these values are not available for short filenames, the user code should
160 contains the length of the filename in the field
162 To keep backward compatibility, a length of 0 for the short filename signals
163 that the end of the directory has been reached.
164 However, the preferred method for detecting the end of the directory
168 If no long filename exists, field
172 is a character string of length 0 for the long filename.
174 On error, \-1 is returned, and
176 is set to indicate the error.
179 .B VFAT_IOCTL_READDIR_BOTH
181 .B VFAT_IOCTL_READDIR_SHORT
182 a return value of 1 signals that a new directory entry has been read and
183 a return value of 0 signals that the end of the directory has been reached.
187 This error is returned by
188 .B VFAT_IOCTL_READDIR_BOTH
190 .B VFAT_IOCTL_READDIR_SHORT
191 if the file descriptor
193 refers to a removed, but still open directory.
196 This error is returned by
197 .B VFAT_IOCTL_READDIR_BOTH
199 .B VFAT_IOCTL_READDIR_SHORT
200 if the file descriptor
202 does not refer to a directory.
207 does not refer to an object in a FAT filesystem.
209 For further error values, see
212 .B VFAT_IOCTL_READDIR_BOTH
214 .B VFAT_IOCTL_READDIR_SHORT
215 first appeared in Linux 2.0.
217 .B FAT_IOCTL_GET_ATTRIBUTES
219 .B FAT_IOCTL_SET_ATTRIBUTES
221 .\" just before we got Git history
224 .B FAT_IOCTL_GET_VOLUME_ID
225 was introduced in version 3.11
226 .\" commit 6e5b93ee55d401f1619092fb675b57c28c9ed7ec
229 This API is Linux-specific.
231 .SS Toggling the archive flag
232 The following program demonstrates the usage of
234 to manipulate file attributes.
235 The program reads and displays the archive attribute of a file.
236 After inverting the value of the attribute,
237 the program reads and displays the attribute again.
239 The following was recorded when applying the program for the file
244 # ./toggle_fat_archive_flag /mnt/user/foo
246 Toggling archive flag
247 Archive flag is not set
250 .SS Program source (toggle_fat_archive_flag.c)
254 #include <linux/msdos_fs.h>
258 #include <sys/ioctl.h>
262 * Read file attributes of a file on a FAT filesystem.
263 * Output the state of the archive flag.
271 ret = ioctl(fd, FAT_IOCTL_GET_ATTRIBUTES, &attr);
277 if (attr & ATTR_ARCH)
278 printf("Archive flag is set\en");
280 printf("Archive flag is not set\en");
286 main(int argc, char *argv[])
293 printf("Usage: %s FILENAME\en", argv[0]);
297 fd = open(argv[1], O_RDONLY);
304 * Read and display the FAT file attributes.
309 * Invert archive attribute.
311 printf("Toggling archive flag\en");
312 attr \(ha= ATTR_ARCH;
315 * Write the changed FAT file attributes.
317 ret = ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &attr);
324 * Read and display the FAT file attributes.
334 .SS Reading the volume ID
335 The following program demonstrates the use of
337 to display the volume ID of a FAT filesystem.
339 The following output was recorded when applying the program for
345 $ ./display_fat_volume_id /mnt/user
349 .SS Program source (display_fat_volume_id.c)
353 #include <linux/msdos_fs.h>
357 #include <sys/ioctl.h>
361 main(int argc, char *argv[])
368 printf("Usage: %s FILENAME\en", argv[0]);
372 fd = open(argv[1], O_RDONLY);
381 ret = ioctl(fd, FAT_IOCTL_GET_VOLUME_ID, &id);
388 * Format the output as two groups of 16 bits each.
390 printf("Volume ID %04x\-%04x\en", id >> 16, id & 0xFFFF);
398 .SS Listing a directory
399 The following program demonstrates the use of
403 The following was recorded when applying the program to the directory
408 $ \fB./fat_dir /mnt/user\fP
409 \[char46] \-> \(aq\(aq
410 \[char46]. \-> \(aq\(aq
411 ALONGF\(ti1.TXT \-> \(aqa long filename.txt\(aq
412 UPPER.TXT \-> \(aq\(aq
413 LOWER.TXT \-> \(aqlower.txt\(aq
421 #include <linux/msdos_fs.h>
424 #include <sys/ioctl.h>
428 main(int argc, char *argv[])
430 struct __fat_dirent entry[2];
435 printf("Usage: %s DIRECTORY\en", argv[0]);
440 * Open file descriptor for the directory.
442 fd = open(argv[1], O_RDONLY | O_DIRECTORY);
451 * Read next directory entry.
453 ret = ioctl( fd, VFAT_IOCTL_READDIR_BOTH, entry);
456 * If an error occurs, the return value is \-1.
457 * If the end of the directory list has been reached,
458 * the return value is 0.
459 * For backward compatibility the end of the directory
460 * list is also signaled by d_reclen == 0.
466 * Write both the short name and the long name.
468 printf("%s \-> \(aq%s\(aq\en", entry[0].d_name, entry[1].d_name);
472 perror("VFAT_IOCTL_READDIR_BOTH");
477 * Close the file descriptor.