informix
Informix DataBlade API Programmer's Manual
Writing a User-Defined Routine

Accessing Operating-System Files

The DataBlade API provides file-access functions to access to operating-system files within a C UDR. These functions are very similar to the system file-access functions. They call the corresponding operating-system function to perform their task. However, the DataBlade API file-access functions periodically yield the virtual processor to limit the effects of blocking I/O.

Important: Do not call operating-system file I/O functions from within a C UDR. Use these DataBlade API file-access functions instead. These DataBlade API file-access functions are safer in a C UDR than their operating-system equivalents. For more information, see Avoiding Blocking I/O Calls.

Figure 12-22 on page 12-76 shows the basic file operations along with the DataBlade API file-access functions that perform them and the analogous operating-system calls for these file operations.

Figure 12-22
DataBlade API File-Access Functions

File-Access Operation File-Access Function Operating-System Call
Open the operating-system file: the open operation generates a file descriptor for the file. mi_file_open() open()
Seek to the desired position to begin a read or write operation. mi_file_seek() seek()
Obtain the current seek position. mi_file_tell() tell()
Perform the read or write operation for the specified number of bytes. mi_file_read(), mi_file_write() read(), write()
Obtain status information about a particular smart large object. mi_file_sync() sync()
Close the operating-system file and deallocate the file descriptor. mi_file_close() close()
Unlink (remove) the operating-system file. mi_file_unlink() None
Obtain value of operating-system errno for the file task performed mi_file_errno() None

Tip: The DataBlade API file-access functions execute in client LIBMI applications as well as C UDRs. For DataBlade API modules that you design to run in both client LIBMI applications and UDRs, use these file-access functions. For information on the behavior of these functions in a client LIBMI application, see Appendix A, Writing a Client LIBMI Application.

The DataBlade API accesses operating-system files through file descriptors. These file descriptors are very similar in purpose to operating-system file descriptors. The following table summarizes the memory operations for a file descriptor.

Default Memory Duration Memory Operation Function Name
Duration of client session Constructor mi_file_open()
Destructor mi_file_close(), mi_file_unlink()

Opening a File

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:

Specifying a Filename

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 server environment; that is, it must be set before the database server starts.

For example, Figure 12-23 shows an mi_file_open() call that opens an operating-system file called data_file1, which resides in the directory that the DATA_FILES environment variable specifies.

Figure 12-23
Sample Open of an Operating-System File

Suppose the DATA_FILES environment variable is set to the following directory in the server environment:

The call to mi_file_open() in Figure 12-23 opens the following file:

Calling the Operating-System Open

The mi_file_open() function calls the open system call that your operating system supports to open a file.

On UNIX systems, the open() system call opens an operating-system file.

On Windows NT systems, the _open command opens an operating-system file.

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.

Tip: For more information on the open flags and open mode, refer to the documentation for your operating-system open call. Specifying Open Flags

The mi_file_open() function takes as its second argument the open flags with which you want to open the operating-system file. The open flags provides a masked flag value that specifies information such as access mode (read/write, read-only, write-only). The mi_file_open() function passes these open flags directly to the underlying operating-system call that opens a file. Therefore, 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.

For example, the mi_file_open() call in Figure 12-23 on page 12-77 masks the following open flags for the file is to be opened.

Open-Flag Constant Purpose
O_WRONLY Open the file write-only.
O_APPEND Append new data to the end of the file.
O_CREAT If the file does not exist, create it.

The example in Figure 12-23 on page 12-77 assumes the following:

Specifying the Open Mode

The mi_file_open() function takes as its third argument the open mode in which you want to open the operating-system file. The open mode specifies the ownership for 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 12-23 on page 12-77 specifies an open mode of 0644:

Sharing Open Files

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 file descriptors that mi_file_open() opens are valid until the end of the client session. If you do not explicitly close the files you opened with mi_file_close(), the database server automatically closes them when the client session completes.

The DataBlade API generates an error if your UDR attempts any of the following file I/O tasks:

A Sample File-Access User-Defined Routine

The following sample UDR, logmsg(), uses the DataBlade API file-access functions to output messages to an external file:


Informix DataBlade API Programmer's Manual, Version 9.2
Copyright © 1999, Informix Software, Inc. All rights reserved