OpenOption show have a method to create full path

when using OpenOption to create a new file in a directory that doesn't exist, it makes Not Found Error! now existence checking and make new path is required.

there should be a way to do that with OpenOption.

my suggestion is to define a create_full_path() beside create() to prevent create unnecessary paths by mistake.

If you know you want the directory to exist, and you want to create it if it doesn't, you can just unconditionally call std::fs::create_dir_all on the directory.

Are you looking for the logic of "create file, if it fails with ENOENT then create_dir_all and try again"? That would save one syscall in the case where the directory exists, and cost one syscall in the case where the directory doesn't exist. In some cases that may be the right performance tradeoff.

I don't know if it makes sense to make that part of OpenOptions, though. OpenOptions seems consistently about opening/creating a file, and doesn't fold in other associated operations. I think it would make more sense for this to be a separate function.

3 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.