Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29253 Discussions

Problems with STAT(), FSTAT() portability function on directories

Evan_de_Kock
Beginner
537 Views
Hello,

I am trying to use the STAT() and FSTAT() functions from the IFPORT module to obtain the access mode of a file or directory. Using, for example, STAT(filename, statb), the subsequent inquiries IAND(statb(3),S_IRUSR), IAND(statb(3),S_IWUSR) and IAND(statb(3),S_IXUSR) to determine the read, write and execute permissions works fine for files, but not for directories. In the case of directories, the said IAND() combinations always yields zero, implying no read or write permissions! Incidently, when using the ACCESS() function from IFPORT, I get sensible results for the same test directories. What is my mistake here?

Also, the filesize given by statb(8) is always zero for directories. The same information (zero size) is also obtained when using the GETFILEINFOQQ() function on directories.

I can work my way around the STAT() and FSTAT() problem on Windows system by using the ACCESS() function when working with directories, but this destroys any chance of trying to write portable code (insofar as permitted by IFPORT)since I would like to obtain and use the full information on a file or directory as provided by the statb arraywhen callingSTAT(filename,statb) or FSTAT(unit,statb).

The above comments applies to a Window system - I have not tried the STAT() or FSTAT() functions on a Linux system yet.

Regards,
Evan de Kock
0 Kudos
4 Replies
Steven_L_Intel1
Employee
537 Views
On Windows, directories aren't files and they need to be treated as a special case by code manipiulating them. I did some tests and could not get ACCESS to do anything useful on a directory. I have not tried them, but you may want to see if PXFSTAT does what you want (a bit harder to use, but theoretically more portable.)
0 Kudos
Evan_de_Kock
Beginner
537 Views
On Windows, directories aren't files and they need to be treated as a special case by code manipiulating them. I did some tests and could not get ACCESS to do anything useful on a directory. I have not tried them, but you may want to see if PXFSTAT does what you want (a bit harder to use, but theoretically more portable.)

Dear Steve,

Thanks for your quick reply!. I have read your answers on similar questions being raised on this Forum, and it seems that you are rather fond of phases such as"On Windows, directories aren't files..." and "they need to be treated as special cases...". I acknowledge that directories aren't "files" on Windows, but I believe that the special treatment of directories should be handled internally by the IFPORT functions in order for these function to be members of the so called portability library. In fact, all the documention on IFPORT routinessuch as STAT(), ACCESS() andGETFILEINFOQQ()do not make any explicit distinction between the treatment of files and directories (as they should, seeing that these function are portability routines).

It seems that you are saying that its the user's problem to create his own set of portability routines, which is rather difficult if there are no portability routinessupported by the compiler in the first instance. Nevertheless, I will have a look at the equivalent Posix routines.

Regards,
Evan de Kock
0 Kudos
Steven_L_Intel1
Employee
537 Views
In cases where the library routines can reasonably handle directories, I think they should. Clearly some of them do. I was just making an observation.
0 Kudos
Evan_de_Kock
Beginner
537 Views
In cases where the library routines can reasonably handle directories, I think they should. Clearly some of them do. I was just making an observation.

Steve, thanks for your earlier suggestion on trying to use the PXFSTAT() routine from the posix module IFPOSIX instead of the STAT() and FSTAT() functions. Yes, it is a bit more complicated to use PXFSTAT(), but it works nicely on directories too. I also like the PXFACCESS() routinemore than the IFPORT function ACCESS() - it makes a proper distinction between the non-existence of the specified file and an invalid file name (see one of my other posts).

Thanks again,
Evan
0 Kudos
Reply