Skip to content

Commit

Permalink
use std::println instead of std::cout and std::format (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
koolkdev authored Apr 27, 2024
1 parent 091c845 commit 2b7898a
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 112 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,28 @@ jobs:
sudo apt install gcc-14 g++-14
sudo apt install libfuse-dev
if: matrix.platform == 'ubuntu-latest'

- name: Install requirements (MacOS)
- name: Brew python fix
run: |
brew update
# Temporary fix, see https://github.com/actions/setup-python/issues/577
rm /usr/local/bin/2to3 || true
rm /usr/local/bin/idle3 || true
rm /usr/local/bin/pydoc3 || true
rm /usr/local/bin/python3 || true
rm /usr/local/bin/python3-config || true
brew install llvm
brew install macfuse
shell: bash
if: matrix.platform == 'macos-13'

- name: Install requirements (MacOS arm64)
- name: Install requirements (MacOS)
run: |
brew update
brew install llvm
brew install macfuse
# Hack to properly target the os libcxx dynlib
sed -i '' '/^#define _LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS/d' "$(brew --prefix llvm)"/include/c++/v1/__config_site
echo "CXXFLAGS=-mmacos-version-min=13.3" >> $GITHUB_ENV
shell: bash
if: matrix.platform == 'macos-14'
if: matrix.platform == 'macos-13' || matrix.platform == 'macos-14'

- name: Set compiler
run: |
Expand Down
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,7 @@ brew install cmake ninja pkg-config llvm macfuse
```
Build using the installled llvm:
```
# For x86-64
export CC=/usr/local/opt/llvm/bin/clang
export CXX=/usr/local/opt/llvm/bin/clang++
# For arm
export CC=/opt/homebrew/opt/llvm/bin/clang
export CXX=/opt/homebrew/opt/llvm/bin/clang++
export CC="$(brew --prefix llvm)"/bin/clang
export CXX="$(brew --prefix llvm)"/bin/clang++
export LDFLAGS=-L"$(brew --prefix llvm)"/lib/c++
```
44 changes: 22 additions & 22 deletions wfs-extract/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
#include <boost/program_options.hpp>
#include <cstdio>
#include <filesystem>
#include <format>
#include <fstream>
#include <iostream>
#include <memory>
#include <print>
#include <vector>

#include <wfslib/wfslib.h>
Expand All @@ -30,7 +30,7 @@ void dumpdir(const std::filesystem::path& target,
target_dir /= path;
if (!std::filesystem::exists(target_dir)) {
if (!std::filesystem::create_directories(target_dir)) {
std::cerr << "Error: Failed to create directory " << target_dir.string() << std::endl;
std::println(std::cerr, "Error: Failed to create directory {}", target_dir.string());
return;
}
}
Expand All @@ -39,7 +39,7 @@ void dumpdir(const std::filesystem::path& target,
try {
auto item = throw_if_error(item_or_error);
if (verbose)
std::cout << "Dumping /" << npath.generic_string() << std::endl;
std::println("Dumping /{}", npath.generic_string());
if (item->is_directory()) {
dumpdir(target, std::dynamic_pointer_cast<Directory>(item), npath, verbose);
} else if (item->is_file()) {
Expand All @@ -52,7 +52,7 @@ void dumpdir(const std::filesystem::path& target,
stream.read(data.data(), std::min(data.size(), to_read));
auto read = stream.gcount();
if (read <= 0) {
std::cerr << "Error: Failed to read /" << npath.generic_string() << std::endl;
std::println(std::cerr, "Error: Failed to read /{}", npath.generic_string());
break;
}
output_file.write(data.data(), read);
Expand All @@ -61,7 +61,7 @@ void dumpdir(const std::filesystem::path& target,
output_file.close();
}
} catch (const WfsException& e) {
std::cout << std::format("Error: Failed to dump {} ({})\n", prettify_path(npath), e.what());
std::println(std::cerr, "Error: Failed to dump {} ({})", prettify_path(npath), e.what());
}
}
}
Expand Down Expand Up @@ -112,10 +112,10 @@ int main(int argc, char* argv[]) {
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);

if (vm.count("help")) {
std::cout << "usage: wfs-extract --input <input file> [--type <type>]" << std::endl
<< " [--otp <path> [--seeprom <path>]]" << std::endl
<< " [--dump-path <directory to dump>] [--verbose]" << std::endl
<< std::endl;
std::println("usage: wfs-extract --input <input file> [--type <type>]");
std::println(" [--otp <path> [--seeprom <path>]]");
std::println(" [--dump-path <directory to dump>] [--verbose]");
std::println("");
std::cout << desc << std::endl;
return 0;
}
Expand All @@ -139,8 +139,8 @@ int main(int argc, char* argv[]) {
throw boost::program_options::error("Missing --seeprom");

} catch (const boost::program_options::error& e) {
std::cerr << "Error: " << e.what() << std::endl;
std::cerr << "Use --help to display program options" << std::endl;
std::println(std::cerr, "Error: {}", e.what());
std::println(std::cerr, "Use --help to display program options");
return 1;
}

Expand All @@ -154,9 +154,9 @@ int main(int argc, char* argv[]) {
auto wfs_with_usr_dir = Recovery::OpenUsrDirectoryWithoutWfsDeviceHeader(device, key);
if (!wfs_with_usr_dir.has_value()) {
if (wfs_with_usr_dir.error() == WfsError::kInvalidWfsVersion) {
std::cerr
<< "Error: Didn't find directory at the expected location, either the /usr dir is also corrupted or "
"the keys are wrong";
std::println(std::cerr,
"Error: Didn't find directory at the expected location, either the /usr dir is also corrupted "
"or the keys are wrong");
} else {
throw WfsException(wfs_with_usr_dir.error());
}
Expand All @@ -169,39 +169,39 @@ int main(int argc, char* argv[]) {
} else if (dump_path.starts_with("usr/")) {
dump_path = dump_path.substr(4);
} else {
std::cerr << "Error: can only dump the /usr directory in this mode";
std::println(std::cerr, "Error: can only dump the /usr directory in this mode");
return 1;
}
}
auto dir = (*wfs_with_usr_dir)->GetDirectory(dump_path);
if (!dir) {
std::cerr << "Error: Didn't find directory /usr/" << dump_path << " in wfs" << std::endl;
std::println(std::cerr, "Error: Didn't find directory /usr/{} in wfs", dump_path);
return 1;
}
dumpdir(std::filesystem::path(output_path), dir, "usr/" + dump_path, verbose);
std::cout << "Done!" << std::endl;
std::println("Done!");
return 0;
}

// Regular mode
auto detection_result = Recovery::DetectDeviceParams(device, key);
if (detection_result.has_value()) {
if (*detection_result == WfsError::kInvalidWfsVersion)
std::cerr << "Error: Incorrect WFS version, possible wrong keys";
std::println(std::cerr, "Error: Incorrect WFS version, possible wrong keys");
else
throw WfsException(*detection_result);
return 1;
}
auto dir = throw_if_error(WfsDevice::Open(device, key))->GetDirectory(dump_path);
if (!dir) {
std::cerr << "Error: Didn't find directory " << dump_path << " in wfs" << std::endl;
std::println(std::cerr, "Error: Didn't find directory {} in wfs", dump_path);
return 1;
}
std::cout << "Dumping..." << std::endl;
std::println("Dumping...");
dumpdir(std::filesystem::path(output_path), dir, dump_path, verbose);
std::cout << "Done!" << std::endl;
std::println("Done!");
} catch (std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
std::println(std::cerr, "Error: {}", e.what());
return 1;
}
return 0;
Expand Down
33 changes: 16 additions & 17 deletions wfs-file-injector/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
*/

#include <boost/program_options.hpp>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <memory>
#include <print>
#include <vector>

#include <wfslib/wfslib.h>
Expand Down Expand Up @@ -60,11 +60,10 @@ int main(int argc, char* argv[]) {
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);

if (vm.count("help")) {
std::cout << "usage: wfs-file-injector --image <wfs image> [--type <type>]" << std::endl
<< " [--otp <path> [--seeprom <path>]]" << std::endl
<< " --inject-file <file to inject> --inject-path <file path in wfs>"
<< std::endl
<< std::endl;
std::println("usage: wfs-file-injector --image <wfs image> [--type <type>]");
std::println(" [--otp <path> [--seeprom <path>]]");
std::println(" --inject-file <file to inject> --inject-path <file path in wfs>");
std::println("");
std::cout << desc << std::endl;
return 0;
}
Expand All @@ -85,21 +84,21 @@ int main(int argc, char* argv[]) {
throw boost::program_options::error("Missing --seeprom");

} catch (const boost::program_options::error& e) {
std::cerr << "Error: " << e.what() << std::endl;
std::cerr << "Use --help to display program options" << std::endl;
std::println(std::cerr, "Error: {}", e.what());
std::println(std::cerr, "Use --help to display program options");
return 1;
}

auto key = get_key(type, otp_path, seeprom_path);

std::ifstream input_file(inject_file, std::ios::binary | std::ios::in);
if (input_file.fail()) {
std::cerr << "Failed to open file to inject" << std::endl;
std::println(std::cerr, "Failed to open file to inject");
return 1;
}
input_file.seekg(0, std::ios::end);
if (static_cast<uint64_t>(input_file.tellg()) > SIZE_MAX) {
std::cerr << "Error: File to inject too big" << std::endl;
std::println(std::cerr, "Error: File to inject too big");
return 1;
}
size_t file_size = static_cast<size_t>(input_file.tellg());
Expand All @@ -109,19 +108,19 @@ int main(int argc, char* argv[]) {
auto detection_result = Recovery::DetectDeviceParams(device, key);
if (detection_result.has_value()) {
if (*detection_result == WfsError::kInvalidWfsVersion)
std::cerr << "Error: Incorrect WFS version, possible wrong keys";
std::println(std::cerr, "Error: Incorrect WFS version, possible wrong keys");
else
throw WfsException(*detection_result);
return 1;
}
auto file = throw_if_error(WfsDevice::Open(device, key))->GetFile(inject_path);
if (!file) {
std::cerr << "Error: Didn't find file " << inject_path << " in wfs" << std::endl;
std::println(std::cerr, "Error: Didn't find file {} in wfs", inject_path);
return 1;
}
if (file_size > file->SizeOnDisk()) {
std::cerr << "Error: File to inject too big (wanted size: " << file_size
<< " bytes, available size: " << file->SizeOnDisk() << ")" << std::endl;
std::println(std::cerr, "Error: File to inject too big (wanted size: {} bytes, available size: {})", file_size,
file->SizeOnDisk());
return 1;
}
File::stream stream(file);
Expand All @@ -131,7 +130,7 @@ int main(int argc, char* argv[]) {
input_file.read(data.data(), std::min(data.size(), to_copy));
auto read = input_file.gcount();
if (read <= 0) {
std::cerr << "Error: Failed to read file to inject" << std::endl;
std::println(std::cerr, "Error: Failed to read file to inject");
return 1;
}
stream.write(data.data(), read);
Expand All @@ -142,9 +141,9 @@ int main(int argc, char* argv[]) {
if (file_size < file->Size()) {
file->Resize(file_size);
}
std::cout << "Done!" << std::endl;
std::println("Done!");
} catch (std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
std::println(std::cerr, "Error: {}", e.what());
return 1;
}
return 0;
Expand Down
31 changes: 18 additions & 13 deletions wfs-fuse/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <mutex>
#include <string>

static std::shared_ptr<WfsDevice> wfs_device;
static std::shared_ptr<WfsDevice> g_wfs_device;

struct locked_stream {
std::unique_ptr<File::stream> stream;
Expand All @@ -21,7 +21,7 @@ struct locked_stream {
static int wfs_getattr(const char* path, struct stat* stbuf) {
memset(stbuf, 0, sizeof(struct stat));

auto item = wfs_device->GetObject(path);
auto item = g_wfs_device->GetObject(path);
if (!item)
return -ENOENT;
if (item->is_directory()) {
Expand All @@ -47,7 +47,7 @@ static int wfs_readdir(const char* path, void* buf, fuse_fill_dir_t filler, off_
(void)offset;
(void)fi;

auto item = wfs_device->GetObject(path);
auto item = g_wfs_device->GetObject(path);
if (!item || !item->is_directory())
return -ENOENT;

Expand All @@ -62,7 +62,7 @@ static int wfs_readdir(const char* path, void* buf, fuse_fill_dir_t filler, off_
}

static int wfs_open(const char* path, struct fuse_file_info* fi) {
auto item = wfs_device->GetObject(path);
auto item = g_wfs_device->GetObject(path);
if (!item->is_file())
return -ENOENT;

Expand Down Expand Up @@ -96,7 +96,7 @@ static int wfs_read(const char* path, char* buf, size_t size, off_t offset, stru

int wfs_readlink(const char* path, [[maybe_unused]] char* buf, [[maybe_unused]] size_t size) {
// TODO
auto item = wfs_device->GetObject(path);
auto item = g_wfs_device->GetObject(path);
if (!item || !item->is_link())
return -ENOENT;

Expand Down Expand Up @@ -190,7 +190,7 @@ int main(int argc, char* argv[]) {
return 1;
}
if (fuse_opt_parse(&args, &param, wfs_opts, wfs_process_arg)) {
printf("failed to parse option\n");
fprintf(stderr, "failed to parse option\n");
return 1;
}

Expand All @@ -202,15 +202,15 @@ int main(int argc, char* argv[]) {
is_mlc = param.type && !strcmp(param.type, "mlc");
is_plain = param.type && !strcmp(param.type, "plain");
if (!is_usb && !is_mlc && !is_plain) {
printf("Unsupported type (--type=usb/mlc/plain)\n");
fprintf(stderr, "Unsupported type (--type=usb/mlc/plain)\n");
return 1;
}
if ((is_mlc || is_usb) && !param.otp) {
printf("Missing otp file (--otp)\n");
fprintf(stderr, "Missing otp file (--otp)\n");
return 1;
}
if (is_usb && !param.seeprom) {
printf("Missing seeprom file (--seeprom)\n");
fprintf(stderr, "Missing seeprom file (--seeprom)\n");
return 1;
}
if (param.otp)
Expand All @@ -224,14 +224,19 @@ int main(int argc, char* argv[]) {
auto detection_result = Recovery::DetectDeviceParams(device, key);
if (detection_result.has_value()) {
if (*detection_result == WfsError::kInvalidWfsVersion)
std::cerr << "Error: Incorrect WFS version, possible wrong keys";
fprintf(stderr, "Error: Incorrect WFS version, possible wrong keys\n");
else
throw WfsException(*detection_result);
fprintf(stderr, "Error: %s\n", WfsException(*detection_result).what());
return 1;
}
wfs_device = throw_if_error(WfsDevice::Open(device, key));
auto wfs_device = WfsDevice::Open(device, key);
if (!wfs_device.has_value()) {
fprintf(stderr, "Error: %s\n", WfsException(wfs_device.error()).what());
return 1;
}
g_wfs_device = std::move(*wfs_device);
} catch (std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
fprintf(stderr, "Error: %s\n", e.what());
return 1;
}

Expand Down
Loading

0 comments on commit 2b7898a

Please sign in to comment.