| ... | ... | @@ -25,105 +25,87 @@ pub const File = struct { |
| 25 | 25 | |
| 26 | 26 | pub const OpenError = windows.CreateFileError || os.OpenError; |
| 27 | 27 | |
| 28 | | /// Deprecated; call `std.fs.Dir.openRead` directly. |
| 28 | /// TODO https://github.com/ziglang/zig/issues/3802 |
| 29 | pub const OpenFlags = struct { |
| 30 | read: bool = true, |
| 31 | write: bool = false, |
| 32 | }; |
| 33 | |
| 34 | /// TODO https://github.com/ziglang/zig/issues/3802 |
| 35 | pub const CreateFlags = struct { |
| 36 | /// Whether the file will be created with read access. |
| 37 | read: bool = false, |
| 38 | |
| 39 | /// If the file already exists, and is a regular file, and the access |
| 40 | /// mode allows writing, it will be truncated to length 0. |
| 41 | truncate: bool = true, |
| 42 | |
| 43 | /// Ensures that this open call creates the file, otherwise causes |
| 44 | /// `error.FileAlreadyExists` to be returned. |
| 45 | exclusive: bool = false, |
| 46 | |
| 47 | /// For POSIX systems this is the file system mode the file will |
| 48 | /// be created with. |
| 49 | mode: Mode = default_mode, |
| 50 | }; |
| 51 | |
| 52 | /// Deprecated; call `std.fs.Dir.openFile` directly. |
| 29 | 53 | pub fn openRead(path: []const u8) OpenError!File { |
| 30 | | return std.fs.Dir.cwd().openRead(path); |
| 54 | return std.fs.Dir.cwd().openFile(path, .{}); |
| 31 | 55 | } |
| 32 | 56 | |
| 33 | | /// Deprecated; call `std.fs.Dir.openReadC` directly. |
| 57 | /// Deprecated; call `std.fs.Dir.openFileC` directly. |
| 34 | 58 | pub fn openReadC(path_c: [*:0]const u8) OpenError!File { |
| 35 | | return std.fs.Dir.cwd().openReadC(path_c); |
| 59 | return std.fs.Dir.cwd().openFileC(path_c, .{}); |
| 36 | 60 | } |
| 37 | 61 | |
| 38 | | /// Deprecated; call `std.fs.Dir.openReadW` directly. |
| 62 | /// Deprecated; call `std.fs.Dir.openFileW` directly. |
| 39 | 63 | pub fn openReadW(path_w: [*]const u16) OpenError!File { |
| 40 | | return std.fs.Dir.cwd().openReadW(path_w); |
| 64 | return std.fs.Dir.cwd().openFileW(path_w, .{}); |
| 41 | 65 | } |
| 42 | 66 | |
| 43 | | /// Calls `openWriteMode` with `default_mode` for the mode. |
| 44 | | /// TODO: deprecate this and move it to `std.fs.Dir`. |
| 67 | /// Deprecated; call `std.fs.Dir.createFile` directly. |
| 45 | 68 | pub fn openWrite(path: []const u8) OpenError!File { |
| 46 | | return openWriteMode(path, default_mode); |
| 69 | return std.fs.Dir.cwd().createFile(path, .{}); |
| 47 | 70 | } |
| 48 | 71 | |
| 49 | | /// If the path does not exist it will be created. |
| 50 | | /// If a file already exists in the destination it will be truncated. |
| 51 | | /// Call close to clean up. |
| 52 | | /// TODO: deprecate this and move it to `std.fs.Dir`. |
| 72 | /// Deprecated; call `std.fs.Dir.createFile` directly. |
| 53 | 73 | pub fn openWriteMode(path: []const u8, file_mode: Mode) OpenError!File { |
| 54 | | if (builtin.os == .windows) { |
| 55 | | const path_w = try windows.sliceToPrefixedFileW(path); |
| 56 | | return openWriteModeW(&path_w, file_mode); |
| 57 | | } |
| 58 | | const path_c = try os.toPosixPath(path); |
| 59 | | return openWriteModeC(&path_c, file_mode); |
| 74 | return std.fs.Dir.cwd().createFile(path, .{ .mode = file_mode }); |
| 60 | 75 | } |
| 61 | 76 | |
| 62 | | /// Same as `openWriteMode` except `path` is null-terminated. |
| 63 | | /// TODO: deprecate this and move it to `std.fs.Dir`. |
| 64 | | pub fn openWriteModeC(path: [*:0]const u8, file_mode: Mode) OpenError!File { |
| 65 | | if (builtin.os == .windows) { |
| 66 | | const path_w = try windows.cStrToPrefixedFileW(path); |
| 67 | | return openWriteModeW(&path_w, file_mode); |
| 68 | | } |
| 69 | | const O_LARGEFILE = if (@hasDecl(os, "O_LARGEFILE")) os.O_LARGEFILE else 0; |
| 70 | | const flags = O_LARGEFILE | os.O_WRONLY | os.O_CREAT | os.O_CLOEXEC | os.O_TRUNC; |
| 71 | | const fd = try os.openC(path, flags, file_mode); |
| 72 | | return openHandle(fd); |
| 77 | /// Deprecated; call `std.fs.Dir.createFileC` directly. |
| 78 | pub fn openWriteModeC(path_c: [*:0]const u8, file_mode: Mode) OpenError!File { |
| 79 | return std.fs.Dir.cwd().createFileC(path_c, .{ .mode = file_mode }); |
| 73 | 80 | } |
| 74 | 81 | |
| 75 | | /// Same as `openWriteMode` except `path` is null-terminated and UTF16LE encoded |
| 76 | | /// TODO: deprecate this and move it to `std.fs.Dir`. |
| 82 | /// Deprecated; call `std.fs.Dir.createFileW` directly. |
| 77 | 83 | pub fn openWriteModeW(path_w: [*:0]const u16, file_mode: Mode) OpenError!File { |
| 78 | | const handle = try windows.CreateFileW( |
| 79 | | path_w, |
| 80 | | windows.GENERIC_WRITE, |
| 81 | | windows.FILE_SHARE_WRITE | windows.FILE_SHARE_READ | windows.FILE_SHARE_DELETE, |
| 82 | | null, |
| 83 | | windows.CREATE_ALWAYS, |
| 84 | | windows.FILE_ATTRIBUTE_NORMAL, |
| 85 | | null, |
| 86 | | ); |
| 87 | | return openHandle(handle); |
| 84 | return std.fs.Dir.cwd().createFileW(path_w, .{ .mode = file_mode }); |
| 88 | 85 | } |
| 89 | 86 | |
| 90 | | /// If the path does not exist it will be created. |
| 91 | | /// If a file already exists in the destination this returns OpenError.PathAlreadyExists |
| 92 | | /// Call close to clean up. |
| 93 | | /// TODO: deprecate this and move it to `std.fs.Dir`. |
| 87 | /// Deprecated; call `std.fs.Dir.createFile` directly. |
| 94 | 88 | pub fn openWriteNoClobber(path: []const u8, file_mode: Mode) OpenError!File { |
| 95 | | if (builtin.os == .windows) { |
| 96 | | const path_w = try windows.sliceToPrefixedFileW(path); |
| 97 | | return openWriteNoClobberW(&path_w, file_mode); |
| 98 | | } |
| 99 | | const path_c = try os.toPosixPath(path); |
| 100 | | return openWriteNoClobberC(&path_c, file_mode); |
| 89 | return std.fs.Dir.cwd().createFile(path, .{ |
| 90 | .mode = file_mode, |
| 91 | .exclusive = true, |
| 92 | }); |
| 101 | 93 | } |
| 102 | 94 | |
| 103 | | /// TODO: deprecate this and move it to `std.fs.Dir`. |
| 104 | | pub fn openWriteNoClobberC(path: [*:0]const u8, file_mode: Mode) OpenError!File { |
| 105 | | if (builtin.os == .windows) { |
| 106 | | const path_w = try windows.cStrToPrefixedFileW(path); |
| 107 | | return openWriteNoClobberW(&path_w, file_mode); |
| 108 | | } |
| 109 | | const O_LARGEFILE = if (@hasDecl(os, "O_LARGEFILE")) os.O_LARGEFILE else 0; |
| 110 | | const flags = O_LARGEFILE | os.O_WRONLY | os.O_CREAT | os.O_CLOEXEC | os.O_EXCL; |
| 111 | | const fd = try os.openC(path, flags, file_mode); |
| 112 | | return openHandle(fd); |
| 95 | /// Deprecated; call `std.fs.Dir.createFileC` directly. |
| 96 | pub fn openWriteNoClobberC(path_c: [*:0]const u8, file_mode: Mode) OpenError!File { |
| 97 | return std.fs.Dir.cwd().createFileC(path_c, .{ |
| 98 | .mode = file_mode, |
| 99 | .exclusive = true, |
| 100 | }); |
| 113 | 101 | } |
| 114 | 102 | |
| 115 | | /// TODO: deprecate this and move it to `std.fs.Dir`. |
| 103 | /// Deprecated; call `std.fs.Dir.createFileW` directly. |
| 116 | 104 | pub fn openWriteNoClobberW(path_w: [*:0]const u16, file_mode: Mode) OpenError!File { |
| 117 | | const handle = try windows.CreateFileW( |
| 118 | | path_w, |
| 119 | | windows.GENERIC_WRITE, |
| 120 | | windows.FILE_SHARE_WRITE | windows.FILE_SHARE_READ | windows.FILE_SHARE_DELETE, |
| 121 | | null, |
| 122 | | windows.CREATE_NEW, |
| 123 | | windows.FILE_ATTRIBUTE_NORMAL, |
| 124 | | null, |
| 125 | | ); |
| 126 | | return openHandle(handle); |
| 105 | return std.fs.Dir.cwd().createFileW(path_w, .{ |
| 106 | .mode = file_mode, |
| 107 | .exclusive = true, |
| 108 | }); |
| 127 | 109 | } |
| 128 | 110 | |
| 129 | 111 | pub fn openHandle(handle: os.fd_t) File { |