authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-26 13:37:34-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-26 18:32:45-04:00
log44a049e01eef654fee82a1ee4e1aaf37447319ce
tree55fe1e7ae6920f19439d4f7d3424152a133203fb
parent2f040a23c8b968db56ab4c4725d6651f5ea3418e
signature Commit is signed but in an unrecognized format.

more cleanup. down to just the `@hasDecl` builtin


11 files changed, 63 insertions(+), 42 deletions(-)

build.zig+1-1
......@@ -166,7 +166,7 @@ fn dependOnLib(b: *Builder, lib_exe_obj: var, dep: LibraryDep) void {
166166}
167167
168168fn fileExists(filename: []const u8) !bool {
169 fs.File.access(filename) catch |err| switch (err) {
169 fs.File.exists(filename) catch |err| switch (err) {
170170 error.PermissionDenied,
171171 error.FileNotFound,
172172 => return false,
std/build.zig+1-1
......@@ -846,7 +846,7 @@ pub const Target = union(enum) {
846846 }
847847
848848 pub fn oFileExt(self: Target) []const u8 {
849 const abi = switch (self.*) {
849 const abi = switch (self) {
850850 Target.Native => builtin.abi,
851851 Target.Cross => |t| t.abi,
852852 };
std/child_process.zig+4-28
......@@ -50,24 +50,8 @@ pub const ChildProcess = struct {
5050 err_pipe: if (os.windows.is_the_target) void else [2]i32,
5151 llnode: if (os.windows.is_the_target) void else LinkedList(*ChildProcess).Node,
5252
53 pub const SpawnError = error{
54 ProcessFdQuotaExceeded,
55 Unexpected,
56 NotDir,
57 SystemResources,
58 FileNotFound,
59 NameTooLong,
60 SymLinkLoop,
61 FileSystem,
62 OutOfMemory,
63 AccessDenied,
64 PermissionDenied,
65 InvalidUserId,
66 ResourceLimitReached,
67 InvalidExe,
68 IsDir,
69 FileBusy,
70 };
53 pub const SpawnError = error{OutOfMemory} || os.ExecveError || os.SetIdError ||
54 os.ChangeCurDirError || windows.CreateProcessError;
7155
7256 pub const Term = union(enum) {
7357 Exited: i32,
......@@ -640,7 +624,7 @@ fn windowsCreateProcess(app_name: [*]u16, cmd_line: [*]u16, envp_ptr: ?[*]u16, c
640624 // However this would imply that programs compiled with -DUNICODE could not pass
641625 // environment variables to programs that were not, which seems unlikely.
642626 // More investigation is needed.
643 if (windows.CreateProcessW(
627 return windows.CreateProcessW(
644628 app_name,
645629 cmd_line,
646630 null,
......@@ -651,15 +635,7 @@ fn windowsCreateProcess(app_name: [*]u16, cmd_line: [*]u16, envp_ptr: ?[*]u16, c
651635 cwd_ptr,
652636 lpStartupInfo,
653637 lpProcessInformation,
654 ) == 0) {
655 switch (windows.GetLastError()) {
656 windows.ERROR.FILE_NOT_FOUND => return error.FileNotFound,
657 windows.ERROR.PATH_NOT_FOUND => return error.FileNotFound,
658 windows.ERROR.INVALID_PARAMETER => unreachable,
659 windows.ERROR.INVALID_NAME => return error.InvalidName,
660 else => |err| return windows.unexpectedError(err),
661 }
662 }
638 );
663639}
664640
665641/// Caller must dealloc.
std/fs.zig+5-2
......@@ -2,7 +2,10 @@ const builtin = @import("builtin");
22const std = @import("std.zig");
33const os = std.os;
44const mem = std.mem;
5const base64 = std.base64;
6const crypto = std.crypto;
57const Allocator = std.mem.Allocator;
8const assert = std.debug.assert;
69
710pub const path = @import("fs/path.zig");
811pub const File = @import("fs/file.zig").File;
......@@ -73,7 +76,7 @@ pub fn atomicSymLink(allocator: *Allocator, existing_path: []const u8, new_path:
7376 mem.copy(u8, tmp_path[0..], dirname);
7477 tmp_path[dirname.len] = path.sep;
7578 while (true) {
76 try getRandomBytes(rand_buf[0..]);
79 try crypto.randomBytes(rand_buf[0..]);
7780 b64_fs_encoder.encode(tmp_path[dirname.len + 1 ..], rand_buf);
7881
7982 if (symLink(existing_path, tmp_path)) {
......@@ -159,7 +162,7 @@ pub const AtomicFile = struct {
159162 tmp_path_buf[tmp_path_len] = 0;
160163
161164 while (true) {
162 try getRandomBytes(rand_buf[0..]);
165 try crypto.randomBytes(rand_buf[0..]);
163166 b64_fs_encoder.encode(tmp_path_buf[dirname_component_len..tmp_path_len], rand_buf);
164167
165168 const file = File.openWriteNoClobberC(&tmp_path_buf, mode) catch |err| switch (err) {
std/fs/file.zig+3-3
......@@ -137,17 +137,17 @@ pub const File = struct {
137137
138138 /// Test for the existence of `path`.
139139 /// `path` is UTF8-encoded.
140 pub fn exists(path: []const u8) AccessError!void {
140 pub fn exists(path: []const u8) !void {
141141 return os.access(path, os.F_OK);
142142 }
143143
144144 /// Same as `exists` except the parameter is null-terminated.
145 pub fn existsC(path: [*]const u8) AccessError!void {
145 pub fn existsC(path: [*]const u8) !void {
146146 return os.accessC(path, os.F_OK);
147147 }
148148
149149 /// Same as `exists` except the parameter is null-terminated UTF16LE-encoded.
150 pub fn existsW(path: [*]const u16) AccessError!void {
150 pub fn existsW(path: [*]const u16) !void {
151151 return os.accessW(path, os.F_OK);
152152 }
153153
std/os.zig+2-1
......@@ -86,7 +86,7 @@ pub fn close(fd: fd_t) void {
8686 }
8787}
8888
89pub const GetRandomError = error{};
89pub const GetRandomError = OpenError;
9090
9191/// Obtain a series of random bytes. These bytes can be used to seed user-space
9292/// random number generators or for cryptographic purposes.
......@@ -641,6 +641,7 @@ pub const ExecveError = error{
641641 NotDir,
642642 FileBusy,
643643 ProcessFdQuotaExceeded,
644 SystemFdQuotaExceeded,
644645 NameTooLong,
645646
646647 Unexpected,
std/os/bits/linux/errno.zig+1
......@@ -279,6 +279,7 @@ pub const ESOCKTNOSUPPORT = 94;
279279
280280/// Operation not supported on transport endpoint
281281pub const EOPNOTSUPP = 95;
282pub const ENOTSUP = EOPNOTSUPP;
282283
283284/// Protocol family not supported
284285pub const EPFNOSUPPORT = 96;
std/os/linux.zig+2-2
......@@ -382,8 +382,8 @@ pub fn unlinkat(dirfd: i32, path: [*]const u8, flags: u32) usize {
382382 return syscall3(SYS_unlinkat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), flags);
383383}
384384
385pub fn waitpid(pid: i32, status: *i32, options: i32) usize {
386 return syscall4(SYS_wait4, @bitCast(usize, isize(pid)), @ptrToInt(status), @bitCast(usize, isize(options)), 0);
385pub fn waitpid(pid: i32, status: *i32, flags: u32) usize {
386 return syscall4(SYS_wait4, @bitCast(usize, isize(pid)), @ptrToInt(status), flags, 0);
387387}
388388
389389var vdso_clock_gettime = @ptrCast(?*const c_void, init_vdso_clock_gettime);
std/os/windows.zig+40
......@@ -1140,6 +1140,46 @@ pub fn GetEnvironmentVariableW(lpName: LPWSTR, lpBuffer: LPWSTR, nSize: DWORD) G
11401140 return rc;
11411141}
11421142
1143pub const CreateProcessError = error{
1144 FileNotFound,
1145 InvalidName,
1146 Unexpected,
1147};
1148
1149pub fn CreateProcessW(
1150 lpApplicationName: ?LPWSTR,
1151 lpCommandLine: LPWSTR,
1152 lpProcessAttributes: ?*SECURITY_ATTRIBUTES,
1153 lpThreadAttributes: ?*SECURITY_ATTRIBUTES,
1154 bInheritHandles: BOOL,
1155 dwCreationFlags: DWORD,
1156 lpEnvironment: ?*c_void,
1157 lpCurrentDirectory: ?LPWSTR,
1158 lpStartupInfo: *STARTUPINFOW,
1159 lpProcessInformation: *PROCESS_INFORMATION,
1160) CreateProcessError!void {
1161 if (kernel32.CreateProcessW(
1162 lpApplicationName,
1163 lpCommandLine,
1164 lpProcessAttributes,
1165 lpThreadAttributes,
1166 bInheritHandle,
1167 dwCreationFlags,
1168 lpEnvironment,
1169 lpCurrentDirectory,
1170 lpStartupInfo,
1171 lpProcessInformation,
1172 ) == 0) {
1173 switch (kernel32.GetLastError()) {
1174 ERROR.FILE_NOT_FOUND => return error.FileNotFound,
1175 ERROR.PATH_NOT_FOUND => return error.FileNotFound,
1176 ERROR.INVALID_PARAMETER => unreachable,
1177 ERROR.INVALID_NAME => return error.InvalidName,
1178 else => |err| return unexpectedError(err),
1179 }
1180 }
1181}
1182
11431183pub fn cStrToPrefixedFileW(s: [*]const u8) ![PATH_MAX_WIDE + 1]u16 {
11441184 return sliceToPrefixedFileW(mem.toSliceConst(u8, s));
11451185}
std/rand.zig+3-3
......@@ -1,10 +1,10 @@
1// The engines provided here should be initialized from an external source. For now, getRandomBytes
2// from the os package is the most suitable. Be sure to use a CSPRNG when required, otherwise using
1// The engines provided here should be initialized from an external source. For now, randomBytes
2// from the crypto package is the most suitable. Be sure to use a CSPRNG when required, otherwise using
33// a normal PRNG will be faster and use substantially less stack space.
44//
55// ```
66// var buf: [8]u8 = undefined;
7// try std.os.getRandomBytes(buf[0..]);
7// try std.crypto.randomBytes(buf[0..]);
88// const seed = mem.readIntSliceLittle(u64, buf[0..8]);
99//
1010// var r = DefaultPrng.init(seed);
test/tests.zig+1-1
......@@ -112,7 +112,7 @@ pub fn addCliTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const M
112112 const exe = b.addExecutable("test-cli", "test/cli.zig");
113113 const run_cmd = exe.run();
114114 run_cmd.addArgs([][]const u8{
115 fs.path.realAlloc(b.allocator, b.zig_exe) catch unreachable,
115 fs.realpathAlloc(b.allocator, b.zig_exe) catch unreachable,
116116 b.pathFromRoot(b.cache_root),
117117 });
118118