我正在使用帖子 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/