Temporary Files in the IFS

Article ID: 19705

Q: I've read some of your articles on IFS programming, and I have a question. In typical iSeries programming, temporary files are placed in the QTEMP library, which is automatically destroyed when the job ends. Is there a similar facility in the IFS?

A: Not exactly the same, no. There's a directory named /tmp that is often used for temporary files in the IFS. Unlike QTEMP, however, it's an ordinary directory that's available to every job on the system. If you write a file to /tmp, that file will be visible to other jobs on the system.

This presents a problem. How do you create temporary files that are different for each job so that the same program can be run by many users without conflict?

The ILE C runtime library contains an API called tmpnam(). This API returns a unique filename in the /tmp directory. There's also a flag that you can pass to the open() API called O_EXCL that starts for "exclusively create." This flag tells the API not to open a file unless it doesn't exist and can be created from scratch. Using this flag protects you from accidentally duplicating an existing file.

When you do this, remember that every job on the system can see the files in /tmp. It's a good idea to set the permissions bits on the file so that only the owner can read it. That way if there's sensitive information, other people won't be able to see it.

Finally, unlike QTEMP, the objects in this directory are not automatically deleted for you. You have to remember to delete them so that they don't build up over time. The unlink() API will delete a file in the IFS.

Here's an example of creating a temporary file in the IFS with the ILE RPG language:

    H DFTACTGRP(*NO) BNDDIR('QC2LE')

     /copy ifsio_h

    D tmpnam          PR              *   extproc('_C_IFS_tmpnam')
    D   string                      39A   options(*omit)

    d filename        s             40A   varying
    D fd              s             10I 0

     /free

        // Creating a temporary work file in the IFS
        //
        //   a) Get a temporary filename
        //   b) Exclusively create the file, and only allow the
        //        owner to read/write from it.
        //   c) When done, close & delete the file.
        // (a)

          filename = %str(tmpnam(*OMIT));

        // (b)

          fd = open( filename
                   : O_CREAT + O_EXCL + O_RDWR
                   : S_IRUSR + S_IWUSR );

          if fd = -1;
             // open failed, check errno for the reason why.
          endif;

        // ...  insert code here that uses the temporary file as
        //      required  ...

        // (c)

          callp close(fd);
          callp unlink(filename);

          *inlr = *on;

     /end-free

You can download the source code for this article, including the /copy file, from the iSeries Network Web site at the following link: http://www.iseriesnetwork.com/noderesources/code/clubtechcode/TempFile.zip.

The articles that I've written for iSeries NEWS about IFS programming in ILE RPG can be read at the following links if you have a ProVIP membership with the iSeries Network:
http://www.iseriesnetwork.com/article.cfm?ID=19312
http://www.iseriesnetwork.com/article.cfm?ID=19473

There's also a tutorial on IFS programming on my Web site at the following link:
http://www.scottklement.com/rpg/ifs.html

ProVIP Sponsors

ProVIP Sponsors