Fixed bug no.207.
authorDave West <davew@uncensored.citadel.org>
Mon, 29 Oct 2007 23:35:38 +0000 (23:35 +0000)
committerDave West <davew@uncensored.citadel.org>
Mon, 29 Oct 2007 23:35:38 +0000 (23:35 +0000)
Don't show non regular files for download.
Sub directories are not supported.

citadel/room_ops.c

index caed7a3054684917e6f1be2f1af594446a542ee5..af6cb2149babc141aafbc0394f8191aae5eaf43a 100644 (file)
@@ -12,6 +12,7 @@
 #include <sys/stat.h>
 #include <ctype.h>
 #include <string.h>
+#include <dirent.h>    /* for cmd_rdir to read contents of the directory */
 
 #if TIME_WITH_SYS_TIME
 # include <sys/time.h>
@@ -1114,14 +1115,15 @@ void cmd_whok(void)
 void cmd_rdir(void)
 {
        char buf[256];
-       char flnm[256];
        char comment[256];
-       FILE *ls, *fd;
+       FILE *fd;
        struct stat statbuf;
-       char tempfilename[PATH_MAX];
-
+       DIR *filedir = NULL;
+       struct dirent *filedir_entry;
+       int d_namelen;
+       char buf2[SIZ];
+       
        if (CtdlAccessCheck(ac_logged_in)) return;
-       CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
        
        getroom(&CC->room, CC->room.QRname);
        getuser(&CC->user, CC->curr_user);
@@ -1136,55 +1138,48 @@ void cmd_rdir(void)
                cprintf("%d not here.\n", ERROR + HIGHER_ACCESS_REQUIRED);
                return;
        }
-       cprintf("%d %s|%s/%s\n",
-                       LISTING_FOLLOWS, 
-                       config.c_fqdn,
-                       ctdl_file_dir, 
-                       CC->room.QRdirname);
-
-       snprintf(buf, sizeof buf, 
-                        "ls %s/%s >%s 2>/dev/null",
-                        ctdl_file_dir,
-                        CC->room.QRdirname, 
-                        tempfilename);
-       system(buf);
-
-       snprintf(buf, sizeof buf, 
-                        "%s/%s/filedir",
-                        ctdl_file_dir,
-                        CC->room.QRdirname);
-       fd = fopen(buf, "r");
-       if (fd == NULL)
-               fd = fopen("/dev/null", "r");
-
-       ls = fopen(tempfilename, "r");
-       while (fgets(flnm, sizeof flnm, ls) != NULL) {
-               flnm[strlen(flnm) - 1] = 0;
-               if (strcasecmp(flnm, "filedir")) {
-                       snprintf(buf, sizeof buf, 
-                                        "%s/%s/%s",
-                                        ctdl_file_dir,
-                                        CC->room.QRdirname,
-                                        flnm);
-                       stat(buf, &statbuf);
-                       safestrncpy(comment, "", sizeof comment);
-                       fseek(fd, 0L, 0);
-                       while ((fgets(buf, sizeof buf, fd) != NULL)
-                              && (IsEmptyStr(comment))) {
-                               buf[strlen(buf) - 1] = 0;
-                               if ((!strncasecmp(buf, flnm, strlen(flnm)))
-                                   && (buf[strlen(flnm)] == ' '))
-                                       safestrncpy(comment,
-                                           &buf[strlen(flnm) + 1],
-                                           sizeof comment);
+       cprintf("%d %s|%s/%s\n", LISTING_FOLLOWS, config.c_fqdn, ctdl_file_dir, CC->room.QRdirname);
+       
+       snprintf(buf, sizeof buf, "%s/%s", ctdl_file_dir, CC->room.QRdirname);
+       filedir = opendir (buf);
+       if (filedir)
+       {
+               snprintf(buf, sizeof buf, "%s/%s/filedir", ctdl_file_dir, CC->room.QRdirname);
+               fd = fopen(buf, "r");
+               if (fd == NULL)
+                       fd = fopen("/dev/null", "r");
+               while ((filedir_entry = readdir(filedir)))
+               {
+                       if (strcasecmp(filedir_entry->d_name, "filedir") && filedir_entry->d_name[0] != '.')
+                       {
+#ifdef _DIRENT_HAVE_D_NAMELEN
+                               d_namelen = filedir_entry->d_namelen;
+#else
+                               d_namelen = strlen(filedir_entry->d_name);
+#endif
+                               snprintf(buf, sizeof buf, "%s/%s/%s", ctdl_file_dir, CC->room.QRdirname, filedir_entry->d_name);
+                               stat(buf, &statbuf);    /* stat the file */
+                               if (!(statbuf.st_mode & S_IFREG))
+                               {
+                                       snprintf(buf2, sizeof buf2, "Command RDIR found something that is not a useable file. It should be cleaned up.\n RDIR found this non regular file:\n%s\n", buf);
+                                       aide_message(buf2, "RDIR found bad file");
+                                       continue;       /* not a useable file type so don't show it */
+                               }
+                               safestrncpy(comment, "", sizeof comment);
+                               fseek(fd, 0L, 0);       /* rewind descriptions file */
+                               /* Get the description from the descriptions file */
+                               while ((fgets(buf, sizeof buf, fd) != NULL) && (IsEmptyStr(comment))) 
+                               {
+                                       buf[strlen(buf) - 1] = 0;
+                                       if ((!strncasecmp(buf, filedir_entry->d_name, d_namelen)) && (buf[d_namelen] == ' '))
+                                               safestrncpy(comment, &buf[d_namelen + 1], sizeof comment);
+                               }
+                               cprintf("%s|%ld|%s\n", filedir_entry->d_name, (long)statbuf.st_size, comment);
                        }
-                       cprintf("%s|%ld|%s\n", flnm, (long)statbuf.st_size, comment);
                }
+               fclose(fd);
+               closedir(filedir);
        }
-       fclose(ls);
-       fclose(fd);
-       unlink(tempfilename);
-
        cprintf("000\n");
 }