From fa5312283489ddac85c3e390ef961af674bcbce7 Mon Sep 17 00:00:00 2001 From: FactorialN <8838579+FactorialN@users.noreply.github.com> Date: Fri, 1 Dec 2023 14:52:14 -0800 Subject: [PATCH] fixed an issue when reading and writing binary files --- lib/files.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/files.cpp b/lib/files.cpp index 9f8ca84..4805094 100644 --- a/lib/files.cpp +++ b/lib/files.cpp @@ -573,7 +573,10 @@ int FilesOperation::fischl_write(const char *path, const char *buf, size_t size, inode.inode_num = fi->fh; fs->inode_manager->load_inode(&inode); //size_t len = (inode.metadata.size/IO_BLOCK_SIZE) * IO_BLOCK_SIZE; // Assuming each block is 4096 bytes - char *buffer = strdup(buf); + // Determine the length of the buffer + // Allocate memory for the new buffer + char* buffer = (char*)malloc(size); + memcpy(buffer, buf, size); size_t bytes_write = fs->write(&inode, buffer, size, offset); /*size_t block_index = offset / IO_BLOCK_SIZE; // Starting block index size_t block_offset = offset % IO_BLOCK_SIZE; // Offset within the first block @@ -588,6 +591,7 @@ int FilesOperation::fischl_write(const char *path, const char *buf, size_t size, block_offset = 0; // Only the first block might have a non-zero offset }*/ fs->inode_manager->save_inode(&inode); + free(buffer); return bytes_write; // Return the actual number of bytes read }