aboutsummaryrefslogtreecommitdiff
path: root/core/os/dir_linux.odin
blob: 1ca2ef9b42903e0a070367629a83802c12a967ab (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#+private
package os

import "core:sys/linux"

Read_Directory_Iterator_Impl :: struct {
	prev_fi:        File_Info,
	dirent_backing: []u8,
	dirent_buflen:  int,
	dirent_off:     int,
}

@(require_results)
_read_directory_iterator :: proc(it: ^Read_Directory_Iterator) -> (fi: File_Info, index: int, ok: bool) {
	scan_entries :: proc(it: ^Read_Directory_Iterator, dfd: linux.Fd, entries: []u8, offset: ^int) -> (fd: linux.Fd, file_name: string) {
		for d in linux.dirent_iterate_buf(entries, offset) {
			file_name = linux.dirent_name(d)
			if file_name == "." || file_name == ".." {
				continue
			}

			file_name_cstr := cstring(raw_data(file_name))
			entry_fd, errno := linux.openat(dfd, file_name_cstr, {.NOFOLLOW, .PATH})
			if errno == .NONE {
				return entry_fd, file_name
			} else {
				read_directory_iterator_set_error(it, file_name, _get_platform_error(errno))
			}
		}

		return -1, ""
	}

	index = it.index
	it.index += 1

	dfd := linux.Fd(_fd(it.f))

	entries := it.impl.dirent_backing[:it.impl.dirent_buflen]
	entry_fd, file_name := scan_entries(it, dfd, entries, &it.impl.dirent_off)

	for entry_fd == -1 {
		if len(it.impl.dirent_backing) == 0 {
			it.impl.dirent_backing = make([]u8, 512, file_allocator())
		}

		loop: for {
			buflen, errno := linux.getdents(linux.Fd(dfd), it.impl.dirent_backing[:])
			#partial switch errno {
			case .EINVAL:
				delete(it.impl.dirent_backing, file_allocator())
				n := len(it.impl.dirent_backing) * 2
				it.impl.dirent_backing = make([]u8, n, file_allocator())
				continue
			case .NONE:
				if buflen == 0 {
					return
				}
				it.impl.dirent_off = 0
				it.impl.dirent_buflen = buflen
				entries = it.impl.dirent_backing[:buflen]
				break loop
			case:
				read_directory_iterator_set_error(it, name(it.f), _get_platform_error(errno))
				return
			}
		}

		entry_fd, file_name = scan_entries(it, dfd, entries, &it.impl.dirent_off)
	}
	defer linux.close(entry_fd)

	// PERF: reuse the fullpath string like on posix and wasi.
	file_info_delete(it.impl.prev_fi, file_allocator())

	err: Error
	fi, err = _fstat_internal(entry_fd, file_allocator())
	it.impl.prev_fi = fi

	if err != nil {
		temp_allocator := TEMP_ALLOCATOR_GUARD({})
		path, _ := _get_full_path(entry_fd, temp_allocator)
		read_directory_iterator_set_error(it, path, err)
	}

	ok = true
	return
}

_read_directory_iterator_init :: proc(it: ^Read_Directory_Iterator, f: ^File) {
	// NOTE: Allow calling `init` to target a new directory with the same iterator.
	it.impl.dirent_buflen = 0
	it.impl.dirent_off = 0

	if f == nil || f.impl == nil {
		read_directory_iterator_set_error(it, "", .Invalid_File)
		return
	}

	stat: linux.Stat
	errno := linux.fstat(linux.Fd(fd(f)), &stat)
	if errno != .NONE {
		read_directory_iterator_set_error(it, name(f), _get_platform_error(errno))
		return
	}

	if (stat.mode & linux.S_IFMT) != linux.S_IFDIR {
		read_directory_iterator_set_error(it, name(f), .Invalid_Dir)
		return
	}
}

_read_directory_iterator_destroy :: proc(it: ^Read_Directory_Iterator) {
	if it == nil {
		return
	}

	delete(it.impl.dirent_backing, file_allocator())
	file_info_delete(it.impl.prev_fi, file_allocator())
}