c - 如何在 C 中授予完整文件权限

标签 c file file-permissions

我正在使用帖子 592448 中显示的代码示例尝试授予完整文件权限。当我使用以下代码编译代码片段时:

 gcc -shared -mno-cygwin -Wall -o native.dll native.c

我得到以下错误:

native.c:8: error: conflicting types for 'mode_t'
/usr/i686-pc-mingw32/sys-root/mingw/include/sys/types.h:99: error: previous declaration of 'mode_t' was here
native.c:21: error: parse error before numeric constant
native.c:22: error: parse error before numeric constant
native.c:23: error: parse error before numeric constant
native.c:25: error: parse error before "mode_t"
native.c:26: error: parse error before "mode_t"
native.c:28: error: parse error before "mode_t"
native.c:29: error: parse error before "mode_t"

我剥离了代码以减少到下面,编译正常但似乎没有根据需要更改文件权限。

#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>

#ifdef _WIN32
#   include <io.h>

typedef signed int md_t;
static const md_t MS_MODE_MASK = 0x0000ffff;           ///< low word

int fchmod(const char * path, md_t mode)
{
    int result = _chmod(path, (mode & MS_MODE_MASK));

    if (result != 0)
    {
        result = errno;
    }

    return (result);
}
#else
int fchmod(const char * path, md_t mode)
{
    int result = chmod(path, mode);

    if (result != 0)
    {
        result = errno;
    }

    return (result);
}
#endif

关于如何让它工作的任何指示?

最佳答案

请注意,在 Windows 上,所有这一切都可以将文件设置为只读或不设置,Windows 文件权限与 UNIX 类型文件权限不同。

如果这就是您想要做的全部:它以什么方式不起作用?

编辑:关于您在别处定义了 mode_t 的初始错误:/usr/i686-pc-mingw32/sys-root/mingw/include/sys/types.h:99 并尝试将其重新定义为 typedef int mode_t;

来自 MSDN :

If write permission is not given, the file is read-only. Note that all files are always readable; it is not possible to give write-only permission. Thus the modes _S_IWRITE and _S_IREAD | _S_IWRITE are equivalent.

关于c - 如何在 C 中授予完整文件权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10446599/

相关文章:

c# - 在 C# 中使用 FileSystemWatcher 确定复制/写入进度

eclipse - 无法打开 Eclipse(配置区域.. 错误)

c - 如何解决球体的循环性?

c - 声明隐藏了 c 中的局部变量

c - Ruby C 扩展,使用 Ruby 数据类型还是 native C 来提高性能?

python - 如何从python中的文件中获取列表

linux - Perl 中 "open function"和 "touch command"的区别?

linux - 如何以编程方式防止 Linux 文件系统上的文件在有限时间内被编辑

java - websphere 的 OS 用户创建文件夹和写入文件需要什么 Linux 权限?

c++ - 将 4 个套接字字节转换为一个 int