Support filesystems that don't return filetypes in readdir()
authorWouter Verhelst <w@uter.be>
Thu, 23 Feb 2012 07:57:41 +0000 (08:57 +0100)
committerWouter Verhelst <w@uter.be>
Thu, 23 Feb 2012 07:57:41 +0000 (08:57 +0100)
nbd-server.c

index 2cd066de367bf89267468143dd4fe8f4a9749761..0e409454136cd5518fcad223386de803f0c9c8c3 100644 (file)
@@ -768,6 +768,7 @@ GArray* do_cfile_dir(gchar* dir, GError** e) {
        gchar* fname;
        GArray* retval = NULL;
        GArray* tmp;
+       struct stat stbuf;
 
        if(!dir) {
                g_set_error(e, errdomain, CFILE_DIR_UNKNOWN, "Invalid directory specified: %s", strerror(errno));
@@ -776,13 +777,24 @@ GArray* do_cfile_dir(gchar* dir, GError** e) {
        errno=0;
        while((de = readdir(dirh))) {
                int saved_errno=errno;
+               fname = g_build_filename(dir, de->d_name, NULL);
                switch(de->d_type) {
+                       case DT_UNKNOWN:
+                               /* Filesystem doesn't return type of
+                                * file through readdir. Run stat() on
+                                * the file instead */
+                               if(stat(fname, &stbuf)) {
+                                       perror("stat");
+                                       goto err_out;
+                               }
+                               if (!S_ISREG(stbuf.st_mode)) {
+                                       goto next;
+                               }
                        case DT_REG:
                                /* Skip unless the name ends with '.conf' */
                                if(strcmp((de->d_name + strlen(de->d_name) - 5), ".conf")) {
                                        continue;
                                }
-                               fname = g_build_filename(dir, de->d_name, NULL);
                                tmp = parse_cfile(fname, FALSE, e);
                                errno=saved_errno;
                                if(*e) {
@@ -792,10 +804,11 @@ GArray* do_cfile_dir(gchar* dir, GError** e) {
                                        retval = g_array_new(FALSE, TRUE, sizeof(SERVER));
                                retval = g_array_append_vals(retval, tmp->data, tmp->len);
                                g_array_free(tmp, TRUE);
-                               g_free(fname);
                        default:
                                break;
                }
+       next:
+               g_free(fname);
        }
        if(errno) {
                g_set_error(e, errdomain, CFILE_READDIR_ERR, "Error trying to read directory: %s", strerror(errno));