HOME

TheInfoList



OR:

For most file systems, a
program Program, programme, programmer, or programming may refer to: Business and management * Program management, the process of managing several related projects * Time management * Program, a part of planning Arts and entertainment Audio * Progra ...
initializes access to a
file File or filing may refer to: Mechanical tools and processes * File (tool), a tool used to ''remove'' fine amounts of material from a workpiece **Filing (metalworking), a material removal process in manufacturing ** Nail file, a tool used to gent ...
in a file system using the open
system call In computing, a system call (commonly abbreviated to syscall) is the programmatic way in which a computer program requests a service from the operating system on which it is executed. This may include hardware-related services (for example, acc ...
. This allocates resources associated to the file (the file descriptor), and returns a
handle A handle is a part of, or attachment to, an object that allows it to be grasped and manipulated by hand. The design of each type of handle involves substantial ergonomic issues, even where these are dealt with intuitively or by following tr ...
that the
process A process is a series or set of activities that interact to produce a result; it may occur once-only or be recurrent or periodic. Things called a process include: Business and management *Business process, activities that produce a specific se ...
will use to refer to that file. In some cases the open is performed by the first access. The same file may be opened simultaneously by several processes, and even by the same process, resulting in several file descriptors for the same file; depending on the file organization and filesystem. Operations on the descriptors such as moving the file pointer or closing it are independentthey do not affect other descriptors for the same file. Operations on the file, such as a
write Writing is a medium of human communication which involves the representation of a language through a system of physically inscribed, mechanically transferred, or digitally represented symbols. Writing systems do not themselves constitute h ...
, can be seen by operations on the other descriptors: a later read can read the newly written data. During the open, the filesystem may allocate memory for buffers, or it may wait until the first operation. The absolute file path is resolved. This may include connecting to a remote host and notifying an operator that a removable medium is required. It may include the initialization of a communication device. At this point an error may be returned if the host or medium is not available. The first access to at least the
directory Directory may refer to: * Directory (computing), or folder, a file system structure in which to store computer files * Directory (OpenVMS command) * Directory service, a software application for organizing information about a computer network's u ...
within the filesystem is performed. An error will usually be returned if the higher level components of the path (
directories Directory may refer to: * Directory (computing), or folder, a file system structure in which to store computer files * Directory (OpenVMS command) * Directory service, a software application for organizing information about a computer network's ...
) cannot be located or accessed. An error will be returned if the file is expected to exist and it does not or if the file should not already exist and it does. If the file is expected to exist and it does, the file access, as restricted by permission flags within the file meta data or
access control list In computer security, an access-control list (ACL) is a list of permissions associated with a system resource (object). An ACL specifies which users or system processes are granted access to objects, as well as what operations are allowed on gi ...
, is validated against the requested type of operations. This usually requires an additional filesystem access although in some filesystems meta-flags may be part of the directory structure. If the file is being created, the filesystem may allocate the default initial amount of storage or a specified amount depending on the file system capabilities. If this fails an error will be returned. Updating the directory with the new entry may be performed or it may be delayed until the
close Close may refer to: Music * ''Close'' (Kim Wilde album), 1988 * ''Close'' (Marvin Sapp album), 2017 * ''Close'' (Sean Bonniwell album), 1969 * "Close" (Sub Focus song), 2014 * "Close" (Nick Jonas song), 2016 * "Close" (Rae Sremmurd song), 201 ...
is performed. Various other errors which may occur during the open include directory update failures, un-permitted multiple connections, media failures, communication link failures and device failures. The return value must always be examined and an error specific action taken. In many cases programming language-specific run-time library opens may perform additional actions including initializing a run-time library structure related to the file. As soon as a file is no longer needed, the program should close it. This will cause run-time library and filesystem buffers to be updated to the physical media and permit other processes to access the data if exclusive use had been required. Some run-time libraries may close a file if the program calls the run-time exit. Some filesystems may perform the necessary operations if the program terminates. Neither of these is likely to take place in the event of a kernel or power failure. This can cause damaged filesystem structures requiring the running of privileged and lengthy filesystem utilities during which the entire filesystem may be inaccessible.


open call arguments

#The
pathname A path is a string of characters used to uniquely identify a location in a directory structure. It is composed by following the directory tree hierarchy in which components, separated by a delimiting character, represent each directory. The del ...
to the file, #The kind of access requested on the file (read, write, append etc.), #The initial file permission is requested using the third argument called mode. This argument is relevant only when a new file is being created. After using the file, the process should close the file using
close Close may refer to: Music * ''Close'' (Kim Wilde album), 1988 * ''Close'' (Marvin Sapp album), 2017 * ''Close'' (Sean Bonniwell album), 1969 * "Close" (Sub Focus song), 2014 * "Close" (Nick Jonas song), 2016 * "Close" (Rae Sremmurd song), 201 ...
call, which takes the file descriptor of the file to be closed. Some filesystems include a disposition to permit releasing the file. Some computer languages include run-time libraries which include additional functionality for particular filesystems. The open (or some auxiliary routine) may include specifications for key size, record size, connection speed. Some open routines include specification of the program code to be executed in the event of an error.


Perl language form

open FILEHANDLE,MODE EXPR for example: open(my $fh, ">", "output.txt");
Perl Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offic ...
also uses the tie function of the Tie::File module to associate an array with a file. The tie::AnyDBM_File function associates a hash with a file.


C library POSIX definition

The
open
call is standardized by the
POSIX The Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines both the system- and user-level application programming inter ...
specification for
C language C (''pronounced like the letter c'') is a general-purpose computer programming language. It was created in the 1970s by Dennis Ritchie, and remains very widely used and influential. By design, C's features cleanly reflect the capabilities o ...
: int open(const char *path, int oflag, .../*,mode_t mode */); int openat(int fd, const char *path, int oflag, ...); int creat(const char *path, mode_t mode); FILE *fopen(const char *restrict filename, const char *restrict mode); The value returned is a file descriptor which is a reference to a process specific structure which contains, among other things, a position pointer that indicates which place in the file will be acted upon by the next operation. Open may return
−1 In mathematics, −1 (also known as negative one or minus one) is the additive inverse of 1, that is, the number that when added to 1 gives the additive identity element, 0. It is the negative integer greater than negative two (−2) and less t ...
indicating a failure with errno detailing the error. The file system also updates a global table of all open files which is used for determining if a file is currently in use by any process.


path

The name of the file to open. It includes the
file path A path is a string of characters used to uniquely identify a location in a directory structure. It is composed by following the directory tree hierarchy in which components, separated by a delimiting character, represent each directory. The del ...
defining where, in which file system, the file is found (or should be created). openat expects a relative path.


oflag

This argument formed by OR'ing together optional parameters and (from < fcntl.h>) one of: Option parameters include: *O_APPEND data written will be appended to the end of the file. The file operations will always adjust the position pointer to the end of the file. *O_CREAT Create the file if it does not exist; otherwise the open fails setting
errno errno.h is a header file in the standard library of the C programming language. It defines macros for reporting and retrieving error conditions using the symbol errno (short for "error number").International Standard for Programming Language C ( ...
to ENOENT. *O_EXCL Used with O_CREAT if the file already exists, then fail, setting errno to EEXIST. *O_TRUNC If the file already exists then discard its previous contents, reducing it to an empty file. Not applicable for a device or named pipe. Additional flags and errors are defined i
open
call. creat() is implemented as: int creat(const char *path, mode_t mode)

uses string flags such as r, w, a and + and returns a file pointer used wit

an


mode

Optional and relevant only when creating a new file, defines the
file permissions Most file systems include attributes of files and directories that control the ability of users to read, change, navigate, and execute the contents of the file system. In some cases, menu options or functions may be made visible or hidden dependin ...
. These include read, write or execute the file by the owner, group or all users. The mode is masked by the calling process's
umask In computing, umask is a command that determines the settings of a mask that controls how file permissions are set for newly created files. It may also affect how the file permissions are changed explicitly. is also a function that sets the ma ...
: bits set in the umask are cleared in the mode.


See also

* File descriptor – how it works and other functions related to open


Notes


References

* Advanced Programming in the UNIX Environment by W. Richard Stevens *UNIX concept & application by Sumitabh Das {{Computer files C POSIX library System calls Articles with example C code