tags : Linux, Storage, Network Programming
- A file is an abstract resource from which a sequence of bytes is read from or written to.
- The source and destination of those is unspecified, those could be anything.
Everything in unix are files, hard drives, pipes, network devices, stdin/stdout
etc. Letās separate it into regular and special files.
Regular files
These are disk files. See Filesystems
Directory files
- See Directory layer
- File that contains mapping of file names with inode numbers
- We call the process of removing a
dentry
āunlinkingā. If no names point to the inode, the last link is removed, then the file is considered removed and the data freed.
Symbolic link files
- Returns only a single string of text representing the location of its target, the other file it points to. Like an alias.
- An improvement is fast symlink which is basically storing the filepath in the inode structure if the structure can take that in.
Hard link files
- Hard links are simply directory entries.
- All files have at least one directory entry and thus all files must at least have one hard link.
- They are that association between a
name
and aninode
. - Multiple hard links = Multiple names to the same file
- Creating hard links to directories is forbidden (infinite loop)
- Max no. of hard links to a single file is limited by the size of the ref counter inside the inode
Named pipe files
- Also called
FIFO
, File used for IPC. - Traditional pipe is āunnamedā and disappear as soon as the process finishes, the named pipe is a file that can be reused. Allows for transfer of info in a pipeline manner through processes that donāt share the same parent.
- Created via
mkfifo
, can be deleted usingrm
- Not so popular nowadays, use sockets instead.
Socket files
- Unix domain sockets
AF_UNIX
- Similar to named pipe
- While named pipe are unidirectional, sockets are full duplex.
- What can we send
- Data
- File Descriptors: Allows a process to manipulate a file it may not already have access to.
Device files
- All devices, pseudo or not are implemented as device drivers in the kernel. (This statement is not verified)
- Device files have
major
andminor
number stored asinode
attributes. The meaning of a device file determined by these 2 numbers.major
number: The driver to useminor
number: The device that the driver controls
- Use: Communication w a piece of hardware, Accessing system resources(
/dev/null
,/dev/random
)
Device Driver
- Not all device files are handled by a file system driver.
- Device files are handled by specific drivers for every one of them. Itās the Device Driver, a program that controls some type of device attached to the computer, sw interface to hw devices.
- Device Driver Functions: HW device discovery, detecting state changes, sending events to udevd etc.
Character device
- Unbuffered / Stream. Usually read/written sequentially.
- Also called
raw devices
sometimes.
Block device
- Buffered. May be accessed randomly.
- Usually for things like disks, which behave like large, fixed-size files.
- Linux specific: To represent disk devices, Linux only uses block devices, it does give option to simulate character file opens at all.
- See arch wiki for naming conventions of different physical block devices.
Pseudo device
- These are device nodes that do not have a backing physical device. They are either character or block devices.
- These are sometimes also called virtual devices.
major
number(device driver) is usually1
. (eg. Trystat /dev/null
) - These devices are mapped to certain paths in the file system. Mostly thatās
/dev
- Can be created using
mknod
/dev
- Naming convention used by files in
/dev
is a complete mess. - There will be entries in
/dev
for devices you do not have. (i.e They donāt always correspond to physical devices) /dev
in modern system will have adevtmpfs
fs mounted.devtmpfs
is a device manager in the form of a filesystem that displays each device as files.- It allows devices to be dynamically shown without storing on disk (itās stored in memory).
- Some examples:
/dev/null
,/dev/zero
,/dev/full
,/dev/random
,/dev/stdin