Q: I can use the CHKOBJ command to check if an object exists. Is there a similar command for IFS objects? I've found CHKDLO, but it doesn't seem to work.
A: CHKDLO is only able to check objects in the QDLS filesystem, which is only a small part of the IFS. The access() API is the best way to check if an object exists, but you can't call it directly from a CL program since CL does not support passing parameters by value. My solution to this is to create a CHKIFSOBJ command and have the command call an ILE RPG program as its CPP.
Command source:
/* THIS CHECKS IF AN IFS OBJECT/DIRECTORY EXISTS, RETURNS AN +
*ESCAPE MESSAGE IF IT DOES NOT. +
SCOTT KLEMENT, DEC 11, 2003 +
TO COMPILE: +
CRTBNDRPG CHKIFSR4 SRCFILE(MYLIB/QRPGLESRC) DBGVIEW(*LIST) +
CRTCMD CHKIFSOBJ SRCFILE(MYLIB/QCMDSRC) PGM(CHKIFSR4) +
*/
CMD PROMPT('Check IFS Object or Directory')
PARM KWD(OBJ) TYPE(*CHAR) LEN(128) MIN(1) +
VARY(*YES *INT2) CASE(*MIXED) +
CHOICE(OBJECT) PROMPT('IFS Object or Dir +
to check')
ILE RPG source:
* This RPG program demonstrates how to check if an IFS object
* exists. (it might be a stream file, or directory, or any other
* object type)
* Scott Klement, Dec 11, 2003
*
* To compile:
* CRTBNDRPG CHKIFSR4 SRCFILE(MYLIB/QRPGLESRC) DBGVIEW(*LIST)
* CRTCMD CHKIFSOBJ SRCFILE(MYLIB/QCMDSRC) PGM(CHKIFSR4)
*
H DFTACTGRP(*NO) BNDDIR('QC2LE')
**********************************************************************
* Access mode flags for access()
*
* F_OK = File Exists
* R_OK = Read Access
* W_OK = Write Access
* X_OK = Execute or Search
**********************************************************************
D F_OK C 0
D R_OK C 4
D W_OK C 2
D X_OK C 1
*--------------------------------------------------------------------
* Determine file accessibility
*
* int access(const char *path, int amode)
*
*--------------------------------------------------------------------
D access PR 10I 0 ExtProc('access')
D Path * Value Options(*string)
D amode 10I 0 Value
*--------------------------------------------------------------------
* getErrno(): Returns a pointer to the 'errno' variable, which
* tells us what happened when a Unix-type API fails
*
* This requires the QC2LE binding directory.
*--------------------------------------------------------------------
D getErrno PR * ExtProc('__errno')
D EscErrno PR
D stmfile s 128A varying
c *entry plist
c parm stmfile
c if access(StmFile: F_OK) <> 0
c callp EscErrno
c endif
c eval *inlr = *on
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* End program with an escape message that corresponds to
* the error returned by the Unix-type APIs "errno"
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
P EscErrno B
D EscErrno PI
D QMHSNDPM PR ExtPgm('QMHSNDPM')
D MessageID 7A Const
D QualMsgF 20A Const
D MsgData 1A Const
D MsgDtaLen 10I 0 Const
D MsgType 10A Const
D CallStkEnt 10A Const
D CallStkCnt 10I 0 Const
D MessageKey 4A
D ErrorCode 1A
D dsEC DS
D dsECBytesP 10I 0 inz(0)
D dsECBytesA 10I 0 inz(0)
D TheKey S 4A
D MsgID S 7A
D p_errno s *
D errno s 10I 0 based(p_errno)
D errnum s 4P 0
c eval p_errno = getErrno
c eval errnum = errno
c eval MsgID = 'CPE' + %editc(errnum:'X')
c callp QMHSNDPM(MsgID: 'QCPFMSG *LIBL':
c ' ': 0: '*ESCAPE':
c '*': 3: TheKey: dsEC)
P E
Here's a link to more information about the access() API from the Unix-type
APIs section of the IBM Information Center:
http://publib.boulder.ibm.com/iseries/v5r2/ic2924/info/apis/access.htm
There's also a tutorial on using the IFS from RPG IV programs on my Web
page. Here's a link to it:
http://www.scottklement.com/rpg/ifs.html
Some other people have written CHKIFSOBJ commands as well. Here are a few of them:
RPG IV utility FileExists by Nick Roux
http://www.iseriesnetwork.com/artarchive/index.cfm?fuseaction=viewarticle&CO_ContentID=10708
Command ChkIFSObj and CL program ChkIFSObjC by David Leland
http://www.iseriesnetwork.com/artarchive/index.cfm?fuseaction=viewarticle&CO_ContentID=10708
ILE CL utility ChkObjIFS by Herman Van der Staey
http://www.iseriesnetwork.com/Forums/Thread.cfm?CFApp=55&Thread_ID=31361&mc=6
CL utility by James Hoopes
http://www.iseriesnetwork.com/artarchive/index.cfm?fuseaction=viewarticle&CO_ContentID=6484
C function ChkIFSObj by Mark Phippard
http://groups.google.com/groups?q=ChkIfsObj&hl=en&lr=&ie=UTF-8&safe=off&selm=xUckUn0f9GA.150%40newslink400.duke.com&rnum=1