|
| 1 | +#include <dirent.h> /* Defines DT_* constants */ |
| 2 | +#include <fcntl.h> |
| 3 | +#include <stdint.h> |
| 4 | +#include <stdio.h> |
| 5 | +#include <unistd.h> |
| 6 | +#include <stdlib.h> |
| 7 | +#include <sys/stat.h> |
| 8 | +#include <sys/syscall.h> |
| 9 | +#include <cassert> |
| 10 | + |
| 11 | +#include <memory> |
| 12 | +#include <string> |
| 13 | +#include <vector> |
| 14 | +#include <algorithm> |
| 15 | + |
| 16 | +#define handle_error(msg) \ |
| 17 | + do { perror(msg); exit(EXIT_FAILURE); } while (0) |
| 18 | + |
| 19 | +struct test_dirent64 { |
| 20 | + unsigned long d_ino; |
| 21 | + off_t d_off; |
| 22 | + unsigned char d_type; |
| 23 | + std::string d_name; |
| 24 | + |
| 25 | + bool operator ==(const test_dirent64 &b) const { |
| 26 | + return d_ino == b.d_ino && |
| 27 | + d_off == b.d_off && |
| 28 | + d_type == b.d_type && |
| 29 | + d_name == b.d_name; |
| 30 | + } |
| 31 | +}; |
| 32 | + |
| 33 | +// This code is loosely based on the example found under https://man7.org/linux/man-pages/man2/getdents.2.html |
| 34 | +void test_getdents64(const char *dir_path, size_t buf_size, std::vector<test_dirent64> &dirents) { |
| 35 | + struct linux_dirent64 { |
| 36 | + unsigned long d_ino; |
| 37 | + off_t d_off; |
| 38 | + unsigned short d_reclen; |
| 39 | + unsigned char d_type; |
| 40 | + char d_name[]; |
| 41 | + }; |
| 42 | + |
| 43 | + int fd = open(dir_path, O_RDONLY | O_DIRECTORY); |
| 44 | + if (fd == -1) |
| 45 | + handle_error("open"); |
| 46 | + |
| 47 | + std::unique_ptr<char []> buf_ptr(new char[buf_size]); |
| 48 | + char *buf = buf_ptr.get(); |
| 49 | + |
| 50 | + for (;;) { |
| 51 | + long nread = syscall(SYS_getdents64, fd, buf, buf_size); |
| 52 | + if (nread == -1) |
| 53 | + handle_error("getdents64"); |
| 54 | + |
| 55 | + if (nread == 0) |
| 56 | + break; |
| 57 | + |
| 58 | + printf("--------------- nread=%ld ---------------\n", nread); |
| 59 | + printf("inode# file type d_reclen d_off d_name\n"); |
| 60 | + for (long bpos = 0; bpos < nread;) { |
| 61 | + auto *d = (struct linux_dirent64 *) (buf + bpos); |
| 62 | + printf("%8ld ", d->d_ino); |
| 63 | + |
| 64 | + char d_type = d->d_type; |
| 65 | + printf("%-10s ", (d_type == DT_REG) ? "regular" : |
| 66 | + (d_type == DT_DIR) ? "directory" : |
| 67 | + (d_type == DT_FIFO) ? "FIFO" : |
| 68 | + (d_type == DT_SOCK) ? "socket" : |
| 69 | + (d_type == DT_LNK) ? "symlink" : |
| 70 | + (d_type == DT_BLK) ? "block dev" : |
| 71 | + (d_type == DT_CHR) ? "char dev" : "???"); |
| 72 | + |
| 73 | + printf("%4d %10jd %s\n", d->d_reclen, |
| 74 | + (intmax_t) d->d_off, d->d_name); |
| 75 | + bpos += d->d_reclen; |
| 76 | + |
| 77 | + test_dirent64 dirent; |
| 78 | + dirent.d_ino = d->d_ino; |
| 79 | + dirent.d_off = d->d_off; |
| 80 | + dirent.d_type = d_type; |
| 81 | + dirent.d_name = d->d_name; |
| 82 | + dirents.push_back(dirent); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + close(fd); |
| 87 | +} |
| 88 | + |
| 89 | +#define LARGE_BUF_SIZE 1024 |
| 90 | +#define SMALL_BUF_SIZE 128 |
| 91 | + |
| 92 | +int main() |
| 93 | +{ |
| 94 | + // Verify that getdents64 works correctly against /proc directory and yields |
| 95 | + // correct results |
| 96 | + std::vector<test_dirent64> dirents_1; |
| 97 | + test_getdents64("/proc", LARGE_BUF_SIZE, dirents_1); |
| 98 | + |
| 99 | + assert(std::count_if(dirents_1.begin(), dirents_1.end(), [](test_dirent64 d) { return d.d_type == DT_REG; }) >= 3); |
| 100 | + assert(std::count_if(dirents_1.begin(), dirents_1.end(), [](test_dirent64 d) { return d.d_type == DT_DIR; }) >= 5); |
| 101 | + |
| 102 | + assert(std::find_if(dirents_1.begin(), dirents_1.end(), [](test_dirent64 d) { return d.d_name == ".."; }) != dirents_1.end()); |
| 103 | + assert(std::find_if(dirents_1.begin(), dirents_1.end(), [](test_dirent64 d) { return d.d_name == "cpuinfo"; }) != dirents_1.end()); |
| 104 | + assert(std::find_if(dirents_1.begin(), dirents_1.end(), [](test_dirent64 d) { return d.d_name == "sys"; }) != dirents_1.end()); |
| 105 | + |
| 106 | + // Verify that getdents64 works with smaller buffer and yields same results as above |
| 107 | + std::vector<test_dirent64> dirents_2; |
| 108 | + test_getdents64("/proc", SMALL_BUF_SIZE, dirents_2); |
| 109 | + |
| 110 | + assert(std::equal(dirents_1.begin(), dirents_1.end(), dirents_2.begin())); |
| 111 | +} |
0 commit comments