Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read vfat attributes of files in Linux using C

Tags:

c

linux

I have a FAT filesystem mounted in Linux with the vfat driver. I want to know how I would be able to read the vfat attributes of a file such as "hidden" and "read-only".

Based on what I've read so far, if I use the stat() glibc command on a file, I would only be getting the file attributes listed here: http://www.gnu.org/s/libc/manual/html_node/Attribute-Meanings.html#Attribute-Meanings

These don't contain any vfat attributes however. Can anybody help?

Thanks,

Katsupoy

like image 702
Katsupoy Avatar asked Oct 29 '09 15:10

Katsupoy


People also ask

How do I check the attributes of a file in Linux?

You can list the attribute of the contents of a particular directory with lsattr command followed with a file or directory name as the argument. As the ls -l command, the -d option with lsattr will list the attributes of the directory itself instead of the files in that directory.

Which command is used to show file attributes in Linux?

How to view and change the file attributes in Windows. To adjust the attributes of a file in MS-DOS or the Windows command line, use the attrib command. In Linux, Linux variants, and Unix to adjust the file attributes use the chmod command.

Does Linux support Vfat?

Linux has several filesystem drivers for the File Allocation Table (FAT) filesystem format. These are commonly known by the names used in the mount command to invoke particular drivers in the kernel: msdos, vfat, and umsdos.

What is Vfat used for in Linux?

VFAT enables a hard disk drive to store files with names that are more than eight characters long.


1 Answers

FAT's DOS attributes do not map well to the UNIX filesystem model, so Linux's vfat driver does not reflect them.

Instead of mounting the filesystem, use mtools to read the filesystem from userspace.


Edit I lied. Apparently the vfat driver is able to deal with these DOS attributes, at least as of 2.4.29 (I think; my historical logs don't go back that far).

#include <inttypes.h>
#include <sys/ioctl.h>
#include <linux/msdos_fs.h>

int fd = open("/mnt/fat/...", ...);
__u32 attrs;
ioctl(fd, FAT_IOCTL_GET_ATTRIBUTES, &attrs);
attrs = ATTR_NONE;  /* ATTR_{RO,HIDDEN,SYS,VOLUME,DIR,ARCH} */
ioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &attrs);
like image 155
ephemient Avatar answered Sep 22 '22 04:09

ephemient