summaryrefslogtreecommitdiffhomepage
path: root/images/basic/integrationtest/test_rewinddir.c
blob: f1a4085e1ea1cfe82194041f88e313cb9ea5fef2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>

int main(int argc, char** argv) {
  const char kDirPath[] = "rewinddir_test_dir";
  const char kFileBasename[] = "rewinddir_test_file";

  // Create the test directory.
  if (mkdir(kDirPath, 0755) < 0) {
    err(1, "mkdir(%s)", kDirPath);
  }

  // The test directory should initially be empty.
  DIR* dir = opendir(kDirPath);
  if (!dir) {
    err(1, "opendir(%s)", kDirPath);
  }
  int failed = 0;
  while (1) {
    errno = 0;
    struct dirent* d = readdir(dir);
    if (!d) {
      if (errno != 0) {
        err(1, "readdir");
      }
      break;
    }
    if (strcmp(d->d_name, ".") != 0 && strcmp(d->d_name, "..") != 0) {
      warnx("unexpected file %s in new directory", d->d_name);
      failed = 1;
    }
  }

  // Create a file in the test directory.
  char* file_path = malloc(strlen(kDirPath) + 1 + strlen(kFileBasename));
  if (!file_path) {
    errx(1, "malloc");
  }
  strcpy(file_path, kDirPath);
  file_path[strlen(kDirPath)] = '/';
  strcpy(file_path + strlen(kDirPath) + 1, kFileBasename);
  if (mknod(file_path, 0644, 0) < 0) {
    err(1, "mknod(%s)", file_path);
  }

  // After rewinddir(), re-reading the directory stream should yield the new
  // file.
  rewinddir(dir);
  size_t found_file = 0;
  while (1) {
    errno = 0;
    struct dirent* d = readdir(dir);
    if (!d) {
      if (errno != 0) {
        err(1, "readdir");
      }
      break;
    }
    if (strcmp(d->d_name, kFileBasename) == 0) {
      found_file++;
    } else if (strcmp(d->d_name, ".") != 0 && strcmp(d->d_name, "..") != 0) {
      warnx("unexpected file %s in new directory", d->d_name);
      failed = 1;
    }
  }
  if (found_file != 1) {
    warnx("readdir returned file %s %zu times, wanted 1", kFileBasename,
          found_file);
    failed = 1;
  }

  return failed;
}