在 Linux(或 POSIX)中以只读方式重新打开文件
Re-open file read-only in Linux (or POSIX)
我在 read/write 模式下打开文件并进行一系列读取、写入和查找(来自用户输入)。
稍后我想将文件设置为只读以防止对其进行任何进一步的写入。
是否有 Linux(或 POSIX)函数可以做到这一点?也许有人 fcntl
打电话?
或者我唯一的选择是保存文件中的当前位置,关闭它并重新打开RD_ONLY
?
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int fd = open("/path/to/file", O_RDWR);
// mixture of:
write(fd, ...);
lseek(fd, ...);
read (fd, ...);
// etc
...
// make file read-only ???
read (fd, ...); // OK
lseek(fd, ...); // OK
write(fd, ...); // error
正如 POSIX 文档所说(重点是我的),至少通过调用 fcntl
是不可能的:
fcntl()
:
F_SETFL
Set the file status flags, defined in fcntl.h
, for the file
description associated with fildes from the corresponding bits in the
third argument, arg, taken as type int. Bits corresponding to the file
access mode and the file creation flags, as defined in fcntl.h
, that
are set in arg shall be ignored. If any bits in arg other than those
mentioned here are changed by the application, the result is
unspecified.
和
fcntl.h
O_ACCMODE Mask for file access modes.
The header shall define the
following symbolic constants for use as the file access modes for
open(), openat(), and fcntl(). The values shall be unique, except that
O_EXEC and O_SEARCH may have equal values. The values shall be
suitable for use in #if preprocessing directives.
O_EXEC Open for execute only (non-directory files). The result is
unspecified if this flag is applied to a directory.
O_RDONLY Open for
reading only.
O_RDWR Open for reading and writing.
O_SEARCH Open
directory for search only. The result is unspecified if this flag is
applied to a non-directory file.
O_WRONLY Open for writing only.
我在 read/write 模式下打开文件并进行一系列读取、写入和查找(来自用户输入)。
稍后我想将文件设置为只读以防止对其进行任何进一步的写入。
是否有 Linux(或 POSIX)函数可以做到这一点?也许有人 fcntl
打电话?
或者我唯一的选择是保存文件中的当前位置,关闭它并重新打开RD_ONLY
?
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int fd = open("/path/to/file", O_RDWR);
// mixture of:
write(fd, ...);
lseek(fd, ...);
read (fd, ...);
// etc
...
// make file read-only ???
read (fd, ...); // OK
lseek(fd, ...); // OK
write(fd, ...); // error
正如 POSIX 文档所说(重点是我的),至少通过调用 fcntl
是不可能的:
fcntl()
:F_SETFL
Set the file status flags, defined in
fcntl.h
, for the file description associated with fildes from the corresponding bits in the third argument, arg, taken as type int. Bits corresponding to the file access mode and the file creation flags, as defined infcntl.h
, that are set in arg shall be ignored. If any bits in arg other than those mentioned here are changed by the application, the result is unspecified.
和
fcntl.h
O_ACCMODE Mask for file access modes.
The header shall define the following symbolic constants for use as the file access modes for open(), openat(), and fcntl(). The values shall be unique, except that O_EXEC and O_SEARCH may have equal values. The values shall be suitable for use in #if preprocessing directives.
O_EXEC Open for execute only (non-directory files). The result is unspecified if this flag is applied to a directory.
O_RDONLY Open for reading only.
O_RDWR Open for reading and writing.
O_SEARCH Open directory for search only. The result is unspecified if this flag is applied to a non-directory file.
O_WRONLY Open for writing only.