libs-team icon indicating copy to clipboard operation
libs-team copied to clipboard

fs::append

Open lolbinarycat opened this issue 9 months ago • 3 comments

Proposal

Problem statement

There is no concise way to write a simple "open and append to file" operation.

Motivating examples or use cases

  1. Placeholder logging implementation
  2. A command that runs multiple times, and each run appends a short string to a specific file, so the results can be collected and interpreted later.
  3. Providing an equivalent to >> when line-for-line translating a shell script into rust.

Solution sketch

// in std::fs

pub fn append<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> Result<()>

This would be a parallel to the existing fs::write and fs::read.

It would create the file if it does not exist.

Alternatives

Create File::open_append as a parallel to File::create and File::create_new.

Links and related work

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.

lolbinarycat avatar Mar 07 '25 19:03 lolbinarycat

Beware that appending a file in a loop like that is a Shlemiel the painter algorithm on Windows (and perhaps others) as the virus scanner will re-scan the whole file every time.

This should absolutely not be used as a "Placeholder logging implementation"; even in a placeholder it should keep the file open.

scottmcm avatar Mar 07 '25 19:03 scottmcm

It seems like it would still be fine on linux, no? this pattern is quite common in shell scripts.

lolbinarycat avatar Mar 07 '25 19:03 lolbinarycat

Alternatives

Create File::open_append as

#512 has already been approved

the8472 avatar Mar 07 '25 19:03 the8472

We discussed this in the libs-api meeting and decided to reject it in favor of #512.

Amanieu avatar Apr 01 '25 17:04 Amanieu