LCOV - code coverage report
Current view: top level - lib/replace/tests - os2_delete.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 57 61 93.4 %
Date: 2024-04-21 15:09:00 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :   test readdir/unlink pattern that OS/2 uses
       3             :   tridge@samba.org July 2005
       4             : */
       5             : 
       6             : #include <stdio.h>
       7             : #include <stdlib.h>
       8             : #include <sys/stat.h>
       9             : #include <unistd.h>
      10             : #include <sys/types.h>
      11             : #include <dirent.h>
      12             : #include <errno.h>
      13             : #include <string.h>
      14             : #include <fcntl.h>
      15             : #include "replace-test.h"
      16             : 
      17             : #define NUM_FILES 700
      18             : #define READDIR_SIZE 100
      19             : #define DELETE_SIZE 4
      20             : 
      21             : #define TESTDIR "test.dir"
      22             : 
      23             : static int test_readdir_os2_delete_ret;
      24             : 
      25             : #define FAILED(d) (printf("failure: readdir [\nFailed for %s - %d = %s\n]\n", d, errno, strerror(errno)), test_readdir_os2_delete_ret = 1)
      26             : 
      27             : #ifndef MIN
      28             : #define MIN(a,b) ((a)<(b)?(a):(b))
      29             : #endif
      30             : 
      31             : #ifdef _WIN32
      32             : #define mkdir(d,m) _mkdir(d)
      33             : #endif
      34             : 
      35           4 : static void cleanup(void)
      36             : {
      37             :         /* I'm a lazy bastard */
      38           4 :         if (system("rm -rf " TESTDIR)) {
      39           0 :                 FAILED("system");
      40             :         }
      41           4 :         mkdir(TESTDIR, 0700) == 0 || FAILED("mkdir");
      42           4 : }
      43             : 
      44           4 : static void create_files(void)
      45             : {
      46           1 :         int i;
      47        2804 :         for (i=0;i<NUM_FILES;i++) {
      48         700 :                 char fname[40];
      49         700 :                 int fd;
      50        2800 :                 snprintf(fname, sizeof(fname), TESTDIR "/test%u.txt", i);
      51        2800 :                 fd = open(fname, O_CREAT|O_RDWR, 0600);
      52        2800 :                 if (fd < 0) {
      53           0 :                         FAILED("open");
      54             :                 }
      55        2800 :                 if (close(fd) != 0) {
      56           0 :                         FAILED("close");
      57             :                 }
      58             :         }
      59           4 : }
      60             : 
      61         704 : static int os2_delete(DIR *d)
      62             : {
      63         176 :         off_t offsets[READDIR_SIZE];
      64         176 :         int i, j;
      65         176 :         struct dirent *de;
      66         176 :         char names[READDIR_SIZE][256];
      67             : 
      68             :         /* scan, remembering offsets */
      69         704 :         for (i=0, de=readdir(d);
      70       65904 :              de && i < READDIR_SIZE;
      71       65200 :              de=readdir(d), i++) {
      72       65200 :                 offsets[i] = telldir(d);
      73             :                 /* strlcpy not available here */
      74       65200 :                 snprintf(names[i], sizeof(names[i]), "%s", de->d_name);
      75             :         }
      76             : 
      77         704 :         if (i == 0) {
      78           3 :                 return 0;
      79             :         }
      80             : 
      81             :         /* delete the first few */
      82        3500 :         for (j=0; j<MIN(i, DELETE_SIZE); j++) {
      83         700 :                 char fname[40];
      84        2800 :                 snprintf(fname, sizeof(fname), TESTDIR "/%s", names[j]);
      85        2800 :                 unlink(fname) == 0 || FAILED("unlink");
      86             :         }
      87             : 
      88             :         /* seek to just after the deletion */
      89         700 :         seekdir(d, offsets[j-1]);
      90             : 
      91             :         /* return number deleted */
      92         700 :         return j;
      93             : }
      94             : 
      95           4 : int test_readdir_os2_delete(void)
      96             : {
      97           4 :         int total_deleted = 0;
      98           1 :         DIR *d;
      99           1 :         struct dirent *de;
     100             : 
     101           4 :         test_readdir_os2_delete_ret = 0;
     102             : 
     103           4 :         cleanup();
     104           4 :         create_files();
     105             : 
     106           4 :         d = opendir(TESTDIR "/test0.txt");
     107           4 :         if (d != NULL) FAILED("opendir() on file succeed");
     108           4 :         if (errno != ENOTDIR) FAILED("opendir() on file didn't give ENOTDIR");
     109           4 :         if (d != NULL) closedir(d);
     110             : 
     111           4 :         d = opendir(TESTDIR);
     112             : 
     113             :         /* skip past . and .. */
     114           4 :         de = readdir(d);
     115           4 :         strcmp(de->d_name, ".") == 0 || FAILED("match .");
     116           4 :         de = readdir(d);
     117           4 :         strcmp(de->d_name, "..") == 0 || FAILED("match ..");
     118             : 
     119         876 :         while (1) {
     120         704 :                 int n = os2_delete(d);
     121         704 :                 if (n == 0) break;
     122         700 :                 total_deleted += n;
     123             :         }
     124           4 :         closedir(d);
     125             : 
     126           4 :         fprintf(stderr, "Deleted %d files of %d\n", total_deleted, NUM_FILES);
     127             : 
     128           4 :         rmdir(TESTDIR) == 0 || FAILED("rmdir");
     129             : 
     130           4 :         if (system("rm -rf " TESTDIR) == -1) {
     131           0 :                 FAILED("system");
     132             :         }
     133             : 
     134           4 :         return test_readdir_os2_delete_ret;
     135             : }

Generated by: LCOV version 1.14