Commit 6bafc25cb6b90b144a794c049e978318b760e5b7
1 parent
8ea36403
Added failure when chunk cannot be saved
Showing
1 changed file
with
9 additions
and
3 deletions
chunk.c
@@ -72,14 +72,20 @@ int overwriteChunk(const char* regionFolder, ChunkID chunk, void* chunkData, siz | @@ -72,14 +72,20 @@ int overwriteChunk(const char* regionFolder, ChunkID chunk, void* chunkData, siz | ||
72 | void* compressedChunk; | 72 | void* compressedChunk; |
73 | ssize_t compressedChunkLength = deflateGzip(chunkData,chunkLength,&compressedChunk,(header.compressionType == COMPRESSION_TYPE_ZLIB)); | 73 | ssize_t compressedChunkLength = deflateGzip(chunkData,chunkLength,&compressedChunk,(header.compressionType == COMPRESSION_TYPE_ZLIB)); |
74 | if(compressedChunkLength > totalChunkLength) { | 74 | if(compressedChunkLength > totalChunkLength) { |
75 | - // insert new 4096 blocks | 75 | + // Haven't determined if we can just allocate a new 4KiB sector for the chunk |
76 | + // To avoid corrupting the region, let's just make the function fail and retry on another chunk that has | ||
77 | + // free space at the end | ||
78 | + close(fd); | ||
79 | + free(compressedChunk); | ||
80 | + fprintf(stderr,"Not enough free space to overwrite the chunk.\n\nOriginal chunk size (with padding): %d\nNew chunk size:%d\n",totalChunkLength,(unsigned int)compressedChunkLength); | ||
81 | + return -6; | ||
76 | } | 82 | } |
77 | header.length = __bswap_32((uint32_t)compressedChunkLength+1); | 83 | header.length = __bswap_32((uint32_t)compressedChunkLength+1); |
78 | if(write(fd,&header,sizeof(ChunkHeader)) <= 0) { | 84 | if(write(fd,&header,sizeof(ChunkHeader)) <= 0) { |
79 | close(fd); | 85 | close(fd); |
80 | free(compressedChunk); | 86 | free(compressedChunk); |
81 | fprintf(stderr,"Unable to read chunk header: %s\n",strerror(errno)); | 87 | fprintf(stderr,"Unable to read chunk header: %s\n",strerror(errno)); |
82 | - return -6; | 88 | + return -7; |
83 | } | 89 | } |
84 | ssize_t nWritten = 0; | 90 | ssize_t nWritten = 0; |
85 | size_t totalWritten = 0; | 91 | size_t totalWritten = 0; |
@@ -92,7 +98,7 @@ int overwriteChunk(const char* regionFolder, ChunkID chunk, void* chunkData, siz | @@ -92,7 +98,7 @@ int overwriteChunk(const char* regionFolder, ChunkID chunk, void* chunkData, siz | ||
92 | fprintf(stderr,"Unable to write chunk: %s\n",strerror(errno)); | 98 | fprintf(stderr,"Unable to write chunk: %s\n",strerror(errno)); |
93 | close(fd); | 99 | close(fd); |
94 | free(compressedChunk); | 100 | free(compressedChunk); |
95 | - return -7; | 101 | + return -8; |
96 | } | 102 | } |
97 | totalWritten += nWritten; | 103 | totalWritten += nWritten; |
98 | } | 104 | } |