The DataBlade API provides file-access functions for access to operating-system files from within a C UDR. These functions provide file management that is similar to what operating-system file-access functions provide. The DataBlade API file-access functions call the corresponding operating-system functions to perform their tasks; however, the DataBlade API functions periodically yield the virtual processor to limit the effects of blocking I/O.
Table 91 lists the DataBlade API functions for the basic file-access operations and the analogous operating-system calls for these operations.
The DataBlade API accesses operating-system files through file descriptors. These file descriptors are similar in purpose to operating-system file descriptors. The following table summarizes the memory durations for a file descriptor.
Memory Duration | Memory Operation | Function Name |
---|---|---|
Duration of session
(PER_SESSION) |
Constructor | mi_file_open( ) |
Destructor | mi_file_close( ), mi_file_unlink( ) |
The mi_file_open( ) function is the constructor function for a file descriptor. Through the file descriptor, you access an operating-system file. This section provides the following information on how to open a file:
The filename argument of mi_file_open( ) identifies the operating-system file to open. This filename is relative to the server computer. You can include an environment variable in the filename path for the mi_file_open( ) and mi_file_to_file( ) file-access functions. This environment variable must be set in the database server environment; that is, it must be set before the database server starts.
For example, Figure 82 shows an mi_file_open( ) call that opens the operating-system file data_file1, which resides in the directory that the DATA_FILES environment variable specifies.
fd = mi_file_open("$DATA_FILES/data_file1", O_WRONLY | O_APPEND | O_CREAT, 0644);
Suppose the DATA_FILES environment variable is set to the following directory in the database server environment:
/usr/local/app/load_files
The call to mi_file_open( ) in Figure 82 opens the following file:
/usr/local/app/load_files/data_file1
To open a file, the mi_file_open( ) function calls the open system call that your operating system supports.
The mi_file_open( ) function provides the following information about the file to the appropriate system call.
Argument of mi_file_open( ) | Information Provided |
---|---|
open_flags (second argument) | Access mode for the operating-system file |
open_mode (third argument) | Open mode for the operating-system file. |
The function takes this information and passes it directly to the underlying operating-system call. Therefore, mi_file_open( ) supports the access modes and open modes that your operating-system open call supports.
The mi_file_open( ) function takes as its second argument the open flags with which to open the operating-system file. The open flags value provides two pieces of information:
The mi_file_open( ) function passes these open flags directly to the underlying operating-system call that opens a file, so you must use flag values that your operating system supports. Also, you must include the operating-system header file (such as fcntl.h) that defines the open-flag constants you use.
The DataBlade API file-access functions support access to a file on either the server or client computer. By default, the mi_file_open( ) function opens a file on the server computer. To open a server file, you can omit the file-mode flag or specify the MI_O_SERVER_FILE file-mode flag. To open a client file, you must include the MI_O_CLIENT_FILE file-mode flag as part of the open flags.
For example, the mi_file_open( ) call in Figure 82 masks the following open flags for the file to open.
The example in Figure 82 is based on the following assumptions:
The mi_file_open( ) function takes as its third argument the open mode in which to open the operating-system file. The open mode specifies the ownership of the file. The mi_file_open( ) function passes this open mode directly to the underlying operating-system call. The semantics for mode must match those that the underlying operating-system call supports.
For example, the mi_file_open( ) call in Figure 82 specifies an open mode of 0644:
All UDRs that execute under the same connection can share a file (because they have the same connection descriptor). For example, if UDR1 opens a file, UDR2 can read, write to, or close this file, as long as these two UDRs execute under the same connection. However, UDRs that do not execute under the same connection cannot share a file.
The DataBlade API generates an error if your UDR attempts any of the following file I/O tasks:
To close an operating-system file, free the associated file descriptor. A file descriptor remains active until either of the following events occurs:
In a C UDR, a file descriptor has a memory duration of PER_SESSION. Files remain open after the UDR closes the connection.
In a client LIBMI application, a connection descriptor has a scope of the session. For more information, see Accessing Operating-System Files in Client LIBMI Applications.
The DataBlade API provides the mi_file_to_file( ) function to enable you to copy an operating-system file between the computer on which the database server runs and the client computer. This function provides the open-mode flags for the new operating-system file. Unlike the mi_file_open( ) function, these open-mode flags are not those of the underlying operating-system open function. Instead, mi_file_to_file( ) supports the set of DataBlade API file-mode constants in Table 43.
Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]