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

please add an option to File.writeAsBytes to avoid overwriting. #59587

Open
stephane-archer opened this issue Nov 22, 2024 · 6 comments
Open
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-io type-enhancement A request for a change that isn't a bug

Comments

@stephane-archer
Copy link

stephane-archer commented Nov 22, 2024

var outputFile = File("a.jpg");
if (!outputFile.exist()) {
    // another program creates a.jpg
    await outputFile.writeAsBytes(jpegData); // boom overwrite!!!
}

please add an option to File.writeAsBytes to avoid overwriting.

maybe something like await outputFile.writeAsBytes(jpegData, exclusive: true);

@dart-github-bot
Copy link
Collaborator

Summary: User requests File.writeAsBytes option to prevent overwriting existing files, avoiding data loss in concurrent scenarios. Proposed solution: exclusive flag.

@dart-github-bot dart-github-bot added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-enhancement A request for a change that isn't a bug labels Nov 22, 2024
@julemand101
Copy link
Contributor

What behavior do you expect in case the file already exists? Throw exception?

@stephane-archer
Copy link
Author

@julemand101 yes

@lrhn lrhn added area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-io and removed area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. labels Nov 22, 2024
@aam
Copy link
Contributor

aam commented Nov 22, 2024

Have you looked at https://api.dart.dev/dart-io/File/create.html which allows you to specify exclusive: true to ensure new file is created, get PathExistsException if file already exists?

@stephane-archer
Copy link
Author

var file = await File("a.txt").create(recursive : false,
exclusive: true,
});
// someone write to a.txt
file.white(data); // overwrite!

From my understanding create guarantee you to create the file, but from my understanding File is not a file descriptor so now that the file is created overwrite can still happens

@mraleph
Copy link
Member

mraleph commented Nov 23, 2024

@stephane-archer AFAIK there is no way to prevent that in POSIX though - if the other program does not take precautions (e.g. does not use O_EXCL with O_CREAT) then you can't stop it from writing something into the same file you have opened.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-io type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

6 participants