Skip to content

Commit

Permalink
libmbpio: Use Outcome for error handling
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Gunnerson <[email protected]>
  • Loading branch information
chenxiaolong committed May 27, 2018
1 parent 8558403 commit 400d5c8
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 252 deletions.
5 changes: 2 additions & 3 deletions bootimgtool/bootimgtool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@

// libmbpio
#include <mbpio/directory.h>
#include <mbpio/error.h>
#include <mbpio/path.h>

#define FIELD_CMDLINE "cmdline"
Expand Down Expand Up @@ -863,9 +862,9 @@ static bool unpack_main(int argc, char *argv[])

prepend_if_empty(paths, output_dir, prefix);

if (!mb::io::create_directories(output_dir)) {
if (auto r = mb::io::create_directories(output_dir); !r) {
fprintf(stderr, "%s: Failed to create directory: %s\n",
output_dir.c_str(), mb::io::last_error_string().c_str());
output_dir.c_str(), r.error().message().c_str());
return false;
}

Expand Down
6 changes: 3 additions & 3 deletions libmbpatcher/src/patchers/zippatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ bool ZipPatcher::patch_zip()
FileUtils::create_temporary_dir(m_pc.temp_directory());

if (!pass1(temp_dir, exclude_from_pass1)) {
io::delete_recursively(temp_dir);
(void) io::delete_recursively(temp_dir);
return false;
}

Expand All @@ -244,11 +244,11 @@ bool ZipPatcher::patch_zip()
// On the second pass, run the autopatchers on the rest of the files

if (!pass2(temp_dir, exclude_from_pass1)) {
io::delete_recursively(temp_dir);
(void) io::delete_recursively(temp_dir);
return false;
}

io::delete_recursively(temp_dir);
(void) io::delete_recursively(temp_dir);

for (const CopySpec &spec : to_copy) {
if (m_cancelled) return false;
Expand Down
5 changes: 2 additions & 3 deletions libmbpatcher/src/private/miniziputils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
#include "mblog/logging.h"

#include "mbpio/directory.h"
#include "mbpio/error.h"
#include "mbpio/path.h"

#include "mz_os.h"
Expand Down Expand Up @@ -374,9 +373,9 @@ bool MinizipUtils::extract_file(void *handle, const std::string &directory)
full_path += std::string{file_info->filename, file_info->filename_size};

std::string parent_path = io::dir_name(full_path);
if (!io::create_directories(parent_path)) {
if (auto r = io::create_directories(parent_path); !r) {
LOGW("%s: Failed to create directory: %s",
parent_path.c_str(), io::last_error_string().c_str());
parent_path.c_str(), r.error().message().c_str());
}

StandardFile file;
Expand Down
1 change: 0 additions & 1 deletion libmbpio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ foreach(variant ${variants})
${uvariant}
src/delete.cpp
src/directory.cpp
src/error.cpp
src/path.cpp
)

Expand Down
5 changes: 3 additions & 2 deletions libmbpio/include/mbpio/delete.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2015-2017 Andrew Gunnerson <[email protected]>
* Copyright (C) 2015-2018 Andrew Gunnerson <[email protected]>
*
* This file is part of DualBootPatcher
*
Expand All @@ -22,10 +22,11 @@
#include <string>

#include "mbcommon/common.h"
#include "mbcommon/outcome.h"

namespace mb::io
{

MB_EXPORT bool delete_recursively(const std::string &path);
MB_EXPORT oc::result<void> delete_recursively(const std::string &path);

}
5 changes: 3 additions & 2 deletions libmbpio/include/mbpio/directory.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2015-2017 Andrew Gunnerson <[email protected]>
* Copyright (C) 2015-2018 Andrew Gunnerson <[email protected]>
*
* This file is part of DualBootPatcher
*
Expand All @@ -22,10 +22,11 @@
#include <string>

#include "mbcommon/common.h"
#include "mbcommon/outcome.h"

namespace mb::io
{

MB_EXPORT bool create_directories(const std::string &path);
MB_EXPORT oc::result<void> create_directories(const std::string &path);

}
42 changes: 0 additions & 42 deletions libmbpio/include/mbpio/error.h

This file was deleted.

5 changes: 3 additions & 2 deletions libmbpio/include/mbpio/posix/delete.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2015-2017 Andrew Gunnerson <[email protected]>
* Copyright (C) 2015-2018 Andrew Gunnerson <[email protected]>
*
* This file is part of DualBootPatcher
*
Expand All @@ -22,10 +22,11 @@
#include <string>

#include "mbcommon/common.h"
#include "mbcommon/outcome.h"

namespace mb::io::posix
{

MB_EXPORT bool delete_recursively(const std::string &path);
MB_EXPORT oc::result<void> delete_recursively(const std::string &path);

}
5 changes: 3 additions & 2 deletions libmbpio/include/mbpio/win32/delete.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2015-2017 Andrew Gunnerson <[email protected]>
* Copyright (C) 2015-2018 Andrew Gunnerson <[email protected]>
*
* This file is part of DualBootPatcher
*
Expand All @@ -22,10 +22,11 @@
#include <string>

#include "mbcommon/common.h"
#include "mbcommon/outcome.h"

namespace mb::io::win32
{

MB_EXPORT bool delete_recursively(const std::string &path);
MB_EXPORT oc::result<void> delete_recursively(const std::string &path);

}
4 changes: 2 additions & 2 deletions libmbpio/src/delete.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2015-2017 Andrew Gunnerson <[email protected]>
* Copyright (C) 2015-2018 Andrew Gunnerson <[email protected]>
*
* This file is part of DualBootPatcher
*
Expand Down Expand Up @@ -28,7 +28,7 @@
namespace mb::io
{

bool delete_recursively(const std::string &path)
oc::result<void> delete_recursively(const std::string &path)
{
#ifdef _WIN32
return win32::delete_recursively(path);
Expand Down
34 changes: 9 additions & 25 deletions libmbpio/src/directory.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2015-2017 Andrew Gunnerson <[email protected]>
* Copyright (C) 2015-2018 Andrew Gunnerson <[email protected]>
*
* This file is part of DualBootPatcher
*
Expand All @@ -23,9 +23,6 @@

#include "mbcommon/error_code.h"
#include "mbcommon/locale.h"
#include "mbcommon/string.h"

#include "mbpio/error.h"

#ifdef _WIN32
# include <windows.h>
Expand All @@ -37,7 +34,7 @@
namespace mb::io
{

bool create_directories(const std::string &path)
oc::result<void> create_directories(const std::string &path)
{
#ifdef _WIN32
constexpr char delim[] = "/\\";
Expand All @@ -55,8 +52,7 @@ bool create_directories(const std::string &path)
#endif

if (path.empty()) {
set_last_error(Error::InvalidArguments, "Path cannot be empty");
return false;
return std::errc::invalid_argument;
}

// Add leading separator if needed
Expand All @@ -70,37 +66,25 @@ bool create_directories(const std::string &path)
temp += pathsep;

#ifdef _WIN32
auto w_temp = mb::utf8_to_wcs(temp);
if (!w_temp) {
set_last_error(Error::PlatformError, mb::format(
"%s: Failed to convert UTF-16 to UTF-8: %s",
temp.c_str(), w_temp.error().message().c_str()));
return false;
}
OUTCOME_TRY(w_temp, mb::utf8_to_wcs(temp));

DWORD dw_attrib = GetFileAttributesW(w_temp.value().c_str());
DWORD dw_attrib = GetFileAttributesW(w_temp.c_str());
bool exists = (dw_attrib != INVALID_FILE_ATTRIBUTES)
&& (dw_attrib & FILE_ATTRIBUTE_DIRECTORY);
if (!exists && !CreateDirectoryW(w_temp.value().c_str(), nullptr)
if (!exists && !CreateDirectoryW(w_temp.c_str(), nullptr)
&& GetLastError() != ERROR_ALREADY_EXISTS) {
set_last_error(Error::PlatformError, mb::format(
"%s: Failed to create directory: %s",
temp.c_str(), mb::ec_from_win32().message().c_str()));
return false;
return ec_from_win32();
}
#else
if (mkdir(temp.c_str(), 0755) < 0 && errno != EEXIST) {
set_last_error(Error::PlatformError, mb::format(
"%s: Failed to create directory: %s",
temp.c_str(), strerror(errno)));
return false;
return ec_from_errno();
}
#endif

p = strtok_r(nullptr, delim, &save_ptr);
}

return true;
return oc::success();
}

}
Loading

0 comments on commit 400d5c8

Please sign in to comment.