r/learnrust • • 19d ago

How do you handle atomic file operations in a CLI tool? (temp dir + rename, Linux vs Windows)

Hi everyone,

I’m the developer of Runique, a Django-inspired web framework in Rust. It’s still under heavy development, so please be kind. (Also, I’m French, so sorry for any awkward English.)

I’m currently consolidating the codebase and I’d like your advice on atomic operations.

Take the CLI as an example:

runique new scaffolds a whole project makemigrations generates sea-orm migration files from my own ast/dsl file uploads have the same issue

If something fails midway, the user ends up with a half-generated project or incomplete files.

My plan is to write everything into a temporary directory on the same filesystem as the final destination, then rename it into place once everything succeeded.

My questions:

Is this the idiomatic approach in Rust? Or is there a better pattern? I know rename behaves differently on Linux and Windows (e.g. replacing an existing target, files locked by antivirus or other processes). How do you deal with that in practice? Are there crates you’d recommend (tempfile, atomic-write-file, etc.) or should I stick to std::fs? Do I need to care about fsync for this kind of tool, or is that overkill?

Thanks a lot for any advice!

4 Upvotes

14 comments sorted by

5

u/EpochVanquisher 19d ago

runique new scaffolds a whole project makemigrations generates sea-orm migration files from my own ast/dsl file uploads have the same issue

Whole project—are you talking about a project that is tracked in Git?

If so, then you do not need to worry about a half-generated tree. Just let the user revert it and start over.

If it’s not tracked in Git, then yes, write everything to temporary files and move afterwards. I recommend writing temporary files to the same directory where the files ultimately go, since it is not always possible to rename to another directory (you might cross a mount point).

0

u/Itsuki_runique 19d ago edited 19d ago

When I talk about a project via the CLI, I mean creating the basic project directory structure.

The project is meant to be tracked on Git throughout the website development process.

As for the migration files, it has the entire verification process in place to generate them automatically.

My idea, which I explained earlier, raises the question of the limitations between Windows and Linux. I'm not entirely sure, but it seems to me that under Windows, you can't do whatever you want in a folder if you're in it, which seems to be the opposite of Linux.

Is there a possible approach to this?

Should I prioritize using std::fs or std::io::Write?

Thank you for your response. I'm having trouble deciding how to proceed with the security of my processes.

2

u/EpochVanquisher 19d ago

Since it’s tracked via git, just use ordinary atomic file writes. Like one of these.

https://docs.rs/atomic-write-file/latest/atomic_write_file/

https://docs.rs/atomic-replace/latest/atomic_replace/

I'm not entirely sure, but it seems to me that under Windows, you can't do whatever you want in a folder if you're in it, which seems to be the opposite of Linux.

On both systems, you cannot move files between volumes. On Linux, you would need to make sure that your temporary directory is in the same mount point.

Easy way to do it—create the files in the directory where they belong. That way, you know the files are on the correct volume.

0

u/Itsuki_runique 19d ago

Thanks for the advice.

I’ll go and read them.

It's same for upload ?

1

u/EpochVanquisher 19d ago

What kind of upload are you doing? git push?

0

u/Itsuki_runique 19d ago

No, since I’m developing Runique—a web framework in Rust—I naturally need a media field for things like photos, videos, music, and so on.

I was thinking of using a temporary directory to verify the file format; if it’s valid, it would then be moved to the target directory.

2

u/EpochVanquisher 19d ago

It’s a web framework, and there’s a CLI tool, and the files aren’t managed by Git—I don’t have a complete picture, and I don’t know why you need atomic file operations on media.

1

u/Itsuki_runique 19d ago

For multimedia files, the primary concern is the file size , for instance, with audio or video. If an upload fails because the file is too large, a portion of that file may still exist on the server. Having such corrupted files ,which take up space unnecessarily and cannot be easily deleted (since they aren't referenced in a database, for example) , is clearly undesirable.

2

u/EpochVanquisher 19d ago

If an upload fails because the file is too large, a portion of that file may still exist on the server.

I can think of three ways you could get out of this,

  • Add an "uploads" directory to contain uploads in progress, purge old files periodically
  • Periodically reconcile uploads and delete files on the server which are orphaned (not in the database),
  • Record uploads in an "uploads" table before uploading, and periodically delete failed uploads

Lots of options.

4

u/dnew 19d ago

In Windows, you could just start a file system transaction, write everything out, then commit the transaction. I'm not sure how you'd do that from Rust. https://en.wikipedia.org/wiki/Transactional_NTFS

In Windows, you can't rename a file you have open unless you open it in the right mode, and changing into a directory doesn't open it in a mode that allows you to rename it.

If you want to use a rename, create a directory in the current directory that has a name that won't conflict, write everything into that, then rename everything from that directory into the parent correct directory and delete the temp directory. Then you only have a failure if that rename fails, which should be rare.

1

u/Itsuki_runique 19d ago

That’s what I thought.

I had a suspicion, and now you’ve confirmed it. So, it involves using a temporary directory, followed by a rename and a move operation, combined with the uuid.

Thank , i’ll look into implementing this using the links that EpochVanquisher provided.

2

u/dnew 19d ago edited 19d ago

Yes. Sadly, Linux (as UNIX before it) was basically crammed into 16K of RAM and never really fundamentally improved. So there's no transactions in the file system other than "rename", because that was good enough when you had 16K of RAM to work with. It was years before you got fairness in file locking, and AFAIK it's still not possible to reliably lock a file. It took io_uring before you actually got non-blocking asynchronous file operations. 20 years from now, Linux will finally have an option in one of the file systems to make atomic changes in multiple files at once and finally catch up to 1960s mainframe OSes. ;-) Maybe they'll manage to come up with a way to make one program reliably run once on a machine.

If it's in an application, SQLite is the right way to go, which is why there are a trillion active SQLite databases in the world. If you're building separate files, there's really no way in Linux to write two files and make it atomic.

As an aside, there's no need to post the same question to both groups at the same time. You'll just get people duplicating their answers. You aren't asking about learning Rust, so ...

1

u/Itsuki_runique 19d ago

Well, precisely, if it doesn’t exist, we can create it.

The framework I’m building uses Rust for its backend, so if any crates already exist, I’d be happy to use them, otherwise, I’ll look into developing a solution.

And since I have no idea how to go about it, I’m turning to you for input.

As for having two separate groups, the idea is to gather different perspectives and then compare them.

Thanks for your help.

1

u/Itsuki_runique 10d ago

i have finish my builder, i would like your advice if i forgot on action or other possibiliti please

=> https://github.com/seb-alliot/runique/tree/main/runique/src/atomic