Project

General

Profile

« Previous | Next » 

Revision e0b7f8b9

Added by David Sorber over 9 years ago

WIP commit. I started converting the argv list to a std::vector and adding logic to make sure it does not exceed the maximum argv size.

View differences:

software/photo_compress_archiver/PhotoCompressArchiver.cc
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
......
x\
}
const char *PROG_NAME = "./packJPG";
const char *PROG_NAME = "./packjpg";
const char *PROG_OPT1 = "-np";
const char *PROG_OPT2 = "-o";
const char *PROG_OPT3 = "-p";
const uint32_t ARGV_MAX_SIZE(sysconf(_SC_ARG_MAX));
PhotoCompressArchiver::PhotoCompressArchiver(
const std::string& path,
uint32_t num_cores,
bool decompress,
bool keep_orig)
bool keep_orig,
boost::regex& filter_regex)
: m_path(path),
m_num_cores(num_cores),
m_decompress(decompress),
......
m_num_input_files(0),
m_total_uncompressed_size(0),
m_total_compressed_size(0),
p_finder_thread(nullptr)
{}
p_finder_thread(nullptr),
m_filter_empty(true),
m_filter_regex(filter_regex)
{
// This is a somewhat crude way to determining if the filter regex is empty
m_filter_empty = std::string("").compare(filter_regex.str()) == 0;
std::cout << "argv size: " << sysconf(_SC_ARG_MAX) << std::endl;
std::cout << "appname size: " << sizeof(PROG_NAME) << std::endl;
}
PhotoCompressArchiver::~PhotoCompressArchiver()
{}
......
else if (boost::regex_match(dir_iter->path().filename().string(), file_regex))
{
// Found file match
// If the filter is not empty, then check against it before proceeding
if (! m_filter_empty)
{
if (boost::regex_match(dir_iter->path().filename().string(),
m_filter_regex))
{
// The filter regex matched, skip this file
continue;
}
}
m_file_list.push_back(dir_iter->path());
++m_num_input_files;
......
<< std::endl;)
}
#endif
//~ return; // temporary for debugging
uint32_t argv_size = file_sublist->size() + 5;
const char **exec_argv = new const char* [argv_size];
//~ uint32_t argv_size = file_sublist->size() + 5;
//~ const char **exec_argv = new const char* [argv_size];
// Build the argv array; pass in default program options
std::vector<const char*> exec_argv;
exec_argv.push_back(&PROG_NAME[2]);
exec_argv.push_back(PROG_OPT1);
exec_argv.push_back(PROG_OPT2);
exec_argv.push_back(PROG_OPT3);
// Build the argv array; pass in default program options
exec_argv[0] = &PROG_NAME[2]; // discard leading "./"
exec_argv[1] = PROG_OPT1;
exec_argv[2] = PROG_OPT2;
exec_argv[3] = PROG_OPT3;
//~ exec_argv[0] = &PROG_NAME[2]; // discard leading "./"
//~ exec_argv[1] = PROG_OPT1;
//~ exec_argv[2] = PROG_OPT2;
//~ exec_argv[3] = PROG_OPT3;
uint32_t argv_size = sizeof(PROG_NAME) + sizeof(PROG_OPT1) +
sizeof(PROG_OPT2) + sizeof(PROG_OPT3) + 4096;
// Add the sublist files to the argv array
uint32_t idx = 4;
uint32_t idx = 0;
for (auto& image_path : *file_sublist)
{
exec_argv[idx++] = image_path.string().c_str();
uint32_t image_path_len = image_path.string().size();
if (argv_size + image_path_len < ARGV_MAX_SIZE)
{
exec_argv.push_back(image_path.string().c_str());
argv_size += image_path_len + sizeof(char*);
++idx;
}
else
{
break;
}
//~ exec_argv[idx++] = image_path.string().c_str();
}
exec_argv[argv_size - 1] = nullptr;
//~ exec_argv[argv_size - 1] = nullptr;
exec_argv.push_back(nullptr);
std::cout << "IDX: " << idx << std::endl;
// Build a list of the output files names for use in output file monitoring
......
std::vector<std::string*> output_filenames;
for (auto& filepath : *file_sublist)
{
// Create new filename that contains the ".jpg" replaced with ".pjg"
// Create new filename
uint32_t end_pos = filepath.string().find(".");
std::string* filename = new std::string(filepath.string().begin(),
filepath.string().begin() + end_pos);
if (m_decompress)
{
// Decompression mode
(*filename) += ".jpg";
}
else
{
// Compression mode
(*filename) += ".pjg";
}
// Add extension depending on if decompress mode is enabled
(*filename) += (m_decompress ? ".jpg" : ".pjg");
output_filenames.push_back(filename);
}
......
//~ close(filedes[0]);
// Child after fork
execv(PROG_NAME, (char **)exec_argv);
int rc = execv(PROG_NAME, (char **)exec_argv.data());
if (rc)
{
std::cerr << "execv failed: " << errno << std::endl;
}
_exit(EXIT_FAILURE); // exec never returns
}
......
<< status << ENDC << std::endl;)
// Free up the argv array and output filename list
delete[] exec_argv;
//~ delete[] exec_argv;
for (auto output_filename : output_filenames)
{
delete output_filename;
software/photo_compress_archiver/PhotoCompressArchiver.hh
const std::string& path,
uint32_t num_cores,
bool decompress,
bool keep_orig);
bool keep_orig,
boost::regex& filter_regex);
~PhotoCompressArchiver();
......
std::thread* p_finder_thread; // file finder thread
std::vector<std::thread*> m_worker_threads; // worker threads
bool m_filter_empty; // is the filter regex empty
boost::regex& m_filter_regex; // filter regular expression
};
#endif // PHOTOCOMPRESSARCHIVER_H
software/photo_compress_archiver/main.cc
}
header();
std::cout << "USAGE: " << app_name << " [-hkd] <file path>\n\n";
std::cout << "USAGE: " << app_name << " [-hkd] [-f <filter regex>] <file path>\n\n";
std::cout << "Positional arguments:\n";
std::cout << " <file path> path in which to recursively look "
<< "for input files\n\n";
......
std::cout << " -h [ --help ] display this help message\n";
std::cout << " -d [ --decompress ] find '*.pjg' files and decompress them\n";
std::cout << " -k [ --keep-orig ] keep original files; don't delete them\n";
std::cout << " -f [ --filter ] regex with which to filter out input files\n";
std::cout << std::endl;
}
......
("help,h", "display this help message")
("decompress,d", "find '*.pjg' files and decompress them")
("keep-orig,k", "keep orignal files")
("filter,f", bpo::value<std::string>(),
"regex with which to filter out input files")
("file_path", "path in which to recursively look for input files")
;
......
// Parse general/positional options
bpo::store(bpo::command_line_parser(argc, argv).options(general_opts)
.positional(pos_opts).allow_unregistered().run(),
.positional(pos_opts).run(),
general_opts_vm);
// Display help if the option is listed or if no arguments are provided
if (general_opts_vm.count("help") || (argc == 1))
{
usage(argv);
return 0;
}
bpo::notify(general_opts_vm);
}
catch (std::exception& e)
{
......
return -1;
}
// Make a copy of the file search path for passing into the object below
std::string search_path(general_opts_vm["file_path"].as<std::string>());
......
std::cout << " keep original files => ON\n";
}
// Check for, and if present validate the filter regex
boost::regex filter_regex("");
if (general_opts_vm.count("filter"))
{
try
{
filter_regex = general_opts_vm["filter"].as<std::string>().c_str();
}
catch (std::exception& e)
{
std::cerr << BOLD << RED << "ERROR: " << ENDC << "the filter \""
<< general_opts_vm["filter"].as<std::string>()
<< "\" is not a valid regular expression.\n";
std::cerr << " " << e.what() << std::endl;
return -1;
}
}
std::cout << std::endl;
// Create the PCA object and let 'er rip!
auto pca = new PhotoCompressArchiver(search_path, num_cores, decompress,
keep_orig);
keep_orig, filter_regex);
pca->execute();
delete pca;

Also available in: Unified diff