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