| ... | @@ -25,105 +25,87 @@ pub const File = struct { | ... | @@ -25,105 +25,87 @@ pub const File = struct { |
| 25 | | 25 | |
| 26 | pub const OpenError = windows.CreateFileError || os.OpenError; | 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 | pub fn openRead(path: []const u8) OpenError!File { | 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 | pub fn openReadC(path_c: [*:0]const u8) OpenError!File { | 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 | pub fn openReadW(path_w: [*]const u16) OpenError!File { | 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. | 67 | /// Deprecated; call `std.fs.Dir.createFile` directly. |
| 44 | /// TODO: deprecate this and move it to `std.fs.Dir`. | | |
| 45 | pub fn openWrite(path: []const u8) OpenError!File { | 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. | 72 | /// Deprecated; call `std.fs.Dir.createFile` directly. |
| 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`. | | |
| 53 | pub fn openWriteMode(path: []const u8, file_mode: Mode) OpenError!File { | 73 | pub fn openWriteMode(path: []const u8, file_mode: Mode) OpenError!File { |
| 54 | if (builtin.os == .windows) { | 74 | return std.fs.Dir.cwd().createFile(path, .{ .mode = file_mode }); |
| 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); | | |
| 60 | } | 75 | } |
| 61 | | 76 | |
| 62 | /// Same as `openWriteMode` except `path` is null-terminated. | 77 | /// Deprecated; call `std.fs.Dir.createFileC` directly. |
| 63 | /// TODO: deprecate this and move it to `std.fs.Dir`. | 78 | pub fn openWriteModeC(path_c: [*:0]const u8, file_mode: Mode) OpenError!File { |
| 64 | pub fn openWriteModeC(path: [*:0]const u8, file_mode: Mode) OpenError!File { | 79 | return std.fs.Dir.cwd().createFileC(path_c, .{ .mode = file_mode }); |
| 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); | | |
| 73 | } | 80 | } |
| 74 | | 81 | |
| 75 | /// Same as `openWriteMode` except `path` is null-terminated and UTF16LE encoded | 82 | /// Deprecated; call `std.fs.Dir.createFileW` directly. |
| 76 | /// TODO: deprecate this and move it to `std.fs.Dir`. | | |
| 77 | pub fn openWriteModeW(path_w: [*:0]const u16, file_mode: Mode) OpenError!File { | 83 | pub fn openWriteModeW(path_w: [*:0]const u16, file_mode: Mode) OpenError!File { |
| 78 | const handle = try windows.CreateFileW( | 84 | return std.fs.Dir.cwd().createFileW(path_w, .{ .mode = file_mode }); |
| 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); | | |
| 88 | } | 85 | } |
| 89 | | 86 | |
| 90 | /// If the path does not exist it will be created. | 87 | /// Deprecated; call `std.fs.Dir.createFile` directly. |
| 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`. | | |
| 94 | pub fn openWriteNoClobber(path: []const u8, file_mode: Mode) OpenError!File { | 88 | pub fn openWriteNoClobber(path: []const u8, file_mode: Mode) OpenError!File { |
| 95 | if (builtin.os == .windows) { | 89 | return std.fs.Dir.cwd().createFile(path, .{ |
| 96 | const path_w = try windows.sliceToPrefixedFileW(path); | 90 | .mode = file_mode, |
| 97 | return openWriteNoClobberW(&path_w, file_mode); | 91 | .exclusive = true, |
| 98 | } | 92 | }); |
| 99 | const path_c = try os.toPosixPath(path); | | |
| 100 | return openWriteNoClobberC(&path_c, file_mode); | | |
| 101 | } | 93 | } |
| 102 | | 94 | |
| 103 | /// TODO: deprecate this and move it to `std.fs.Dir`. | 95 | /// Deprecated; call `std.fs.Dir.createFileC` directly. |
| 104 | pub fn openWriteNoClobberC(path: [*:0]const u8, file_mode: Mode) OpenError!File { | 96 | pub fn openWriteNoClobberC(path_c: [*:0]const u8, file_mode: Mode) OpenError!File { |
| 105 | if (builtin.os == .windows) { | 97 | return std.fs.Dir.cwd().createFileC(path_c, .{ |
| 106 | const path_w = try windows.cStrToPrefixedFileW(path); | 98 | .mode = file_mode, |
| 107 | return openWriteNoClobberW(&path_w, file_mode); | 99 | .exclusive = true, |
| 108 | } | 100 | }); |
| 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); | | |
| 113 | } | 101 | } |
| 114 | | 102 | |
| 115 | /// TODO: deprecate this and move it to `std.fs.Dir`. | 103 | /// Deprecated; call `std.fs.Dir.createFileW` directly. |
| 116 | pub fn openWriteNoClobberW(path_w: [*:0]const u16, file_mode: Mode) OpenError!File { | 104 | pub fn openWriteNoClobberW(path_w: [*:0]const u16, file_mode: Mode) OpenError!File { |
| 117 | const handle = try windows.CreateFileW( | 105 | return std.fs.Dir.cwd().createFileW(path_w, .{ |
| 118 | path_w, | 106 | .mode = file_mode, |
| 119 | windows.GENERIC_WRITE, | 107 | .exclusive = true, |
| 120 | windows.FILE_SHARE_WRITE | windows.FILE_SHARE_READ | windows.FILE_SHARE_DELETE, | 108 | }); |
| 121 | null, | | |
| 122 | windows.CREATE_NEW, | | |
| 123 | windows.FILE_ATTRIBUTE_NORMAL, | | |
| 124 | null, | | |
| 125 | ); | | |
| 126 | return openHandle(handle); | | |
| 127 | } | 109 | } |
| 128 | | 110 | |
| 129 | pub fn openHandle(handle: os.fd_t) File { | 111 | pub fn openHandle(handle: os.fd_t) File { |