1IOCTL_FAT(2)               Linux Programmer's Manual              IOCTL_FAT(2)
2
3
4

NAME

6       ioctl_fat - manipulating the FAT filesystem
7

SYNOPSIS

9       #include <linux/msdos_fs.h>     /* Definition of [V]FAT_* and
10                                          ATTR_* constants*/"
11       #include <sys/ioctl.h>
12
13       int ioctl(int fd, FAT_IOCTL_GET_ATTRIBUTES, uint32_t *attr);
14       int ioctl(int fd, FAT_IOCTL_SET_ATTRIBUTES, uint32_t *attr);
15       int ioctl(int fd, FAT_IOCTL_GET_VOLUME_ID, uint32_t *id);
16       int ioctl(int fd, VFAT_IOCTL_READDIR_BOTH,
17                 struct __fat_dirent entry[2]);
18       int ioctl(int fd, VFAT_IOCTL_READDIR_SHORT,
19                 struct __fat_dirent entry[2]);
20

DESCRIPTION

22       The  ioctl(2) system call can be used to read and write metadata of FAT
23       filesystems that are not accessible using other system calls.
24
25   Reading and setting file attributes
26       Files and directories in the FAT filesystem possess  an  attribute  bit
27       mask  that  can  be read with FAT_IOCTL_GET_ATTRIBUTES and written with
28       FAT_IOCTL_SET_ATTRIBUTES.
29
30       The fd argument contains a file descriptor for a file or directory.  It
31       is sufficient to create the file descriptor by calling open(2) with the
32       O_RDONLY flag.
33
34       The attr argument contains a pointer to a bit mask.  The  bits  of  the
35       bit mask are:
36
37       ATTR_RO
38              This bit specifies that the file or directory is read-only.
39
40       ATTR_HIDDEN
41              This bit specifies that the file or directory is hidden.
42
43       ATTR_SYS
44              This bit specifies that the file is a system file.
45
46       ATTR_VOLUME
47              This bit specifies that the file is a volume label.  This attri‐
48              bute is read-only.
49
50       ATTR_DIR
51              This bit specifies that this is a directory.  This attribute  is
52              read-only.
53
54       ATTR_ARCH
55              This  bit  indicates  that  this  file  or  directory  should be
56              archived.  It is set when a file is created or modified.  It  is
57              reset by an archiving system.
58
59       The  zero value ATTR_NONE can be used to indicate that no attribute bit
60       is set.
61
62   Reading the volume ID
63       FAT filesystems are identified by a volume ID.  The volume  ID  can  be
64       read with FAT_IOCTL_GET_VOLUME_ID.
65
66       The  fd  argument can be a file descriptor for any file or directory of
67       the filesystem.  It is sufficient to  create  the  file  descriptor  by
68       calling open(2) with the O_RDONLY flag.
69
70       The  id argument is a pointer to the field that will be filled with the
71       volume ID.  Typically the volume ID is displayed to the user as a group
72       of two 16-bit fields:
73
74           printf("Volume ID %04x-%04x\n", id >> 16, id & 0xFFFF);
75
76   Reading short filenames of a directory
77       A  file  or  directory  on a FAT filesystem always has a short filename
78       consisting of up to 8 capital letters, optionally followed by a  period
79       and  up  to  3  capital  letters for the file extension.  If the actual
80       filename does not fit into this scheme, it is stored as a long filename
81       of up to 255 UTF-16 characters.
82
83       The  short  filenames  in a directory can be read with VFAT_IOCTL_READ‐
84       DIR_SHORT.  VFAT_IOCTL_READDIR_BOTH reads both the short and  the  long
85       filenames.
86
87       The  fd argument must be a file descriptor for a directory.  It is suf‐
88       ficient to create the file  descriptor  by  calling  open(2)  with  the
89       O_RDONLY  flag.   The  file descriptor can be used only once to iterate
90       over the directory entries by calling ioctl(2) repeatedly.
91
92       The entry argument is a two-element array of the following structures:
93
94           struct __fat_dirent {
95               long            d_ino;
96               __kernel_off_t  d_off;
97               uint32_t short  d_reclen;
98               char            d_name[256];
99           };
100
101       The first entry in the array is for the short filename.  The second en‐
102       try is for the long filename.
103
104       The  d_ino  and  d_off  fields are filled only for long filenames.  The
105       d_ino field holds the inode number of the directory.  The  d_off  field
106       holds  the  offset of the file entry in the directory.  As these values
107       are not available for short filenames, the user code should simply  ig‐
108       nore them.
109
110       The  field  d_reclen  contains  the length of the filename in the field
111       d_name.  To keep backward compatibility, a length of 0  for  the  short
112       filename  signals that the end of the directory has been reached.  How‐
113       ever, the preferred method for detecting the end of the directory is to
114       test  the  ioctl(2)  return  value.   If no long filename exists, field
115       d_reclen is set to 0 and d_name is a character string of length  0  for
116       the long filename.
117

RETURN VALUE

119       On error, -1 is returned, and errno is set to indicate the error.
120
121       For VFAT_IOCTL_READDIR_BOTH and VFAT_IOCTL_READDIR_SHORT a return value
122       of 1 signals that a new directory entry has  been  read  and  a  return
123       value of 0 signals that the end of the directory has been reached.
124

ERRORS

126       ENOENT This   error   is   returned   by   VFAT_IOCTL_READDIR_BOTH  and
127              VFAT_IOCTL_READDIR_SHORT if the file descriptor fd refers  to  a
128              removed, but still open directory.
129
130       ENOTDIR
131              This   error   is   returned   by   VFAT_IOCTL_READDIR_BOTH  and
132              VFAT_IOCTL_READDIR_SHORT if the file descriptor fd does not  re‐
133              fer to a directory.
134
135       ENOTTY The  file  descriptor  fd  does  not refer to an object in a FAT
136              filesystem.
137
138       For further error values, see ioctl(2).
139

VERSIONS

141       VFAT_IOCTL_READDIR_BOTH and VFAT_IOCTL_READDIR_SHORT first appeared  in
142       Linux 2.0.
143
144       FAT_IOCTL_GET_ATTRIBUTES and FAT_IOCTL_SET_ATTRIBUTES first appeared in
145       Linux 2.6.12.
146
147       FAT_IOCTL_GET_VOLUME_ID was introduced in version  3.11  of  the  Linux
148       kernel.
149

CONFORMING TO

151       This API is Linux-specific.
152

EXAMPLES

154   Toggling the archive flag
155       The  following program demonstrates the usage of ioctl(2) to manipulate
156       file attributes.  The program reads and displays the archive  attribute
157       of  a  file.   After  inverting the value of the attribute, the program
158       reads and displays the attribute again.
159
160       The following was recorded when  applying  the  program  for  the  file
161       /mnt/user/foo:
162
163           # ./toggle_fat_archive_flag /mnt/user/foo
164           Archive flag is set
165           Toggling archive flag
166           Archive flag is not set
167
168   Program source (toggle_fat_archive_flag.c)
169
170       #include <fcntl.h>
171       #include <linux/msdos_fs.h>
172       #include <stdint.h>
173       #include <stdio.h>
174       #include <stdlib.h>
175       #include <sys/ioctl.h>
176       #include <unistd.h>
177
178       /*
179        * Read file attributes of a file on a FAT filesystem.
180        * Output the state of the archive flag.
181        */
182       static uint32_t
183       readattr(int fd)
184       {
185           uint32_t attr;
186           int ret;
187
188           ret = ioctl(fd, FAT_IOCTL_GET_ATTRIBUTES, &attr);
189           if (ret == -1) {
190               perror("ioctl");
191               exit(EXIT_FAILURE);
192           }
193
194           if (attr & ATTR_ARCH)
195               printf("Archive flag is set\n");
196           else
197               printf("Archive flag is not set\n");
198
199           return attr;
200       }
201
202       int
203       main(int argc, char *argv[])
204       {
205           uint32_t attr;
206           int fd;
207           int ret;
208
209           if (argc != 2) {
210               printf("Usage: %s FILENAME\n", argv[0]);
211               exit(EXIT_FAILURE);
212           }
213
214           fd = open(argv[1], O_RDONLY);
215           if (fd == -1) {
216               perror("open");
217               exit(EXIT_FAILURE);
218           }
219
220           /*
221            * Read and display the FAT file attributes.
222            */
223           attr = readattr(fd);
224
225           /*
226            * Invert archive attribute.
227            */
228           printf("Toggling archive flag\n");
229           attr ^= ATTR_ARCH;
230
231           /*
232            * Write the changed FAT file attributes.
233            */
234           ret = ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &attr);
235           if (ret == -1) {
236               perror("ioctl");
237               exit(EXIT_FAILURE);
238           }
239
240           /*
241            * Read and display the FAT file attributes.
242            */
243           readattr(fd);
244
245           close(fd);
246
247           exit(EXIT_SUCCESS);
248       }
249
250   Reading the volume ID
251       The  following  program demonstrates the use of ioctl(2) to display the
252       volume ID of a FAT filesystem.
253
254       The following output was recorded when applying the program for  direc‐
255       tory /mnt/user:
256
257           $ ./display_fat_volume_id /mnt/user
258           Volume ID 6443-6241
259
260   Program source (display_fat_volume_id.c)
261
262       #include <fcntl.h>
263       #include <linux/msdos_fs.h>
264       #include <stdint.h>
265       #include <stdio.h>
266       #include <stdlib.h>
267       #include <sys/ioctl.h>
268       #include <unistd.h>
269
270       int
271       main(int argc, char *argv[])
272       {
273           uint32_t id;
274           int fd;
275           int ret;
276
277           if (argc != 2) {
278               printf("Usage: %s FILENAME\n", argv[0]);
279               exit(EXIT_FAILURE);
280           }
281
282           fd = open(argv[1], O_RDONLY);
283           if (fd == -1) {
284               perror("open");
285               exit(EXIT_FAILURE);
286           }
287
288           /*
289            * Read volume ID.
290            */
291           ret = ioctl(fd, FAT_IOCTL_GET_VOLUME_ID, &id);
292           if (ret == -1) {
293               perror("ioctl");
294               exit(EXIT_FAILURE);
295           }
296
297           /*
298            * Format the output as two groups of 16 bits each.
299            */
300           printf("Volume ID %04x-%04x\n", id >> 16, id & 0xFFFF);
301
302           close(fd);
303
304           exit(EXIT_SUCCESS);
305       }
306
307   Listing a directory
308       The following program demonstrates the use of ioctl(2) to list a direc‐
309       tory.
310
311       The following was recorded when applying the program to  the  directory
312       /mnt/user:
313
314           $ ./fat_dir /mnt/user
315           . -> ''
316           .. -> ''
317           ALONGF~1.TXT -> 'a long filename.txt'
318           UPPER.TXT -> ''
319           LOWER.TXT -> 'lower.txt'
320
321   Program source
322           #include <fcntl.h>
323           #include <linux/msdos_fs.h>
324           #include <stdio.h>
325           #include <stdlib.h>
326           #include <sys/ioctl.h>
327           #include <unistd.h>
328
329           int
330           main(int argc, char *argv[])
331           {
332               struct __fat_dirent entry[2];
333               int fd;
334               int ret;
335
336               if (argc != 2) {
337                   printf("Usage: %s DIRECTORY\n", argv[0]);
338                   exit(EXIT_FAILURE);
339               }
340
341               /*
342                * Open file descriptor for the directory.
343                */
344               fd = open(argv[1], O_RDONLY | O_DIRECTORY);
345               if (fd == -1) {
346                   perror("open");
347                   exit(EXIT_FAILURE);
348               }
349
350               for (;;) {
351
352                   /*
353                    * Read next directory entry.
354                    */
355                   ret = ioctl( fd, VFAT_IOCTL_READDIR_BOTH, entry);
356
357                   /*
358                    * If an error occurs, the return value is -1.
359                    * If the end of the directory list has been reached,
360                    * the return value is 0.
361                    * For backward compatibility the end of the directory
362                    * list is also signaled by d_reclen == 0.
363                    */
364                   if (ret < 1)
365                       break;
366
367                   /*
368                    * Write both the short name and the long name.
369                    */
370                   printf("%s -> '%s'\n", entry[0].d_name, entry[1].d_name);
371               }
372
373               if (ret == -1) {
374                   perror("VFAT_IOCTL_READDIR_BOTH");
375                   exit(EXIT_FAILURE);
376               }
377
378               /*
379                * Close the file descriptor.
380                */
381               close(fd);
382
383               exit(EXIT_SUCCESS);
384           }
385

SEE ALSO

387       ioctl(2)
388

COLOPHON

390       This  page  is  part of release 5.12 of the Linux man-pages project.  A
391       description of the project, information about reporting bugs,  and  the
392       latest     version     of     this    page,    can    be    found    at
393       https://www.kernel.org/doc/man-pages/.
394
395
396
397Linux                             2021-03-22                      IOCTL_FAT(2)
Impressum