FAT_IOCTL_SET_ATTRIBUTES(2const)

πŸ’– FAT_IOCTL_SET_ATTRIBUTES πŸ’–

okay bestie, so this is a <pink>2const</pink> page β€” that means it's one of those lil' constant-focused man pages, not a whole syscall. and today's topic?? file attributes on FAT filesystems. yes, the same FAT from your USB stick. we're getting and setting those attribute bits like the absolute icon you are πŸ’…


✨ NAME ✨

FAT_IOCTL_GET_ATTRIBUTES, FAT_IOCTL_SET_ATTRIBUTES β€” get and set file attributes in a FAT filesystem πŸ¦‹


πŸ“š LIBRARY πŸ“š

Standard C library (libc, -lc) β€” the classic, no cap.


🌸 SYNOPSIS 🌸

#include <linux/msdos_fs.h>  /* Definition of FAT_* and
                                ATTR_* constants */
#include <sys/ioctl.h>

int ioctl(int fd, FAT_IOCTL_GET_ATTRIBUTES, uint32_t *attr);
int ioctl(int fd, FAT_IOCTL_SET_ATTRIBUTES, uint32_t *attr);

like, look at that. two ioctls, one attr pointer. it's giving minimalist queen ✨


πŸ‘‘ DESCRIPTION πŸ‘‘

so here's the tea πŸ“ β€” files and directories living on a FAT filesystem each carry around an attribute bit mask. you can read it with FAT_IOCTL_GET_ATTRIBUTES and write it with FAT_IOCTL_SET_ATTRIBUTES. simple as.

the fd argument is a file descriptor for a file or a directory. and get this β€” it is sufficient to create that file descriptor by calling open(2) with the O_RDONLY flag. so yes, read-only open and you can still set attributes. no cap, that's genuinely how it works 🫢

the attr argument is a pointer to a bit mask. and the bits of that bit mask are... drumroll please πŸ₯

  • ATTR_RO β€” this bit says the file or directory is read-only. πŸ™ˆ
  • ATTR_HIDDEN β€” this bit says the file or directory is hidden. sneaky lil' thing.
  • ATTR_SYS β€” this bit says the file is a system file. main character energy, but system edition.
  • ATTR_VOLUME β€” this bit says the file is a volume label. πŸŽ€ heads up tho: this attribute is read-only.
  • ATTR_DIR β€” this bit says this is a directory. also read-only, so don't come for it πŸ’€
  • ATTR_ARCH β€” this bit indicates that this file or directory should be archived. it gets set when a file is created or modified, and it gets reset by an archiving system. the archiving system's lil' todo list basically ⭐

and the zero value ATTR_NONE can be used to indicate that no attribute bit is set. literally the "nothing here" option 🧚


πŸ”₯ RETURN VALUE πŸ”₯

  • on success, 0 is returned. slay.
  • on error, -1 is returned, and errno is set to indicate the error. 😭

🌈 STANDARDS 🌈

Linux. that's it. that's the standard. πŸ’œ


⭐ HISTORY ⭐

Linux 2.6.12. old enough to be iconic, young enough to still serve.


πŸŽ€ EXAMPLES πŸŽ€

okay so this next part is so fun β€” the following program demonstrates the usage of ioctl(2) to manipulate file attributes. it reads and displays the archive attribute of a file. then, after inverting the value of that attribute, it reads and displays the attribute again. a little glow-up-and-check moment ✨

and the following was recorded when applying the program for the file /mnt/user/foo:

# ./toggle_fat_archive_flag /mnt/user/foo
Archive flag is set
Toggling archive flag
Archive flag is not set

🌸 Program source (toggle_fat_archive_flag.c) 🌸

#include <fcntl.h>
#include <linux/msdos_fs.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>

/*
 * Read file attributes of a file on a FAT filesystem.
 * Output the state of the archive flag.
 */
static uint32_t
readattr(int fd)
{
    int       ret;
    uint32_t  attr;

    ret = ioctl(fd, FAT_IOCTL_GET_ATTRIBUTES, &attr);
    if (ret == -1) {
        perror("ioctl");
        exit(EXIT_FAILURE);
    }

    if (attr & ATTR_ARCH)
        printf("Archive flag is set\n");
    else
        printf("Archive flag is not set\n");

    return attr;
}

int
main(int argc, char *argv[])
{
    int       fd;
    int       ret;
    uint32_t  attr;

    if (argc != 2) {
        printf("Usage: %s FILENAME\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    fd = open(argv[1], O_RDONLY);
    if (fd == -1) {
        perror("open");
        exit(EXIT_FAILURE);
    }

    /*
     * Read and display the FAT file attributes.
     */
    attr = readattr(fd);

    /*
     * Invert archive attribute.
     */
    printf("Toggling archive flag\n");
    attr ^= ATTR_ARCH;

    /*
     * Write the changed FAT file attributes.
     */
    ret = ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &attr);
    if (ret == -1) {
        perror("ioctl");
        exit(EXIT_FAILURE);
    }

    /*
     * Read and display the FAT file attributes.
     */
    readattr(fd);

    close(fd);

    exit(EXIT_SUCCESS);
}

like?? the ^= ATTR_ARCH XOR toggle?? that's the whole trick and it ate πŸ”₯


πŸ¦‹ SEE ALSO πŸ¦‹


and that's the whole page bestie πŸ’– this was Linux man-pages 6.20, dated 2025-05-17, and honestly?? iconic of it to be this cute about bit masks. go forth and toggle ur archive flags, iykyk βœ¨πŸ‘‘πŸ’…

πŸ“¦ where this page comes from
  • arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • πŸ“œ the page's license: Linux-man-pages-copyleft
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms