Monday, February 4, 2008

Check your file in IFS:

Many people are using the Integrated File System (IFS) to store source code and HTML pages and to stage converted database files that are being sent or received between heterogeneous systems. One question that is frequently asked is how to determine if a file exists on the IFS. There are several methods to accomplish this, but probably the easiest and safest way is to call the access() procedure. The C runtime function name access() allows to test a file on the IFS for read/write access or existence.
The IFS API manual tells us that the access() procedure is prototyped, in C, as follows:
int access(const char *path, int amode);
This C prototype doesn't do us much good in RPG. We need to convert it to an RPG IV prototype. Illustrated below is the RPG IV prototype for the access() procedure.
D access PR 10I 0 ExtProc('access')
D szIFSFile * Value OPTIONS(*STRING)
D nAccessMode 10I 0 VALUE
The access() procedure returns 0 if the test succeeds. For example, if you check for file existence and the file exists, 0 is returned.
Of course, to check for the existence of a file, you have to tell the access() procedure what you want to do. To do that, you must specify on the second parameter the type of file access you want. The options for the second parameter are as follows:
D R_OK C Const(4)
D W_OK C Const(2)
D F_OK C Const(0)
It is always better programming to use named constants rather than hard-coded numbers, hence the R_OK, W_OK, and F_OK named constants. If you read the access() documentation, you see that the C language predefines these constants for you (in C language, of course).
Using access()
As mentioned, most people simply want to check to see if a file already exists on the IFS. To do this, you call the access() procedure with the F_OK value specified for its second parameter.
For example, to check to see if the file CUSTMAST.TXT exists in the /MYFILES directory, you could use the following:
D szIFSfile C '/myfiles/customers.txt'
C if access(szIFSFile : F_OK) = 0
// The file exists!!
C endif
In this example, the IFS file name is stored as a named constant (line 1), and on line 2, the access() procedure is called to test for its existence. If it is found, a 0 is returned and processing continues.
As is always the case, to use any of the C runtime library functions, we must include the QC2LE binding directory. As a matter of practice, it is always better to include it in the H-spec for the source code. Here's a typical Header specification from one of the source members:
H BNDDIR('QC2LE') OPTIONS(*SRCSTMT:*NODEBUGIO)
/IF DEFINED(*CRTBNDRPG)
H DFTACTGRP(*NO)
/ENDIF

No comments: