Skip to content

Commit 85f9137

Browse files
authored
Fix deleting of compiled files on MacOS (#1280)
1 parent 17a434b commit 85f9137

File tree

1 file changed

+7
-115
lines changed

1 file changed

+7
-115
lines changed

compiler/paths.jou

Lines changed: 7 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import "stdlib/assert.jou"
22
import "stdlib/ascii.jou"
3-
import "stdlib/list.jou"
3+
import "stdlib/fs.jou"
44
import "stdlib/mem.jou"
55
import "stdlib/str.jou"
66
import "stdlib/io.jou"
@@ -48,105 +48,6 @@ else:
4848
@public
4949
declare dirname(path: byte*) -> byte*
5050

51-
if WINDOWS:
52-
class WIN32_FIND_DATAA:
53-
# WIN32_FIND_DATAA is 320 bytes. We can't fill in the actual members
54-
# here because jou adds a lot of padding.
55-
dummy: byte[320]
56-
57-
const INVALID_HANDLE_VALUE: int64 = -1
58-
59-
declare FindFirstFileA(FileName: byte*, FindFileData: WIN32_FIND_DATAA*) -> int64
60-
declare FindNextFileA(hFindFile: int64, FindFileData: WIN32_FIND_DATAA*) -> int
61-
declare FindClose(hFindFile: int64) -> int
62-
63-
else:
64-
class DIR:
65-
pass
66-
class dirent:
67-
pass
68-
69-
# I ran the following C program to find the offset of d_name in the dirent struct:
70-
#
71-
# #include <dirent.h>
72-
# #include <stddef.h>
73-
# #include <stdio.h>
74-
# int main()
75-
# {
76-
# printf("%d\n", (int) offsetof(struct dirent, d_name));
77-
# return 0;
78-
# }
79-
if NETBSD:
80-
const DIRENT_NAME_OFFSET: int = 13
81-
else:
82-
# Assume linux
83-
const DIRENT_NAME_OFFSET: int = 19
84-
85-
if NETBSD:
86-
# On NetBSD, "opendir" and "readdir" are legacy functions.
87-
# We can't use them because they generate a linker warning.
88-
# The dirent.h header magically renames them at compile time to the following names.
89-
declare __opendir30(name: byte*) -> DIR*
90-
declare __readdir30(dirp: DIR*) -> dirent*
91-
def opendir(name: byte*) -> DIR*:
92-
return __opendir30(name)
93-
def readdir(dirp: DIR*) -> dirent*:
94-
return __readdir30(dirp)
95-
else:
96-
declare opendir(name: byte*) -> DIR*
97-
declare readdir(dirp: DIR*) -> dirent*
98-
99-
declare closedir(dirp: DIR*) -> int
100-
101-
102-
# The listdir() function returns a list of names (not full paths) of everything
103-
# in a given directory, just like os.listdir() in Python. Long names are ignored.
104-
#
105-
# TODO: put this to stdlib instead of here?
106-
def listdir(path: byte*) -> List[byte[100]]:
107-
result = List[byte[100]]{}
108-
109-
if WINDOWS:
110-
find_spec: byte*
111-
asprintf(&find_spec, "%s\\*", path) # Find anything
112-
113-
find_data: WIN32_FIND_DATAA
114-
handle = FindFirstFileA(path, &find_data)
115-
116-
# Seems like there's no easy way to distinguish empty directory and error.
117-
if handle != INVALID_HANDLE_VALUE:
118-
found_something = True
119-
while found_something:
120-
cFileName = &(&find_data as byte*)[44] # cFileName field is at offset 44
121-
short_name: byte[100] = ""
122-
if strlen(cFileName) < sizeof(short_name):
123-
strcpy(short_name, cFileName)
124-
result.append(short_name)
125-
found_something = (FindNextFileA(handle, &find_data) != 0)
126-
FindClose(handle)
127-
128-
else:
129-
dir = opendir(path)
130-
131-
if dir == NULL:
132-
fprintf(get_stderr(), "error: cannot list directory '%s'\n", path)
133-
exit(1)
134-
135-
while True:
136-
entry = readdir(dir)
137-
if entry == NULL:
138-
break
139-
140-
d_name = &(entry as byte*)[DIRENT_NAME_OFFSET]
141-
short_name: byte[100] = ""
142-
if strlen(d_name) < sizeof(short_name):
143-
strcpy(short_name, d_name)
144-
result.append(short_name)
145-
146-
closedir(dir)
147-
148-
return result
149-
15051

15152
def file_exists(path: byte*) -> bool:
15253
f = fopen(path, "r")
@@ -342,24 +243,15 @@ def get_path_to_jou_compiled_subfolder() -> byte*:
342243

343244
# Deletes files in a directory whose name ends with the given suffix.
344245
def delete_by_suffix(dir: byte*, suffix: byte*) -> None:
345-
list = listdir(dir)
346-
347-
for p = list.ptr; p < list.end(); p++:
348-
if ends_with(*p, suffix):
349-
full_path: byte*
350-
asprintf(&full_path, "%s/%s", dir, *p)
351-
246+
iter = DirIter{dir = dir}
247+
while iter.next():
248+
if ends_with(iter.name, suffix):
352249
if WINDOWS:
353-
ret = _unlink(full_path)
250+
ret = _unlink(iter.path)
354251
else:
355-
ret = unlink(full_path)
356-
252+
ret = unlink(iter.path)
357253
if ret == 0 and global_compiler_state.args.verbosity >= 1:
358-
printf("Deleted %s\n", full_path)
359-
360-
free(full_path)
361-
362-
free(list.ptr)
254+
printf("Deleted %s\n", iter.path)
363255

364256

365257
def get_simple_name(jou_file_path: byte*) -> byte[50]:

0 commit comments

Comments
 (0)