How are filenames stored?

6

4

I noticed that renaming a file doesn't change its hash. I also noticed that creating an empty file gives a file of zero bytes, yet it has a filename.

Since the filename seems to travel with the file across devices I was wondering what exactly is the filename? How come it can be changed without altering the hash, and why doesn't it seem to take up any space?

I guess if I understood how they are stored, it would answer those questions.

Louis

Posted 2012-11-28T03:18:02.250

Reputation: 18 859

Answers

10

In NTFS, all file data — file name, creation date, access permissions (by the use of access control lists), and contents — are stored as metadata in the Master File Table. In Windows world, you might also have heard about the File Allocation Table. Other file systems have other names and structures.

All file systems work this way. A certain amount of space is reserved, usually at the very beginning of the disk, right after the boot sector. That space contains a data structure which contains the information to retrieve the files.

The Master File Table is not a file. It is just organized data. As a matter of fact, files are just data that have a beginning and a size, stored in an MFT, FAT, or similar. When you change the file name, you do not change the file, just some bytes in the Master File Table.

Since the name and attributes are different from the data, you can download a file stored on a Linux server to a Windows machine. The data is transferred, not the metadata. You have to name and select a location for the file yourself (although the server and browser can hint you the original file name).

ixe013

Posted 2012-11-28T03:18:02.250

Reputation: 738

4

Your explanation made me wonder how the MFT changes in size over time. How NTFS reserves space for its Master File Table (MFT) has some information about that. I thought this was interesting: NTFS reserves 12.5 percent of the volume for exclusive use of the MFT until and unless the remainder of the volume is completely used up...

If the unreserved space becomes full, space for user files and directories starts to be allocated from the MFT zone...

If the MFT zone becomes full, space for new MFT entries is allocated from the remainder of the disk...

– Louis – 2012-11-28T03:57:41.303

But what if file transfer via torrent p2p or ftp?? How does it work then?? (Without dialogs) – DrStrangeLove – 2013-01-28T04:14:30.340

1It is protocol dependent. Many protocols have a way to send the original file metadata, such as name and creation date. HTTP uses 'content-disposition' for this. Some protocols let the user select the final name, others don't, leaving you to change it after the file is transfered. If you have a question about a specific protocol, I suggest you make it a new question. +Keep in mind that there might not even be a file on the server, like a PDF of an invoice that is generated on the fly when you ask for it. – ixe013 – 2013-01-28T15:29:41.693