Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std.os.fsync, File.sync hit unreachable EINVAL for special files #17950

Open
nissaofthesea opened this issue Nov 10, 2023 · 1 comment
Open
Labels
bug Observed behavior contradicts documented or intended behavior standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@nissaofthesea
Copy link

Zig Version

0.11.0

Steps to Reproduce and Observed Behavior

On x86_64 Linux:

const std = @import("std");

pub fn main() !void {
    const arbitrary_user_input = "/dev/null";
    const file = try std.fs.cwd().openFile(arbitrary_user_input, .{ .mode = .write_only });
    defer file.close();
    try file.writeAll("poetry machine");
    try std.os.fsync(file.handle); // fsync returns EINVAL and std encounters unreachable
}

Expected Behavior

(Some discussion about this in general at #10776)

Currently, std.os.fsync enters unreachable code if fsync returns EINVAL. This means for files like /dev/null, it panics.

It seems reasonable to me that an arbitrary file may be opened (eg. from command line arguments), and that this file could be special. It also seems reasonable that the program may try to sync writes to ensure that data is correctly written to the file.
However, these are currently incompatible without somehow checking that the file is special, and I don't know of a function to check this in std (please correct me if there is one).
I think that returning the error would be more elegant than having this explicit check before the call.

@nissaofthesea nissaofthesea added the bug Observed behavior contradicts documented or intended behavior label Nov 10, 2023
@Vexu Vexu added the standard library This issue involves writing Zig code for the standard library. label Nov 10, 2023
@Vexu Vexu added this to the 0.14.0 milestone Nov 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

No branches or pull requests

3 participants