Thread: Directories as regular files

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    7

    Directories as regular files

    Hello guys, I wasn't sure whether I should post this here or in a Linux forum. Forgive me if I'm wrong. The problem is the following: aren't Linux directories just regular files that have a list of other files as their contents? So why this dummy code works with a regular file but prints "error reading dir" with a directory?

    Code:
    	char *file = "dir";
    	int fd;
    	
    	if ((fd = open(file, O_RDONLY)) == -1)
    		printf("error opening %s\n", file);
    	
    	char buffer[1024];
    	
    	int n;
    	if((n = read(fd, &buffer, 1024)) == -1)
    		printf("error reading %s\n", file);
    	
    	int i;
    	for (i = 0; i < n; ++i)
    		printf("%c", buffer[i]);
    Perhaps directories are different from regular files in Linux? That would be strange. In Unix they are the same or so they are treated in the simple implementation of opendir, readdir, etc in K&R2. Any hints?

    Thanks in advance for your help

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Check errno after trying to read. You're probably getting EISDIR. According to POSIX, EISDIR is set when “[t]he fildes argument refers to a directory and the implementation does not allow the directory to be read using read() or pread(). The readdir() function should be used instead.”

    While traditionally a directory could be read with open() and read(), there is no guarantee that a modern system allows it.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    7
    Check errno after trying to read. You're probably getting EISDIR
    Yes, I just checked and that is what I got. So I guess I can't try out the code from K&R2. In any case I have the functions I mentioned in dirent.h and they work with their code with almost no modification.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The only UNIX OS I am aware of that will allow you to open a directory as a regular file, is SunOS (not Solaris -- I really do mean SunOS).

    Directories might be regular files "under the covers," but POSIX does NOT require that they be openable as such.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error opening files in a different dir
    By Ozzie in forum C++ Programming
    Replies: 3
    Last Post: 10-09-2008, 06:55 AM
  2. Program Deployment and DLL/OCX Files?
    By dfghjk in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2008, 02:47 AM
  3. accessing all files in a folder.
    By pastitprogram in forum C++ Programming
    Replies: 15
    Last Post: 04-30-2008, 10:56 AM
  4. How to add C++ files from directories in makefile.am
    By Bargi in forum Linux Programming
    Replies: 0
    Last Post: 10-15-2007, 04:43 AM
  5. Creating files in specific directories
    By Kyoto Oshiro in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 08:50 PM