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;

Also available in: Unified diff