RFS | XFS

sortdir()

PROTOTYPE

#include <posix.h>

int sortdir(const char *path, CompFunc cmp);

DESCRIPTION

sortdir() sorts the entry list of the directory specified by path. Paths starting with ‘/’ are absolute. Otherwise they are relative to the CWD. This sorting changes the entry order seen by readdir() and its related routines. The new order is persistent across unmounts and remounts.

cmp points to a user-supplied callback function that provides the comparison used for sorting. Its prototype is:

int cmp(const DirEntry e1, const DirEntry *e2);

cmp() returns a negative value if entry e1 should appear before entry e2, zero if their order doesn’t matter, or a positive value if entry e2 should appear before entry e1. sortdir() initializes the DirEntry structures before calling cmp(). The DirEntry type is defined in “posix.h” and reproduced below.

/*
** Structure containing file/dir info for sortdir() comparisons
*/
typedef struct
{
  char   *st_name;   /* file name */
  ino_t   st_ino;    /* file serial number */
  nlink_t st_nlink;  /* number of links */
  off_t   st_size;   /* file size in bytes */
  time_t  st_atime;  /* last data access time (secs since Epoch) */
  time_t  st_mtime;  /* last data modification time (secs since Epoch) */
  time_t  st_ctime;  /* last status change time (secs since Epoch) */
  mode_t  st_mode;   /* file mode */
  uid_t   st_uid;    /* user ID of file's owner */
  gid_t   st_gid;    /* group ID of file's owner */
} DirEntry;

The caller needs execute access to every directory in the path prefix, plus read and write access to the directory being sorted.

If successful, sortdir() returns 0. Otherwise, it sets errno and returns -1.

ERROR CODES

EACCES No execute access to a directory in the path prefix or no read or write access to specified directory.
EBUSY The directory is currently open.
EFAULT path or cmp equals NULL.
ENAMETOOLONG A name in path exceeds the effective maximum file name length.
ENOENT path is the empty string or includes a directory that was not found.
ENOTDIR One of the path components is not a directory.
EROFS The volume is read-only.

EXAMPLE

/***********************************************************************/
/*    cmp_name: Compare directory entries by name                      */
/*                                                                     */
/***********************************************************************/
static int cmp_name(const DirEntry *e1, const DirEntry *e2)
{
  return strcmp(e1->st_name, e2->st_name);
}

  ...

  /*-------------------------------------------------------------------*/
  /* List the directory contents.                                      */
  /*-------------------------------------------------------------------*/
  list_dir("../sort");

  /*-------------------------------------------------------------------*/
  /* Sort directory entries by alphanumeric name order.                */
  /*-------------------------------------------------------------------*/
  if (sortdir("../sort", cmp_name))
    error("sortdir() error");

  /*-------------------------------------------------------------------*/
  /* List the directory contents.                                      */
  /*-------------------------------------------------------------------*/
  list_dir("../sort");