summaryrefslogtreecommitdiff
path: root/src/t_dir_type.c
blob: ba8e38ff4855cd9f562092e545d690221c9e85b2 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2016 CTERA Networks. All Rights Reserved.
 * Author: Amir Goldstein <amir73il@gmail.com>
 */

/*
 * t_dir_type
 *
 * print directory entries and their file type, optionally filtered by d_type
 * or by inode number.
 *
 * ./t_dir_type <path> [u|f|d|c|b|l|p|s|w|<ino>]
 */

#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/syscall.h>

struct linux_dirent64 {
	uint64_t	d_ino;
	int64_t		d_off;
	unsigned short	d_reclen;
	unsigned char	d_type;
	char		d_name[0];
};

#define DT_MASK 15
#define DT_MAX 15
unsigned char type_to_char[DT_MAX] = {
	[DT_UNKNOWN] = 'u',
	[DT_DIR] = 'd',
	[DT_REG] = 'f',
	[DT_LNK] = 'l',
	[DT_CHR] = 'c',
	[DT_BLK] = 'b',
	[DT_FIFO] = 'p',
	[DT_SOCK] = 's',
	[DT_WHT] = 'w',
};

#define DT_CHAR(t) type_to_char[(t)&DT_MASK]

#define BUF_SIZE 4096

int
main(int argc, char *argv[])
{
	int fd, nread;
	char buf[BUF_SIZE];
	struct linux_dirent64 *d;
	int bpos;
	int type = -1; /* -1 means all types */
	uint64_t ino = 0;
	int ret = 1;

	fd = open(argv[1], O_RDONLY | O_DIRECTORY);
	if (fd < 0) {
		perror("open");
		exit(EXIT_FAILURE);
	}

	if (argc > 2 && argv[2][0]) {
		char t = argv[2][0];

		for (type = DT_MAX-1; type >= 0; type--)
			if (DT_CHAR(type) == t)
				break;
		/* no match ends up with type = -1 */
		if (type < 0)
			ino = strtoul(argv[2], NULL, 10);
	}

	for ( ; ; ) {
		nread = syscall(SYS_getdents64, fd, buf, BUF_SIZE);
		if (nread == -1) {
			perror("getdents");
			exit(EXIT_FAILURE);
		}

		if (nread == 0)
			break;

		for (bpos = 0; bpos < nread;) {
			d = (struct linux_dirent64 *) (buf + bpos);
			if ((type < 0 || type == (int)d->d_type) &&
			    (!ino || ino == d->d_ino)) {
				ret = 0;
				printf("%s %c\n", d->d_name, DT_CHAR(d->d_type));
			}
			bpos += d->d_reclen;
		}
	}

	return ret;
}