Advertisement
Guest User

NFS File Test

a guest
Jan 27th, 2015
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.51 KB | None | 0 0
  1.  
  2. #include <errno.h> /* EINVAL */
  3. #include <signal.h> /* signal () */
  4.  #include <sys/wait.h> /* wait () */
  5.  #include <unistd.h> /* fork (), execvp (), _exit () */
  6.  
  7. #include <assert.h>
  8. #include <math.h>
  9. #include <stdarg.h>
  10. #include <stdio.h> /* remove () */
  11. #include <string.h> /* memcmp */
  12. #include <stdlib.h> /* qsort */
  13. #include <limits.h> /* INT_MAX */
  14. #include <dirent.h>
  15. #include <event2/util.h>
  16. #include <inttypes.h> /* uintN_t */
  17. #include <time.h> /* time_t */
  18. #ifdef _MSC_VER
  19.  typedef intptr_t ssize_t;
  20. #endif
  21.  
  22. #define TR_PATH_DELIMITER '/'
  23. #define TR_PATH_DELIMITER_STR "/"
  24. #if !defined (__cplusplus)
  25.  #ifdef HAVE_STDBOOL_H
  26.   #include <stdbool.h>
  27.  #elif !defined (__bool_true_false_are_defined)
  28.   #define bool uint8_t
  29.   #define true 1
  30.   #define false 0
  31.  #endif
  32. #endif
  33.  
  34. typedef void * tr_sys_dir_t;
  35.  
  36. typedef struct tr_ptrArray
  37. {
  38.     void ** items;
  39.     int     n_items;
  40.     int     n_alloc;
  41. }
  42. tr_ptrArray;
  43.  
  44. const tr_ptrArray TR_PTR_ARRAY_INIT = { NULL, 0, 0 };
  45.  
  46. void*
  47. tr_malloc (size_t size)
  48. {
  49.   return size ? malloc (size) : NULL;
  50. }
  51.  
  52. #define tr_new(struct_type, n_structs)           \
  53.   ((struct_type *) tr_malloc (sizeof (struct_type) * ((size_t)(n_structs))))
  54.  
  55. char*
  56. tr_buildPath (const char *first_element, ...)
  57. {
  58.   const char * element;
  59.   char * buf;
  60.   char * pch;
  61.   va_list vl;
  62.   size_t bufLen = 0;
  63.  
  64.   /* pass 1: allocate enough space for the string */
  65.   va_start (vl, first_element);
  66.   element = first_element;
  67.   while (element)
  68.     {
  69.       bufLen += strlen (element) + 1;
  70.       element = va_arg (vl, const char*);
  71.     }
  72.   pch = buf = tr_new (char, bufLen);
  73.   va_end (vl);
  74.   if (buf == NULL)
  75.     return NULL;
  76.  
  77.   /* pass 2: build the string piece by piece */
  78.   va_start (vl, first_element);
  79.   element = first_element;
  80.   while (element)
  81.     {
  82.       const size_t elementLen = strlen (element);
  83.       memcpy (pch, element, elementLen);
  84.       pch += elementLen;
  85.       *pch++ = TR_PATH_DELIMITER;
  86.       element = va_arg (vl, const char*);
  87.     }
  88.   va_end (vl);
  89.  
  90.   /* terminate the string. if nonempty, eat the unwanted trailing slash */
  91.   if (pch != buf)
  92.     --pch;
  93.   *pch++ = '\0';
  94.  
  95.   /* sanity checks & return */
  96.   assert (pch - buf == (off_t)bufLen);
  97.   return buf;
  98. }
  99.  
  100. /** @brief Platform-specific invalid directory descriptor constant. */
  101. #define TR_BAD_SYS_DIR ((tr_sys_dir_t)NULL)
  102.  
  103.  
  104. tr_sys_dir_t
  105. tr_sys_dir_open (const char  * path)
  106. {
  107.   tr_sys_dir_t ret;
  108.  
  109.   assert (path != NULL);
  110.  
  111.   ret = opendir (path);
  112.  
  113.   return ret;
  114. }
  115.  
  116. const char *
  117. tr_sys_dir_read_name (tr_sys_dir_t    handle)
  118. {
  119.   const char * ret = NULL;
  120.   struct dirent * entry;
  121.  
  122.   assert (handle != TR_BAD_SYS_DIR);
  123.  
  124.   errno = 0;
  125.   entry = readdir (handle);
  126.  
  127.   if (entry != NULL)
  128.     ret = entry->d_name;
  129.  
  130.   return ret;
  131. }
  132.  
  133. bool
  134. tr_sys_dir_close (tr_sys_dir_t    handle)
  135. {
  136.   bool ret;
  137.  
  138.   assert (handle != TR_BAD_SYS_DIR);
  139.  
  140.   ret = closedir (handle) != -1;
  141.  
  142.   return ret;
  143. }
  144.  
  145.  
  146. /**
  147.  * This convoluted code does something (seemingly) simple:
  148.  * remove the torrent's local files.
  149.  *
  150.  * Fun complications:
  151.  * 1. Try to preserve the directory hierarchy in the recycle bin.
  152.  * 2. If there are nontorrent files, don't delete them...
  153.  * 3. ...unless the other files are "junk", such as .DS_Store
  154.  */
  155. static void
  156. deleteLocalData (const char *tmpdir)
  157. {
  158.   tr_sys_dir_t odir;
  159.  
  160.   /***
  161.   ****  Remove tmpdir.
  162.   ****
  163.   ****  Try deleting the top-level files & folders to preserve
  164.   ****  the directory hierarchy in the recycle bin.
  165.   ****  If case that fails -- for example, rmdir () doesn't
  166.   ****  delete nonempty folders -- go from the bottom up too.
  167.   ***/
  168.   fprintf(stderr,  "Started Delete on %s\n", tmpdir);
  169.   /* try deleting the local data's top-level files & folders */
  170.   if ((odir = tr_sys_dir_open (tmpdir)) != TR_BAD_SYS_DIR)
  171.     {
  172.       const char * name;
  173.       int i = 0;
  174.       while ((name = tr_sys_dir_read_name (odir)) != NULL && i <6)
  175.         {
  176.           if (strcmp (name, ".") != 0 && strcmp (name, "..") != 0)
  177.             {
  178.               i++;
  179.               char * file = tr_buildPath (tmpdir, name, NULL);
  180.               fprintf(stderr,  "Try Delete on %s\n", file);
  181.               //The call to remove(file) is causing tr_sys_dir_read_name (odir) to return the first item again instead of NULL when reaching the end of the directory listings
  182.               remove (file);
  183.             }
  184.         }
  185.         fprintf(stderr,  "Closing with i = %i\n", i);
  186.       tr_sys_dir_close (odir);
  187.     }
  188. }
  189.  
  190.  
  191.  
  192. int
  193.     main (void)
  194.     {
  195.     deleteLocalData("/mnt/test/Downloads/Completed/gap/");
  196.         return 0;
  197.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement