File System : Developers perspective
Basic System Calls
This is a continuation of my good friend and esteemed batchmate Pranav Venkatesh's article on File System introduction.
We will bring more basic and advanced articles in the future and maybe in next one I will dive deep in File system internals.
Below are the most used syscalls when working with file systems. code snippets are in C but other languages use a similar approach. C Compiler might not implement resource-sharing checks other languages might so read the docs like a good programmer.
Open
in order to do any operation on file writing reading. We need to open that file in process.
flags are for specifying operation mode and permission level is given by integer.
Close
After our work is done we have to free the resource and reset all the registers. for that, we use close with the file variable name.
Read
number = read (fd, buffer, count);
where fd
is the descriptor returned by open syscall. buffer
is the address of the variable where the data will be stored. count
is the quantity to be read.
Write
number = write (fd, buffer, count);
all the parameters are same as the Read syscall but it writes the data into fd from the buffer region.
File creation
df= creat(const char *path, mode_t mod);
df is an integer returned by creat if -1 there was an error. path is the string where to create the file and mode is access rights.
Change access rights
int chmod(const char* path, mode_t mod);
it will return 0 if it is successful and -1 if error. parameters are the same as creat. using this in the script is not recommended but if some repair work automation needs arise it's the syscall with high ease of use. we might need it when we intend to change access right during runtime.
Note: if using it with the child process and the parent process is dead some security features might flag it as malicious.
Change ownership
This system call is used to modify the owner (UID) and the group (GID) that a certain file belongs to.
int chown(const char* path, uid_t owner, gid_t grp);
Link and Unlink
hard link
is a mirror of the actual file and uses lot of space when the original file is deleted it does not affect the link.
int link(const char* oldpath, const char* newpath);
Symlink
symbolic link on the other hand is just a pointer to file. the original file is deleted then data is lost and takes a lot less space.
int symlink(const char* oldpath, const char* newpath);
Unlink
int unlink(const char* path);
Subscribe to my newsletter
Read articles from Diviyam Pathak directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by