Skip to content

Commit

Permalink
COMMON: Handle writing failed in dumpArchive when not enough storage
Browse files Browse the repository at this point in the history
  • Loading branch information
ankushdutt committed Aug 8, 2023
1 parent 7959c43 commit eed7d80
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
13 changes: 11 additions & 2 deletions common/archive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ int Archive::listMatchingMembers(ArchiveMemberList &list, const Path &pattern, b
return matches;
}

void Archive::dumpArchive(String destPath) {
Common::Error Archive::dumpArchive(String destPath) {
Common::ArchiveMemberList files;

listMembers(files);
Expand All @@ -91,6 +91,7 @@ void Archive::dumpArchive(String destPath) {
for (auto &f : files) {
Common::Path filePath = f->getPathInArchive().punycodeEncode();
debug(1, "File: %s", filePath.toString().c_str());

Common::SeekableReadStream *stream = f->createReadStream();

uint32 len = stream->size();
Expand All @@ -107,7 +108,14 @@ void Archive::dumpArchive(String destPath) {
if (!out.open(outPath.toString(), true)) {
warning("Archive::dumpArchive(): Can not open dump file %s", outPath.toString().c_str());
} else {
out.write(data, len);
uint32 writtenBytes = out.write(data, len);
if (writtenBytes < len) {
// Not all data was written
out.close();
delete stream;
free(data);
return Common::Error(Common::kWritingFailed, "Not enough storage space! Please free up some storage and try again");
}
out.flush();
out.close();
}
Expand All @@ -116,6 +124,7 @@ void Archive::dumpArchive(String destPath) {
}

free(data);
return Common::kNoError;
}

char Archive::getPathSeparator() const {
Expand Down
3 changes: 2 additions & 1 deletion common/archive.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "common/hashmap.h"
#include "common/hash-str.h"
#include "common/singleton.h"
#include "common/error.h"

namespace Common {

Expand Down Expand Up @@ -168,7 +169,7 @@ class Archive {
/**
* Dump all files from the archive to the given directory
*/
void dumpArchive(String destPath);
Common::Error dumpArchive(String destPath);

/**
* Returns the separator used by internal paths in the archive
Expand Down

0 comments on commit eed7d80

Please sign in to comment.