authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-21 16:07:28-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-21 16:07:28-04:00
log51852d2587b931767a12d42ce39d5c191eea10ea
treefd875c1aa365a8264510eb4c43f874c438a6036d
parentbda5539e9d8b5f15b8165393e4118c8601188276

fix windows


20 files changed, 305 insertions(+), 117 deletions(-)

src-self-hosted/compilation.zig+1
......@@ -302,6 +302,7 @@ pub const Compilation = struct {
302302 UnsupportedLinkArchitecture,
303303 UserResourceLimitReached,
304304 InvalidUtf8,
305 BadPathName,
305306 };
306307
307308 pub const Event = union(enum) {
src-self-hosted/errmsg.zig+1-1
......@@ -235,7 +235,7 @@ pub const Msg = struct {
235235 const allocator = msg.getAllocator();
236236 const tree = msg.getTree();
237237
238 const cwd = try os.getCwd(allocator);
238 const cwd = try os.getCwdAlloc(allocator);
239239 defer allocator.free(cwd);
240240
241241 const relpath = try os.path.relative(allocator, cwd, msg.realpath);
src-self-hosted/introspect.zig+1-1
......@@ -14,7 +14,7 @@ pub fn testZigInstallPrefix(allocator: *mem.Allocator, test_path: []const u8) ![
1414 const test_index_file = try os.path.join(allocator, test_zig_dir, "std", "index.zig");
1515 defer allocator.free(test_index_file);
1616
17 var file = try os.File.openRead(allocator, test_index_file);
17 var file = try os.File.openRead(test_index_file);
1818 file.close();
1919
2020 return test_zig_dir;
src-self-hosted/libc_installation.zig+7-8
......@@ -233,7 +233,7 @@ pub const LibCInstallation = struct {
233233 const stdlib_path = try std.os.path.join(loop.allocator, search_path, "stdlib.h");
234234 defer loop.allocator.free(stdlib_path);
235235
236 if (try fileExists(loop.allocator, stdlib_path)) {
236 if (try fileExists(stdlib_path)) {
237237 self.include_dir = try std.mem.dupe(loop.allocator, u8, search_path);
238238 return;
239239 }
......@@ -257,7 +257,7 @@ pub const LibCInstallation = struct {
257257 const stdlib_path = try std.os.path.join(loop.allocator, result_buf.toSliceConst(), "stdlib.h");
258258 defer loop.allocator.free(stdlib_path);
259259
260 if (try fileExists(loop.allocator, stdlib_path)) {
260 if (try fileExists(stdlib_path)) {
261261 self.include_dir = result_buf.toOwnedSlice();
262262 return;
263263 }
......@@ -285,7 +285,7 @@ pub const LibCInstallation = struct {
285285 }
286286 const ucrt_lib_path = try std.os.path.join(loop.allocator, result_buf.toSliceConst(), "ucrt.lib");
287287 defer loop.allocator.free(ucrt_lib_path);
288 if (try fileExists(loop.allocator, ucrt_lib_path)) {
288 if (try fileExists(ucrt_lib_path)) {
289289 self.lib_dir = result_buf.toOwnedSlice();
290290 return;
291291 }
......@@ -360,7 +360,7 @@ pub const LibCInstallation = struct {
360360 }
361361 const kernel32_path = try std.os.path.join(loop.allocator, result_buf.toSliceConst(), "kernel32.lib");
362362 defer loop.allocator.free(kernel32_path);
363 if (try fileExists(loop.allocator, kernel32_path)) {
363 if (try fileExists(kernel32_path)) {
364364 self.kernel32_lib_dir = result_buf.toOwnedSlice();
365365 return;
366366 }
......@@ -449,12 +449,11 @@ fn fillSearch(search_buf: *[2]Search, sdk: *c.ZigWindowsSDK) []Search {
449449 return search_buf[0..search_end];
450450}
451451
452fn fileExists(allocator: *std.mem.Allocator, path: []const u8) !bool {
453 if (std.os.File.access(allocator, path)) |_| {
452fn fileExists(path: []const u8) !bool {
453 if (std.os.File.access(path)) |_| {
454454 return true;
455455 } else |err| switch (err) {
456 error.NotFound, error.PermissionDenied => return false,
457 error.OutOfMemory => return error.OutOfMemory,
456 error.FileNotFound, error.PathNotFound, error.PermissionDenied => return false,
458457 else => return error.FileSystem,
459458 }
460459}
src-self-hosted/test.zig+2-2
......@@ -94,7 +94,7 @@ pub const TestContext = struct {
9494 }
9595
9696 // TODO async I/O
97 try std.io.writeFile(allocator, file1_path, source);
97 try std.io.writeFile(file1_path, source);
9898
9999 var comp = try Compilation.create(
100100 &self.zig_compiler,
......@@ -128,7 +128,7 @@ pub const TestContext = struct {
128128 }
129129
130130 // TODO async I/O
131 try std.io.writeFile(allocator, file1_path, source);
131 try std.io.writeFile(file1_path, source);
132132
133133 var comp = try Compilation.create(
134134 &self.zig_compiler,
std/build.zig+3-3
......@@ -267,7 +267,7 @@ pub const Builder = struct {
267267 if (self.verbose) {
268268 warn("rm {}\n", installed_file);
269269 }
270 _ = os.deleteFile(self.allocator, installed_file);
270 _ = os.deleteFile(installed_file);
271271 }
272272
273273 // TODO remove empty directories
......@@ -1182,7 +1182,7 @@ pub const LibExeObjStep = struct {
11821182
11831183 if (self.build_options_contents.len() > 0) {
11841184 const build_options_file = try os.path.join(builder.allocator, builder.cache_root, builder.fmt("{}_build_options.zig", self.name));
1185 try std.io.writeFile(builder.allocator, build_options_file, self.build_options_contents.toSliceConst());
1185 try std.io.writeFile(build_options_file, self.build_options_contents.toSliceConst());
11861186 try zig_args.append("--pkg-begin");
11871187 try zig_args.append("build_options");
11881188 try zig_args.append(builder.pathFromRoot(build_options_file));
......@@ -1917,7 +1917,7 @@ pub const WriteFileStep = struct {
19171917 warn("unable to make path {}: {}\n", full_path_dir, @errorName(err));
19181918 return err;
19191919 };
1920 io.writeFile(self.builder.allocator, full_path, self.data) catch |err| {
1920 io.writeFile(full_path, self.data) catch |err| {
19211921 warn("unable to write {}: {}\n", full_path, @errorName(err));
19221922 return err;
19231923 };
std/cstr.zig+6-5
......@@ -9,10 +9,9 @@ pub const line_sep = switch (builtin.os) {
99 else => "\n",
1010};
1111
12/// Deprecated, use mem.len
1213pub fn len(ptr: [*]const u8) usize {
13 var count: usize = 0;
14 while (ptr[count] != 0) : (count += 1) {}
15 return count;
14 return mem.len(u8, ptr);
1615}
1716
1817pub fn cmp(a: [*]const u8, b: [*]const u8) i8 {
......@@ -27,12 +26,14 @@ pub fn cmp(a: [*]const u8, b: [*]const u8) i8 {
2726 }
2827}
2928
29/// Deprecated, use mem.toSliceConst
3030pub fn toSliceConst(str: [*]const u8) []const u8 {
31 return str[0..len(str)];
31 return mem.toSliceConst(u8, str);
3232}
3333
34/// Deprecated, use mem.toSlice
3435pub fn toSlice(str: [*]u8) []u8 {
35 return str[0..len(str)];
36 return mem.toSlice(u8, str);
3637}
3738
3839test "cstr fns" {
std/event/fs.zig-4
......@@ -382,7 +382,6 @@ pub async fn openRead(loop: *Loop, path: []const u8) os.File.OpenError!os.FileHa
382382 },
383383
384384 builtin.Os.windows => return os.windowsOpen(
385 loop.allocator,
386385 path,
387386 windows.GENERIC_READ,
388387 windows.FILE_SHARE_READ,
......@@ -411,7 +410,6 @@ pub async fn openWriteMode(loop: *Loop, path: []const u8, mode: os.File.Mode) os
411410 },
412411 builtin.Os.windows,
413412 => return os.windowsOpen(
414 loop.allocator,
415413 path,
416414 windows.GENERIC_WRITE,
417415 windows.FILE_SHARE_WRITE | windows.FILE_SHARE_READ | windows.FILE_SHARE_DELETE,
......@@ -435,7 +433,6 @@ pub async fn openReadWrite(
435433 },
436434
437435 builtin.Os.windows => return os.windowsOpen(
438 loop.allocator,
439436 path,
440437 windows.GENERIC_WRITE|windows.GENERIC_READ,
441438 windows.FILE_SHARE_WRITE | windows.FILE_SHARE_READ | windows.FILE_SHARE_DELETE,
......@@ -593,7 +590,6 @@ pub async fn writeFileMode(loop: *Loop, path: []const u8, contents: []const u8,
593590
594591async fn writeFileWindows(loop: *Loop, path: []const u8, contents: []const u8) !void {
595592 const handle = try os.windowsOpen(
596 loop.allocator,
597593 path,
598594 windows.GENERIC_WRITE,
599595 windows.FILE_SHARE_WRITE | windows.FILE_SHARE_READ | windows.FILE_SHARE_DELETE,
std/io.zig+3-4
......@@ -254,9 +254,8 @@ pub fn OutStream(comptime WriteError: type) type {
254254 };
255255}
256256
257/// `path` needs to be copied in memory to add a null terminating byte, hence the allocator.
258pub fn writeFile(allocator: *mem.Allocator, path: []const u8, data: []const u8) !void {
259 var file = try File.openWrite(allocator, path);
257pub fn writeFile(path: []const u8, data: []const u8) !void {
258 var file = try File.openWrite(path);
260259 defer file.close();
261260 try file.write(data);
262261}
......@@ -268,7 +267,7 @@ pub fn readFileAlloc(allocator: *mem.Allocator, path: []const u8) ![]u8 {
268267
269268/// On success, caller owns returned buffer.
270269pub fn readFileAllocAligned(allocator: *mem.Allocator, path: []const u8, comptime A: u29) ![]align(A) u8 {
271 var file = try File.openRead(allocator, path);
270 var file = try File.openRead(path);
272271 defer file.close();
273272
274273 const size = try file.getEndPos();
std/io_test.zig+1-1
......@@ -45,7 +45,7 @@ test "write a file, read it, then delete it" {
4545 assert(mem.eql(u8, contents["begin".len .. contents.len - "end".len], data));
4646 assert(mem.eql(u8, contents[contents.len - "end".len ..], "end"));
4747 }
48 try os.deleteFile(allocator, tmp_file_name);
48 try os.deleteFile(tmp_file_name);
4949}
5050
5151test "BufferOutStream" {
std/mem.zig+17-2
......@@ -179,8 +179,8 @@ pub fn secureZero(comptime T: type, s: []T) void {
179179 // NOTE: We do not use a volatile slice cast here since LLVM cannot
180180 // see that it can be replaced by a memset.
181181 const ptr = @ptrCast([*]volatile u8, s.ptr);
182 const len = s.len * @sizeOf(T);
183 @memset(ptr, 0, len);
182 const length = s.len * @sizeOf(T);
183 @memset(ptr, 0, length);
184184}
185185
186186test "mem.secureZero" {
......@@ -252,6 +252,20 @@ pub fn eql(comptime T: type, a: []const T, b: []const T) bool {
252252 return true;
253253}
254254
255pub fn len(comptime T: type, ptr: [*]const T) usize {
256 var count: usize = 0;
257 while (ptr[count] != 0) : (count += 1) {}
258 return count;
259}
260
261pub fn toSliceConst(comptime T: type, ptr: [*]const T) []const T {
262 return ptr[0..len(T, ptr)];
263}
264
265pub fn toSlice(comptime T: type, ptr: [*]T) []T {
266 return ptr[0..len(T, ptr)];
267}
268
255269/// Returns true if all elements in a slice are equal to the scalar value provided
256270pub fn allEqual(comptime T: type, slice: []const T, scalar: T) bool {
257271 for (slice) |item| {
......@@ -809,3 +823,4 @@ pub fn endianSwap(comptime T: type, x: T) T {
809823test "std.mem.endianSwap" {
810824 assert(endianSwap(u32, 0xDEADBEEF) == 0xEFBEADDE);
811825}
826
std/os/child_process.zig+1-4
......@@ -453,10 +453,7 @@ pub const ChildProcess = struct {
453453 const any_ignore = (self.stdin_behavior == StdIo.Ignore or self.stdout_behavior == StdIo.Ignore or self.stderr_behavior == StdIo.Ignore);
454454
455455 const nul_handle = if (any_ignore) blk: {
456 const nul_file_path = "NUL";
457 var fixed_buffer_mem: [nul_file_path.len + 1]u8 = undefined;
458 var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);
459 break :blk try os.windowsOpen(&fixed_allocator.allocator, "NUL", windows.GENERIC_READ, windows.FILE_SHARE_READ, windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL);
456 break :blk try os.windowsOpen("NUL", windows.GENERIC_READ, windows.FILE_SHARE_READ, windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL);
460457 } else blk: {
461458 break :blk undefined;
462459 };
std/os/file.zig+30-22
......@@ -7,6 +7,7 @@ const assert = std.debug.assert;
77const posix = os.posix;
88const windows = os.windows;
99const Os = builtin.Os;
10const windows_util = @import("windows/util.zig");
1011
1112const is_posix = builtin.os != builtin.Os.windows;
1213const is_windows = builtin.os == builtin.Os.windows;
......@@ -102,21 +103,42 @@ pub const File = struct {
102103
103104 pub const AccessError = error{
104105 PermissionDenied,
105 NotFound,
106 PathNotFound,
107 FileNotFound,
106108 NameTooLong,
107109 BadMode,
108110 BadPathName,
109111 Io,
110112 SystemResources,
111 OutOfMemory,
113
114 /// On Windows, file paths must be valid Unicode.
115 InvalidUtf8,
112116
113117 Unexpected,
114118 };
115119
120 /// Call from Windows-specific code if you already have a UTF-16LE encoded, null terminated string.
121 /// Otherwise use `access` or `accessC`.
122 pub fn accessW(path: [*]const u16) AccessError!void {
123 if (os.windows.GetFileAttributesW(path) != os.windows.INVALID_FILE_ATTRIBUTES) {
124 return;
125 }
126
127 const err = windows.GetLastError();
128 switch (err) {
129 windows.ERROR.FILE_NOT_FOUND => return error.FileNotFound,
130 windows.ERROR.PATH_NOT_FOUND => return error.PathNotFound,
131 windows.ERROR.ACCESS_DENIED => return error.PermissionDenied,
132 else => return os.unexpectedErrorWindows(err),
133 }
134 }
135
136 /// Call if you have a UTF-8 encoded, null-terminated string.
137 /// Otherwise use `access` or `accessW`.
116138 pub fn accessC(path: [*]const u8) AccessError!void {
117139 if (is_windows) {
118 // this needs to convert to UTF-16LE and call accessW
119 @compileError("TODO support windows");
140 const path_w = try windows_util.cStrToPrefixedFileW(path);
141 return accessW(&path_w);
120142 }
121143 if (is_posix) {
122144 const result = posix.access(path, posix.F_OK);
......@@ -137,28 +159,14 @@ pub const File = struct {
137159 posix.ENOMEM => return error.SystemResources,
138160 else => return os.unexpectedErrorPosix(err),
139161 }
140 } else if (is_windows) {
141 if (os.windows.GetFileAttributesA(path) != os.windows.INVALID_FILE_ATTRIBUTES) {
142 return;
143 }
144
145 const err = windows.GetLastError();
146 switch (err) {
147 windows.ERROR.FILE_NOT_FOUND,
148 windows.ERROR.PATH_NOT_FOUND,
149 => return error.NotFound,
150 windows.ERROR.ACCESS_DENIED => return error.PermissionDenied,
151 else => return os.unexpectedErrorWindows(err),
152 }
153 } else {
154 @compileError("TODO implement access for this OS");
155162 }
163 @compileError("Unsupported OS");
156164 }
157165
158166 pub fn access(path: []const u8) AccessError!void {
159167 if (is_windows) {
160 // this needs to convert to UTF-16LE and call accessW
161 @compileError("TODO support windows");
168 const path_w = try windows_util.sliceToPrefixedFileW(path);
169 return accessW(&path_w);
162170 }
163171 if (is_posix) {
164172 var path_with_null: [posix.PATH_MAX]u8 = undefined;
......@@ -167,7 +175,7 @@ pub const File = struct {
167175 path_with_null[path.len] = 0;
168176 return accessC(&path_with_null);
169177 }
170 @compileError("TODO implement access for this OS");
178 @compileError("Unsupported OS");
171179 }
172180
173181 /// Upon success, the stream is in an uninitialized state. To continue using it,
std/os/get_app_data_dir.zig+2-1
......@@ -10,6 +10,7 @@ pub const GetAppDataDirError = error{
1010};
1111
1212/// Caller owns returned memory.
13/// TODO determine if we can remove the allocator requirement
1314pub fn getAppDataDir(allocator: *mem.Allocator, appname: []const u8) GetAppDataDirError![]u8 {
1415 switch (builtin.os) {
1516 builtin.Os.windows => {
......@@ -22,7 +23,7 @@ pub fn getAppDataDir(allocator: *mem.Allocator, appname: []const u8) GetAppDataD
2223 )) {
2324 os.windows.S_OK => {
2425 defer os.windows.CoTaskMemFree(@ptrCast(*c_void, dir_path_ptr));
25 const global_dir = unicode.utf16leToUtf8(allocator, utf16lePtrSlice(dir_path_ptr)) catch |err| switch (err) {
26 const global_dir = unicode.utf16leToUtf8Alloc(allocator, utf16lePtrSlice(dir_path_ptr)) catch |err| switch (err) {
2627 error.UnexpectedSecondSurrogateHalf => return error.AppDataDirUnavailable,
2728 error.ExpectedSecondSurrogateHalf => return error.AppDataDirUnavailable,
2829 error.DanglingSurrogateHalf => return error.AppDataDirUnavailable,
std/os/index.zig+91-20
......@@ -45,7 +45,7 @@ pub const MAX_PATH_BYTES = switch (builtin.os) {
4545 // If it would require 4 UTF-8 bytes, then there would be a surrogate
4646 // pair in the UTF-16LE, and we (over)account 3 bytes for it that way.
4747 // +1 for the null byte at the end, which can be encoded in 1 byte.
48 Os.windows => 32767 * 3 + 1,
48 Os.windows => windows_util.PATH_MAX_WIDE * 3 + 1,
4949 else => @compileError("Unsupported OS"),
5050};
5151
......@@ -326,6 +326,8 @@ pub const PosixWriteError = error{
326326 NoSpaceLeft,
327327 AccessDenied,
328328 BrokenPipe,
329
330 /// See https://github.com/ziglang/zig/issues/1396
329331 Unexpected,
330332};
331333
......@@ -439,6 +441,8 @@ pub const PosixOpenError = error{
439441 NoSpaceLeft,
440442 NotDir,
441443 PathAlreadyExists,
444
445 /// See https://github.com/ziglang/zig/issues/1396
442446 Unexpected,
443447};
444448
......@@ -600,6 +604,8 @@ pub const PosixExecveError = error{
600604 FileNotFound,
601605 NotDir,
602606 FileBusy,
607
608 /// See https://github.com/ziglang/zig/issues/1396
603609 Unexpected,
604610};
605611
......@@ -736,20 +742,25 @@ pub fn getCwdAlloc(allocator: *Allocator) ![]u8 {
736742pub const GetCwdError = error{Unexpected};
737743
738744/// The result is a slice of out_buffer.
745/// TODO with well defined copy elision we could make the API of this function better.
739746pub fn getCwd(out_buffer: *[MAX_PATH_BYTES]u8) GetCwdError![]u8 {
740747 switch (builtin.os) {
741748 Os.windows => {
742 var utf16le_buf: [windows_util.PATH_MAX_UTF16]u16 = undefined;
743 const result = windows.GetCurrentDirectoryW(utf16le_buf.len, &utf16le_buf);
749 var utf16le_buf: [windows_util.PATH_MAX_WIDE]u16 = undefined;
750 const casted_len = @intCast(windows.DWORD, utf16le_buf.len); // TODO shouldn't need this cast
751 const casted_ptr = ([*]u16)(&utf16le_buf); // TODO shouldn't need this cast
752 const result = windows.GetCurrentDirectoryW(casted_len, casted_ptr);
744753 if (result == 0) {
745754 const err = windows.GetLastError();
746755 switch (err) {
747756 else => return unexpectedErrorWindows(err),
748757 }
749758 }
750 assert(result <= buf.len);
759 assert(result <= utf16le_buf.len);
751760 const utf16le_slice = utf16le_buf[0..result];
752 return std.unicode.utf16leToUtf8(out_buffer, utf16le_buf);
761 // Trust that Windows gives us valid UTF-16LE.
762 const end_index = std.unicode.utf16leToUtf8(out_buffer, utf16le_slice) catch unreachable;
763 return out_buffer[0..end_index];
753764 },
754765 else => {
755766 const err = posix.getErrno(posix.getcwd(out_buffer, out_buffer.len));
......@@ -764,7 +775,9 @@ pub fn getCwd(out_buffer: *[MAX_PATH_BYTES]u8) GetCwdError![]u8 {
764775
765776test "os.getCwd" {
766777 // at least call it so it gets compiled
767 _ = getCwd(debug.global_allocator);
778 _ = getCwdAlloc(debug.global_allocator);
779 var buf: [MAX_PATH_BYTES]u8 = undefined;
780 _ = getCwd(&buf);
768781}
769782
770783pub const SymLinkError = PosixSymLinkError || WindowsSymLinkError;
......@@ -779,6 +792,8 @@ pub fn symLink(allocator: *Allocator, existing_path: []const u8, new_path: []con
779792
780793pub const WindowsSymLinkError = error{
781794 OutOfMemory,
795
796 /// See https://github.com/ziglang/zig/issues/1396
782797 Unexpected,
783798};
784799
......@@ -809,6 +824,8 @@ pub const PosixSymLinkError = error{
809824 NoSpaceLeft,
810825 ReadOnlyFileSystem,
811826 NotDir,
827
828 /// See https://github.com/ziglang/zig/issues/1396
812829 Unexpected,
813830};
814831
......@@ -867,7 +884,7 @@ pub fn atomicSymLink(allocator: *Allocator, existing_path: []const u8, new_path:
867884 b64_fs_encoder.encode(tmp_path[dirname.len + 1 ..], rand_buf);
868885
869886 if (symLink(allocator, existing_path, tmp_path)) {
870 return rename(allocator, tmp_path, new_path);
887 return rename(tmp_path, new_path);
871888 } else |err| switch (err) {
872889 error.PathAlreadyExists => continue,
873890 else => return err, // TODO zig should know this set does not include PathAlreadyExists
......@@ -886,8 +903,15 @@ pub const DeleteFileError = error{
886903 NotDir,
887904 SystemResources,
888905 ReadOnlyFileSystem,
889 OutOfMemory,
890906
907 /// On Windows, file paths must be valid Unicode.
908 InvalidUtf8,
909
910 /// On Windows, file paths cannot contain these characters:
911 /// '/', '*', '?', '"', '<', '>', '|'
912 BadPathName,
913
914 /// See https://github.com/ziglang/zig/issues/1396
891915 Unexpected,
892916};
893917
......@@ -900,7 +924,18 @@ pub fn deleteFile(file_path: []const u8) DeleteFileError!void {
900924}
901925
902926pub fn deleteFileWindows(file_path: []const u8) !void {
903 @compileError("TODO rewrite with DeleteFileW and no allocator");
927 const file_path_w = try windows_util.sliceToPrefixedFileW(file_path);
928
929 if (windows.DeleteFileW(&file_path_w) == 0) {
930 const err = windows.GetLastError();
931 switch (err) {
932 windows.ERROR.FILE_NOT_FOUND => return error.FileNotFound,
933 windows.ERROR.ACCESS_DENIED => return error.AccessDenied,
934 windows.ERROR.FILENAME_EXCED_RANGE => return error.NameTooLong,
935 windows.ERROR.INVALID_PARAMETER => return error.NameTooLong,
936 else => return unexpectedErrorWindows(err),
937 }
938 }
904939}
905940
906941pub fn deleteFilePosixC(file_path: [*]const u8) !void {
......@@ -1028,7 +1063,7 @@ pub const AtomicFile = struct {
10281063 pub fn deinit(self: *AtomicFile) void {
10291064 if (!self.finished) {
10301065 self.file.close();
1031 deleteFile(self.allocator, self.tmp_path) catch {};
1066 deleteFile(self.tmp_path) catch {};
10321067 self.allocator.free(self.tmp_path);
10331068 self.finished = true;
10341069 }
......@@ -1037,7 +1072,7 @@ pub const AtomicFile = struct {
10371072 pub fn finish(self: *AtomicFile) !void {
10381073 assert(!self.finished);
10391074 self.file.close();
1040 try rename(self.allocator, self.tmp_path, self.dest_path);
1075 try rename(self.tmp_path, self.dest_path);
10411076 self.allocator.free(self.tmp_path);
10421077 self.finished = true;
10431078 }
......@@ -1075,7 +1110,15 @@ pub fn renameC(old_path: [*]const u8, new_path: [*]const u8) !void {
10751110
10761111pub fn rename(old_path: []const u8, new_path: []const u8) !void {
10771112 if (is_windows) {
1078 @compileError("TODO rewrite with MoveFileExW and no allocator");
1113 const flags = windows.MOVEFILE_REPLACE_EXISTING | windows.MOVEFILE_WRITE_THROUGH;
1114 const old_path_w = try windows_util.sliceToPrefixedFileW(old_path);
1115 const new_path_w = try windows_util.sliceToPrefixedFileW(new_path);
1116 if (windows.MoveFileExW(&old_path_w, &new_path_w, flags) == 0) {
1117 const err = windows.GetLastError();
1118 switch (err) {
1119 else => return unexpectedErrorWindows(err),
1120 }
1121 }
10791122 } else {
10801123 var old_path_with_null: [posix.PATH_MAX]u8 = undefined;
10811124 if (old_path.len >= posix.PATH_MAX) return error.NameTooLong;
......@@ -1099,11 +1142,10 @@ pub fn makeDir(dir_path: []const u8) !void {
10991142 }
11001143}
11011144
1102pub fn makeDirWindows(allocator: *Allocator, dir_path: []const u8) !void {
1103 const path_buf = try cstr.addNullByte(allocator, dir_path);
1104 defer allocator.free(path_buf);
1145pub fn makeDirWindows(dir_path: []const u8) !void {
1146 const dir_path_w = try windows_util.sliceToPrefixedFileW(dir_path);
11051147
1106 if (windows.CreateDirectoryA(path_buf.ptr, null) == 0) {
1148 if (windows.CreateDirectoryW(&dir_path_w, null) == 0) {
11071149 const err = windows.GetLastError();
11081150 return switch (err) {
11091151 windows.ERROR.ALREADY_EXISTS => error.PathAlreadyExists,
......@@ -1144,13 +1186,14 @@ pub fn makeDirPosix(dir_path: []const u8) !void {
11441186
11451187/// Calls makeDir recursively to make an entire path. Returns success if the path
11461188/// already exists and is a directory.
1189/// TODO determine if we can remove the allocator requirement from this function
11471190pub fn makePath(allocator: *Allocator, full_path: []const u8) !void {
11481191 const resolved_path = try path.resolve(allocator, full_path);
11491192 defer allocator.free(resolved_path);
11501193
11511194 var end_index: usize = resolved_path.len;
11521195 while (true) {
1153 makeDir(allocator, resolved_path[0..end_index]) catch |err| switch (err) {
1196 makeDir(resolved_path[0..end_index]) catch |err| switch (err) {
11541197 error.PathAlreadyExists => {
11551198 // TODO stat the file and return an error if it's not a directory
11561199 // this is important because otherwise a dangling symlink
......@@ -1188,6 +1231,7 @@ pub const DeleteDirError = error{
11881231 ReadOnlyFileSystem,
11891232 OutOfMemory,
11901233
1234 /// See https://github.com/ziglang/zig/issues/1396
11911235 Unexpected,
11921236};
11931237
......@@ -1256,20 +1300,30 @@ const DeleteTreeError = error{
12561300 FileSystem,
12571301 FileBusy,
12581302 DirNotEmpty,
1303
1304 /// On Windows, file paths must be valid Unicode.
1305 InvalidUtf8,
1306
1307 /// On Windows, file paths cannot contain these characters:
1308 /// '/', '*', '?', '"', '<', '>', '|'
1309 BadPathName,
1310
1311 /// See https://github.com/ziglang/zig/issues/1396
12591312 Unexpected,
12601313};
1314
1315/// TODO determine if we can remove the allocator requirement
12611316pub fn deleteTree(allocator: *Allocator, full_path: []const u8) DeleteTreeError!void {
12621317 start_over: while (true) {
12631318 var got_access_denied = false;
12641319 // First, try deleting the item as a file. This way we don't follow sym links.
1265 if (deleteFile(allocator, full_path)) {
1320 if (deleteFile(full_path)) {
12661321 return;
12671322 } else |err| switch (err) {
12681323 error.FileNotFound => return,
12691324 error.IsDir => {},
12701325 error.AccessDenied => got_access_denied = true,
12711326
1272 error.OutOfMemory,
12731327 error.SymLinkLoop,
12741328 error.NameTooLong,
12751329 error.SystemResources,
......@@ -1277,6 +1331,8 @@ pub fn deleteTree(allocator: *Allocator, full_path: []const u8) DeleteTreeError!
12771331 error.NotDir,
12781332 error.FileSystem,
12791333 error.FileBusy,
1334 error.InvalidUtf8,
1335 error.BadPathName,
12801336 error.Unexpected,
12811337 => return err,
12821338 }
......@@ -1383,6 +1439,7 @@ pub const Dir = struct {
13831439 PathAlreadyExists,
13841440 OutOfMemory,
13851441
1442 /// See https://github.com/ziglang/zig/issues/1396
13861443 Unexpected,
13871444 };
13881445
......@@ -1685,6 +1742,8 @@ pub fn posix_setregid(rgid: u32, egid: u32) !void {
16851742
16861743pub const WindowsGetStdHandleErrs = error{
16871744 NoStdHandles,
1745
1746 /// See https://github.com/ziglang/zig/issues/1396
16881747 Unexpected,
16891748};
16901749
......@@ -2012,7 +2071,7 @@ pub fn unexpectedErrorPosix(errno: usize) UnexpectedError {
20122071/// Call this when you made a windows DLL call or something that does SetLastError
20132072/// and you get an unexpected error.
20142073pub fn unexpectedErrorWindows(err: windows.DWORD) UnexpectedError {
2015 if (unexpected_error_tracing) {
2074 if (true) {
20162075 debug.warn("unexpected GetLastError(): {}\n", err);
20172076 debug.dumpCurrentStackTrace(null);
20182077 }
......@@ -2215,6 +2274,7 @@ pub const PosixBindError = error{
22152274 /// The socket inode would reside on a read-only filesystem.
22162275 ReadOnlyFileSystem,
22172276
2277 /// See https://github.com/ziglang/zig/issues/1396
22182278 Unexpected,
22192279};
22202280
......@@ -2258,6 +2318,7 @@ const PosixListenError = error{
22582318 /// The socket is not of a type that supports the listen() operation.
22592319 OperationNotSupported,
22602320
2321 /// See https://github.com/ziglang/zig/issues/1396
22612322 Unexpected,
22622323};
22632324
......@@ -2311,6 +2372,7 @@ pub const PosixAcceptError = error{
23112372 /// Firewall rules forbid connection.
23122373 BlockedByFirewall,
23132374
2375 /// See https://github.com/ziglang/zig/issues/1396
23142376 Unexpected,
23152377};
23162378
......@@ -2356,6 +2418,7 @@ pub const LinuxEpollCreateError = error{
23562418 /// There was insufficient memory to create the kernel object.
23572419 SystemResources,
23582420
2421 /// See https://github.com/ziglang/zig/issues/1396
23592422 Unexpected,
23602423};
23612424
......@@ -2410,6 +2473,7 @@ pub const LinuxEpollCtlError = error{
24102473 /// for example, a regular file or a directory.
24112474 FileDescriptorIncompatibleWithEpoll,
24122475
2476 /// See https://github.com/ziglang/zig/issues/1396
24132477 Unexpected,
24142478};
24152479
......@@ -2452,6 +2516,7 @@ pub const LinuxEventFdError = error{
24522516 ProcessFdQuotaExceeded,
24532517 SystemFdQuotaExceeded,
24542518
2519 /// See https://github.com/ziglang/zig/issues/1396
24552520 Unexpected,
24562521};
24572522
......@@ -2474,6 +2539,7 @@ pub const PosixGetSockNameError = error{
24742539 /// Insufficient resources were available in the system to perform the operation.
24752540 SystemResources,
24762541
2542 /// See https://github.com/ziglang/zig/issues/1396
24772543 Unexpected,
24782544};
24792545
......@@ -2527,6 +2593,7 @@ pub const PosixConnectError = error{
25272593 /// that for IP sockets the timeout may be very long when syncookies are enabled on the server.
25282594 ConnectionTimedOut,
25292595
2596 /// See https://github.com/ziglang/zig/issues/1396
25302597 Unexpected,
25312598};
25322599
......@@ -2748,6 +2815,7 @@ pub const SpawnThreadError = error{
27482815 /// Not enough userland memory to spawn the thread.
27492816 OutOfMemory,
27502817
2818 /// See https://github.com/ziglang/zig/issues/1396
27512819 Unexpected,
27522820};
27532821
......@@ -2935,6 +3003,8 @@ pub fn posixFStat(fd: i32) !posix.Stat {
29353003pub const CpuCountError = error{
29363004 OutOfMemory,
29373005 PermissionDenied,
3006
3007 /// See https://github.com/ziglang/zig/issues/1396
29383008 Unexpected,
29393009};
29403010
......@@ -3005,6 +3075,7 @@ pub const BsdKQueueError = error{
30053075 /// The system-wide limit on the total number of open files has been reached.
30063076 SystemFdQuotaExceeded,
30073077
3078 /// See https://github.com/ziglang/zig/issues/1396
30083079 Unexpected,
30093080};
30103081
std/os/path.zig+9-7
......@@ -16,6 +16,8 @@ pub const sep_windows = '\\';
1616pub const sep_posix = '/';
1717pub const sep = if (is_windows) sep_windows else sep_posix;
1818
19pub const sep_str = [1]u8{sep};
20
1921pub const delimiter_windows = ';';
2022pub const delimiter_posix = ':';
2123pub const delimiter = if (is_windows) delimiter_windows else delimiter_posix;
......@@ -337,7 +339,7 @@ pub fn resolveSlice(allocator: *Allocator, paths: []const []const u8) ![]u8 {
337339pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 {
338340 if (paths.len == 0) {
339341 assert(is_windows); // resolveWindows called on non windows can't use getCwd
340 return os.getCwd(allocator);
342 return os.getCwdAlloc(allocator);
341343 }
342344
343345 // determine which disk designator we will result with, if any
......@@ -432,7 +434,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 {
432434 },
433435 WindowsPath.Kind.None => {
434436 assert(is_windows); // resolveWindows called on non windows can't use getCwd
435 const cwd = try os.getCwd(allocator);
437 const cwd = try os.getCwdAlloc(allocator);
436438 defer allocator.free(cwd);
437439 const parsed_cwd = windowsParsePath(cwd);
438440 result = try allocator.alloc(u8, max_size + parsed_cwd.disk_designator.len + 1);
......@@ -448,7 +450,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 {
448450 } else {
449451 assert(is_windows); // resolveWindows called on non windows can't use getCwd
450452 // TODO call get cwd for the result_disk_designator instead of the global one
451 const cwd = try os.getCwd(allocator);
453 const cwd = try os.getCwdAlloc(allocator);
452454 defer allocator.free(cwd);
453455
454456 result = try allocator.alloc(u8, max_size + cwd.len + 1);
......@@ -516,7 +518,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 {
516518pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 {
517519 if (paths.len == 0) {
518520 assert(!is_windows); // resolvePosix called on windows can't use getCwd
519 return os.getCwd(allocator);
521 return os.getCwdAlloc(allocator);
520522 }
521523
522524 var first_index: usize = 0;
......@@ -538,7 +540,7 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 {
538540 result = try allocator.alloc(u8, max_size);
539541 } else {
540542 assert(!is_windows); // resolvePosix called on windows can't use getCwd
541 const cwd = try os.getCwd(allocator);
543 const cwd = try os.getCwdAlloc(allocator);
542544 defer allocator.free(cwd);
543545 result = try allocator.alloc(u8, max_size + cwd.len + 1);
544546 mem.copy(u8, result, cwd);
......@@ -577,7 +579,7 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 {
577579}
578580
579581test "os.path.resolve" {
580 const cwd = try os.getCwd(debug.global_allocator);
582 const cwd = try os.getCwdAlloc(debug.global_allocator);
581583 if (is_windows) {
582584 if (windowsParsePath(cwd).kind == WindowsPath.Kind.Drive) {
583585 cwd[0] = asciiUpper(cwd[0]);
......@@ -591,7 +593,7 @@ test "os.path.resolve" {
591593
592594test "os.path.resolveWindows" {
593595 if (is_windows) {
594 const cwd = try os.getCwd(debug.global_allocator);
596 const cwd = try os.getCwdAlloc(debug.global_allocator);
595597 const parsed_cwd = windowsParsePath(cwd);
596598 {
597599 const result = testResolveWindows([][]const u8{ "/usr/local", "lib\\zig\\std\\array_list.zig" });
std/os/test.zig+7-7
......@@ -10,9 +10,9 @@ const AtomicRmwOp = builtin.AtomicRmwOp;
1010const AtomicOrder = builtin.AtomicOrder;
1111
1212test "makePath, put some files in it, deleteTree" {
13 try os.makePath(a, "os_test_tmp/b/c");
14 try io.writeFile(a, "os_test_tmp/b/c/file.txt", "nonsense");
15 try io.writeFile(a, "os_test_tmp/b/file2.txt", "blah");
13 try os.makePath(a, "os_test_tmp" ++ os.path.sep_str ++ "b" ++ os.path.sep_str ++ "c");
14 try io.writeFile("os_test_tmp" ++ os.path.sep_str ++ "b" ++ os.path.sep_str ++ "c" ++ os.path.sep_str ++ "file.txt", "nonsense");
15 try io.writeFile("os_test_tmp" ++ os.path.sep_str ++ "b" ++ os.path.sep_str ++ "file2.txt", "blah");
1616 try os.deleteTree(a, "os_test_tmp");
1717 if (os.Dir.open(a, "os_test_tmp")) |dir| {
1818 @panic("expected error");
......@@ -23,14 +23,14 @@ test "makePath, put some files in it, deleteTree" {
2323
2424test "access file" {
2525 try os.makePath(a, "os_test_tmp");
26 if (os.File.access(a, "os_test_tmp/file.txt")) |ok| {
26 if (os.File.access("os_test_tmp" ++ os.path.sep_str ++ "file.txt")) |ok| {
2727 @panic("expected error");
2828 } else |err| {
29 assert(err == error.NotFound);
29 assert(err == error.FileNotFound);
3030 }
3131
32 try io.writeFile(a, "os_test_tmp/file.txt", "");
33 try os.File.access(a, "os_test_tmp/file.txt");
32 try io.writeFile("os_test_tmp" ++ os.path.sep_str ++ "file.txt", "");
33 try os.File.access("os_test_tmp" ++ os.path.sep_str ++ "file.txt");
3434 try os.deleteTree(a, "os_test_tmp");
3535}
3636
std/os/windows/kernel32.zig+16-10
......@@ -4,10 +4,8 @@ pub extern "kernel32" stdcallcc fn CancelIoEx(hFile: HANDLE, lpOverlapped: LPOVE
44
55pub extern "kernel32" stdcallcc fn CloseHandle(hObject: HANDLE) BOOL;
66
7pub extern "kernel32" stdcallcc fn CreateDirectoryA(
8 lpPathName: LPCSTR,
9 lpSecurityAttributes: ?*SECURITY_ATTRIBUTES,
10) BOOL;
7pub extern "kernel32" stdcallcc fn CreateDirectoryA( lpPathName: [*]const u8, lpSecurityAttributes: ?*SECURITY_ATTRIBUTES) BOOL;
8pub extern "kernel32" stdcallcc fn CreateDirectoryW( lpPathName: [*]const u16, lpSecurityAttributes: ?*SECURITY_ATTRIBUTES) BOOL;
119
1210pub extern "kernel32" stdcallcc fn CreateFileA(
1311 lpFileName: [*]const u8, // TODO null terminated pointer type
......@@ -59,7 +57,8 @@ pub extern "kernel32" stdcallcc fn CreateIoCompletionPort(FileHandle: HANDLE, Ex
5957
6058pub extern "kernel32" stdcallcc fn CreateThread(lpThreadAttributes: ?LPSECURITY_ATTRIBUTES, dwStackSize: SIZE_T, lpStartAddress: LPTHREAD_START_ROUTINE, lpParameter: ?LPVOID, dwCreationFlags: DWORD, lpThreadId: ?LPDWORD) ?HANDLE;
6159
62pub extern "kernel32" stdcallcc fn DeleteFileA(lpFileName: LPCSTR) BOOL;
60pub extern "kernel32" stdcallcc fn DeleteFileA(lpFileName: [*]const u8) BOOL;
61pub extern "kernel32" stdcallcc fn DeleteFileW(lpFileName: [*]const u16) BOOL;
6362
6463pub extern "kernel32" stdcallcc fn ExitProcess(exit_code: UINT) noreturn;
6564
......@@ -73,8 +72,8 @@ pub extern "kernel32" stdcallcc fn GetCommandLineA() LPSTR;
7372
7473pub extern "kernel32" stdcallcc fn GetConsoleMode(in_hConsoleHandle: HANDLE, out_lpMode: *DWORD) BOOL;
7574
76pub extern "kernel32" stdcallcc fn GetCurrentDirectoryA(nBufferLength: WORD, lpBuffer: ?[*]CHAR) DWORD;
77pub extern "kernel32" stdcallcc fn GetCurrentDirectoryW(nBufferLength: WORD, lpBuffer: ?[*]WCHAR) DWORD;
75pub extern "kernel32" stdcallcc fn GetCurrentDirectoryA(nBufferLength: DWORD, lpBuffer: ?[*]CHAR) DWORD;
76pub extern "kernel32" stdcallcc fn GetCurrentDirectoryW(nBufferLength: DWORD, lpBuffer: ?[*]WCHAR) DWORD;
7877
7978pub extern "kernel32" stdcallcc fn GetCurrentThread() HANDLE;
8079pub extern "kernel32" stdcallcc fn GetCurrentThreadId() DWORD;
......@@ -87,7 +86,8 @@ pub extern "kernel32" stdcallcc fn GetExitCodeProcess(hProcess: HANDLE, lpExitCo
8786
8887pub extern "kernel32" stdcallcc fn GetFileSizeEx(hFile: HANDLE, lpFileSize: *LARGE_INTEGER) BOOL;
8988
90pub extern "kernel32" stdcallcc fn GetFileAttributesA(lpFileName: LPCSTR) DWORD;
89pub extern "kernel32" stdcallcc fn GetFileAttributesA(lpFileName: [*]const CHAR) DWORD;
90pub extern "kernel32" stdcallcc fn GetFileAttributesW(lpFileName: [*]const WCHAR) DWORD;
9191
9292pub extern "kernel32" stdcallcc fn GetModuleFileNameA(hModule: ?HMODULE, lpFilename: LPSTR, nSize: DWORD) DWORD;
9393
......@@ -131,8 +131,14 @@ pub extern "kernel32" stdcallcc fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem
131131pub extern "kernel32" stdcallcc fn HeapValidate(hHeap: HANDLE, dwFlags: DWORD, lpMem: ?*const c_void) BOOL;
132132
133133pub extern "kernel32" stdcallcc fn MoveFileExA(
134 lpExistingFileName: LPCSTR,
135 lpNewFileName: LPCSTR,
134 lpExistingFileName: [*]const u8,
135 lpNewFileName: [*]const u8,
136 dwFlags: DWORD,
137) BOOL;
138
139pub extern "kernel32" stdcallcc fn MoveFileExW(
140 lpExistingFileName: [*]const u16,
141 lpNewFileName: [*]const u16,
136142 dwFlags: DWORD,
137143) BOOL;
138144
std/os/windows/util.zig+74-3
......@@ -7,11 +7,17 @@ const mem = std.mem;
77const BufMap = std.BufMap;
88const cstr = std.cstr;
99
10pub const PATH_MAX_UTF16 = 32767;
10// > The maximum path of 32,767 characters is approximate, because the "\\?\"
11// > prefix may be expanded to a longer string by the system at run time, and
12// > this expansion applies to the total length.
13// from https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#maximum-path-length-limitation
14pub const PATH_MAX_WIDE = 32767;
1115
1216pub const WaitError = error{
1317 WaitAbandoned,
1418 WaitTimeOut,
19
20 /// See https://github.com/ziglang/zig/issues/1396
1521 Unexpected,
1622};
1723
......@@ -39,6 +45,8 @@ pub const WriteError = error{
3945 SystemResources,
4046 OperationAborted,
4147 BrokenPipe,
48
49 /// See https://github.com/ziglang/zig/issues/1396
4250 Unexpected,
4351};
4452
......@@ -88,13 +96,28 @@ pub fn windowsIsCygwinPty(handle: windows.HANDLE) bool {
8896pub const OpenError = error{
8997 SharingViolation,
9098 PathAlreadyExists,
99
100 /// When all the path components are found but the file component is not.
91101 FileNotFound,
102
103 /// When one or more path components are not found.
104 PathNotFound,
105
92106 AccessDenied,
93107 PipeBusy,
108 NameTooLong,
109
110 /// On Windows, file paths must be valid Unicode.
111 InvalidUtf8,
112
113 /// On Windows, file paths cannot contain these characters:
114 /// '/', '*', '?', '"', '<', '>', '|'
115 BadPathName,
116
117 /// See https://github.com/ziglang/zig/issues/1396
94118 Unexpected,
95119};
96120
97/// `file_path` needs to be copied in memory to add a null terminating byte, hence the allocator.
98121pub fn windowsOpen(
99122 file_path: []const u8,
100123 desired_access: windows.DWORD,
......@@ -102,7 +125,25 @@ pub fn windowsOpen(
102125 creation_disposition: windows.DWORD,
103126 flags_and_attrs: windows.DWORD,
104127) OpenError!windows.HANDLE {
105 @compileError("TODO rewrite with CreateFileW and no allocator");
128 const file_path_w = try sliceToPrefixedFileW(file_path);
129
130 const result = windows.CreateFileW(&file_path_w, desired_access, share_mode, null, creation_disposition, flags_and_attrs, null);
131
132 if (result == windows.INVALID_HANDLE_VALUE) {
133 const err = windows.GetLastError();
134 switch (err) {
135 windows.ERROR.SHARING_VIOLATION => return OpenError.SharingViolation,
136 windows.ERROR.ALREADY_EXISTS => return OpenError.PathAlreadyExists,
137 windows.ERROR.FILE_EXISTS => return OpenError.PathAlreadyExists,
138 windows.ERROR.FILE_NOT_FOUND => return OpenError.FileNotFound,
139 windows.ERROR.PATH_NOT_FOUND => return OpenError.PathNotFound,
140 windows.ERROR.ACCESS_DENIED => return OpenError.AccessDenied,
141 windows.ERROR.PIPE_BUSY => return OpenError.PipeBusy,
142 else => return os.unexpectedErrorWindows(err),
143 }
144 }
145
146 return result;
106147}
107148
108149/// Caller must free result.
......@@ -242,3 +283,33 @@ pub fn windowsGetQueuedCompletionStatus(completion_port: windows.HANDLE, bytes_t
242283 }
243284 return WindowsWaitResult.Normal;
244285}
286
287pub fn cStrToPrefixedFileW(s: [*]const u8) ![PATH_MAX_WIDE+1]u16 {
288 return sliceToPrefixedFileW(mem.toSliceConst(u8, s));
289}
290
291pub fn sliceToPrefixedFileW(s: []const u8) ![PATH_MAX_WIDE+1]u16 {
292 // TODO well defined copy elision
293 var result: [PATH_MAX_WIDE+1]u16 = undefined;
294
295 // > File I/O functions in the Windows API convert "/" to "\" as part of
296 // > converting the name to an NT-style name, except when using the "\\?\"
297 // > prefix as detailed in the following sections.
298 // from https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#maximum-path-length-limitation
299 // Because we want the larger maximum path length for absolute paths, we
300 // disallow forward slashes in zig std lib file functions on Windows.
301 for (s) |byte| switch (byte) {
302 '/', '*', '?', '"', '<', '>', '|' => return error.BadPathName,
303 else => {},
304 };
305 const start_index = if (mem.startsWith(u8, s, "\\\\") or !os.path.isAbsolute(s)) 0 else blk: {
306 const prefix = []u16{'\\', '\\', '?', '\\'};
307 mem.copy(u16, result[0..], prefix);
308 break :blk prefix.len;
309 };
310 const end_index = start_index + try std.unicode.utf8ToUtf16Le(result[start_index..], s);
311 assert(end_index <= result.len);
312 if (end_index == result.len) return error.NameTooLong;
313 result[end_index] = 0;
314 return result;
315}
std/unicode.zig+33-12
......@@ -247,6 +247,8 @@ pub const Utf16LeIterator = struct {
247247 }
248248
249249 pub fn nextCodepoint(it: *Utf16LeIterator) !?u32 {
250 assert(it.i <= it.bytes.len);
251 if (it.i == it.bytes.len) return null;
250252 const c0: u32 = mem.readIntLE(u16, it.bytes[it.i .. it.i + 2]);
251253 if (c0 & ~u32(0x03ff) == 0xd800) {
252254 // surrogate pair
......@@ -254,10 +256,12 @@ pub const Utf16LeIterator = struct {
254256 if (it.i >= it.bytes.len) return error.DanglingSurrogateHalf;
255257 const c1: u32 = mem.readIntLE(u16, it.bytes[it.i .. it.i + 2]);
256258 if (c1 & ~u32(0x03ff) != 0xdc00) return error.ExpectedSecondSurrogateHalf;
259 it.i += 2;
257260 return 0x10000 + (((c0 & 0x03ff) << 10) | (c1 & 0x03ff));
258261 } else if (c0 & ~u32(0x03ff) == 0xdc00) {
259262 return error.UnexpectedSecondSurrogateHalf;
260263 } else {
264 it.i += 2;
261265 return c0;
262266 }
263267 }
......@@ -490,15 +494,15 @@ pub fn utf16leToUtf8Alloc(allocator: *mem.Allocator, utf16le: []const u16) ![]u8
490494 return result.toOwnedSlice();
491495}
492496
493pub fn utf16leToUtf8(utf8: []u8, utf16le: []const u16) !void {
494 var out_index: usize = 0;
497/// Asserts that the output buffer is big enough.
498/// Returns end index.
499pub fn utf16leToUtf8(utf8: []u8, utf16le: []const u16) !usize {
500 var end_index: usize = 0;
495501 var it = Utf16LeIterator.init(utf16le);
496502 while (try it.nextCodepoint()) |codepoint| {
497 const utf8_len = utf8CodepointSequenceLength(codepoint) catch unreachable;
498 try result.resize(result.len + utf8_len);
499 assert((utf8Encode(codepoint, result.items[out_index..]) catch unreachable) == utf8_len);
500 out_index += utf8_len;
503 end_index += try utf8Encode(codepoint, utf8[end_index..]);
501504 }
505 return end_index;
502506}
503507
504508test "utf16leToUtf8" {
......@@ -508,14 +512,14 @@ test "utf16leToUtf8" {
508512 {
509513 mem.writeInt(utf16le_as_bytes[0..], u16('A'), builtin.Endian.Little);
510514 mem.writeInt(utf16le_as_bytes[2..], u16('a'), builtin.Endian.Little);
511 const utf8 = try utf16leToUtf8(std.debug.global_allocator, utf16le);
515 const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, utf16le);
512516 assert(mem.eql(u8, utf8, "Aa"));
513517 }
514518
515519 {
516520 mem.writeInt(utf16le_as_bytes[0..], u16(0x80), builtin.Endian.Little);
517521 mem.writeInt(utf16le_as_bytes[2..], u16(0xffff), builtin.Endian.Little);
518 const utf8 = try utf16leToUtf8(std.debug.global_allocator, utf16le);
522 const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, utf16le);
519523 assert(mem.eql(u8, utf8, "\xc2\x80" ++ "\xef\xbf\xbf"));
520524 }
521525
......@@ -523,7 +527,7 @@ test "utf16leToUtf8" {
523527 // the values just outside the surrogate half range
524528 mem.writeInt(utf16le_as_bytes[0..], u16(0xd7ff), builtin.Endian.Little);
525529 mem.writeInt(utf16le_as_bytes[2..], u16(0xe000), builtin.Endian.Little);
526 const utf8 = try utf16leToUtf8(std.debug.global_allocator, utf16le);
530 const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, utf16le);
527531 assert(mem.eql(u8, utf8, "\xed\x9f\xbf" ++ "\xee\x80\x80"));
528532 }
529533
......@@ -531,7 +535,7 @@ test "utf16leToUtf8" {
531535 // smallest surrogate pair
532536 mem.writeInt(utf16le_as_bytes[0..], u16(0xd800), builtin.Endian.Little);
533537 mem.writeInt(utf16le_as_bytes[2..], u16(0xdc00), builtin.Endian.Little);
534 const utf8 = try utf16leToUtf8(std.debug.global_allocator, utf16le);
538 const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, utf16le);
535539 assert(mem.eql(u8, utf8, "\xf0\x90\x80\x80"));
536540 }
537541
......@@ -539,14 +543,14 @@ test "utf16leToUtf8" {
539543 // largest surrogate pair
540544 mem.writeInt(utf16le_as_bytes[0..], u16(0xdbff), builtin.Endian.Little);
541545 mem.writeInt(utf16le_as_bytes[2..], u16(0xdfff), builtin.Endian.Little);
542 const utf8 = try utf16leToUtf8(std.debug.global_allocator, utf16le);
546 const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, utf16le);
543547 assert(mem.eql(u8, utf8, "\xf4\x8f\xbf\xbf"));
544548 }
545549
546550 {
547551 mem.writeInt(utf16le_as_bytes[0..], u16(0xdbff), builtin.Endian.Little);
548552 mem.writeInt(utf16le_as_bytes[2..], u16(0xdc00), builtin.Endian.Little);
549 const utf8 = try utf16leToUtf8(std.debug.global_allocator, utf16le);
553 const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, utf16le);
550554 assert(mem.eql(u8, utf8, "\xf4\x8f\xb0\x80"));
551555 }
552556}
......@@ -567,3 +571,20 @@ pub fn utf8ToUtf16LeWithNull(allocator: *mem.Allocator, utf8: []const u8) ![]u16
567571 try result.append(0);
568572 return result.toOwnedSlice();
569573}
574
575/// Returns index of next character. If exact fit, returned index equals output slice length.
576/// If ran out of room, returned index equals output slice length + 1.
577/// TODO support codepoints bigger than 16 bits
578pub fn utf8ToUtf16Le(utf16le: []u16, utf8: []const u8) !usize {
579 const utf16le_as_bytes = @sliceToBytes(utf16le[0..]);
580 var end_index: usize = 0;
581
582 var it = (try Utf8View.init(utf8)).iterator();
583 while (it.nextCodepoint()) |codepoint| {
584 if (end_index == utf16le_as_bytes.len) return (end_index / 2) + 1;
585 // TODO surrogate pairs
586 mem.writeInt(utf16le_as_bytes[end_index..], @intCast(u16, codepoint), builtin.Endian.Little);
587 end_index += 2;
588 }
589 return end_index / 2;
590}