-1

On Linux I open a file with the following code:

auto file = ::open("file.dat", O_RDWR | O_CREAT);

but when I try to compile this code for Android (android-ndk-r26-beta2) I get the following compiler error:

error: 'open' called with O_CREAT or O_TMPFILE, but missing mode

what value should I pass to make it compile? Where is the documentation?

As far as I see it is bionic. What is its status?

Alexey Starinsky
  • 3,699
  • 3
  • 21
  • 57

2 Answers2

1

It should be something like:

auto file = ::open("file.dat", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);

see https://codeql.github.com/codeql-query-help/cpp/cpp-open-call-with-mode-argument/

Alexey Starinsky
  • 3,699
  • 3
  • 21
  • 57
1

If you use O_CREAT variant, this is because you want the file to be created if non existing yet. Thus, in the case of creation it is requested that the file have permission bits set to some value (read, write, execute, etc). This is documented in man page of open:

O_CREAT

[...]

The mode argument specifies the file mode bits to be applied when a new file is created. If neither O_CREAT nor O_TMPFILE is specified in flags, then mode is ignored (and can thus be specified as 0, or simply omitted). The mode argument must be supplied if O_CREAT or O_TMPFILE is specified in flags; if it is not supplied, some arbitrary bytes from the stack will be applied as the file mode.

The effective mode is modified by the process's umask in the usual way: in the absence of a default ACL, the mode of the created file is (mode & ~umask).

Note that mode applies only to future accesses of the newly created file; the open() call that creates a read- only file may well return a read/write file descriptor.

The following symbolic constants are provided for mode:

          S_IRWXU  00700 user (file owner) has read, write, and
                   execute permission

          S_IRUSR  00400 user has read permission

          S_IWUSR  00200 user has write permission

          S_IXUSR  00100 user has execute permission

          S_IRWXG  00070 group has read, write, and execute
                   permission

          S_IRGRP  00040 group has read permission

          S_IWGRP  00020 group has write permission

          S_IXGRP  00010 group has execute permission

          S_IRWXO  00007 others have read, write, and execute
                   permission

          S_IROTH  00004 others have read permission

          S_IWOTH  00002 others have write permission

          S_IXOTH  00001 others have execute permission

          According to POSIX, the effect when other bits are set in
          mode is unspecified.  On Linux, the following bits are
          also honored in mode:

          S_ISUID  0004000 set-user-ID bit

          S_ISGID  0002000 set-group-ID bit (see inode(7)).

          S_ISVTX  0001000 sticky bit (see inode(7)).

          [...]

You have to make a call like:

auto file = ::open("file.dat", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);

or something like that.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69