Browse Source

os2: fix lstat logic

Laytan Laats 1 year ago
parent
commit
7474db6a34
2 changed files with 25 additions and 12 deletions
  1. 1 2
      core/os/os2/path_posix.odin
  2. 24 10
      core/os/os2/stat_posix.odin

+ 1 - 2
core/os/os2/path_posix.odin

@@ -96,8 +96,7 @@ _remove_all :: proc(path: string) -> Error {
 }
 }
 
 
 _get_working_directory :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
 _get_working_directory :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) {
-	assert(!is_temp(allocator))
-	TEMP_ALLOCATOR_GUARD()
+	TEMP_ALLOCATOR_GUARD(ignore=is_temp(allocator))
 
 
 	buf: [dynamic]byte
 	buf: [dynamic]byte
 	buf.allocator = temp_allocator()
 	buf.allocator = temp_allocator()

+ 24 - 10
core/os/os2/stat_posix.odin

@@ -97,24 +97,38 @@ _lstat :: proc(name: string, allocator: runtime.Allocator) -> (fi: File_Info, er
 		return
 		return
 	}
 	}
 
 
-	assert(!is_temp(allocator))
-	TEMP_ALLOCATOR_GUARD()
-	cname := temp_cstring(name) or_return
+	TEMP_ALLOCATOR_GUARD(ignore=is_temp(allocator))
 
 
-	rcname := posix.realpath(cname)
-	if rcname == nil {
-		err = .Invalid_Path
-		return
+	// NOTE: can't use realpath here because it tries to resolve symlinks.
+
+	// NOTE: This might not be correct when given "/symlink/foo.txt",
+	// you would want that to resolve "/symlink", but not resolve "foo.txt".
+
+	fullpath := filepath.clean(name, temp_allocator())
+	assert(len(fullpath) > 0)
+	switch {
+	case fullpath[0] == '/':
+		// nothing.
+	case fullpath == ".":
+		fullpath = getwd(temp_allocator()) or_return
+	case len(fullpath) > 1 && fullpath[0] == '.' && fullpath[1] == '/':
+		fullpath = fullpath[2:]
+		fallthrough
+	case:
+		fullpath = concatenate({
+			getwd(temp_allocator()) or_return,
+			"/",
+			fullpath,
+		}, temp_allocator()) or_return
 	}
 	}
-	defer posix.free(rcname)
 
 
 	stat: posix.stat_t
 	stat: posix.stat_t
-	if posix.lstat(rcname, &stat) != .OK {
+	if posix.lstat(temp_cstring(fullpath), &stat) != .OK {
 		err = _get_platform_error()
 		err = _get_platform_error()
 		return
 		return
 	}
 	}
 
 
-	fullpath := clone_string(string(rcname), allocator) or_return
+	fullpath = clone_string(fullpath, allocator) or_return
 	return internal_stat(stat, fullpath), nil
 	return internal_stat(stat, fullpath), nil
 }
 }