xref: /freebsd/tools/test/stress2/misc/seekdir.sh (revision 8a272653d9fbd9fc37691c9aad6a05089b4ecb4d)
1#!/bin/sh
2
3# A regression test for seekdir/telldir
4# submitted by julian@freebsd.org
5# https://reviews.freebsd.org/D2410.
6# Fixed by r282485
7
8. ../default.cfg
9
10odir=`pwd`
11cd /tmp
12sed '1,/^EOF/d' < $odir/$0 > seekdir.c
13rm -f /tmp/seekdir
14mycc -o seekdir -O2 seekdir.c || exit 1
15rm -f seekdir.c
16cd $odir
17
18mount | grep -q "$mntpoint " && umount -f $mntpoint
19mount -o size=1g -t tmpfs tmpfs $mntpoint
20
21cd $mntpoint
22mkdir test2
23/tmp/seekdir > /dev/null
24[ `echo $mntpoint/test2/* | wc -w` -eq 1 ] ||
25    { echo FAIL; status=1; }
26cd $odir
27
28while mount | grep $mntpoint | grep -q tmpfs; do
29	umount $mntpoint || sleep 1
30done
31rm -f /tmp/seekdir
32exit $status
33EOF
34#include <stdio.h>
35#include <sys/types.h>
36#include <dirent.h>
37#include <fcntl.h>
38#include <errno.h>
39#include <string.h>
40#include <stdlib.h>
41#include <unistd.h>
42#include <sysexits.h>
43#include <err.h>
44
45#define CHUNKSIZE 5
46#define TOTALFILES 40
47
48static void
49SeekDir(DIR *dirp, long loc)
50{
51    printf("Seeking back to location %ld\n", loc);
52    seekdir(dirp, loc);
53}
54
55static long
56TellDir(DIR *dirp)
57{
58    long loc;
59
60    loc = telldir(dirp);
61    printf("telldir assigned location %ld\n", loc);
62    return (loc);
63}
64
65int
66main(int argc, char *argv[])
67{
68    DIR            *dirp;
69    int        i;
70    int        j;
71    long        offset = 0, prev_offset = 0;
72    char           *files[100];
73    char        filename[100];
74    int        fd;
75    struct dirent  *dp = NULL;
76
77    if (chdir("./test2") != 0) {
78        err(EX_OSERR, "chdir");
79    }
80
81    /*****************************************************/
82    /* Put a set of sample files in the target directory */
83    /*****************************************************/
84
85    for (i=1; i < TOTALFILES ; i++)
86    {
87        sprintf(filename, "file-%d", i);
88        fd = open(filename, O_CREAT, 0666);
89        if (fd == -1) {
90            err(EX_OSERR, "open");
91        }
92        close(fd);
93    }
94    dirp = opendir(".");
95    offset = TellDir(dirp);
96    for (i = 0; i < 20; i++)
97        files[i] = malloc(20);
98
99    /*******************************************************/
100    /* enumerate and delete small sets of files, one group */
101    /* at a time.                                          */
102    /*******************************************************/
103    do {
104
105        /*****************************************/
106        /* Read in up to CHUNKSIZE file names    */
107        /* i will be the number of files we hold */
108        /*****************************************/
109        for (i = 0; i < CHUNKSIZE; i++) {
110            if ((dp = readdir(dirp)) != NULL) {
111                strcpy(files[i], dp->d_name);
112
113                printf("readdir (%ld) returned file %s\n",
114                    offset, files[i]);
115
116                prev_offset = offset;
117                offset = TellDir(dirp);
118
119            } else {
120                printf("readdir returned null\n");
121                break;
122            }
123        }
124
125/****************************************************************/
126        /* Simuate the last entry not fitting into our (samba's) buffer */
127        /* If we read someting in on the last slot, push it back        */
128        /* Pretend it didn't fit. This is approximately what SAMBA does.*/
129/****************************************************************/
130        if (dp != NULL) {
131            /* Step back */
132            SeekDir(dirp, prev_offset);
133            offset = TellDir(dirp);
134            i--;
135            printf("file %s returned\n", files[i]);
136        }
137
138        /*****************************************/
139        /* i is the number of names we have left.*/
140        /*  Delete them.                         */
141        /*****************************************/
142        for (j = 0; j < i; j++) {
143            if (*files[j] == '.') {
144                printf ("skipping %s\n", files[j]);
145            } else {
146                printf("Unlinking file %s\n", files[j]);
147                if (unlink(files[j]) != 0) {
148                    err(EX_OSERR, "unlink");
149                }
150            }
151        }
152    } while (dp != NULL);
153
154    closedir(dirp);
155    //chdir("..");
156
157}
158