1

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

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

4

1 回答 1

1

请注意,在 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.

于 2012-05-04T11:05:48.670 回答