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 · 9 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.

@stephane-archer
Copy link
Author

@mraleph there is no way to ask for exclusive access until the file is closed on POSIX?

@mraleph
Copy link
Member

mraleph commented Nov 23, 2024

@stephane-archer not to the best of my knowledge. there are file locks but they are advisory (so writers should all cooperate and use them - write operations themselves don't check locks). Mandatory locks API kinda sorta exists - but not really, see man fcntl, section "Mandatory locking".

@stephane-archer
Copy link
Author

Thanks for your answers

So the best we have right know in Dart to write data without overwrite is:

var file = await File("a.txt").create(recursive : false,
exclusive: true,
})
file.writeAsBytes(data);

I think it would be still nice to have a short version for this:

await File("a.txt").writeAsBytes(data, exclusive: true);
// or 

File("a.txt", exclusive: true).writeAsBytes(data);

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