IBM Support

Order of Directory Contents Returned by Calls to readdir()

Troubleshooting


Problem

The sequence that file and directory names are returned by calls to readdir() and readdir_r() is unpredictable.

Resolving The Problem

The sequence that file and directory names are returned by calls to readdir() and readdir_r() is unpredictable.

There is no way to control the sequence of objects returned with these APIs. The order that names are returned is entirely dependent on the implementation of the directory.

In all cases, you may think of a directory simply as a list of names. However, different file systems maintain that list in different ways. The root, QOpenSys, and user-defined file systems all act the same way because they have the same underlying implementation; however, QDLS, QSYS.LIB, and QOPT may act differently because they have different implementations. QNTC and NFS are going to return names according to the rules of the remote server, whether that server is another IBM i, an NT server, or a Linux box.

For the root, QOpenSys, and user-defined file systems, a directory is much like a database file with each name represented by a record with a particular record number. For these file systems, the readdir() API always returns names in sequential record number order. Initially, as names are added to the list, they are simply assigned the next sequential record number, regardless of name. Therefore, when you read the entries with readdir(), you get the names back in the order they were added. However, as soon as an entry is deleted, that record number becomes available for reuse. It will not necessarily be reused for the next entry that is added; however, it might be. Once you start removing or renaming entries in the list and adding new names to the list, the order you will see entries returned can appear quite random. On certain interfaces where the names appear in sorted order (WRKLNK, Qshell 'ls' utility), it is the interface that is sorting the names, not the file system. One example of an interface that does not sort the names is EDTF. If you use EDTF '/', you will see the names as they are returned from the readdir() API. The first entries in the list are those that are normally created right after the root directory itself and are never deleted, like QOpenSys, dev, and home. The entries after that will appear somewhat random, depending on how many objects have been linked and unlinked in that directory.

QDLS, on other hand, always stores the names internally in sorted order and, therefore, readdir() always returns QDLS directory entries in name order. QSYS.LIB also returns entries in sorted order; however, they are first sorted by object type, then by name. All of the *PGM objects will come before all of the *FILE objects, regardless of the individual program and file names. As stated previously, the behavior of the remote file systems will vary based on the file server.

The only way for an application developer to know the order of names returned from the root file system is to create a brand new directory and ensure that objects are only ever added to the directory, never removed. Unfortunately, applications may be adding and removing temporary files without the developer's knowledge, and that will affect the order the names are returned, so there is no truly reliable method to control this.

[{"Type":"MASTER","Line of Business":{"code":"LOB57","label":"Power"},"Business Unit":{"code":"BU058","label":"IBM Infrastructure w\/TPS"},"Product":{"code":"SWG60","label":"IBM i"},"Platform":[{"code":"PF012","label":"IBM i"}],"Version":"6.1.0"}]

Historical Number

636245884

Document Information

Modified date:
18 December 2019

UID

nas8N1010930