Search code examples
cmemorymemcpy

`memcpy` doesn't copy all bytes


I'm trying to copy bytes of a struct to a char* which act as a buffer. To do this I convert my struct(vdnxfs_node) to an array of char. This struct is 64 bytes long, while the buffer is 1024 bytes.
When doing memcpy, however, only the first 4 bytes of the struct are copied into the buffer.

File: 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;
    }
    free(buf);
    free(node_buf);
}

File: 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;

File: stat.c
Brief: Convert all bytes of a node to an array of chars

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;
}

File: disk.c
Brief: Write the content of _buf on a file. The content wrote on disk is always 1024 bytes long as defined in VDNXFS_BUFFER_SZ. _off is the offset inside the block.

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) returns 64, meaning that is not padded. I really think that the problem is not in the actual memcpy but in my vdnxfs_write_disk(...) function, even though I already tested by writing both null char and random characters, and it worked as expected. In my test I wrote 1024 a characters onto the file, and it worked. So, either there's a problem of mine when I try to memcpy node_buf into buf or I am missing something.

Note: If I print node_buf char by char I get this result:

φ                                           ROOT_DIR

However, when analyzing the file with an hex-editor, the first 4 bytes of the block are:

ED 01 01 00

The rest are all 00

It's all store in little-endian mode. 01ED is indeed node->st_mode. I'm sure it is right because I'm using linux permissions, and 755 is 1ED in hex.


Solution

  • After a while I noticed that I was passing to vdnxfs_write_disk the wrong buffer size(I was passing VDXNFS_BUFFER_SZ instead of the size of the data I wanted to write). I was also passing to vdnxfs_write_disk the wrong offset and block. I was actually passing 1 for the block arguments and as fseek's offset is calculated as (block * VDNXFS_BUFFER_SZ) + off the offset of fseek was going over 1024 bytes which was wrong.

    If someone is interested, here is my final (fixed) result of mkfs:

    int mkfs(uint8_t _dev, uint8_t _mode)
    {
        char* buf = (char*)malloc(sizeof(size_t));
        char* signature_buf = vdnxfs_read_disk(_dev, 0, 4, 4);
        memcpy(buf, signature_buf, sizeof(size_t));
        free(signature_buf);
    
        // Signing the disk with the appopriate signature.
        size_t signature = _mode == vdnxfs_system ? VDNXFS_BDSK_SIGNATURE : VDNXFS_DDSK_SIGNATURE;
        // printf("Signature: %X\n", signature);
    
        for(int i = 0; i < 4; i++)
        {
            // Disk signature starts at 4th byte
            buf[i] = ((signature >> (8 * i)) & 0xff);
        }
    
        // Updating the content to disk
        if(!vdnxfs_write_disk(_dev, 0, 4, buf, 4))
        {
            printf("Couldn't sign disk `sda%d`.", _dev);
            return -1;
        }
    
        // Getting the Super Block based on the mode.
        // Boot/System FS stores the superblock from bytes 1024 to 2047
        // Data FS stores the superblock from bytes 512 to 1535
        size_t block = 0;
        size_t off = _mode == vdnxfs_system ? VDNXFS_BFS_SBLK_START : VDNXFS_DFS_SBLK_START;
    
        vdnxfs_node* node = (vdnxfs_node*)calloc(1, 64);
        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");
        // Updating the superblock to disk
        if(!vdnxfs_write_disk(_dev, block, off, encode_dnxfs_node(node), 64))
        {
            printf("Couldn't write superblock to disk `sda%d`.", _dev);
            free(node);
            free(buf);
            return -1;
        }
    
        free(node);
        free(buf);
    }