Nam Vu
Nam Vu

Reputation: 1757

does gzip returns error code in c++ when command fails?

Background: Working in ubuntu18 env (if it matters), I'm faking a tons of large *.csv files into Pcap format for my data handler. Upon finishing the convert, I'm running "gzip -f " + file_name and then check whether or not if it returns !0, then I know it fails. Here is the code:

if (convert()) {
    m_ofs.close();
    string command = "gzip -f " + m_out_file_name;
    if (system(command.c_str()) != 0) {
      const string info{"Cannot execute 'gzip' command, please check if system has installed gzip"};
      m_logger->printf("%s\n",info.c_str());
    }

However, as of now, when my converter finish, I do not have a way to show progress and I have a feeling that some people will stop the converter before the gzip command is finished (I mean it take a whole hour to convert and ip each file). Is there anyway to check for when that happens so I can log the error? For example:

if (convert()) {
    m_ofs.close();
    string command = "gzip -f " + m_out_file_name;
    if (system(command.c_str()) != 0) {

        /* EXAMPLE CODE */
        if (errno == USER_PRESS_CTR_C){
          m_logger->printf("Converter finished but user canceled compression\n");
        }
        /* EXAMPLE CODE */ 

      const string info{"Cannot execute 'gzip' command, please check if system has installed gzip"};
      m_logger->printf("%s\n",info.c_str());
    }

Another word is does system("gzip -f file"); returns any useful error code when fails?

[Edit] Thanks all for the help, I ended up installing and using zlib LOL It's been almost two years and I can't believe this is what I used to ask, really thank you for your helps!

Upvotes: 0

Views: 1254

Answers (2)

rici
rici

Reputation: 241701

You should definitely be using zlib instead of shelling out a gzip command, and particularly if you will immediately be reading the decompressed file. You'll find that it is faster to process the decompressed data on the fly because it involves a lot less disk I/O. (That's especially true if the final output is compressed.)

But, to answer your questions:

  1. On Linux, the return value of system() indicates whether the command succeeded or failed. The return value will be:

    • -1 if system failed to create a process to run the command in.
    • 127 if the command could not be executed (for example, because the command executable doesn't exist)
    • the same as a return value from wait if the command terminated normally (by returning or calling exit), or if it were terminated by a signal.

So that makes it pretty easy to tell if the command was interrupted:

int status = system(command);
if (status == 0)
  fprintf(stderr, "%s\n", "Command executed successfully");
else if (status == 127)
  fprintf(stderr, "%s\n", "Command could not be executed");
else if (WIFSIGNALLED(status) {
  if (WSIGTERM(status) == SIGINT)
    fprintf(stderr, "%s\n", "Command terminated by interrupt (SIGINT)");
  else
    fprintf(stderr, "%s\n", "Command terminated by signal %d\n");
}
else
  fprintf(stderr, "%s\n", "Command terminated for unknown reason");
  1. The return value from gzip tells you whether the command succeded or not. See above.

Upvotes: 2

EvilTeach
EvilTeach

Reputation: 28837

Google-fu suggests that it returns 0 on success, 1 on failure, and 2 on warning. For better error messages, consider calling a library that gives more information instead. This link may lead you to a viable choice.

Upvotes: 4

Related Questions