Results 1 to 7 of 7

Thread: C++ Dirent.h - File traversal algorithm

  1. #1
    Join Date
    Apr 2008
    Location
    England
    Beans
    603
    Distro
    Ubuntu 10.04 Lucid Lynx

    C++ Dirent.h - File traversal algorithm

    Hi all,

    Im currently creating an algorithm for directory traversal. The algorithm runs a method if a file is found and moves into the directory if a directory is found.

    Code:
      
    if(entry->type == DT_DIR){
    
    //Run method inside this directory
    } if(entry->type == DT_REG){
    // Do stuff on the file
    }
    The problem, however, is that in some cases the entry->type is thought to be a file when it is a Directory. Has anyone come across this before, and does anyone have any solutions?

    Thanks for your time.

  2. #2
    Join Date
    May 2006
    Beans
    1,790

    Re: C++ Dirent.h - File traversal algorithm

    Quote Originally Posted by Sub101 View Post
    Hi all,

    Im currently creating an algorithm for directory traversal. The algorithm runs a method if a file is found and moves into the directory if a directory is found.

    Code:
      
    if(entry->type == DT_DIR){
    
    //Run method inside this directory
    } if(entry->type == DT_REG){
    // Do stuff on the file
    }
    The problem, however, is that in some cases the entry->type is thought to be a file when it is a Directory. Has anyone come across this before, and does anyone have any solutions?

    Thanks for your time.
    I think more details are needed. Why not the whole program, reduced so that it still shows the error?

  3. #3
    Join Date
    Feb 2010
    Location
    Silicon Valley
    Beans
    1,898
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: C++ Dirent.h - File traversal algorithm

    Oops. More details needed. Could you be changing the type?
    Last edited by gmargo; March 1st, 2011 at 04:55 PM.

  4. #4
    Join Date
    Feb 2011
    Location
    Falls Church, VA, USA
    Beans
    38
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: C++ Dirent.h - File traversal algorithm

    Quote Originally Posted by gmargo View Post
    Oops. More details needed.
    Seconded!
    Familiar with and used frequently:
    XP Pro, 7 Pro, Windows Server 2008 (EEW!), Mac OS X 10.5-10.6, SUSE Linux Enterprise, Xubuntu 8.04 LTS, Ubuntu 10.04 LTS.

  5. #5
    Join Date
    Nov 2010
    Location
    Down the rabbit hole
    Beans
    435
    Distro
    Ubuntu Development Release

    Re: C++ Dirent.h - File traversal algorithm

    I strongly suggest you stop using dirent.d_type for checking the type of the file.
    I just made this mistake in my app and had to rewrite it to use stat instead.
    That's because i.e. on XFS dirent.d_type is not supported because dirent only guarantees that the filename (d_name) always be present.

    char d_name[]
    This is the null-terminated file name component. This is the only field you can count on in all POSIX systems.
    Source:
    http://www.gnu.org/s/libc/manual/htm...ectory-Entries

    Researching this I found bugs on launchpad about apps because they've been relying on dirent.d_type and the solution was to switch over to stat.

    PS: since you're using dirent/stat (and if file size etc matters to your algorithm) don't forget about the LFS issue, which in glib's gio and other higher level APIs is taken care of automatically.
    Last edited by t1497f35; March 1st, 2011 at 05:44 PM.
    Your ads here, just 9.99$/week !!

  6. #6
    Join Date
    Apr 2006
    Location
    Atlanta, USA
    Beans
    427

    Re: C++ Dirent.h - File traversal algorithm

    Quote Originally Posted by t1497f35 View Post
    I strongly suggest you stop using dirent.d_type for checking the type of the file.
    I just made this mistake in my app and had to rewrite it to use stat instead.
    That's because i.e. on XFS dirent.d_type is not supported because dirent only guarantees that the filename (d_name) always be present.

    Source:
    http://www.gnu.org/s/libc/manual/htm...ectory-Entries

    Researching this I found bugs on launchpad about apps because they've been relying on dirent.d_type and the solution was to switch over to stat.
    I think this is good advice, use dirent to determine the filename, and then do a stat or lstat on the file. Something like this (in C, in C++ you could use a std::stringstream or std::string instead of namebuf/snprintf/etc):

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <dirent.h>
    #include <unistd.h>
    #include <string.h>
    #include <limits.h>
    
    static void list_dir(const char* base_dir) 
    {
        DIR* dir;
        struct dirent* ent;
        struct stat buf;
        
        char namebuf[PATH_MAX];
        size_t base_len = strlen(base_dir);
        
        if (!(dir = opendir(base_dir))) {
            perror("failed to open directory");
            return;
        }
        
        while((ent = readdir(dir)) != NULL) {
            /* ignore .. and . */
            if (!strcmp(ent->d_name, "..") || !strcmp(ent->d_name, ".")) {
                continue;
            }
            
            snprintf(namebuf, sizeof(namebuf), "%s%s%s",
                     base_dir,
                     (base_dir[base_len - 1] == '/' ? "" : "/"),
                     ent->d_name);
    
            if (lstat(namebuf, &buf) != 0) {
                perror(namebuf);
                continue;
            }
    
            if (S_ISREG(buf.st_mode)) {
                printf("FILE: %s\n", namebuf);
            } else if (S_ISLNK(buf.st_mode)) {
                printf("LINK: %s\n", namebuf);
            } else if (S_ISDIR(buf.st_mode)) {
                printf("DIR:  %s\n", namebuf);
                /* recurse, if desired */
                /* list_dir(namebuf);  */
            }
        }
    
        closedir(dir);
    }
    Here we are, trapped in the amber of the moment. There is no why.

  7. #7
    Join Date
    Apr 2008
    Location
    England
    Beans
    603
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: C++ Dirent.h - File traversal algorithm

    Thanks for your help all. Ill use stat as suggested.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •