File Attributes and Operations
A file is the basic unit of data storage, and it carries a rich set of attributes and supports a rich set of operations. Understanding what a file knows about itself, and what you can ask it to do, is essential to understanding how a file system works underneath.
File attributes are the metadata that describe a file’s characteristics and state. This information is stored in the directory entry or the inode of the file system.
Basic Attributes
1. File Name
- Role: the identifier a user uses to recognize and reach the file
- Characteristics: usually chosen by the user, and may contain a wide range of characters
- Restrictions: different file systems impose different limits on name length and permitted characters
- Examples:
document.txt,image.jpg,program.exe
2. File Type
- Regular file: stores user data or program code
- Directory file: holds references to other files
- Device file: stands for a hardware device
- Symbolic link: a shortcut that points at another file
- Pipe file: used for inter-process communication
3. File Size
- Definition: the number of bytes of data the file contains
- Units: bytes (B), kilobytes (KB), megabytes (MB), and so on
- Dynamic: the size of a file changes as data is written and deleted
4. Location Information
- Physical location: the physical address of the file on the storage device
- Logical location: the path of the file within the directory structure
- Storage layout: whether the file is stored contiguously or scattered
Time Attributes
1. Creation Time
- Definition: the timestamp of the moment the file was first created
- Use: file management and auditing
- Characteristic: usually cannot be modified
2. Modification Time
- Definition: the time the contents of the file were last modified
- Trigger: updated whenever the contents of the file change
- Use: deciding whether a file needs to be backed up or recompiled
3. Access Time
- Definition: the time the file was last accessed
- Trigger: updated when the file is read, written, or executed
- Use: gathering statistics on how files are used
4. Attribute Change Time
- Definition: the time the attributes of the file, such as permissions or owner, were last changed
- Trigger: updated whenever the metadata of the file changes
Permission Attributes
1. Owner
- Definition: the creator of the file or its current owner
- Rights: holds complete control over the file
- Change: only the superuser or the current owner can change it
2. Group
- Definition: the user group the file belongs to
- Role: grants shared permissions to the members of the group
- Change: the owner or the superuser can change it
3. Permissions
- Read permission (r): allows the contents of the file to be read
- Write permission (w): allows the contents of the file to be modified
- Execute permission (x): allows the file to be executed, for program files
4. Special Permissions
- SUID: the file runs with the identity of its owner when executed
- SGID: the file runs with the identity of its group when executed
- Sticky Bit: only the owner of the file can delete it
Other Attributes
1. File Identifiers
- inode number: the unique identifier of the file within the file system
- device number: the identifier of the storage device the file lives on
- link count: the number of hard links that point at the file
2. Storage Attributes
- block size: the basic unit in which a file system allocates storage space
- blocks occupied: the number of storage blocks the file actually uses
- fragmentation: whether the file is stored contiguously
File operations are the basic functions a file system offers to users; together they give complete control over a file.
Basic File Operations
1. Create
// C example
FILE *fp = fopen("newfile.txt", "w");
if (fp != NULL) {
printf("File created successfully\n");
fclose(fp);
}
What it does:
- Creates a new file at the given location
- Allocates storage space
- Initializes the file attributes
- Updates the directory structure
2. Delete
// C example
if (remove("file.txt") == 0) {
printf("File deleted successfully\n");
} else {
printf("Failed to delete the file\n");
}
What it does:
- Releases the storage space occupied by the file
- Removes the file’s entry from the directory
- Updates the accounting information of the file system
3. Open
// C example
FILE *fp = fopen("file.txt", "r");
if (fp != NULL) {
// The file was opened successfully; read and write operations are now allowed
fclose(fp);
}
What it does:
- Verifies that the file exists and that access is permitted
- Creates a file descriptor
- Initializes the file pointer
- Loads the metadata of the file
4. Close
// C example
fclose(fp); // close the file
What it does:
- Flushes buffered data
- Releases the file descriptor
- Updates the file attributes, such as the access time
- Frees system resources
Data Read and Write Operations
1. Read
// C example
char buffer[1024];
size_t bytes_read = fread(buffer, 1, sizeof(buffer), fp);
if (bytes_read > 0) {
printf("Read %zu bytes\n", bytes_read);
}
What it does:
- Reads a given amount of data from the current position in the file
- Advances the file pointer
- Updates the access time of the file
- Handles read errors
2. Write
// C example
char data[] = "Hello, World!";
size_t bytes_written = fwrite(data, 1, strlen(data), fp);
if (bytes_written > 0) {
printf("Wrote %zu bytes\n", bytes_written);
}
What it does:
- Writes data at the current position in the file
- Allocates the storage space that is needed
- Updates the file size and the modification time
- Handles write errors
3. Seek
// C example
fseek(fp, 100, SEEK_SET); // seek to byte 100
fseek(fp, -50, SEEK_CUR); // move 50 bytes back from the current position
fseek(fp, 0, SEEK_END); // seek to the end of the file
What it does:
- Moves the file pointer
- Supports both relative and absolute positioning
- Prepares the file for subsequent read and write operations
Advanced File Operations
1. Rename
// C example
if (rename("oldname.txt", "newname.txt") == 0) {
printf("File renamed successfully\n");
}
What it does:
- Changes the name of the file
- Leaves the contents and attributes of the file untouched
- Updates the directory structure
2. Copy
// C example
FILE *source = fopen("source.txt", "r");
FILE *dest = fopen("dest.txt", "w");
char buffer[1024];
size_t bytes;
while ((bytes = fread(buffer, 1, sizeof(buffer), source)) > 0) {
fwrite(buffer, 1, bytes, dest);
}
fclose(source);
fclose(dest);
What it does:
- Creates a complete copy of the file
- Leaves the original file unchanged
- Allocates storage space for the new file
3. Move
// C example
// Move within the same file system
if (rename("file.txt", "newdir/file.txt") == 0) {
printf("File moved successfully\n");
}
What it does:
- Changes the place of the file within the directory structure
- Within a single file system, usually only the directory entry has to be updated
- Across file systems, the file must be copied and the original deleted
File Attribute Operations
1. Changing Permissions
// C example
#include <sys/stat.h>
chmod("file.txt", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
What it does:
- Changes the access permissions of the file
- Only the owner of the file or the superuser may do this
- Takes effect immediately
2. Changing the Owner
// C example
#include <unistd.h>
chown("file.txt", new_uid, new_gid);
What it does:
- Changes the owner and the group of the file
- Only the superuser may do this
- Affects the access permissions of the file
3. Changing Timestamps
// C example
#include <utime.h>
struct utimbuf times;
times.actime = time(NULL); // access time
times.modtime = time(NULL); // modification time
utime("file.txt", ×);
What it does:
- Changes the access and modification times of the file
- Is used for file synchronization and backup
Common Error Types
1. Permission Errors
- EACCES: insufficient permissions
- EPERM: the operation was not permitted
2. File Does Not Exist
- ENOENT: the file or directory does not exist
- ENOTDIR: a component of the path is not a directory
3. Out of Storage Space
- ENOSPC: the device has no space left
- EDQUOT: the user’s quota has been reached
4. File Already Exists
- EEXIST: the file already exists
- EISDIR: a file operation was attempted on a directory
An Example of Error Handling
// C example
FILE *fp = fopen("file.txt", "r");
if (fp == NULL) {
perror("Failed to open the file");
switch (errno) {
case EACCES:
printf("Insufficient permissions\n");
break;
case ENOENT:
printf("The file does not exist\n");
break;
default:
printf("Other error\n");
}
return -1;
}
| Term | Abbreviation | Explanation |
|---|---|---|
| File Attribute | - | The metadata of a file |
| File Size | - | The number of bytes occupied by a file |
| Creation Time | - | The time at which a file was created |
| Modification Time | - | The time at which a file was last modified |
| Access Time | - | The time at which a file was last accessed |
| File Owner | - | The owner of a file |
| File Permission | - | The access rights of a file |
| File Type | - | Regular file, directory, device, and so on |
| Regular File | - | A file that stores data |
| Directory File | - | A file that stores directory entries |
| Character Device File | - | A special file for a character device |
| Block Device File | - | A special file for a block device |
| Symbolic Link | - | A link that points at another file |
| Hard Link | - | A link that shares the same inode |
| Pipe File | - | A pipe used for inter-process communication |
| Socket File | - | A socket used for network communication |
| File Mode | - | The type and the permissions of a file |
| User ID | UID | The identifier of a user |
| Group ID | GID | The identifier of a user group |
| Read Permission | r | The right to read a file |
| Write Permission | w | The right to modify a file |
| Execute Permission | x | The right to execute a file |
| Set User ID | SUID | Sets the effective UID on execution |
| Set Group ID | SGID | Sets the effective GID on execution |
