aboutsummaryrefslogtreecommitdiff
path: root/core/os/dir.odin
blob: 9ad5f451e4a83d38cf858550df6b9623642d04e8 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package os2

import "base:runtime"
import "core:slice"
import "core:strings"

read_dir :: read_directory

/*
	Reads the file `f` (assuming it is a directory) and returns the unsorted directory entries.
	This returns up to `n` entries OR all of them if `n <= 0`.
*/
@(require_results)
read_directory :: proc(f: ^File, n: int, allocator: runtime.Allocator) -> (files: []File_Info, err: Error) {
	if f == nil {
		return nil, .Invalid_File
	}

	n := n
	size := n
	if n <= 0 {
		n = -1
		size = 100
	}

	temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator })

	it := read_directory_iterator_create(f)
	defer _read_directory_iterator_destroy(&it)

	dfi := make([dynamic]File_Info, 0, size, temp_allocator)
	defer if err != nil {
		for fi in dfi {
			file_info_delete(fi, allocator)
		}
	}

	for fi, index in read_directory_iterator(&it) {
		if n > 0 && index == n {
			break
		}

		_ = read_directory_iterator_error(&it) or_break

		append(&dfi, file_info_clone(fi, allocator) or_return)
	}

	_ = read_directory_iterator_error(&it) or_return

	return slice.clone(dfi[:], allocator)
}


/*
	Reads the file `f` (assuming it is a directory) and returns all of the unsorted directory entries.
*/
@(require_results)
read_all_directory :: proc(f: ^File, allocator: runtime.Allocator) -> (fi: []File_Info, err: Error) {
	return read_directory(f, -1, allocator)
}

/*
	Reads the named directory by path (assuming it is a directory) and returns the unsorted directory entries.
	This returns up to `n` entries OR all of them if `n <= 0`.
*/
@(require_results)
read_directory_by_path :: proc(path: string, n: int, allocator: runtime.Allocator) -> (fi: []File_Info, err: Error) {
	f := open(path) or_return
	defer close(f)
	return read_directory(f, n, allocator)
}

/*
	Reads the named directory by path (assuming it is a directory) and returns all of the unsorted directory entries.
*/
@(require_results)
read_all_directory_by_path :: proc(path: string, allocator: runtime.Allocator) -> (fi: []File_Info, err: Error) {
	return read_directory_by_path(path, -1, allocator)
}



Read_Directory_Iterator :: struct {
	f: ^File,
	err: struct {
		err:  Error,
		path: [dynamic]byte,
	},
	index: int,
	impl: Read_Directory_Iterator_Impl,
}

/*
Creates a directory iterator with the given directory.

For an example on how to use the iterator, see `read_directory_iterator`.
*/
read_directory_iterator_create :: proc(f: ^File) -> (it: Read_Directory_Iterator) {
	read_directory_iterator_init(&it, f)
	return
}

/*
Initialize a directory iterator with the given directory.

This procedure may be called on an existing iterator to reuse it for another directory.

For an example on how to use the iterator, see `read_directory_iterator`.
*/
read_directory_iterator_init :: proc(it: ^Read_Directory_Iterator, f: ^File) {
	it.err.err = nil
	it.err.path.allocator = file_allocator()
	clear(&it.err.path)

	it.f = f
	it.index = 0

	_read_directory_iterator_init(it, f)
}

/*
Destroys a directory iterator.
*/
read_directory_iterator_destroy :: proc(it: ^Read_Directory_Iterator) {
	if it == nil {
		return
	}

	delete(it.err.path)

	_read_directory_iterator_destroy(it)
}

/*
Retrieve the last error that happened during iteration.
*/
@(require_results)
read_directory_iterator_error :: proc(it: ^Read_Directory_Iterator) -> (path: string, err: Error) {
	return string(it.err.path[:]), it.err.err
}

@(private)
read_directory_iterator_set_error :: proc(it: ^Read_Directory_Iterator, path: string, err: Error) {
	if err == nil {
		return
	}

	resize(&it.err.path, len(path))
	copy(it.err.path[:], path)

	it.err.err = err
}

/*
Returns the next file info entry for the iterator's directory.

The given `File_Info` is reused in subsequent calls so a copy (`file_info_clone`) has to be made to
extend its lifetime.

Example:
	package main

	import "core:fmt"
	import "core:os"

	main :: proc() {
		f, oerr := os.open("core")
		ensure(oerr == nil)
		defer os.close(f)

		it := os.read_directory_iterator_create(f)
		defer os.read_directory_iterator_destroy(&it)

		for info in os.read_directory_iterator(&it) {
			// Optionally break on the first error:
			// Supports not doing this, and keeping it going with remaining items.
			// _ = os.read_directory_iterator_error(&it) or_break

			// Handle error as we go:
			// Again, no need to do this as it will keep going with remaining items.
			if path, err := os.read_directory_iterator_error(&it); err != nil {
				fmt.eprintfln("failed reading %s: %s", path, err)
				continue
			}

			// Or, do not handle errors during iteration, and just check the error at the end.


			fmt.printfln("%#v", info)
		}

		// Handle error if one happened during iteration at the end:
		if path, err := os.read_directory_iterator_error(&it); err != nil {
			fmt.eprintfln("read directory failed at %s: %s", path, err)
		}
	}
*/
@(require_results)
read_directory_iterator :: proc(it: ^Read_Directory_Iterator) -> (fi: File_Info, index: int, ok: bool) {
	if it.f == nil {
		return
	}

	if it.index == 0 && it.err.err != nil {
		return
	}

	return _read_directory_iterator(it)
}

// Recursively copies a directory to `dst` from `src`
copy_directory_all :: proc(dst, src: string, dst_perm := 0o755) -> Error {
	when #defined(_copy_directory_all_native) {
		return _copy_directory_all_native(dst, src, dst_perm)
	} else {
		return _copy_directory_all(dst, src, dst_perm)
	}
}

@(private)
_copy_directory_all :: proc(dst, src: string, dst_perm := 0o755) -> Error {
	err := make_directory(dst, dst_perm)
	if err != nil && err != .Exist {
		return err
	}

	temp_allocator := TEMP_ALLOCATOR_GUARD({})

	abs_src := get_absolute_path(src, temp_allocator) or_return
	abs_dst := get_absolute_path(dst, temp_allocator) or_return

	dst_buf := make([dynamic]byte, 0, len(abs_dst) + 256, temp_allocator) or_return

	w: Walker
	walker_init_path(&w, src)
	defer walker_destroy(&w)

	for info in walker_walk(&w) {
		_ = walker_error(&w) or_break

		rel := strings.trim_prefix(info.fullpath, abs_src)

		non_zero_resize(&dst_buf, 0)
		reserve(&dst_buf, len(abs_dst) + len(Path_Separator_String) + len(rel)) or_return
		append(&dst_buf, abs_dst)
		append(&dst_buf, Path_Separator_String)
		append(&dst_buf, rel)

		if info.type == .Directory {
			err = make_directory(string(dst_buf[:]), dst_perm)
			if err != nil && err != .Exist {
				return err
			}
		} else {
			copy_file(string(dst_buf[:]), info.fullpath) or_return
		}
	}

	_ = walker_error(&w) or_return

	return nil
}