0% found this document useful (0 votes)
9 views15 pages

Linux Files

Uploaded by

Arun Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views15 pages

Linux Files

Uploaded by

Arun Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

File Handling

File I/O (Handling Files using System Calls)

• There are various system calls available for file I/O such as open(), create(), read(),
write(), fcntl(), lseek(), dup(), dup2(), close().
• Out of these the following are the four key system calls for performing file I/O. The
programming languages and software packages typically employ these calls only
indirectly, via I/O libraries for example, fopen() employs open() system call, fgetc(),
fscanf(), fgets(), fread() are designed on read() system call.
• open(): opens and possibly creates a file or device and returns a file descriptor
• read(): read from a file descriptor
• write(): write to a file descriptor
• close(): close a file descriptor
• These system calls are called UNIX I/O model. One of the distinguishing features of the
UNIX I/O model is the concept of universality of I/O. This means that the same four
system calls— open(), read(), write(), and close() are used to perform I/O on all types of
files, including devices such as terminals. Consequently, if you write a program using only
these system calls, that program will work on any type of file.
File Descriptor
• All system calls for performing I/O refer to open files using a file descriptor, a (small)
nonnegative integer.
• File descriptors are used to refer to all types of open files, including pipes, FIFOs, sockets,
terminals, devices, and regular files.
• Each process has its own set of file descriptors. By convention, most programs expect to
be able to use the three standard file descriptors. Following table shows Standard File
Descriptors
• These three descriptors are opened on File
the program’s behalf by the shell, before Descriptor Purpose POSIX Name Stdio Stream

the program is started. standard


0 STDIN_FILENO stdin
input
• Or, more precisely, the program inherits
standard STDOUT_FILEN
copies of the shell’s file descriptors, and 1 stdout
output O
the shell normally operates with these
standard
three file descriptors always open. 2 STDERR_FILENO stderr
error
Opening a file… open() a system call
• The open() system call either opens an existing file or creates and opens a new file.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
• int open(const char *pathname, int flags);
• int open(const char *pathname, int flags, mode_t mode);
• int creat(const char *pathname, mode_t mode);
• Returns file descriptor on success, or –1 on error.
• The file to be opened is identified by the pathname argument.
• If pathname is a symbolic link, it is dereferenced.
• On success, open() returns a file descriptor that is used to refer to the file in subsequent system
calls.
• It is guaranteed to use the lowest-numbered unused file descriptor for the process. If an error
occurs, open() returns -1 and errno is set accordingly.
• The flags argument is a bit mask that specifies the access mode for the file, using one of
the constants shown below.
• The flag constants are divided into the following groups:
File access mode flags: These are the O_RDONLY, O_WRONLY, and O_RDWR flags. They can
be retrieved using the fcntl() F_GETFL operation.
• O_RDONLY: Open the file for reading only
• O_WRONLY: Open the file for writing only
• O_RDWR: Open the file for both reading and writing
File creation flags: They control various aspects of the behavior of the open() call, as well
as options for subsequent I/O operations. These flags can’t be retrieved or changed.
• O_CREAT: Create file if it doesn’t already exist
• O_DIRECT: File I/O bypasses buffer cache
• O_DIRECTORY: Fail if pathname is not a directory
• O_EXCL With O_CREAT: create file exclusively
• O_NOCTTY: Don’t let pathname become the controlling terminal
• O_NOFOLLOW: Don’t dereference symbolic links
• O_TRUNC: Truncate existing file to zero length
• Open file status flags:They can be retrieved and modified using the fcntl()
F_GETFL and F_SETFL operations. These flags are sometimes simply called the file
status flags.
• O_APPEND: Writes are always appended to end of file
• O_ASYNC: Generate a signal when I/O is possible
• O_NONBLOCK: Open in nonblocking mode
#include<stdio.h>
#include<sys/types.h>
In the program file is opened in read only mode, so
#include<sys/stat.h>
#include<fcntl.h>
if file is present it will be open for reading and if file
main() is not present, open() returns -1 and sets error
{ number. Using PID you can check the file
int fd; descriptors opened for the said process,
fd=open("datafile",O_RDONLY);
$ls /proc/PID/fdinfo
if(fd<0)
{
perror("open");
return;
}
printf("fd=%d\n",fd);
printf("pid=%d\n",getpid());
while(1);
}
• In C language file handling, fopen(“datafile”, “r”) is just similar to above call to
open(). Similarly if you want to open file in “w” mode you can call open() as,
• fopen(“data”, “w”); → open("data", O_WRONLY|O_CREAT|O_TRUNC, 0666);
• fopen(“data”, “w+”); → open("data", O_RDWR|O_CREAT|O_TRUNC, 0666);
• fopen(“data”, “a+”); → open("data", O_RDWR|O_CREAT|O_APPEND, 0666);
• When open() is used to create a new file, the mode bit-mask argument specifies
the permissions to be placed on the file.
• If the open() call doesn’t specify O_CREAT, mode can be omitted.
• The permissions actually placed on a new file depend not just on the mode
argument, but also on the process umask.
• In early UNIX implementations, open() had only two arguments and could not be
used to create a new file. Instead, the creat() system call was used to create and
open a new file. The creat() system call creates and opens a new file with the
given pathname, or if the file already exists, opens the file and truncates it to zero
length.
• Calling creat() is equivalent to the following open() call:
• fd = open(pathname, O_WRONLY | O_CREAT | O_TRUNC, mode);
• Because the open() flags argument provides greater control over how the file is
opened (e.g., we can specify O_RDWR instead of O_WRONLY), creat() is now
obsolete.
Reading from a file

• The read() system call reads data from the open file referred to by the descriptor
fd.
• #include <unistd.h>
• ssize_t read(int fd, void *buffer, size_t count);
• Returns number of bytes read, 0 on EOF, or –1 on error
• The count argument specifies the maximum number of bytes to read. The buffer
argument supplies the address of the memory buffer into which the input data is
to be placed. This buffer must be at least count bytes long. A successful call to
read() returns the number of bytes actually read, or 0 if end-of file is
encountered. On error, the usual –1 is returned. (size_t is unsigned integer type
whereas ssize_t is signed integer type.)
Write system call.

• The write() system call writes data to an open file.


• #include <unistd.h>
• ssize_t write(int fd, void *buffer, size_t count);
• Returns number of bytes written, or –1 on error
• The buffer is the address of the data to be written; count is the number of bytes
to write from buffer; and fd is a file descriptor referring to the file to which data is
to be written. On success, write() returns the number of bytes actually written;
this may be less than count.
Closing an open file.

• The close() system call closes an open file descriptor, freeing it for
subsequent reuse by the process. When a process terminates, all of its
open file descriptors are automatically closed.
• #include <unistd.h>
• int close(int fd);
• Returns 0 on success, or –1 on error
• It is usually good practice to close unnecessary file descriptors explicitly,
since this makes our code more readable and reliable in the face of
subsequent modifications.
• Furthermore, file descriptors are a consumable resource, so failure to close
a file descriptor could result in a process running out of descriptors. This is
a particularly important issue when writing long-lived programs that deal
with multiple files.
Summarizing File concept we learnt so far…

• A hard disk is divided into one or more partitions, each of which may contain a
file system.
• A file system is an organized collection of regular files and directories.
• Linux implements a wide variety of file systems, including the traditional ext2/3/4
file system.
• The extX file system is conceptually similar to early UNIX file systems, consisting
of a boot block, a superblock, an i-node table, and a data area containing file data
blocks.
• Each file has an entry in the file system’s i-node table. This entry contains various
types of information about the file, including its type, size, link count, ownership,
permissions, timestamps, and pointers to the file’s data blocks.
Summarizing File concept we learnt so far…

• The stat() system call retrieves information about a file (metadata), most of which
is drawn from the file i-node.
• This information includes file ownership, file permissions, and file timestamps.
Each file has an associated user ID (owner) and group ID, as well as a set of
permission bits. For permissions purposes, file users are divided into three
categories: owner (also known as user), group, and other.
• Three permissions may be granted to each category of user: read, write, and
execute. The same scheme is used with directories, although the permission bits
have slightly different meanings.
• The chmod() system call changes permissions of a file. The umask() system call
sets a mask of permission bits that are always turned off when the calling process
creates a file.
Summarizing File concept we learnt so far…

• An i-node doesn’t contain a file’s name. Instead, files are assigned names via entries in
directories, which are tables listing filename and i-node number correspondences.
• These directory entries are called (hard) links. A file may have multiple links, all of which
enjoy equal status.
• Symbolic links are similar to hard links in some respects, with the differences that
symbolic links can cross file system boundaries and can refer to directories. A symbolic
link is just a file containing the name of another file. A symbolic link is not included in the
(target) i-node’s link count, and it may be left dangling if the filename to which it refers is
removed.
• To scan the contents of a directory, you can use opendir(), readdir(), and related
functions. For file handling various system calls are available in unix, open(), read(),
write(), close().

You might also like