pmt: reformat code
This commit is contained in:
@@ -14,109 +14,151 @@
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
#include "functions.hpp"
|
||||
#include <PartitionManager/PartitionManager.hpp>
|
||||
#include <cerrno>
|
||||
#include <chrono>
|
||||
#include <cstdlib>
|
||||
#include <fcntl.h>
|
||||
#include <cerrno>
|
||||
#include <unistd.h>
|
||||
#include <future>
|
||||
#include <chrono>
|
||||
#include <PartitionManager/PartitionManager.hpp>
|
||||
#include <private/android_filesystem_config.h>
|
||||
#include "functions.hpp"
|
||||
#include <unistd.h>
|
||||
|
||||
#define BFUN "backupFunction"
|
||||
|
||||
namespace PartitionManager {
|
||||
pair backupFunction::runAsync(const std::string &partitionName, const std::string &outputName, int bufferSize) {
|
||||
if (!Variables->PartMap->hasPartition(partitionName)) return {
|
||||
format("Couldn't find partition: %s", partitionName.data()), false
|
||||
};
|
||||
pair backupFunction::runAsync(const std::string &partitionName,
|
||||
const std::string &outputName, int bufferSize) {
|
||||
if (!Variables->PartMap->hasPartition(partitionName))
|
||||
return {format("Couldn't find partition: %s", partitionName.data()), false};
|
||||
|
||||
LOGN(BFUN, INFO) << "back upping " << partitionName << " as " << outputName << std::endl;
|
||||
LOGN(BFUN, INFO) << "back upping " << partitionName << " as " << outputName
|
||||
<< std::endl;
|
||||
|
||||
if (Variables->onLogical && !Variables->PartMap->isLogical(partitionName)) {
|
||||
if (Variables->forceProcess)
|
||||
LOGN(BFUN, WARNING) << "Partition " << partitionName <<
|
||||
" is exists but not logical. Ignoring (from --force, -f)." << std::endl;
|
||||
else return {format("Used --logical (-l) flag but is not logical partition: %s", partitionName.data()), false};
|
||||
}
|
||||
if (Variables->onLogical && !Variables->PartMap->isLogical(partitionName)) {
|
||||
if (Variables->forceProcess)
|
||||
LOGN(BFUN, WARNING)
|
||||
<< "Partition " << partitionName
|
||||
<< " is exists but not logical. Ignoring (from --force, -f)."
|
||||
<< std::endl;
|
||||
else
|
||||
return {
|
||||
format("Used --logical (-l) flag but is not logical partition: %s",
|
||||
partitionName.data()),
|
||||
false};
|
||||
}
|
||||
|
||||
if (Helper::fileIsExists(outputName) && !Variables->forceProcess) return {
|
||||
format("%s is exists. Remove it, or use --force (-f) flag.", outputName.data()), false
|
||||
};
|
||||
if (Helper::fileIsExists(outputName) && !Variables->forceProcess)
|
||||
return {format("%s is exists. Remove it, or use --force (-f) flag.",
|
||||
outputName.data()),
|
||||
false};
|
||||
|
||||
setupBufferSize(bufferSize, partitionName);
|
||||
LOGN(BFUN, INFO) << "Using buffer size (for back upping " << partitionName << "): " << bufferSize << std::endl;
|
||||
setupBufferSize(bufferSize, partitionName);
|
||||
LOGN(BFUN, INFO) << "Using buffer size (for back upping " << partitionName
|
||||
<< "): " << bufferSize << std::endl;
|
||||
|
||||
// Automatically close file descriptors and delete allocated memories (arrays)
|
||||
Helper::garbageCollector collector;
|
||||
// Automatically close file descriptors and delete allocated memories (arrays)
|
||||
Helper::garbageCollector collector;
|
||||
|
||||
const int pfd = Helper::openAndAddToCloseList(Variables->PartMap->getRealPathOf(partitionName), collector, O_RDONLY);
|
||||
if (pfd < 0) return {format("Can't open partition: %s: %s", partitionName.data(), strerror(errno)), false};
|
||||
const int pfd = Helper::openAndAddToCloseList(
|
||||
Variables->PartMap->getRealPathOf(partitionName), collector, O_RDONLY);
|
||||
if (pfd < 0)
|
||||
return {format("Can't open partition: %s: %s", partitionName.data(),
|
||||
strerror(errno)),
|
||||
false};
|
||||
|
||||
const int ffd = Helper::openAndAddToCloseList(outputName, collector, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||
if (ffd < 0) return {format("Can't create/open output file %s: %s", outputName.data(), strerror(errno)), false};
|
||||
const int ffd = Helper::openAndAddToCloseList(
|
||||
outputName, collector, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||
if (ffd < 0)
|
||||
return {format("Can't create/open output file %s: %s", outputName.data(),
|
||||
strerror(errno)),
|
||||
false};
|
||||
|
||||
LOGN(BFUN, INFO) << "Writing partition " << partitionName << " to file: " << outputName << std::endl;
|
||||
auto *buffer = new char[bufferSize];
|
||||
collector.delAfterProgress(buffer);
|
||||
memset(buffer, 0x00, bufferSize);
|
||||
LOGN(BFUN, INFO) << "Writing partition " << partitionName
|
||||
<< " to file: " << outputName << std::endl;
|
||||
auto *buffer = new char[bufferSize];
|
||||
collector.delAfterProgress(buffer);
|
||||
memset(buffer, 0x00, bufferSize);
|
||||
|
||||
ssize_t bytesRead;
|
||||
while ((bytesRead = read(pfd, buffer, bufferSize)) > 0) {
|
||||
if (const ssize_t bytesWritten = write(ffd, buffer, bytesRead); bytesWritten != bytesRead)
|
||||
return {format("Can't write partition to output file %s: %s", outputName.data(), strerror(errno)), false};
|
||||
}
|
||||
ssize_t bytesRead;
|
||||
while ((bytesRead = read(pfd, buffer, bufferSize)) > 0) {
|
||||
if (const ssize_t bytesWritten = write(ffd, buffer, bytesRead);
|
||||
bytesWritten != bytesRead)
|
||||
return {format("Can't write partition to output file %s: %s",
|
||||
outputName.data(), strerror(errno)),
|
||||
false};
|
||||
}
|
||||
|
||||
if (!Helper::changeOwner(outputName, AID_EVERYBODY, AID_EVERYBODY))
|
||||
LOGN(BFUN, WARNING) << "Failed to change owner of output file: " << outputName << ". Access problems maybe occur in non-root mode" << std::endl;
|
||||
if (!Helper::changeMode(outputName, 0660))
|
||||
LOGN(BFUN, WARNING) << "Failed to change mode of output file as 660: " << outputName << ". Access problems maybe occur in non-root mode" << std::endl;
|
||||
if (!Helper::changeOwner(outputName, AID_EVERYBODY, AID_EVERYBODY))
|
||||
LOGN(BFUN, WARNING) << "Failed to change owner of output file: "
|
||||
<< outputName
|
||||
<< ". Access problems maybe occur in non-root mode"
|
||||
<< std::endl;
|
||||
if (!Helper::changeMode(outputName, 0660))
|
||||
LOGN(BFUN, WARNING) << "Failed to change mode of output file as 660: "
|
||||
<< outputName
|
||||
<< ". Access problems maybe occur in non-root mode"
|
||||
<< std::endl;
|
||||
|
||||
return {format("%s partition successfully back upped to %s", partitionName.data(), outputName.data()), true};
|
||||
}
|
||||
return {format("%s partition successfully back upped to %s",
|
||||
partitionName.data(), outputName.data()),
|
||||
true};
|
||||
}
|
||||
|
||||
bool backupFunction::init(CLI::App &_app) {
|
||||
LOGN(BFUN, INFO) << "Initializing variables of backup function." << std::endl;
|
||||
cmd = _app.add_subcommand("backup", "Backup partition(s) to file(s)");
|
||||
cmd->add_option("partition(s)", rawPartitions, "Partition name(s)")->required();
|
||||
cmd->add_option("output(s)", rawOutputNames, "File name(s) (or path(s)) to save the partition image(s)");
|
||||
cmd->add_option("-O,--output-directory", outputDirectory, "Directory to save the partition image(s)")->check(CLI::ExistingDirectory);
|
||||
cmd->add_option("-b,--buffer-size", bufferSize, "Buffer size for reading partition(s) and writing to file(s)");
|
||||
bool backupFunction::init(CLI::App &_app) {
|
||||
LOGN(BFUN, INFO) << "Initializing variables of backup function." << std::endl;
|
||||
cmd = _app.add_subcommand("backup", "Backup partition(s) to file(s)");
|
||||
cmd->add_option("partition(s)", rawPartitions, "Partition name(s)")
|
||||
->required();
|
||||
cmd->add_option("output(s)", rawOutputNames,
|
||||
"File name(s) (or path(s)) to save the partition image(s)");
|
||||
cmd->add_option("-O,--output-directory", outputDirectory,
|
||||
"Directory to save the partition image(s)")
|
||||
->check(CLI::ExistingDirectory);
|
||||
cmd->add_option(
|
||||
"-b,--buffer-size", bufferSize,
|
||||
"Buffer size for reading partition(s) and writing to file(s)");
|
||||
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool backupFunction::run() {
|
||||
processCommandLine(partitions, outputNames, rawPartitions, rawOutputNames, ',', true);
|
||||
if (!outputNames.empty() && partitions.size() != outputNames.size())
|
||||
throw CLI::ValidationError("You must provide an output name(s) as long as the partition name(s)");
|
||||
bool backupFunction::run() {
|
||||
processCommandLine(partitions, outputNames, rawPartitions, rawOutputNames,
|
||||
',', true);
|
||||
if (!outputNames.empty() && partitions.size() != outputNames.size())
|
||||
throw CLI::ValidationError(
|
||||
"You must provide an output name(s) as long as the partition name(s)");
|
||||
|
||||
std::vector<std::future<pair>> futures;
|
||||
for (size_t i = 0; i < partitions.size(); i++) {
|
||||
std::string partitionName = partitions[i];
|
||||
std::string outputName = outputNames.empty() ? partitionName + ".img" : outputNames[i];
|
||||
if (!outputDirectory.empty()) outputName.insert(0, outputDirectory + '/');
|
||||
std::vector<std::future<pair>> futures;
|
||||
for (size_t i = 0; i < partitions.size(); i++) {
|
||||
std::string partitionName = partitions[i];
|
||||
std::string outputName =
|
||||
outputNames.empty() ? partitionName + ".img" : outputNames[i];
|
||||
if (!outputDirectory.empty()) outputName.insert(0, outputDirectory + '/');
|
||||
|
||||
futures.push_back(std::async(std::launch::async, runAsync, partitionName, outputName, bufferSize));
|
||||
LOGN(BFUN, INFO) << "Created thread backup upping " << partitionName << std::endl;
|
||||
}
|
||||
futures.push_back(std::async(std::launch::async, runAsync, partitionName,
|
||||
outputName, bufferSize));
|
||||
LOGN(BFUN, INFO) << "Created thread backup upping " << partitionName
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
std::string end;
|
||||
bool endResult = true;
|
||||
for (auto &future: futures) {
|
||||
auto [fst, snd] = future.get();
|
||||
if (!snd) { end += fst + '\n'; endResult = false; }
|
||||
else println("%s", fst.c_str());
|
||||
}
|
||||
std::string end;
|
||||
bool endResult = true;
|
||||
for (auto &future : futures) {
|
||||
auto [fst, snd] = future.get();
|
||||
if (!snd) {
|
||||
end += fst + '\n';
|
||||
endResult = false;
|
||||
} else println("%s", fst.c_str());
|
||||
}
|
||||
|
||||
if (!endResult) throw Error("%s", end.c_str());
|
||||
if (!endResult) throw Error("%s", end.c_str());
|
||||
|
||||
LOGN(BFUN, INFO) << "Operation successfully completed." << std::endl;
|
||||
return endResult;
|
||||
}
|
||||
LOGN(BFUN, INFO) << "Operation successfully completed." << std::endl;
|
||||
return endResult;
|
||||
}
|
||||
|
||||
bool backupFunction::isUsed() const { return cmd->parsed(); }
|
||||
bool backupFunction::isUsed() const { return cmd->parsed(); }
|
||||
|
||||
const char *backupFunction::name() const { return BFUN; }
|
||||
const char *backupFunction::name() const { return BFUN; }
|
||||
} // namespace PartitionManager
|
||||
|
||||
Reference in New Issue
Block a user