`mcmcpy不会复制所有字节

问题描述 投票:0回答:1

我正在尝试将结构的字节复制到充当缓冲区的char *。为此,我将struct(vdnxfs_node)转换为char数组。该结构的长度为64个字节,而缓冲区为1024个字节。但是,当执行memcpy时,仅将结构的前4个字节复制到缓冲区中。

文件:fs.c

int mkfs(uint8_t _dev, uint8_t _mode)
{
    // VDNXFS_BUFFER_SZ = 1024
    char* buf = (char*)malloc(VDNXFS_BUFFER_SZ);

    // Other code non relevant to the question...

    // vdnxfs_init_new_node(); returns an `vdnxfs_node*` initialized
    // as `calloc(1, sizeof(vdnxfs_node));`
    vdnxfs_node* node = vdnxfs_init_new_node();
    node->st_mode = VDNXFS_ST_IRWXU | VDNXFS_ST_IRGRP | VDNXFS_ST_IXGRP | VDNXFS_ST_IROTH | VDNXFS_ST_IXOTH;
    node->st_uid = 1;
    node->st_gid = 1;
    node->st_pid = 0;
    node->st_size = 0;
    node->st_slnk = 1;
    node->st_atime = 0xffff;
    node->st_mtime = 0xffff;
    node->st_ctime = 0xffff;
    strcpy(node->st_name, "ROOT_DIR\0");

    // With this I get the array of bytes(char) of the struct
    char* node_buf = encode_dnxfs_node(node);

    // Updating the buffer
    memcpy(buf, node_buf, sizeof(vdnxfs_node));

    // Here I write the content of `buf` to a file.
    if(!vdnxfs_write_disk(_dev, block, off, buf))
    {
        printf("Couldn't write superblock to disk `sda%d`.", _dev);
        return -1;
    }
}

文件:stat.h

typedef struct __vdnxfs_node
{
    uint16_t st_mode;
    uint16_t st_uid;
    uint16_t st_gid;
    uint32_t st_pid; // Parent ID
    uint32_t st_size;
    uint8_t st_slnk; // Number of symbolic links
    time_t st_atime;
    time_t st_mtime;
    time_t st_ctime;
    char st_name[16];
}vdnxfs_node;

文件:stat.c简介:将节点的所有字节转换为字符数组

char* encode_dnxfs_node(vdnxfs_node* node)
{
    if(!node) // Node is null
    {
        return NULL;
    }
    char* buffer = (char*)malloc(sizeof(vdnxfs_node));

    if(!buffer)
    {
        printf("Failed to allocate buffer for encoding.");
        return NULL;
    }

    memcpy(buffer, node, sizeof(vdnxfs_node));

    return buffer;
}

文件:disk.c简介:将_buf的内容写入文件。磁盘上写入的内容始终为1024 bytes,如VDNXFS_BUFFER_SZ中所定义。 _off是块内的偏移量。

char* vdnxfs_write_disk(uint8_t _dev, size_t _block, size_t _off, char* _buf)
{
    char* fl_name = (char*)calloc(1, 7);
    strcpy(fl_name, vdnxfs_disks[_dev]->fl_name);

    FILE* fp_buf = fopen(fl_name, "r+b");

    if(!fp_buf)
    {
        printf("DISK DRIVER: Couldn't open Disk %s\n", fl_name);
        return NULL;
    }

    // If succesful
    if(!fseek(fp_buf, (_block * VDNXFS_BUFFER_SZ) + _off, SEEK_SET))
    {
        fputs(_buf, fp_buf);
    }else { return NULL; }

    fclose(fp_buf);

    return _buf;
}

sizeof(vdnxfs_node)返回64,表示未填充。我确实认为问题不在实际的memcpy中,而是在我的vdnxfs_write_disk(...)函数中,即使我已经通过编写null char和random字符进行了测试,并且可以按预期工作。在我的测试中,我在该文件上写入了1024个a字符,并且可以正常工作。因此,当我尝试将memcpy node_buf转换为buf时出现我的问题,或者我丢失了某些内容。

注意:如果按字符打印node_buf char,则会得到以下结果:

φ                                           ROOT_DIR

但是,当使用十六进制编辑器分析文件时,块的前4个字节为:

ED 01 01 00

其余都是00

全部以小端模式存储。 01ED的确是node->st_mode。我确定这是正确的,因为我使用的是Linux权限,并且755为十六进制的1ED

c memory memcpy
1个回答
-1
投票

如果sizeof(vdnxfs_node)返回值4,这就是原因。

© www.soinside.com 2019 - 2024. All rights reserved.