dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.28k stars 1.58k forks source link

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

Open stephane-archer opened 12 hours ago

stephane-archer commented 12 hours ago
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 commented 12 hours ago

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

julemand101 commented 11 hours ago

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

stephane-archer commented 8 hours ago

@julemand101 yes

aam commented 1 hour ago

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?