What a File Is
Chapter Ninety-Eight
Syllabus topic Module 2, "File System Interface - File Concept"
Pages 394 to 397 of 452
In one line
A file is a named sequence of bytes on secondary storage, and everything the system knows about it besides its contents is called its attributes.
The definition
A file is a named collection of related information recorded on secondary storage. It is the smallest logical unit of storage: the operating system offers nothing smaller with a name.
From the operating system's point of view a file is a sequence of bytes and nothing more. It does not know whether it holds a program, a picture or a letter. That is the whole reason a file system is usable by programs nobody had thought of when it was written, and it is the answer to whatever a question asks about the structure the operating system imposes on a file.
| Who gives a file meaning | How |
|---|---|
| the program that wrote it | by the format it chose |
| the user | by the name and often the extension |
| the operating system | it does not, beyond a few types it must recognise, such as a program it can execute |
The attributes
The list a question asks for, and it is worth learning all nine.
| Attribute | What it is |
|---|---|
| name | the only part a person uses; kept in the directory |
| identifier | a number that names the file inside the file system, with no name in it: the inode number |
| type | for systems that distinguish them: regular file, directory, device, and so on |
| location | a pointer to the file's blocks on the device |
| size | in bytes, and sometimes also in blocks |
| protection | who may read, write, and execute it |
| times | of creation, last modification and last access |
| owner | the user, and usually a group as well |
| link count | how many names in the directory tree refer to it |
The attributes are not in the file. They are kept in the directory structure, on the disk, beside the file rather than inside it. That is why the size of a file and the bytes of a file are two separate things, and it is why reading a file's name costs a directory access and not a read of the file.
What the lab machine keeps
Every attribute above can be asked for by name, and the figures here are worth reading twice.
$ printf 'hello\n' > greeting.txt
$ stat -c 'size %s bytes, allocated %b blocks of 512, links %h, type %F, mode %A, owner %U' greeting.txt
size 6 bytes, allocated 8 blocks of 512, links 1, type regular file, mode -rw-r--r--, owner student
$ echo "$(stat -c %s greeting.txt) bytes of data occupy $(( $(stat -c %b greeting.txt) * 512 )) bytes of disk"
6 bytes of data occupy 4096 bytes of disk
$ od -c greeting.txt
0000000 h e l l o \n
0000006What a File Is
Three things are proved there.
| The machine says | What it means |
|---|---|
| six bytes of data occupy 4096 bytes of disk | the file system allocates a block at a time, so a six byte file takes a whole 4 kilobyte block. 4090 bytes are wasted, and that is Chapter seventy two's internal fragmentation in a file system |
h e l l o \n and then 0000006 | the file really is a sequence of bytes: five letters and the newline printf wrote, and no structure of any kind around them |
| type regular file, mode -rw-r--r--, owner student | the attributes are kept by the system and answered without reading the file at all |
The 4,090 wasted bytes are the reason a file system chooses its block size carefully, and Chapter one hundred returns to it: a large block wastes more on small files and transfers large files faster.
The operations
The six MU's textbook names, and the two that surround them.
| Operation | What the system call does |
|---|---|
| create | find space, and make a directory entry |
| write | write at the current position, and advance it |
| read | read from the current position, and advance it |
| reposition, or seek | move the current position without transferring anything |
| delete | release the space and remove the directory entry |
| truncate | keep the attributes, throw the contents away, set the size to zero |
| open | Chapter ninety nine: find the file and set up the state the other calls use |
| close | write the state back and release it |
Every one of read, write and seek works on one current position per open file, and that position is the subject of the next chapter.
Truncate is not delete. The file keeps its name, its owner, its permissions and its identifier: only the contents go. It exists because a program that rewrites a file wants the name and permissions preserved.
Internal structure
Three ways a system can see the inside of a file, and a question about "file structure" wants the distinction.
| Model | What the system knows | Who uses it |
|---|---|---|
| no structure: a stream of bytes | nothing at all | UNIX, Linux, and everything modern |
| records: a sequence of fixed or variable length records | where each record starts | older systems, and databases inside their own files |
| a structured file, a defined format the system understands | the file's whole layout | systems that support only a few file types |
A system that understands file formats must be changed for every new format; a system that sees bytes need never change. The byte stream won, and the cost is that every program has to know its own formats.
What a File Is
Packing records into blocks
Since the disk deals in blocks and a program may deal in records, something has to put one into the other.
| If | Then |
|---|---|
| a record is smaller than a block | several records go in one block, and the leftover space is internal fragmentation |
| a record is larger than a block | the record spans blocks, and reading it needs two transfers |
| records do not divide the block evenly | the remainder is wasted, or a record is split |
The 4,090 bytes measured above is this arithmetic at its worst: one record of six bytes in a block of 4096.
Distinctions that carry marks
| A file's contents | A file's attributes | |
|---|---|---|
| Where they live | in data blocks | in the directory structure and the inode |
| Read by | read | stat |
| Needed to list a directory | no | yes |
| Size | Allocated space | |
|---|---|---|
| What it counts | the bytes of data | the blocks given to the file |
| The six byte file above | 6 | 4096 |
| The difference is | internal fragmentation |
| Delete | Truncate | |
|---|---|---|
| The directory entry | goes | stays |
| The attributes | go | stay |
| The contents | go | go |
| The size afterwards | there is no file | zero |
What it does not mean
A file is not a stream of characters with a meaning. It is bytes; the meaning is the program's.
An extension is not a type. On most systems it is a convention the programs agree on, and the operating system does not enforce it.
The size is not the space used. A six byte file used 4,096 bytes on the lab machine.
The identifier is not the name. It is a number, and Chapter one hundred one shows one file with two names and one number.
Attributes are not stored in the file. Reading them does not read the file.
Quick revision
- A file is a named collection of related information on secondary storage, and the
smallest logical unit of storage.
- To the operating system it is a sequence of bytes with no structure; the meaning belongs to
the program.
- Attributes: name, identifier, type, location, size, protection, times, owner, link count,
and they live in the directory structure, not in the file.
- Measured: a 6 byte file occupies 4096 bytes of disk, so 4,090 bytes are wasted:
internal fragmentation in a file system.
- Operations: create, write, read, reposition, delete, truncate, with open and close
around them.
- Truncate keeps the name and the attributes and throws away only the contents.
- File structure models: no structure (the byte stream, and it won), records, or a
structured file the system understands, which needs changing for every new format.
- Records are packed into blocks, wasting the remainder, or span blocks and cost two
What a File Is
transfers.
Test yourself
- Define a file. A named collection of related information recorded on secondary storage,
and the smallest logical unit of storage the operating system names.
- What structure does the operating system impose on a file's contents? None: it sees a
sequence of bytes, and the meaning belongs to the program that wrote it.
- List six file attributes. Name, identifier, type, location, size, protection, times, owner
and link count are nine; any six of those.
- Where are the attributes kept, and why does it matter? In the directory structure and the
inode, not inside the file, so they can be read without reading the file. 5. A file holds 6 bytes and the machine reports 8 blocks of 512 bytes. How much disk does it use and what is the waste called? 4,096 bytes, so 4,090 are wasted: internal fragmentation.
- Name the six file operations. Create, write, read, reposition or seek, delete and
truncate.
- Distinguish delete from truncate. Delete removes the directory entry and the attributes
with the contents; truncate keeps the name, the owner, the permissions and the identifier, and sets the size to zero.
- Give the three models of internal file structure, and say which is used today. No
structure, a byte stream; records; and a structured file the system understands. The byte stream is what modern systems use, and the cost is that every program must know its own formats.
The rest of this subject
These notes are cut from the University's printed syllabus. Open the syllabus itself, or the past papers, for the same subject.