2012-12-18

modFileSys: directory and file manipulation in Fortran

Update (2016-02-24): I have moved the content of modfilesys into the Fortyxima library.

Recently, the DFTB+ project needed a simple feature extension: The main program had to delete/create a scratch directory which stores a bunch of auxiliary files (surface Greens functions). Until now, we have relied on the fact, that the directory is just there (e.g. being magically created by some wrapper script around the code), which turned out to be a pain as nobody wrote such a magical script and consequently we usually just forgot to create that directory.  Quite annoying if your job waits a few days in the queue just to produce a reminder about you being fool enough having forgotten to create the obligatory directory...

Simple tasks usually require simple solutions, unless they are operating system related and the programming language of your choice happens to be Fortran; for this special combination things tend to be rather complicated. The first quick and dirty approach coming up was to (ab)use the system call:

call system("rm –rf directory")
call system("mkdir directory")

While working as expected, it felt like a punch in the face to mix shell commands with  Fortran statements. So, why not writing something more appropriate, maybe using the shiny standardized C-interface feature of Fortran 2003, which is by now supported by practically all Fortran compilers? Should not take more than an afternoon, should it?

Well, it took somewhat longer, but finally the first working version of MODFILESYS was created. Now, after having wrapped the libc file system interface with modern Fortran 2003 wrappers, we can create and remove directories (even recursively!), check the existence of files, manipulate links and symlinks, and list the content of directories. Things I always missed in Fortran, now being there, being simple and fortranic! So, from now on, we just simply issue

call rmdir("directory", children=.true.)
call mkdir("directory")

in order to remove and recreate our beloved/behated scratch folders. Although being only subtle differences in the actual program lines, what an aesthetic difference in the method under the hood!