Commit f99205ed authored by Seblu's avatar Seblu
Browse files

bug sur les chevrons d'envoie et de receptions

verifie si le script dir existe
vide le script dir au demarrage
improve error messaging
parent 7c2e640b
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -2,5 +2,5 @@ host=trinity
pass=sex
server=localhost
port=18136
scriptdir=examples/scripts/
scriptdir=/tmp/sld
verbose=1
+41 −23
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <dirent.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
@@ -43,7 +44,6 @@ void SLDaemon::usage(const char *argv0) const {
	    << "  -H name      : Set server host to name." << std::endl
	    << "  -P number    : Set server port to number." << std::endl
	    << "  -V           : Print version and exit." << std::endl;

}

SLDaemon::Options *SLDaemon::getoptions(int argc, char *argv[]) const {
@@ -68,42 +68,39 @@ SLDaemon::Options *SLDaemon::getoptions(int argc, char *argv[]) const {
    }
    else if (!strcmp(argv[i], "-H")) {
      if (++i >= argc)
	throw Error(ERR_USAGE, "No enough argument for option -h.");
	throw Error(ERR_USAGE, "No enough argument for option -h");
      opt.server = string(argv[i]);
    }
    else if (!strcmp(argv[i], "-P")) {
      if (++i >= argc)
	throw Error(ERR_USAGE, "No enough argument for option -p.");
	throw Error(ERR_USAGE, "No enough argument for option -p");
      char *endptr;
      opt.port = strtol(argv[i], &endptr, 10);
      if (!(*argv[i] != '\0' && *endptr == '\0'))
	throw Error(ERR_USAGE, "Unable to convert port to a number.");
	throw Error(ERR_USAGE, "Unable to convert port to a number");
    }
    else if (!strcmp(argv[i], "-f")) {
      if (++i >= argc)
	throw Error(ERR_USAGE, "No enough argument for option -f.");
	throw Error(ERR_USAGE, "No enough argument for option -f");
      opt.conffile = string(argv[i]);
    }
    else if (!strcmp(argv[i], "-d")) {
      if (++i >= argc)
	throw Error(ERR_USAGE, "No enough argument for option -d.");
	throw Error(ERR_USAGE, "No enough argument for option -d");
      opt.scriptdir = string(argv[i]);
    }
    else if (!strcmp(argv[i], "-l")) {
      if (++i >= argc)
	throw Error(ERR_USAGE, "No enough argument for option -l.");
	throw Error(ERR_USAGE, "No enough argument for option -l");
      opt.login = string(argv[i]);
    }
    else if (!strcmp(argv[i], "-p")) {
      if (++i >= argc)
	throw Error(ERR_USAGE, "No enough argument for option -p.");
	throw Error(ERR_USAGE, "No enough argument for option -p");
      opt.pass = string(argv[i]);
    }
    else {
      Error *err = new Error(ERR_USAGE);
      *err << "Invalid options : " << string(argv[i]) << ".";
      throw *err;
    }
    else
      throw Error(ERRR_USAGE, (string) "Invalid options : " + argv[i]);
  }
  mopt = new Options();
  *mopt = opt;
@@ -173,25 +170,27 @@ void SLDaemon::check_options() const {

  // Check validy of arguement
  if (options_.server == "")
    throw Error(ERR_BADPARAM, "No server address specified.");
    throw Error(ERR_BADPARAM, "No server address specified");
  if (options_.port == 0)
    throw Error(ERR_BADPARAM, "No server port specified.");
    throw Error(ERR_BADPARAM, "No server port specified");
  if (options_.port < 1 || options_.port > 65535)
    throw Error(ERR_BADPARAM, "Bad server port number (1 - 65535).");
    throw Error(ERR_BADPARAM, "Bad server port number (1 - 65535)");
  if (options_.login == "")
    throw Error(ERR_BADPARAM, "No login specified.");
    throw Error(ERR_BADPARAM, "No login specified");
  if (options_.pass == "")
    throw Error(ERR_BADPARAM, "No pass specified.");
    throw Error(ERR_BADPARAM, "No pass specified");
  if (options_.scriptdir == "")
    throw Error(ERR_BADPARAM, "No scripts directory specified.");
    throw Error(ERR_BADPARAM, "No scripts directory specified");

  // TODO: Check dir exist
  // TODO: Empty scripts dir
  // Check dir script exist
  if (euidaccess(options_.scriptdir.c_str(), W_OK))
    throw Error(ERR_BADPARAM, "Unable to write into script directory");
}
void SLDaemon::run() {
  char *line;

  check_options();
  clean_dir(options_.scriptdir);
  connect();
  auth();
  while (1) {
@@ -217,7 +216,7 @@ void SLDaemon::run() {
      else if (!strcmp(line, "UPDATE\n"))
	cmd_update();
      else
	sendln("sld: Invalid command.");
	sendln("Invalid command.");
    }
    catch (const Error &e) {
      sendln(e.message());
@@ -324,7 +323,7 @@ char *SLDaemon::recvln() {
    throw Error(ERR_FILE, (string) "recvln: " + strerror(errno));
  }
  if (verbose())
    std::cout << SND_DATA << line;
    std::cout << RCV_DATA << line;

  return line;
}
@@ -738,6 +737,25 @@ string SLDaemon::md5(const string &file) const {
  return string(digest);
}

void SLDaemon::clean_dir(const string &dir) const {
  DIR *ds;
  struct dirent *de;

  ds = opendir(dir.c_str());
  if (ds == 0)
    throw Error(ERR_FILE, "clear_dir: Unable to read dir script");

  while ((de = readdir(ds))) {
    if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
      continue;
    if (remove(de->d_name)) {
      closedir(ds);
      throw Error(ERR_FILE,(string) "clear_dir: remove("+ de->d_name + "): " + strerror(errno));
    }
  }
  closedir(ds);
}

//******************************************************************************
// Class Options functions
//******************************************************************************
+1 −0
Original line number Diff line number Diff line
@@ -94,6 +94,7 @@ protected:

  // others functions
  string md5(const string &file) const;
  void clean_dir(const string &dir) const;

private:
  static SLDaemon *instance_;
+2 −1
Original line number Diff line number Diff line
@@ -45,9 +45,10 @@ int main(int argc, char *argv[])
    daemon.run();
  }
  catch (const Error &e) {
    std::cerr <<"sld: error: " << e.message() << std::endl;
    std::cerr <<"sld: error: " << e.message() << "." << std::endl;
    return e.code();
  }

  return ERR_UNKNOWN;
}