2018-02-10 17:55:13 -08:00
|
|
|
const std = @import("../index.zig");
|
|
|
|
const builtin = @import("builtin");
|
|
|
|
const os = std.os;
|
|
|
|
const mem = std.mem;
|
|
|
|
const math = std.math;
|
|
|
|
const assert = std.debug.assert;
|
|
|
|
const posix = os.posix;
|
|
|
|
const windows = os.windows;
|
|
|
|
const Os = builtin.Os;
|
|
|
|
|
|
|
|
const is_posix = builtin.os != builtin.Os.windows;
|
|
|
|
const is_windows = builtin.os == builtin.Os.windows;
|
|
|
|
|
|
|
|
pub const File = struct {
|
|
|
|
/// The OS-specific file descriptor or file handle.
|
|
|
|
handle: os.FileHandle,
|
|
|
|
|
|
|
|
const OpenError = os.WindowsOpenError || os.PosixOpenError;
|
|
|
|
|
|
|
|
/// `path` needs to be copied in memory to add a null terminating byte, hence the allocator.
|
|
|
|
/// Call close to clean up.
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn openRead(allocator: *mem.Allocator, path: []const u8) OpenError!File {
|
2018-02-10 17:55:13 -08:00
|
|
|
if (is_posix) {
|
2018-05-28 17:23:55 -07:00
|
|
|
const flags = posix.O_LARGEFILE | posix.O_RDONLY;
|
2018-02-10 17:55:13 -08:00
|
|
|
const fd = try os.posixOpen(allocator, path, flags, 0);
|
|
|
|
return openHandle(fd);
|
|
|
|
} else if (is_windows) {
|
2018-05-28 17:23:55 -07:00
|
|
|
const handle = try os.windowsOpen(
|
|
|
|
allocator,
|
|
|
|
path,
|
|
|
|
windows.GENERIC_READ,
|
|
|
|
windows.FILE_SHARE_READ,
|
|
|
|
windows.OPEN_EXISTING,
|
|
|
|
windows.FILE_ATTRIBUTE_NORMAL,
|
|
|
|
);
|
2018-02-10 17:55:13 -08:00
|
|
|
return openHandle(handle);
|
|
|
|
} else {
|
|
|
|
@compileError("TODO implement openRead for this OS");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Calls `openWriteMode` with os.default_file_mode for the mode.
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn openWrite(allocator: *mem.Allocator, path: []const u8) OpenError!File {
|
2018-02-10 17:55:13 -08:00
|
|
|
return openWriteMode(allocator, path, os.default_file_mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If the path does not exist it will be created.
|
|
|
|
/// If a file already exists in the destination it will be truncated.
|
|
|
|
/// `path` needs to be copied in memory to add a null terminating byte, hence the allocator.
|
|
|
|
/// Call close to clean up.
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn openWriteMode(allocator: *mem.Allocator, path: []const u8, file_mode: os.FileMode) OpenError!File {
|
2018-02-10 17:55:13 -08:00
|
|
|
if (is_posix) {
|
2018-05-28 17:23:55 -07:00
|
|
|
const flags = posix.O_LARGEFILE | posix.O_WRONLY | posix.O_CREAT | posix.O_CLOEXEC | posix.O_TRUNC;
|
2018-02-10 17:55:13 -08:00
|
|
|
const fd = try os.posixOpen(allocator, path, flags, file_mode);
|
|
|
|
return openHandle(fd);
|
|
|
|
} else if (is_windows) {
|
2018-05-28 17:23:55 -07:00
|
|
|
const handle = try os.windowsOpen(
|
|
|
|
allocator,
|
|
|
|
path,
|
|
|
|
windows.GENERIC_WRITE,
|
|
|
|
windows.FILE_SHARE_WRITE | windows.FILE_SHARE_READ | windows.FILE_SHARE_DELETE,
|
|
|
|
windows.CREATE_ALWAYS,
|
|
|
|
windows.FILE_ATTRIBUTE_NORMAL,
|
|
|
|
);
|
2018-02-10 17:55:13 -08:00
|
|
|
return openHandle(handle);
|
|
|
|
} else {
|
|
|
|
@compileError("TODO implement openWriteMode for this OS");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If the path does not exist it will be created.
|
|
|
|
/// If a file already exists in the destination this returns OpenError.PathAlreadyExists
|
|
|
|
/// `path` needs to be copied in memory to add a null terminating byte, hence the allocator.
|
|
|
|
/// Call close to clean up.
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn openWriteNoClobber(allocator: *mem.Allocator, path: []const u8, file_mode: os.FileMode) OpenError!File {
|
2018-02-10 17:55:13 -08:00
|
|
|
if (is_posix) {
|
2018-05-28 17:23:55 -07:00
|
|
|
const flags = posix.O_LARGEFILE | posix.O_WRONLY | posix.O_CREAT | posix.O_CLOEXEC | posix.O_EXCL;
|
2018-02-10 17:55:13 -08:00
|
|
|
const fd = try os.posixOpen(allocator, path, flags, file_mode);
|
|
|
|
return openHandle(fd);
|
|
|
|
} else if (is_windows) {
|
2018-05-28 17:23:55 -07:00
|
|
|
const handle = try os.windowsOpen(
|
|
|
|
allocator,
|
|
|
|
path,
|
|
|
|
windows.GENERIC_WRITE,
|
|
|
|
windows.FILE_SHARE_WRITE | windows.FILE_SHARE_READ | windows.FILE_SHARE_DELETE,
|
|
|
|
windows.CREATE_NEW,
|
|
|
|
windows.FILE_ATTRIBUTE_NORMAL,
|
|
|
|
);
|
2018-02-10 17:55:13 -08:00
|
|
|
return openHandle(handle);
|
|
|
|
} else {
|
|
|
|
@compileError("TODO implement openWriteMode for this OS");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn openHandle(handle: os.FileHandle) File {
|
2018-05-28 17:23:55 -07:00
|
|
|
return File{ .handle = handle };
|
2018-02-10 17:55:13 -08:00
|
|
|
}
|
|
|
|
|
2018-06-11 23:18:11 -07:00
|
|
|
pub const AccessError = error{
|
2018-06-11 22:55:08 -07:00
|
|
|
PermissionDenied,
|
|
|
|
NotFound,
|
|
|
|
NameTooLong,
|
|
|
|
BadMode,
|
|
|
|
BadPathName,
|
|
|
|
Io,
|
|
|
|
SystemResources,
|
|
|
|
OutOfMemory,
|
|
|
|
|
|
|
|
Unexpected,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub fn access(allocator: *mem.Allocator, path: []const u8, file_mode: os.FileMode) AccessError!bool {
|
2018-04-13 02:27:09 -07:00
|
|
|
const path_with_null = try std.cstr.addNullByte(allocator, path);
|
|
|
|
defer allocator.free(path_with_null);
|
|
|
|
|
|
|
|
if (is_posix) {
|
|
|
|
// mode is ignored and is always F_OK for now
|
|
|
|
const result = posix.access(path_with_null.ptr, posix.F_OK);
|
|
|
|
const err = posix.getErrno(result);
|
|
|
|
if (err > 0) {
|
|
|
|
return switch (err) {
|
|
|
|
posix.EACCES => error.PermissionDenied,
|
|
|
|
posix.EROFS => error.PermissionDenied,
|
|
|
|
posix.ELOOP => error.PermissionDenied,
|
|
|
|
posix.ETXTBSY => error.PermissionDenied,
|
|
|
|
posix.ENOTDIR => error.NotFound,
|
|
|
|
posix.ENOENT => error.NotFound,
|
|
|
|
|
|
|
|
posix.ENAMETOOLONG => error.NameTooLong,
|
|
|
|
posix.EINVAL => error.BadMode,
|
|
|
|
posix.EFAULT => error.BadPathName,
|
|
|
|
posix.EIO => error.Io,
|
|
|
|
posix.ENOMEM => error.SystemResources,
|
|
|
|
else => os.unexpectedErrorPosix(err),
|
|
|
|
};
|
|
|
|
}
|
2018-04-12 03:23:58 -07:00
|
|
|
return true;
|
2018-04-13 02:27:09 -07:00
|
|
|
} else if (is_windows) {
|
2018-06-11 22:55:08 -07:00
|
|
|
if (os.windows.GetFileAttributesA(path_with_null.ptr) != os.windows.INVALID_FILE_ATTRIBUTES) {
|
2018-04-13 02:27:09 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const err = windows.GetLastError();
|
|
|
|
return switch (err) {
|
|
|
|
windows.ERROR.FILE_NOT_FOUND => error.NotFound,
|
|
|
|
windows.ERROR.ACCESS_DENIED => error.PermissionDenied,
|
|
|
|
else => os.unexpectedErrorWindows(err),
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
@compileError("TODO implement access for this OS");
|
2018-04-12 03:23:58 -07:00
|
|
|
}
|
|
|
|
}
|
2018-02-10 17:55:13 -08:00
|
|
|
|
|
|
|
/// Upon success, the stream is in an uninitialized state. To continue using it,
|
|
|
|
/// you must use the open() function.
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn close(self: *File) void {
|
2018-02-10 17:55:13 -08:00
|
|
|
os.close(self.handle);
|
|
|
|
self.handle = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Calls `os.isTty` on `self.handle`.
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn isTty(self: *File) bool {
|
2018-02-10 17:55:13 -08:00
|
|
|
return os.isTty(self.handle);
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn seekForward(self: *File, amount: isize) !void {
|
2018-02-10 17:55:13 -08:00
|
|
|
switch (builtin.os) {
|
|
|
|
Os.linux, Os.macosx, Os.ios => {
|
|
|
|
const result = posix.lseek(self.handle, amount, posix.SEEK_CUR);
|
|
|
|
const err = posix.getErrno(result);
|
|
|
|
if (err > 0) {
|
|
|
|
return switch (err) {
|
|
|
|
posix.EBADF => error.BadFd,
|
|
|
|
posix.EINVAL => error.Unseekable,
|
|
|
|
posix.EOVERFLOW => error.Unseekable,
|
|
|
|
posix.ESPIPE => error.Unseekable,
|
|
|
|
posix.ENXIO => error.Unseekable,
|
|
|
|
else => os.unexpectedErrorPosix(err),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Os.windows => {
|
|
|
|
if (windows.SetFilePointerEx(self.handle, amount, null, windows.FILE_CURRENT) == 0) {
|
|
|
|
const err = windows.GetLastError();
|
|
|
|
return switch (err) {
|
|
|
|
windows.ERROR.INVALID_PARAMETER => error.BadFd,
|
|
|
|
else => os.unexpectedErrorWindows(err),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
else => @compileError("unsupported OS"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn seekTo(self: *File, pos: usize) !void {
|
2018-02-10 17:55:13 -08:00
|
|
|
switch (builtin.os) {
|
|
|
|
Os.linux, Os.macosx, Os.ios => {
|
|
|
|
const ipos = try math.cast(isize, pos);
|
|
|
|
const result = posix.lseek(self.handle, ipos, posix.SEEK_SET);
|
|
|
|
const err = posix.getErrno(result);
|
|
|
|
if (err > 0) {
|
|
|
|
return switch (err) {
|
|
|
|
posix.EBADF => error.BadFd,
|
|
|
|
posix.EINVAL => error.Unseekable,
|
|
|
|
posix.EOVERFLOW => error.Unseekable,
|
|
|
|
posix.ESPIPE => error.Unseekable,
|
|
|
|
posix.ENXIO => error.Unseekable,
|
|
|
|
else => os.unexpectedErrorPosix(err),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Os.windows => {
|
|
|
|
const ipos = try math.cast(isize, pos);
|
|
|
|
if (windows.SetFilePointerEx(self.handle, ipos, null, windows.FILE_BEGIN) == 0) {
|
|
|
|
const err = windows.GetLastError();
|
|
|
|
return switch (err) {
|
|
|
|
windows.ERROR.INVALID_PARAMETER => error.BadFd,
|
|
|
|
else => os.unexpectedErrorWindows(err),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
else => @compileError("unsupported OS: " ++ @tagName(builtin.os)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn getPos(self: *File) !usize {
|
2018-02-10 17:55:13 -08:00
|
|
|
switch (builtin.os) {
|
|
|
|
Os.linux, Os.macosx, Os.ios => {
|
|
|
|
const result = posix.lseek(self.handle, 0, posix.SEEK_CUR);
|
|
|
|
const err = posix.getErrno(result);
|
|
|
|
if (err > 0) {
|
|
|
|
return switch (err) {
|
|
|
|
posix.EBADF => error.BadFd,
|
|
|
|
posix.EINVAL => error.Unseekable,
|
|
|
|
posix.EOVERFLOW => error.Unseekable,
|
|
|
|
posix.ESPIPE => error.Unseekable,
|
|
|
|
posix.ENXIO => error.Unseekable,
|
|
|
|
else => os.unexpectedErrorPosix(err),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
Os.windows => {
|
2018-05-28 17:23:55 -07:00
|
|
|
var pos: windows.LARGE_INTEGER = undefined;
|
2018-05-31 07:56:59 -07:00
|
|
|
if (windows.SetFilePointerEx(self.handle, 0, *pos, windows.FILE_CURRENT) == 0) {
|
2018-02-10 17:55:13 -08:00
|
|
|
const err = windows.GetLastError();
|
|
|
|
return switch (err) {
|
|
|
|
windows.ERROR.INVALID_PARAMETER => error.BadFd,
|
|
|
|
else => os.unexpectedErrorWindows(err),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(pos >= 0);
|
|
|
|
if (@sizeOf(@typeOf(pos)) > @sizeOf(usize)) {
|
|
|
|
if (pos > @maxValue(usize)) {
|
|
|
|
return error.FilePosLargerThanPointerRange;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return usize(pos);
|
|
|
|
},
|
|
|
|
else => @compileError("unsupported OS"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn getEndPos(self: *File) !usize {
|
2018-02-10 17:55:13 -08:00
|
|
|
if (is_posix) {
|
2018-06-16 14:01:23 -07:00
|
|
|
const stat = try os.posixFStat(self.handle);
|
2018-02-10 17:55:13 -08:00
|
|
|
return usize(stat.size);
|
|
|
|
} else if (is_windows) {
|
|
|
|
var file_size: windows.LARGE_INTEGER = undefined;
|
|
|
|
if (windows.GetFileSizeEx(self.handle, &file_size) == 0) {
|
|
|
|
const err = windows.GetLastError();
|
|
|
|
return switch (err) {
|
|
|
|
else => os.unexpectedErrorWindows(err),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (file_size < 0)
|
|
|
|
return error.Overflow;
|
|
|
|
return math.cast(usize, u64(file_size));
|
|
|
|
} else {
|
|
|
|
@compileError("TODO support getEndPos on this OS");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-28 17:23:55 -07:00
|
|
|
pub const ModeError = error{
|
2018-02-10 17:55:13 -08:00
|
|
|
BadFd,
|
|
|
|
SystemResources,
|
|
|
|
Unexpected,
|
|
|
|
};
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn mode(self: *File) ModeError!os.FileMode {
|
2018-02-10 17:55:13 -08:00
|
|
|
if (is_posix) {
|
|
|
|
var stat: posix.Stat = undefined;
|
|
|
|
const err = posix.getErrno(posix.fstat(self.handle, &stat));
|
|
|
|
if (err > 0) {
|
|
|
|
return switch (err) {
|
|
|
|
posix.EBADF => error.BadFd,
|
|
|
|
posix.ENOMEM => error.SystemResources,
|
|
|
|
else => os.unexpectedErrorPosix(err),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-04-12 07:38:32 -07:00
|
|
|
// TODO: we should be able to cast u16 to ModeError!u32, making this
|
|
|
|
// explicit cast not necessary
|
|
|
|
return os.FileMode(stat.mode);
|
2018-02-10 17:55:13 -08:00
|
|
|
} else if (is_windows) {
|
|
|
|
return {};
|
|
|
|
} else {
|
|
|
|
@compileError("TODO support file mode on this OS");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-28 17:23:55 -07:00
|
|
|
pub const ReadError = error{};
|
2018-02-10 17:55:13 -08:00
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn read(self: *File, buffer: []u8) !usize {
|
2018-02-10 17:55:13 -08:00
|
|
|
if (is_posix) {
|
|
|
|
var index: usize = 0;
|
|
|
|
while (index < buffer.len) {
|
2018-06-03 22:09:15 -07:00
|
|
|
const amt_read = posix.read(self.handle, buffer.ptr + index, buffer.len - index);
|
2018-02-10 17:55:13 -08:00
|
|
|
const read_err = posix.getErrno(amt_read);
|
|
|
|
if (read_err > 0) {
|
|
|
|
switch (read_err) {
|
2018-05-28 17:23:55 -07:00
|
|
|
posix.EINTR => continue,
|
2018-02-10 17:55:13 -08:00
|
|
|
posix.EINVAL => unreachable,
|
|
|
|
posix.EFAULT => unreachable,
|
2018-05-28 17:23:55 -07:00
|
|
|
posix.EBADF => return error.BadFd,
|
|
|
|
posix.EIO => return error.Io,
|
|
|
|
else => return os.unexpectedErrorPosix(read_err),
|
2018-02-10 17:55:13 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (amt_read == 0) return index;
|
|
|
|
index += amt_read;
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
} else if (is_windows) {
|
|
|
|
var index: usize = 0;
|
|
|
|
while (index < buffer.len) {
|
|
|
|
const want_read_count = windows.DWORD(math.min(windows.DWORD(@maxValue(windows.DWORD)), buffer.len - index));
|
|
|
|
var amt_read: windows.DWORD = undefined;
|
2018-06-05 15:03:21 -07:00
|
|
|
if (windows.ReadFile(self.handle, @ptrCast(*c_void, buffer.ptr + index), want_read_count, &amt_read, null) == 0) {
|
2018-02-10 17:55:13 -08:00
|
|
|
const err = windows.GetLastError();
|
|
|
|
return switch (err) {
|
|
|
|
windows.ERROR.OPERATION_ABORTED => continue,
|
|
|
|
windows.ERROR.BROKEN_PIPE => return index,
|
|
|
|
else => os.unexpectedErrorWindows(err),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (amt_read == 0) return index;
|
|
|
|
index += amt_read;
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
} else {
|
|
|
|
unreachable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const WriteError = os.WindowsWriteError || os.PosixWriteError;
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn write(self: *File, bytes: []const u8) WriteError!void {
|
2018-02-10 17:55:13 -08:00
|
|
|
if (is_posix) {
|
|
|
|
try os.posixWrite(self.handle, bytes);
|
|
|
|
} else if (is_windows) {
|
|
|
|
try os.windowsWrite(self.handle, bytes);
|
|
|
|
} else {
|
|
|
|
@compileError("Unsupported OS");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|