我试图将结构的字节复制到充当缓冲区的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;
}
free(buf);
free(node_buf);
}
文件: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(...)
函数中,即使我已经通过编写空字符和随机字符进行了测试,并且可以按预期工作。在我的测试中,我在文件上写入了1024个a
字符,并且可以正常工作。
因此,当我尝试将memcpy
node_buf
插入buf
时出现我的问题,或者我丢失了一些东西。
注意:如果按字符打印node_buf
char,我将得到以下结果:
φ ROOT_DIR
但是,当使用十六进制编辑器分析文件时,该块的前4个字节为:
ED 01 01 00
其余全部为00
所有内容都以小端模式存储。 01ED
的确是node->st_mode
。我确定这是正确的,因为我使用的是Linux权限,并且755
是1ED
的十六进制。
答案 0 :(得分:0)
过了一会儿,我发现我传递给vdnxfs_write_disk
的缓冲区大小是错误的(我传递的是VDXNFS_BUFFER_SZ
而不是我想要写入的数据的大小)。我还将错误的偏移量和块传递给vdnxfs_write_disk
。
我实际上是为块参数传递1
,并且由于fseek
的偏移量计算为(block * VDNXFS_BUFFER_SZ) + off
,因此fseek
的偏移量超过了1024 bytes
,这是错误的
如果有人感兴趣,这是我对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);
}