Definition
File metadata is the information a file system keeps about a file: its type, size, owner, permission bits, link count, identity and timestamps. File content is the sequence of bytes stored in the file, which a program obtains by reading it. Metadata describes the file; content is what the file holds. On Unix-like systems the stat call returns the metadata without reading any content. Neither determines the other: two files can have equal metadata and different content, or equal content and different metadata.
The idea
Checking content is slow, because every byte must be read. Checking metadata is fast: one stat call per file. So many tools judge from metadata whether content changed. The build tool make rebuilds a target only when an input has a newer modification time than the target. The file copier rsync by default skips a file whose size and modification time match the copy at the destination. Both work well in ordinary use, and both are fooled by a file whose bytes changed while its size and modification time stayed the same.
Metadata is the label on a box, not the contents. The system usually writes the label as the box is filled, which is why the label is usually right. But whoever may change the box can also rewrite parts of the label, and two boxes with identical labels can hold different things. Metadata also has a strength of its own: a checker can confirm that a file holding a secret key is a regular file, owned by the right user and readable by nobody else, without permission to read the key.
How it works
On Unix-like systems the metadata lives in the file’s inode (index node), the record the file system keeps for each file, and stat returns it. The fields are:
type: regular file, directory, symbolic link §13.10, device and others;
mode: the permission bits, which say who may read, write and execute the file;
owner and group: numeric user and group identifiers;
size, in bytes;
link count: the number of directory entries that name the file;
identity: the device number of the file system and the inode number, which together identify the file while it exists;
timestamps: the modification time (last change of content), the status-change time (last change of content or of other metadata, such as owner or permissions) and the access time (last read); some systems also record a creation time.
Three facts about timestamps decide what they can prove. First, the owner of a file can set its modification and access times to any value, for example with touch -d, or with touch -r, which copies them from another file; copying tools that preserve times do the same. Second, the system sets the status-change time to the current clock time whenever the content or the metadata is changed, including when a program sets the other two times explicitly (an ordinary read, which updates only the access time, does not count). No call sets the status-change time to a chosen value; only an administrator can influence it, for example by changing the system clock. Third, timestamps have a limited resolution: nanoseconds on many current file systems, but 2 seconds for modification times on FAT (File Allocation Table) file systems. Two writes within one such interval can leave the same modification time.
Each question therefore needs its own check.
Same content as a trusted reference? Compare the bytes, or compare a digest (a fingerprint of the bytes computed by a cryptographic hash function §5.1) with a digest obtained from a trusted place.
Same file object? Compare device and inode numbers, while the file is known to exist; an inode number can be reused after its file is deleted.
Right type, owner and permissions? Read the metadata.
Unchanged while being read? Open the file once, call
fstaton the resulting file descriptor §15.21 (the handle the program receives when it opens a file), read the content through that descriptor, callfstatagain, and reject the result if the size, modification time or status-change time moved. A moved time proves a change. An unmoved time does not prove the absence of one, because of the resolution limit.
Worked example
File p holds the 4 bytes 41 42 43 44, written in hexadecimal, which are the ASCII text ABCD. File q holds 57 58 59 5A, the text WXYZ. Both have size 4 bytes, permission bits 644 in octal and the same owner. Running touch -r p q gives q the modification time of p, say 1,700,000,000 seconds after 1 January 1970.
A check of size and modification time now finds and , and declares the files the same. A byte comparison finds that all 4 of the 4 positions differ. Their SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output) digests begin e12e115acf4552b2 for p and 21e32f5321cad49a for q. Only the status-change time of q hints at what happened, because it records when touch ran; the size-and-time check never looked at it.
Conversely, cp p r creates r with the same 4 bytes and therefore the same digest, beginning e12e115acf4552b2. Its metadata differs: r has a new inode number, a modification time equal to the moment of copying, and the copying user as owner. Equal content came with unequal metadata.
Stated precisely
Let be the tuple of metadata fields that a check compares for file , and let be its content, the byte string obtained by reading from start to end. With , the example gives and . For any that includes the inode number, it gives and . So neither nor holds in general.
Content equality is established either directly, by , or by for a cryptographic hash function , under the assumption that is collision resistant §5.5. A metadata match may stand in for content equality only when three conditions hold, and a claim that relies on it must name them:
nobody who might want to hide a change can write the file or set its times;
every write changes the compared metadata, which needs a timestamp resolution finer than the interval between writes;
the metadata and the content are read from the same file object.
A property that involves both, such as “this exact content, in a regular file owned by the administrator”, needs both checks, made on the same open file.
Limits and common mistakes
Equal size and timestamps are a hint that tools use for speed, not a content guarantee. A deliberate change, or two writes within one timestamp interval, defeats them. Git, for example, uses stored metadata to decide which files to read again, and reads again any file whose modification time is too close to its last check to be trusted.
A digest says nothing about metadata. A file with the right content can still have the wrong owner, be writable by everyone, or be reached through a symbolic link that leads somewhere unexpected.
An inode number identifies a file only while the file exists. After deletion the number can be given to a new file, so a stored pair of device and inode numbers can later match a different file. The Inode reuse §15.24 entry treats this.
Hashing a file while another process writes it can produce the digest of a mixture of old and new bytes that never existed as one version. A stable version needs a copy taken into storage that nobody else writes, a file system snapshot, or a lock that every writer respects.
A timestamp is not a statement of origin. A modification time records when the content last changed according to the clock of the machine that changed it. It does not say who wrote the content, and the file’s owner can rewrite it.
Questions answered
Can two objects have identical size and timestamps but different bytes?
Yes. Size and timestamps are metadata, information the file system keeps about a file, and they do not determine the content, the bytes stored in the file. Two 4-byte files holding ABCD and WXYZ have equal sizes, and the command touch -r gives one the other’s modification and access times. It also happens without intent: two files of equal length written within one timestamp interval, which is 2 seconds for modification times on FAT (File Allocation Table) file systems, can have equal modification, status-change and access times.
To establish that two files hold the same bytes, compare the bytes, or compare digests computed by a cryptographic hash function with a digest from a trusted source. Check type, owner and permissions separately when they matter, and record which properties were actually checked.
Related entries: Content-addressed identity (§13.2), Cryptographic digest versus path identity (§13.3), Inode reuse (§15.24), Hard link versus symbolic link (§13.10), Cryptographic hash function (§5.1).