open()--Open File
Syntax
#include <fcntl.h> int open(const char *path, int oflag, . . .);Service Program Name: QP0LLIB1
Default Public Authority: *USE
Threadsafe: Conditional; see Usage Notes.
The open() function opens a file and returns a number called a file descriptor. You can use this file descriptor to refer to the file in subsequent I/O operations such as read() or write(). In these subsequent operations, the file descriptor is commonly identified by the argument fildes or descriptor. Each file opened by a job gets a new file descriptor.
If the last element of the path is a symbolic link, the open() function resolves the contents of the symbolic link.
open() positions the file offset (an indicator showing where the next read or write will take place in the file) at the beginning of the file. However, there are options that can change the position.
open() clears the FD_CLOEXEC file descriptor flag for the new file descriptor. See fcntl()--Perform File Control Command for additional information about the FD_CLOEXEC flag.
The open() function also can be used to open a directory. The resulting file descriptor can be used in some functions that have a fildes parameter.
If the file being opened has been saved and its storage freed, the file is restored during this open() function. The storage extension exit program registered against the QIBM_QTA_STOR_EX400 exit point is called to restore the object. (See the Storage Extension Exit Program for details). If the file cannot successfully be restored, open() fails with the EOFFLINE error number.
For information about the exit point which can be associated with open(), see Integrated File System Scan on Open Exit Programs.
Parameters
- path
- (Input) A pointer to the null-terminated path name of the file to be
opened.
This parameter is assumed to be represented in the CCSID (coded character set identifier) currently in effect for the job. If the CCSID of the job is 65535, this parameter is assumed to be represented in the default CCSID of the job.
When a new file is created, the new file name is assumed to be represented in the language and country currently in effect for the job.
See QlgOpen()--Open a File (using NLS-enabled path name) for a description and an example of supplying the path in any CCSID.
- oflag
- (Input) The file status flags and file access modes of the file to be
opened. See Using the oflag Parameter.
Note: The open64() API sets the O_LARGEFILE flag internally.
- mode
- (Input) An optional third parameter of type mode_t that is
required if the O_CREAT flag is set. It specifies the file permission bits to
be used when a file is created. For a description of the permission bits, see
chmod()--Change File Authorizations.
- conversion ID
- (Input) An optional fourth parameter of type unsigned int that
is required if the O_CCSID or O_CODEPAGE flag is set.
If the O_CCSID flag is set, this parameter specifies a CCSID. If the O_CODEPAGE flag is set, this parameter specifies a code page used to derive a CCSID.
The specified or derived CCSID is assumed to be the CCSID of the data in the file, when a new file is created. This CCSID is associated with the file during file creation.
When the O_TEXT_CREAT flag and its prerequisite flags are not set, the specified or derived CCSID is the CCSID in which data is to be returned (when reading from a file), or the CCSID in which data is being supplied (when writing to a file).
See Using CCSIDs and code pages for more details.
- text file creation conversion ID
- (Input) An optional fifth parameter of type unsigned int that
is required if the O_TEXT_CREAT flag, along with prerequisite flags O_TEXTDATA,
O_CREAT, and either O_CCSID or O_CODEPAGE, is set. Note: because O_EXCL is not
required, this parameter may apply to files that already exist.
When O_CCSID flag is set, this parameter specifies a CCSID. If the O_CODEPAGE flag is set, this parameter specifies a code page used to derive a CCSID.
The specified or derived CCSID will be used as the CCSID of this open instance. Therefore, this will be the CCSID in which data is to be returned (when reading from a file), or the CCSID in which data is being supplied (when writing to a file). Data will be stored in the CCSID associated with the open file. Note: if the file was not created by this open operation, the file's CCSID may be different than the CCSID associated with the conversion ID parameter.
See Using CCSIDs and code pages for more details.
Using the oflag Parameter
One of the following values must be specified in oflag:
- O_RDONLY
- Open for reading only.
- O_WRONLY
- Open for writing only.
- O_RDWR
- Open for both reading and writing.
One or more of the following also can be specified in oflag:
- O_APPEND
- Position the file offset at the end of the file before each write
operation.
- O_CREAT
- The call to open() has a mode argument.
If the file being opened already exists, O_CREAT has no effect, except when O_EXCL is also specified (see the following description of O_EXCL.
If the file being opened does not exist, it is created and then opened. In the QSYS.LIB, Independent ASP QSYS.LIB, QNTC and NFS file systems, the create and open operations occur as separate steps. Therefore an [EBUSY] error could be returned if another user opens the object with a conflicting file sharing mode after the create step, but before the open step. See Usage Notes for more information about the O_CREAT open flag and file sharing mode. The user ID (uid) of the file is set to the effective uid of the job. If the object is being created in the "root" (/), QOpenSys, and user-defined file systems, the following applies. If the S_ISGID bit of the parent directory is off, the group ID (GID) is set to the effective GID of the thread creating the object. If the S_ISGID bit of the parent directory is on, the group ID (GID) of the new object is set to the GID of the parent directory. For all other file systems, the group ID (GID) of the file is set to the GID of the directory in which the file is created. File permission bits are set according to mode, except for those set in the file mode creation mask of the job. The S_ISUID (set-user-ID) and S_ISGID (set-group-ID) bits are also set according to mode. The file type bits in mode are ignored. All other bits in mode must be cleared (not set) or a [EINVAL] error is returned.
- O_EXCL
- Ignored if O_CREAT is not set. If both O_EXCL and O_CREAT are specified,
open() fails if the file already exists. If both O_EXCL and
O_CREAT are specified, and path names a symbolic link,
open() fails regardless of the contents of the symbolic link.
- O_LARGEFILE
- Open a large file. The descriptor returned can be used with the other APIs
to operate on files larger than 2GB (GB = 1073741824) minus 1 byte. The file
systems that do not support large files will just ignore the O_LARGEFILE open
flag if it is set. The O_LARGEFILE flag is ignored by the file systems when
open() is used to open a directory.
- O_TRUNC
- Truncate the file to zero length if the file exists and it is a "regular
file" (a stream file that can support positioning the file offset). The mode
and owner of the file are not changed. O_TRUNC applies only to regular files.
O_TRUNC has no effect on FIFO special files. The O_TRUNC behavior applies only
when the file is successfully opened with O_RDWR or O_WRONLY.
Truncation of the file will return the [EOVERFLOW] error if the file is larger than 2 GB minus 1 byte and if the O_LARGEFILE open flag is not also specified on the open() call. (Note that open64() sets the O_LARGEFILE open flag automatically.)
If the file exists and it is a regular file, the S_ISUID (set-user-ID) and S_ISGID (set-group-ID) bits of the file mode are cleared.
If the file has a digital signature, open() with the O_TRUNC open flag causes the signature to be deleted.
- O_TEXTDATA
- Determines how the data is processed when a file is opened.
- If O_TEXTDATA is specified, the data is processed as text.
The data is read from the file and written to the file assuming it is in textual form. When the data is read from the file, it is converted from the CCSID of the file to the CCSID of the job or the CCSID specified by the application receiving the data. When data is written to the file, it is converted to the CCSID of the file from the CCSID of the job or the CCSID specified by the application.
For true stream files, any line-formatting characters (such as carriage return, tab, and end-of-file) are just converted from one CCSID to another.
When reading from a record file that is being used as a stream file, end-of-line characters are added to the end of the data in each record. When writing to the record file:
- End-of-line characters are removed.
- Records are padded with blanks (for a source physical file member) or nulls
(for a data physical file member).
- Tab characters are replaced by the appropriate number of blanks to the next tab position.
- End-of-line characters are removed.
- If O_TEXTDATA is not specified, the data is processed as binary. The data is read from the file and written to the file without any conversion. The application is responsible for handling the data.
See Using CCSIDs and code pages for more details on text conversions.
- If O_TEXTDATA is specified, the data is processed as text.
- O_CCSID
- The call to open has a fourth argument (conversion ID), which is
to be interpreted as a CCSID. Text conversions between any two CCSIDs supported
by the iconv() API can be performed.
This flag cannot be specified with the O_CODEPAGE flag.
See Using CCSIDs and code pages for more details.
- O_CODEPAGE
- The call to open has a fourth argument (conversion ID), which is
to be interpreted as a code page. Only single-byte-to-single-byte or
double-byte-to-double-byte text conversions are allowed.
This flag cannot be specified with the O_CCSID flag.
See Using CCSIDs and code pages for more details.
- O_TEXT_CREAT
- The call to open has a fifth argument (text file creation conversion
ID), which is to be interpreted as either a code page or CCSID, depending
on whether the O_CODEPAGE or O_CCSID was set.
If the O_TEXT_CREAT flag is specified, all of the following flags must also be specified: O_CREAT, O_TEXTDATA, and either O_CODEPAGE or O_CCSID. If all of these prerequisite flags are not specified when O_TEXT_CREAT is specified, then the call to open will fail with error condition [EINVAL].
This flag indicates that the textual data read from or written to this file will be converted between the CCSID specified or derived from the text file creation conversion ID and the CCSID of the file. When data is read from the file, it is converted from the CCSID of the file to the CCSID specified or derived from the text file creation conversion ID. When data is written to the file, it is converted to the CCSID of the file from the CCSID specified or derived from the text file creation conversion ID.
See Using CCSIDs and code pages for more details.
- O_INHERITMODE
- Create the file with the same data authorities as the parent directory that
the file is created in. Any data authorities passed in the mode
parameter are ignored. The mode parameter, however, must still be specified
with a valid mode value. This flag is ignored if the O_CREAT flag is not set.
The "root" (/), QOpenSys, QSYS.LIB, independent ASP QSYS.LIB, and QDLS file systems support this flag on an open() with the O_CREAT flag set. The QOPT file system ignores this flag because files in this file system do not have data authorities.
- O_NONBLOCK
- Return without delay from certain operations on this open descriptor.
If O_NONBLOCK is specified when opening a FIFO:
- An open() for reading only or reading and writing access
returns without delay.
- An open() for writing only returns an error if no job currently has the FIFO open for reading. The errno value will be ENXIO.
If O_NONBLOCK is not specified when opening a FIFO:
- An open() for reading only blocks the calling thread until
another thread opens the FIFO for writing.
- An open() for writing only blocks the calling thread until
another thread opens the FIFO for reading.
- An open() for reading and writing returns without delay.
The O_NONBLOCK open flag is ignored for all other object types.
- An open() for reading only or reading and writing access
returns without delay.
- O_SYNC
- Updates to the file will be performed synchronously. All file data and file
attributes relative to the I/O operation are written to permanent storage
before the update operation returns. Update operations include, but are not
limited to, the following: ftruncate(),
open() with O_TRUNC, write(),
and fclear().
- O_DSYNC
- Updates to the file will be performed synchronously, but only the file data
is written to permanent storage before the update operation returns. Update
operations include, but are not limited to, the following:
ftruncate(), open() with O_TRUNC,
write(),
and fclear().
- O_RSYNC
- Read operations to the file will be performed synchronously. Pending update
requests affecting the data to be read are written to permanent storage. This
flag is used in combination with O_SYNC or O_DSYNC. When O_RSYNC and O_SYNC are
set, all file data and file attributes are written to permanent storage before
the read operation returns. When O_RSYNC and O_DSYNC are set, all file data is
written to permanent storage before the read operation returns.
- O_FORCE_SCAN
- One or more of the following conditions will be ignored when
determining whether the integrated file system scan-related exit programs
will be called:
- The Scan file systems control (QSCANFSCTL) system value specification of *FSVRONLY.
- The object was marked to not be scanned (e.g. scan attribute is *NO).
- The object was marked to be scanned only if the object changed (e.g. scan attribute is *CHGONLY).
A file sharing mode may also be specified in the oflag. If none are specified, a default sharing mode of O_SHARE_RDWR is used. No more than one of the following may be specified:
- O_SHARE_RDONLY
- Share with readers only. Open the file only if both of the following are
true:
- The file currently is not open for writing.
- The access intent does not conflict with the sharing mode of another open instance of this file.
Once opened with this sharing mode, any request to open this file for writing fails with the [EBUSY] error.
- O_SHARE_WRONLY
- Share with writers only. Open the file only if both of the following are
true:
- The file is not currently open for reading.
- The access intent does not conflict with the sharing mode of another open instance of this file.
Once opened with this sharing mode, any request to open this file for reading fails with the [EBUSY] error.
- The file is not currently open for reading.
- O_SHARE_RDWR
- Share with readers and writers. Open the file only if the access intent of
this open does not conflict with the sharing mode of another open instance of
this file.
- O_SHARE_NONE
- Share with neither readers nor writers. Open the file only if the file is not currently open. Once the file is opened with this sharing mode, any request to open this file for reading or writing fails with the [EBUSY] error.
All other bits in oflag must be cleared (not set).
Notes:
- If O_WRONLY or O_RDWR is specified and the file is checked out by a user
profile other than that of the current job, the open() fails
with the [EBUSY] error.
- If O_WRONLY or O_RDWR is specified and the file is marked "read-only," the
open() fails with the [EROOBJ] error.
- If O_CREAT is specified and the file did not previously exist, a successful
open() sets the access time, change time, modification time,
and creation time for the new file. It also updates the change time and
modification time of the directory that contains the new file (the parent
directory of the new file).
If O_TRUNC is specified and the file previously existed, a successful open() updates the change time and modification time for the file.
- Sharing Files
If a sharing mode is not specified in the oflag parameter, a default sharing mode of O_SHARE_RDWR is used. The open() may fail with the [EBUSY] error number if the file is already open with a sharing mode that conflicts with the access intent of this open() request.
Directories may only be opened with a sharing mode of O_SHARE_RDWR. If any other sharing mode is specified, the open() fails with error number [EINVAL].
For *CHRSF files, a sharing mode of O_SHARE_RDWR is used regardless of the sharing mode specified in the oflag parameter. The sharing mode specified in the oflag parameter is ignored.
The following table shows when conflicts will occur:
File Sharing Conflicts Access Intent Sharing Mode Readers Only Writers Only Readers
and WritersNo Others
(Exclusive)O_RDONLY OK EBUSY OK EBUSY O_WRONLY EBUSY OK OK EBUSY O_RDWR EBUSY EBUSY OK EBUSY
Using CCSIDs and code pages
If the O_CCSID or O_CODEPAGE flag is specified, but O_CREAT is not, the mode parameter must be specified, but its value will be ignored.
The value of conversion ID must be less than 65536. The [EINVAL] error will be returned if it is not.
When a new file is created:
- conversion ID is used to derive a CCSID to be associated with the
new file (the "file CCSID") and this open instance (the "open CCSID"). If the
file is to contain textual data, this CCSID is assumed to be the CCSID of the
data, unless the O_TEXT_CREAT flag and its prerequisite flags were also
specified.
- If neither O_CCSID nor O_CODEPAGE is specified, or if O_CCSID is specified
and conversion ID is zero (0), the file CCSID is set to the CCSID of
the job. If the job CCSID is 65535, the file CCSID is set to the default CCSID
of the job.
- For this open instance, if the O_TEXT_CREAT flag and its prerequisite flags
were not specified, the file CCSID and open CCSID are the same and no text
conversion will take place on data written to or read from the file, whether
O_TEXTDATA is specified or not. If you would like to associate the new file
with the CCSID specified in conversion ID, but you would also like to
have text conversion occur between the file's CCSID and a different CCSID,
consider using the O_TEXT_CREAT flag and corresponding text file creation
conversion ID parameter.
- The QSYS.LIB and independent ASP QSYS.LIB file systems cannot associate the derived CCSID with the database file member being created. Rather, the CCSID of the new member is the CCSID of the database file in which the member is being created. Data read or written during this open instance is converted from or to the CCSID of the database file.
When an existing file is opened and O_TEXTDATA is not specified:
- The value of conversion ID is ignored.
When an existing file is opened and O_TEXTDATA is specified:
- conversion ID is used to derive a CCSID to be associated with this
open instance (the "open CCSID").
- If neither O_CCSID nor O_CODEPAGE is specified, or if O_CCSID is specified
and conversion ID is zero (0), the open CCSID is set to the CCSID of
the job. If the job CCSID is 65535, the open CCSID is set to the default CCSID
of the job.
- The system will convert from the file CCSID to
the open CCSID when reading data from the file, and convert from the open CCSID
to the file CCSID when writing data to the file.
- If O_CCSID is not specified, and the file CCSID and open CCSID are not the same, and one of them is not strictly single-byte, open() will fail with errno set to [ECONVERT].
See Examples for a sample program that creates a new file and then opens it for data conversion.
Authorities
Note: Adopted authority is not used.
Authorization Required for open() (excluding QSYS.LIB, independent ASP QSYS.LIB,and QDLS) | ||
---|---|---|
Object Referred to | Authority Required |
errno |
Each directory in the path name preceding the object to be opened | *X | EACCES |
Existing object when access mode is O_RDONLY | *R | EACCES |
Existing object when access mode is O_WRONLY | *W | EACCES |
Existing object when access mode is O_RDWR | *RW | EACCES |
Existing object when O_TRUNC is specified | *W | EACCES |
Parent directory of object to be created when object does not exist and O_CREAT is specified | *WX | EACCES |
Authorization Required for open() in the QSYS.LIB and independent ASP QSYS.LIB File Systems | ||
---|---|---|
Object Referred to | Authority Required |
errno |
Each directory in the path name preceding the object to be opened | *X | EACCES |
Existing object when access mode is O_RDONLY | *R | EACCES |
Existing object when access mode is O_WRONLY | *W | EACCES |
Existing object when access mode is O_RDWR | *RW | EACCES |
Existing object when object is a save file | *RWX | EACCES |
Existing object when O_TRUNC is specified | *W | EACCES |
Parent directory of object to be created when object does not exist and O_CREAT is specified | *OBJMGT or *OBJALTER |
EACCES |
Parent directory of object to be created when object does not exist and object type is *USRSPC or save file | *RX and *Add | EACCES |
Parent directory of the parent directory of object to be created when object does not exist, O_CREAT is specified, and object being created is a physical file member | *ADD | EACCES |
Authorization Required for open() in the QDLS File System | ||
---|---|---|
Object Referred to | Authority Required |
errno |
Each directory in the path name preceding the object to be opened | *X | EACCES |
Existing object when access mode is O_RDONLY | *R | EACCES |
Existing object when access mode is O_WRONLY | *W | EACCES |
Existing object when access mode is O_RDWR | *RW | EACCES |
Existing object when O_TRUNC is specified | *W | EACCES |
Parent directory of object to be created when object does not exist and O_CREAT is specified | *CHANGE | EACCES |
Return Value
value | open() was successful. The value returned is the file descriptor. |
-1 | open() was not successful. The errno global variable is set to indicate the error. |
Error Conditions
If open() is not successful, errno usually indicates one of the following errors. Under some conditions, errno could indicate an error other than those listed here.
Error condition | Additional information |
---|---|
[EACCES] |
If you are accessing a remote file through the Network File System, update operations to file permissions at the server are not reflected at the client until updates to data that is stored locally by the Network File System take place. (Several options on the Add Mounted File System (ADDMFS) command determine the time between refresh operations of local data.) Access to a remote file may also fail due to different mappings of user IDs (UID) or group IDs (GID) on the local and remote systems. |
[EAGAIN] | |
[EBADFID] | |
[EBADNAME] | |
[EBUSY] |
The open sharing mode may conflict with another open of this file, or O_WRONLY or O_RDWR is specified and the file is checked out by another user. In the QSYS.LIB and independent ASP QSYS.LIB file systems, if the O_TEXTDATA flag was specified, the file may be already open in this job or another job where the O_TEXTDATA flag was not specified. Or if the O_TEXTDATA flag was not specified, the file may be already open in this job or another job where the O_TEXTDATA flag was specified. |
[ECONVERT] | |
[EDAMAGE] | |
[EDEADLK] | |
[EEXIST] | |
[EFAULT] | |
[EFILECVT] | |
[EINTR] | |
[EINVAL] | For example
|
[EIO] | |
[EISDIR] |
The path name given is a directory. Write access or O_TRUNC has been specified and is not valid for a directory. |
[EJRNDAMAGE] | |
[EJRNENTTOOLONG] | |
[EJRNINACTIVE] | |
[EJRNRCVSPC] | |
[ELOOP] | |
[EMFILE] | |
[ENAMETOOLONG] | |
[ENEWJRN] | |
[ENEWJRNRCV] | |
[ENFILE] | |
[ENOENT] | |
[ENOMEM] | |
[ENOSPC] | |
[ENOSYS] | |
[ENOSYSRSC] | |
[ENOTAVAIL] | |
[ENOTDIR] | |
[ENOTSUP] | |
[ENXIO] |
O_NONBLOCK and O_WRONLY open flags are specified, path refers to a FIFO, and no job has the FIFO open for reading. |
[EOFFLINE] | |
[EOVERFLOW] |
The size of the specified file cannot be represented correctly in a variable of type off_t (the file is larger than 2GB minus 1 byte). |
[EPERM] | |
[EROOBJ] | |
[ESCANFAILURE] | |
[ESTALE] |
If you are accessing a remote file through the Network File System, the file may have been deleted at the server. |
[ETXTBSY] | |
[EUNKNOWN] |
Additionally, if interaction with a file server is required to access the object, errno could also indicate one of the following errors:
Error condition | Additional information |
---|---|
[EADDRNOTAVAIL] | |
[ECONNABORTED] | |
[ECONNREFUSED] | |
[ECONNRESET] | |
[EHOSTDOWN] | |
[EHOSTUNREACH] | |
[ENETDOWN] | |
[ENETRESET] | |
[ENETUNREACH] | |
[ETIMEDOUT] | |
[EUNATCH] |
Error Messages
The following messages may be sent from this function:
Message ID | Error Message Text |
---|---|
CPE3418 E | Possible APAR condition or hardware failure. |
CPFA0D4 E | File system error occurred. Error number &1. |
CPF3CF2 E | Error(s) occurred during running of &1 API. |
CPF9872 E | Program or service program &1 in library &2 ended. Reason code &3. |
Usage Notes
- This function will fail with error number [ENOTSAFE] when all the following
conditions are true:
- Where multiple threads exist in the job.
- The object on which this function is operating resides in a file system
that is not threadsafe. Only the following file systems are threadsafe for this
function:
- "Root" (/)
- QOpenSys
- User-defined
- QNTC
- QSYS.LIB
- Independent ASP QSYS.LIB
- QOPT
- Network File System
- QFileSvr.400
- Where multiple threads exist in the job.
- "Root" (/), QOpenSys, and User-Defined File System Differences
The user who creates the file becomes its owner. The S_ISGID bit of the directory affects what the group ID (GID) is for objects that are created in the directory. If the S_ISGID bit of the parent directory is off, the group ID (GID) is set to the effective GID of the thread creating the object. If the S_ISGID bit of the parent directory is on, the group ID is copied from the parent directory in which the file is created.
When you do not specify O_INHERITMODE for the oflag parameter, the owner, primary group, and public object authorities (*OBJEXIST, *OBJMGT, *OBJALTER, and *OBJREF) are copied from the parent directory's owner, primary group, and public object authorities. This occurs even when the new file has a different owner than the parent directory. The owner, primary group, and public data authorities (*R, *W, and *X) are derived from the permissions specified in the mode (except for those permissions that are also set in the file mode creation mask). The new file does not have any private authorities or authorization list. It only has authorities for the owner, primary group, and public.
When you specify O_INHERITMODE for the oflag parameter, the owner, primary group, and public data and object authorities (*R, *W, *X, *OBJEXIST, *OBJMGT, *OBJALTER, and *OBJREF) are copied from the parent directory's owner, primary group, and public data and object authorities. In addition, the private authorities (if any) and authorization list (if any) are copied from the parent directory. If the new file has a different owner than the parent directory and the new file's owner has a private authority in the parent directory, that private authority is not copied from the parent directory. The authority for the owner of the new file is copied from the owner of the parent directory.
There are some restrictions when opening a FIFO for text conversion and the CCSIDs involved are not strictly single-byte:
- Opening a FIFO for reading or reading and writing is not allowed. The
errno global variable is set to [ENOTSUP].
- Any conversion between CCSIDs that are not strictly single-byte must be done by an open instance that has write only access.
- Opening a FIFO for reading or reading and writing is not allowed. The
errno global variable is set to [ENOTSUP].
- QSYS.LIB and Independent ASP QSYS.LIB File System Differences
The following object types are allowed to be opened:
- *MBR (physical file member)
The only types of physical files supported when specifying the O_TEXTDATA flag are program-described physical files that contain a single field and source physical files that contain a single text field. Externally described physical files are supported for binary access only.
- *LIB (library)
- *FILE (physical file or save file)
- *USRSPC (user space)
Advanced Function Presentation (AFP) objects. These object types cannot be created with open() and can only be opened for reading. If O_CREAT is specified and the object does not exist, open() will fail with the EBADNAME error number. If O_WRONLY or O_RDWR is specified, open() will fail with the ENOTSUP error number. The supported AFP object types are:
- *FNTRSC (font resource)
- *FORMDF (form definition)
- *OVL (overlay)
- *PAGDFN (page definition)
- *PAGSEG (page segment)
When creating a member, the ownership, group profile, and authorities are all derived from the member's parent physical file. The input mode value is ignored.
The group ID is obtained from the primary user profile, if a group profile exists.
The primary group authorities specified in mode are not saved if no primary group exists.
You cannot open a member in a file that has a mixed data CCSID.
The file access time for a database member is updated using the normal rules that apply to database files. At most, the access time is updated once per day.
Due to the restriction that only one job may have a database member open for writing at a time, the sharing modes O_SHARE_WRONLY and O_SHARE_RDWR do not provide the requested level of sharing.
- If O_SHARE_WRONLY is specified, the open() succeeds.
However, in all jobs other than the one that performed this
open(), the actual enforced share mode for this file is equivalent to
O_SHARE_NONE.
- If O_SHARE_RDWR is specified, or if no share mode is specified, the open() succeeds. However, in all jobs other than the one that performed this open(), the actual enforced share mode is equivalent to O_SHARE_RDONLY.
The open() of a database member fails with an [EBUSY] error under any of the following conditions:
- The O_TEXTDATA flag is specified, but the file is already open in this job
or another job where the O_TEXTDATA flag is not specified.
- The O_TEXTDATA flag is not specified, but the file is already open in this
job or another job where the O_TEXTDATA flag and write access are
specified.
- The O_TEXTDATA flag is specified and write access is requested, but the
file is already open in this job or another job where O_TEXTDATA is specified
and write access is also requested.
- The O_CREAT flag is specified, the member already exists, and the QSYS.LIB
or independent ASP QSYS.LIB file system cannot get exclusive access to the
member. They must have exclusive access to clear the old member.
- The O_TEXTDATA flag is not specified (binary mode) and more than one job
tries to obtain write access to the member. This condition does not apply to PC
clients. Because PC clients share the same server job, they can share access to
the member.
- The user attempts to open a member with access intentions that conflict with existing object locks on the member.
This function will fail with error number [ENOTSAFE] if the object on which this function is operating is a save file and multiple threads exist in the job.
This function will fail with error number [ENOTSUP] if the file specified is a save file and the O_RDWR flag is specified. A save file can be opened for either reading only or writing only.
This function will fail with error number [ENOTSUP] if the file specified is a save file and the O_TEXTDATA flag is specified.
If a save file containing data is opened for writing, the O_APPEND or O_TRUNC flag must be specified. Otherwise, the open() will fail with error number [ENOTSUP].
There are some restrictions on sharing modes when opening a save file.
- A save file may not have more than one open descriptor per job, regardless
of the sharing mode specified.
- A save file currently open for reading only cannot be opened again in the
same job for reading or writing. The open() will fail with
errno set to [EBUSY].
- A save file currently open for writing only cannot be opened again in the same job for reading or writing. The open() will fail with errno set to [EBUSY].
- A save file currently open for reading only cannot be opened again in the
same job for reading or writing. The open() will fail with
errno set to [EBUSY].
- Due to the restriction that only one job may have a save file open when the
save file is open for writing, the sharing modes O_SHARE_WRONLY and
O_SHARE_RDWR do not provide the reqested level of sharing.
- If O_SHARE_WRONLY is specified, the open() succeeds.
However, in all jobs other than the one that performed this
open(), the actual enforced share mode for this file is equivalent to
O_SHARE_NONE.
- If O_SHARE_RDWR is specified and the file is opened for reading only, the
open() succeeds. However, in all jobs other than the one that
performed this open(), the actual enforced share mode is
equivalent to O_SHARE_RDONLY.
- If O_SHARE_RDWR is specified and the file is opened for writing only, the open() succeeds. However, in all jobs other than the one that performed this open(), the actual enforced share mode is equivalent to O_SHARE_NONE.
- If O_SHARE_WRONLY is specified, the open() succeeds.
However, in all jobs other than the one that performed this
open(), the actual enforced share mode for this file is equivalent to
O_SHARE_NONE.
Note: Unpredictable results, including loss of data, could occur if, in the same job, a user tries to open the same file for writing at the same time by using both open() API for stream file access and a data management open API for record access.
- *MBR (physical file member)
- QDLS File System Differences
When O_CREAT is specified and a new file is created:
- the owner's object authority is set to *OBJMGT + *OBJEXIST + *OBJALTER +
*OBJREF.
- The primary group and public object authority and all other authorities are
copied from the directory (folder) in which the file is created.
- The owner, primary group, and public data authority (including *OBJOPR) are derived from the permissions specified in mode (except those permissions that are also set in the file mode creation mask).
The primary group authorities specified in mode are not saved if no primary group exists.
QDLS does not store the language ID and country ID with its files. When this information is requested (using the readdir() function), QDLS returns the language ID and country ID of the system.
- the owner's object authority is set to *OBJMGT + *OBJEXIST + *OBJALTER +
*OBJREF.
- QOPT File System Differences
When the volume on which the file is being opened is formatted in Universal Disk Format (UDF):
- The authorization that is checked for the object and preceding directories
in the path name follows the rules described in
Authorization Required for open().
- The volume authorization list is checked for *USE when the access mode is
O_RDONLY. The volume authorization list is checked for *CHANGE when the access
mode is O_RDWR or O_WRONLY.
- The user who creates the file becomes its owner.
- The group ID is copied from the parent directory in which the file is
created.
- The owner, primary group, and public data authorities (*R, *W, and *X) are
derived from the permissions specified in the mode (except those permissions
that are also set in the file mode creation mask).
- When O_INHERITMODE is specified for the oflag parameter, the data
authorities are copied from the parent directory.
- The sharing modes O_SHARE_RDONLY, O_SHARE_WRONLY, and O_SHARE_RDWR do not
provide the requested level of sharing when the access mode is O_RDWR or
O_WRONLY. When the access mode is O_RDWR or O_WRONLY, the resulting sharing
mode semantic will be equivalent to O_SHARE_NONE.
- For newly created files, the same uppercase and lowercase forms in which
the names are entered are preserved. No distinction is made between uppercase
and lowercase when searching for names.
- This function will fail with error number [EINVAL] if the O_SYNC, O_DSYNC, or O_RSYNC open flag is specified.
When the volume on which the file is being opened is not formatted in Universal Disk Format (UDF):
- No authorization checks are made on the object or preceding directories in
the path name.
- The volume authorization list is checked for *USE when the access mode is
O_RDONLY. The volume authorization list is checked for *CHANGE when the access
mode is O_RDWR or O_WRONLY.
- QDFTOWN becomes the owner of the file.
- No group ID is assigned to the file.
- The permissions specified in the mode are ignored. The owner, primary
group, and public data authorities are set to RWX.
- For newly created files, names are created in uppercase. No distinction is made between uppercase and lowercase when searching for names.
- The authorization that is checked for the object and preceding directories
in the path name follows the rules described in
Authorization Required for open().
- Network File System Differences
Local access to remote files through the Network File System may produce unexpected results due to conditions at the server. The creation of a file may fail if permissions and other attributes that are stored locally by the Network File System are more restrictive than those at the server. A later attempt to create a file can succeed when the locally stored data has been refreshed. (Several options on the Add Mounted File System (ADDMFS) command determine the time between refresh operations of local data.) The creation can also succeed after the file system has been remounted.
If you try to re-create a file that was recently deleted, the request may fail because data that was stored locally by the Network File System still has a record of the file's existence. The creation succeeds when the locally stored data has been updated.
Once a file is open, subsequent requests to perform operations on the file can fail because file attributes are checked at the server on each request. If permissions on the file are made more restrictive at the server or the file is unlinked or made unavailable by the server for another client, your operation on an open file descriptor will fail when the local Network File System receives these updates. The local Network File System also impacts operations that retrieve file attributes. Recent changes at the server may not be available at your client yet, and old values may be returned from operations.
The sharing modes O_SHARE_RDONLY, O_SHARE_WRONLY, and O_SHARE_NONE do not provide the requested level of sharing. If any one of these share modes is specified, the resulting share mode semantic will be equivalent to O_SHARE_RDWR.
- This function will fail with the [EOVERFLOW] error if the specified file
exists and its size is too large to be represented in a variable of type off_t
(the file is larger than 2 GB minus 1 byte).
- When you develop in C-based languages and an application is compiled with
the _LARGE_FILES macro defined, the open() API will be mapped
to a call to the open64() API.
- When using this function on the /dev/null or /dev/zero character special file,
the O_CREAT and O_TRUNC open flags have no effect.
- The O_SYNC, O_DSYNC, and O_RSYNC open flags will not cause updates made to
the file via mapped access to be written to permanent storage.
- When specifying the O_CREAT open flag, the "root" (/), QOpenSys, user-defined, QDLS, and QOPT file systems perform the create and open operations atomically. The QSYS.LIB, Independent ASP QSYS.LIB, QNTC and NFS file systems perform the create and open operations as separate steps and may return with an [EBUSY] error if another user opens the object with a conflicting share mode after the create and before the open operation.
Related Information
- The <fcntl.h> file (see Header Files for UNIX®-Type Functions)
- close()--Close File or Socket Descriptor
- creat()--Create or Rewrite File
- dup()--Duplicate Open File Descriptor
- fcntl()--Perform File Control Command
- Integrated File System Scan on Open Exit Programs
- lseek()--Set File Read/Write Offset
- open64()--Open File (Large File Enabled)
- QlgOpen()--Open a File (using NLS-enabled path name)
- read()--Read from Descriptor
- stat()--Get File Information
- umask()--Set Authorization Mask for Job
- write()--Write to Descriptor
Examples
Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.
The following example opens an output file for appending. Because no sharing mode is specified, the O_SHARE_RDWR sharing mode is used.
int fildes; fildes = open("outfile",O_WRONLY | O_APPEND);
The following example creates a new file with read, write, and execute permissions for the user creating the file. If the file already exists, the open() fails. If the open() succeeds, the file is opened for sharing with readers only.
fildes = open("newfile",O_WRONLY|O_CREAT|O_EXCL|O_SHARE_RDONLY,S_IRWXU);
This example first creates an output file for with a specified CCSID. The file is then closed and opened again with data conversion. The open() function is called twice because no data conversion would have occurred when using the first open's descriptor on read or write operations, even if O_TEXTDATA had been specified on that open; however, the second open could be eliminated entirely by using O_TEXT_CREAT on the first open. This is demonstrated in the code example immediately following this example. In this example, EBCDIC data is written to the file and converted to ASCII.
#include <fcntl.h> #include <sys/stat.h> #include <errno.h> #include <stdio.h> int main(int argc, char *argv[]) { int fd; int rc; char name[]="/test.dat"; char data[]="abcdefghijk"; int oflag1 = O_CREAT | O_RDWR | O_CCSID; int oflag2 = O_RDWR | O_TEXTDATA | O_CCSID; mode_t mode = S_IRUSR | S_IWUSR | S_IXUSR; unsigned int file_ccsid = 819; unsigned int open_ccsid = 37; /***************************************************/ /* First create the file with the CCSID 819. */ /***************************************************/ if ((fd=open(name,oflag1,mode,file_ccsid)) < 0) { perror("open() for create failed"); return(0); } if (close(fd) < 0) { perror("close() failed."); return(0); } /***************************************************/ /* Now open the file so EBCDIC (CCSID 37) data */ /* written will be converted to ASCII (CCSID 819).*/ /***************************************************/ if ((fd=open(name,oflag2,mode,open_ccsid)) < 0) { perror("open() with translation failed"); return(0); } /***************************************************/ /* Write some EBCDIC data. */ /***************************************************/ if (-1 == (rc=write(fd, data, strlen(data)))) { perror("write failed"); return(0); } if (0 != (rc=close(fd))) { perror("close failed"); return(0); } }
In this second example, EBCDIC data is written to the file and converted to ASCII. This will produce the same results as the first example, except that it did it by only using one open instead of two.
#include <fcntl.h> #include <sys/stat.h> #include <errno.h> #include <stdio.h> int main(int argc, char *argv[]) { int fd; int rc; char name[]="/test.dat"; char data[]="abcdefghijk"; int oflag1 = O_CREAT | O_RDWR | O_CCSID | O_TEXTDATA | O_TEXT_CREAT | O_EXCL; mode_t mode = S_IRUSR | S_IWUSR | S_IXUSR; unsigned int file_ccsid = 819; unsigned int open_ccsid = 37; /***************************************************/ /* First create the file with the CCSID 819, and */ /* open it such that the data is converted */ /* between the the open CCSID of 37 and the */ /* file's CCSID of 819 when writing data to it. */ /***************************************************/ if ((fd=open(name,oflag1,mode,file_ccsid,open_ccsid)) < 0) { perror("open() for create failed"); return(0); } /***************************************************/ /* Write some EBCDIC data. */ /***************************************************/ if (-1 == (rc=write(fd, data, strlen(data)))) { perror("write failed"); return(0); } /***************************************************/ /* Close the file. */ /***************************************************/ if (0 != (rc=close(fd))) { perror("close failed"); return(0); } }
API introduced: V3R1