2

I'm porting very old project to c++17 (atomics, mutexes, UTF conversions, etc), and faced with CreateFile parameters problem.

Let's say

::CreateFileA(newFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)

Is there 100% equivalent code using std::filesystem permissions and other stuff? So far I can't see something similar to FILE_SHARE_READ or GENERIC_WRITE.

UP: We need this for Linux port.

yudjin
  • 349
  • 5
  • 14
  • 3
    The functions and classes in [the `std::filesystem` namespace](https://en.cppreference.com/w/cpp/filesystem) doesn't really deal with files like that, reading and writing them. That's what you use the file streams for (e.g. `std::ifstream` for input-only files, or `std::ofstream` for output-only files). Besides the linked `std::filesystem` reference, and that whole site, I suggest you get [a few good books about C++](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to read. – Some programmer dude Jun 15 '18 at 05:54
  • 2
    For file access I've never used the C++ infrastructure. Too many features exposed by the Windows API have no equivalent in C++ (e.g. `FILE_FLAG_DELETE_ON_CLOSE`, ACL's, ADS', asynchronous I/O, sparse files, etc.). An RAII wrapper for the file `HANDLE` is as far as I'd go in terms of moving file access to C++. – IInspectable Jun 15 '18 at 08:00
  • @Someprogrammerdude, it deals. Look at [std::filesystem::perms](http://en.cppreference.com/w/cpp/filesystem/perms). I know, that I have to create file using std::stream, but [std::filesystem](http://en.cppreference.com/w/cpp/filesystem) has options for permissions and properties – yudjin Jun 15 '18 at 10:12
  • `GENERIC_WRITE` is the open-mode, not a permission, and as such there's no equivalent in `std::filesystem`. Its equivalence is the modes in [`std::ios_base::openmode`](http://en.cppreference.com/w/cpp/io/ios_base/openmode). With the standard library you can't set file permissions when opening a file-stream. you have to do it in two separate actions. – Some programmer dude Jun 15 '18 at 10:17
  • @Someprogrammerdude - `GENERIC_WRITE` this is not open mode, but exactly permission. it mapped to concrete object type permission. in case file - it mapped to `FILE_GENERIC_WRITE` – RbMm Jun 15 '18 at 10:31
  • @RbMm I think you better [read the documentation](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx). The `dwDesiredAccess` argument is `GENERIC_READ`, `GENERIC_WRITE` or a bitwise OR of both. It's the open-mode, saying what your program want to do with the file, reading from the file or writing to it. – Some programmer dude Jun 15 '18 at 10:50
  • @Someprogrammerdude - no, what is "open mode" ?! this is exactly access. it nothing say about what your program want to do with the file. this is permission – RbMm Jun 15 '18 at 10:57
  • @Someprogrammerdude and *The dwDesiredAccess argument is GENERIC_READ, GENERIC_WRITE or a bitwise OR of both* - this is possible values. but we can use and any [`another`](https://msdn.microsoft.com/en-us/library/windows/desktop/gg258116(v=vs.85).aspx). – RbMm Jun 15 '18 at 11:01
  • 1
    @RbMm Okay, then show me a program that passes `GENERIC_WRITE` as `dwDesiredAccess` and then tries to *read* from the file. `GENERIC_WRITE` is analogous to the [open-mode](http://en.cppreference.com/w/cpp/io/ios_base/openmode) `out`, while `GENERIC_READ` is analogous to `in`. Or the old C [`fopen`](http://en.cppreference.com/w/c/io/fopen) modes `"w"` and `"r"`, respectively. It may be *checked* against the filesystem permisions, but it's not otherwise related to them. – Some programmer dude Jun 15 '18 at 11:01
  • @Someprogrammerdude - i speak about windows api. no any "open-mode" here conception. exist `ACCESS_MASK` value that determines the requested access to the object. we say to windows which access we request for object. which we will be do (if any) with file - this is absolute another question. if program not request `FILE_READ_DATA` but try read from file - it got access denied. but so what ? – RbMm Jun 15 '18 at 11:05
  • For the sharing flags like `FILE_SHARE_READ` you can use the 3rd constructor parameter of the standard file streams, which is a Microsoft extension. See [this answer](https://stackoverflow.com/a/25178473/7571258). You could derive from the standard stream classes to abstract the Windows specifics away for your Linux port. Another option, if you need even more control: Use [Boost.Iostreams](https://www.boost.org/doc/libs/release/libs/iostreams/doc/index.html). – zett42 Jun 15 '18 at 12:13
  • Thank you, @zett42! – yudjin Jun 21 '18 at 06:38

0 Answers0