Deleting IFS object using unlink() API in RPGLE |
In IFS, we generally use the term "link", that's a way to reach the data (file). We use CL command WRKLNK to browse the IFS i.e. to Work with Links and CL command RMVLNK to delete stream file i.e. to remove link. There may be several links to the same data, when we remove the link to the stream file, the system will first remove the file name from the directory, and then it will check whether it was the last link to the data/stream file. If yes, the stream file data will get removed from the IFS.
What is the Unlink() API?
Unlink() is used to remove/delete files from the IFS i.e. to delete/remove the link.
Prototype for the unlink() API in C Language
int unlink(const char *path)
- The return value is an int (integer) and values are 0 which indicate success, or -1 if an error occurred.
- The unlink() function removes a directory entry that refers to a file.
- It accepts only one parameter i.e. input path (null-terminated character string).
Prototype for the unlink() API in RPG
D unlink PR 10I 0 ExtProc('unlink') D ifspath * Value options(*string)
RPGLE program for deleting IFS object using Unlink() API
HDFTACTGRP(*NO) D unlink PR 10I 0 ExtProc('unlink') D ifspath * Value options(*string) D errorifs PR * ExtProc('__errno') D strerror PR * ExtProc('strerror') D error_num 10I 0 value * Difspath s 512a Dreturn_unlink s 10i 0 inz Derror_ptr S * Derror_num S 10I 0 based(error_ptr) Derrormsg_ptr S * Derror_msg S 500a based(errormsg_ptr) C EVAL ifspath = '/home/easyclass/helloworld' C EVAL return_unlink = unlink(%trim(ifspath)) C IF return_unlink < 0 C EVAL error_ptr = errorIFS() C EVAL errormsg_ptr = strerror(error_num) C ENDIF C EVAL *INLR = *ON C RETURN
HDFTACTGRP(*NO) D unlink PR 10I 0 ExtProc('unlink') D ifspath * Value options(*string) D errorifs PR * ExtProc('__errno') D strerror PR * ExtProc('strerror') D error_num 10I 0 value * Difspath s 512a Dreturn_unlink s 10i 0 inz Derror_ptr S * Derror_num S 10I 0 based(error_ptr) Derrormsg_ptr S * Derror_msg S 500a based(errormsg_ptr) /free ifspath = '/home/easyclass/helloworld'; return_unlink = unlink(%trim(ifspath)); if return_unlink < 0; error_ptr = errorIFS(); errormsg_ptr = strerror(error_num); endif; *inlr = *on; return; /end-free
**FREE CTL-OPT DFTACTGRP(*NO); DCL-PR unlink int(10) EXTPROC('unlink'); ifspath pointer VALUE options(*string); END-PR; DCL-PR errorifs pointer EXTPROC('__errno'); END-PR; DCL-PR strerror pointer EXTPROC('strerror'); error_num int(10) VALUE; END-PR; DCL-S ifspath CHAR(512); DCL-S return_unlink int(10); DCL-S error_ptr pointer; DCL-S error_num int(10) based(error_ptr); DCL-S errormsg_ptr pointer; DCL-S error_msg char(500) based(errormsg_ptr); ifspath = '/home/easyclass/helloworld'; return_unlink = unlink(%trim(ifspath)); if return_unlink < 0; error_ptr = errorIFS(); errormsg_ptr = strerror(error_num); endif; *inlr = *on; return;
The above code will delete the ifs stream file named "helloworld" from ifs user home directory if present. In case if file does not exists or any error occurred while deleting the file then the unlink() api will return -1 and we then called error handling apis to get error number and its corresponding error messages.