将st_mode(来自stat)存储在文件中,并在chmod中重用 [英] Store st_mode (from stat) in a file and reuse it in chmod

查看:88
本文介绍了将st_mode(来自stat)存储在文件中,并在chmod中重用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为此感到疯狂.我正在尝试编写一个可以存储和提取文件的档案库.存档文件如下所示:

i'm going crazy for this. I'm trying to write an archive library which can store and extract files. An archive file looks like this:

<0,/home/user/file.txt,154,0755>
file contents (154 byte)

每个文件都由一个标头(< ...>)标识,带有四个标签"(用逗号分隔),文件类型(0表示文件,1表示目录),路径,以字节为单位的大小,权限(八进制).我通过stat系统调用检索大小和权限(我在Linux上).我的问题是我必须将八进制值从st_mode转换为字符串,将其存储在存档文件中(标头中的第四个标签),然后将其提取并与chmod syscall一起使用.

Each file is identified by an header ( <...> ), with four "tags" (separated by commas), file type (0 for file and 1 for directory), path, size in bytes, permissions (in octal). I retrieve the size and the permissions with the stat system call (i'm on Linux). My problem is that i have to convert the octal value from st_mode to a string, store it in the archive file (the fourth tag in the header),then extract it and use it with the chmod syscall.

要将其转换为字符串,我使用:

To convert it to string i use:

char mode[6];
sprintf (mode, "%o", statr.st_mode);

并使用atoi对其进行检索,但是它似乎不起作用.例如,存储在第4个标记中的值为100644,但是chmod将权限设置为错误(我的用户无法读取该文件).

and to retrieve it i use atoi, but it does not seem to work. For example, the value stored in the 4th tag is 100644, but chmod set the permissions wrong (file not readable by my user).

我不确定我是否解释得很好,是否会在需要时发布整个代码(但它不认为存在实现问题,只是从八进制转换为字符串之间的问题)

I'm not sure if i explained well, i wil post the whole code if is needed (but it don't think there are implementation problem, is just a problem between conversion from octal to string)

已解决!实际上strtol方法有效,但我也忘记将其应用于目录(因此,由于错误的文件夹的权限掩码,提取包含文件的目录会导致Segfault).谢谢大家的帮助!

Solved! Actually the strtol method worked, but i had forgotten to apply it to the directories too (so extracting a directory with files inside caused Segfault because of the bad folder's permission mask). Thanks everybody for help!

推荐答案

据我所知,没有Unix标准函数可以做到这一点.

As far as I know, there's not a unix standard function to do this.

您很可能必须屏蔽 st_mode 来获取所需的每个权限,并将其转换为对您有意义的内容.

You will most likely have to mask st_mode to get each permissions bit that you want and translate into something that makes sense to you.

相关帖子

也许有一个更简单的方法可以执行此操作,但是我提供了一个简单的示例,该示例演示了如何获取您感兴趣的每一点.它确实可以编译并且可以按预期运行.

There's maybe an easier way to do this, but I've including a simple example which shows how to get each bit that you are interested in. This does compile and seemed to run as expected.

st_mode.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>


#define SET_UID_BIT     0x4000
#define SET_GID_BIT     0x2000
#define STICKY_BIT      0x1000

// Readable USER bits defined here
#define USR_READ_BIT    0x400
#define USR_WRITE_BIT   0x200
#define USR_EXEC_BIT    0x100

// Readable GROUP bits defined here
#define GRP_READ_BIT    0x040
#define GRP_WRITE_BIT   0x020
#define GRP_EXEC_BIT    0x010

// Readable OTHER bits defined here
#define OTH_READ_BIT    0x004
#define OTH_WRITE_BIT   0x002
#define OTH_EXEC_BIT    0x001


int main()
{
    // The file I'm opening
    const char fp[] = "myfile.txt"; // -rw-rw-r--
    struct stat stats;

    // Get the stats
    if ( 0 == stat( fp, &stats ) )
    {
        // store st_mode
        mode_t mode = stats.st_mode;
        // init printable_mode
        unsigned int printable_mode = 0;        


        // Test for each permission bit in mode.
        // If the bit is present, mark the corresponding bit in printable_mode
        // using defined bits above.
        if( mode & S_ISUID )
        printable_mode |= SET_UID_BIT;

        if( mode & S_ISGID )
        printable_mode |= SET_GID_BIT;

        if( mode & S_ISVTX )
        printable_mode |= STICKY_BIT;

        if( mode & S_IRUSR )
        printable_mode |= USR_READ_BIT;

        if( mode & S_IWUSR )
        printable_mode |= USR_WRITE_BIT;

        if( mode & S_IXUSR )
        printable_mode |= USR_EXEC_BIT;

        if( mode & S_IRGRP )
        printable_mode |= GRP_READ_BIT;

        if( mode & S_IWGRP )
        printable_mode |= GRP_WRITE_BIT;

        if( mode & S_IXGRP )
        printable_mode |= GRP_EXEC_BIT;

        if( mode & S_IROTH )
        printable_mode |= OTH_READ_BIT;

        if( mode & S_IWOTH )
        printable_mode |= OTH_WRITE_BIT;

        if( mode & S_IXOTH )
        printable_mode |= OTH_EXEC_BIT;

        // Let's see what it looks like....
        printf("%04x\n", printable_mode);
        printf("%x\n", mode);
    }

    return 0;
}

这篇关于将st_mode(来自stat)存储在文件中,并在chmod中重用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆