"Fossies" - the Fresh Open Source Software Archive

Member "recoll-1.26.3/utils/ecrontab.cpp" (4 Sep 2019, 6739 Bytes) of package /linux/privat/recoll-1.26.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ecrontab.cpp" see the Fossies "Dox" file reference documentation.

    1 /* Copyright (C) 2004 J.F.Dockes
    2  *   This program is free software; you can redistribute it and/or modify
    3  *   it under the terms of the GNU General Public License as published by
    4  *   the Free Software Foundation; either version 2 of the License, or
    5  *   (at your option) any later version.
    6  *
    7  *   This program is distributed in the hope that it will be useful,
    8  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    9  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   10  *   GNU General Public License for more details.
   11  *
   12  *   You should have received a copy of the GNU General Public License
   13  *   along with this program; if not, write to the
   14  *   Free Software Foundation, Inc.,
   15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   16  */
   17 #ifndef TEST_ECRONTAB
   18 #include "autoconfig.h"
   19 
   20 #include <stdio.h>
   21 
   22 #include "ecrontab.h"
   23 #include "execmd.h"
   24 #include "smallut.h"
   25 #include "log.h"
   26 
   27 // Read crontab file and split it into lines.
   28 static bool eCrontabGetLines(vector<string>& lines)
   29 {
   30     string crontab;
   31     ExecCmd croncmd;
   32     vector<string> args; 
   33     int status;
   34 
   35     // Retrieve current crontab contents. An error here means that no
   36     // crontab exists, and is not fatal, but we return a different
   37     // status than for an empty one
   38     args.push_back("-l");
   39     if ((status = croncmd.doexec("crontab", args, 0, &crontab))) {
   40     lines.clear();
   41     return false;
   42     }
   43 
   44     // Split crontab into lines
   45     stringToTokens(crontab, lines, "\n");
   46     return true;
   47 }
   48 
   49 // Concatenate lines and write crontab
   50 static bool eCrontabWriteFile(const vector<string>& lines, string& reason)
   51 {
   52     string crontab;
   53     ExecCmd croncmd;
   54     vector<string> args; 
   55     int status;
   56 
   57     for (vector<string>::const_iterator it = lines.begin();
   58      it != lines.end(); it++) {
   59     crontab += *it + "\n";
   60     }
   61 
   62     args.push_back("-");
   63     if ((status = croncmd.doexec("crontab", args, &crontab, 0))) {
   64     char nbuf[30]; 
   65     sprintf(nbuf, "0x%x", status);
   66     reason = string("Exec crontab -l failed: status: ") + nbuf;
   67     return false;
   68     }
   69     return true;
   70 }
   71 
   72 // Add / change / delete entry identified by marker and id
   73 bool editCrontab(const string& marker, const string& id, 
   74               const string& sched, const string& cmd, string& reason)
   75 {
   76     vector<string> lines;
   77 
   78     if (!eCrontabGetLines(lines)) {
   79     // Special case: cmd is empty, no crontab, don't create one
   80     if (cmd.empty())
   81         return true;
   82     }
   83 
   84     // Remove old copy if any
   85     for (vector<string>::iterator it = lines.begin();
   86      it != lines.end(); it++) {
   87     // Skip comment
   88     if (it->find_first_of("#") == it->find_first_not_of(" \t"))
   89         continue;
   90 
   91     if (it->find(marker) != string::npos && 
   92         it->find(id) != string::npos) {
   93         lines.erase(it);
   94         break;
   95     }
   96     }
   97 
   98     if (!cmd.empty()) {
   99     string nline = sched + " " + marker + " " + id + " " + cmd;
  100     lines.push_back(nline);
  101     }
  102     
  103     if (!eCrontabWriteFile(lines, reason))
  104     return false;
  105 
  106     return true;
  107 }
  108 
  109 bool checkCrontabUnmanaged(const string& marker, const string& data)
  110 {
  111     vector<string> lines;
  112     if (!eCrontabGetLines(lines)) {
  113     // No crontab, answer is no
  114     return false;
  115     }
  116     // Scan crontab
  117     for (vector<string>::iterator it = lines.begin();
  118      it != lines.end(); it++) {
  119     if (it->find(marker) == string::npos && 
  120         it->find(data) != string::npos) {
  121         return true;
  122     }
  123     }
  124     return false;
  125 }
  126 
  127 /** Retrieve the scheduling for a crontab entry */
  128 bool getCrontabSched(const string& marker, const string& id, 
  129              vector<string>& sched) 
  130 {
  131     LOGDEB0("getCrontabSched: marker["  << (marker) << "], id["  << (id) << "]\n" );
  132     vector<string> lines;
  133     if (!eCrontabGetLines(lines)) {
  134     // No crontab, answer is no
  135     sched.clear();
  136     return false;
  137     }
  138     string line;
  139 
  140     for (vector<string>::iterator it = lines.begin();
  141      it != lines.end(); it++) {
  142     // Skip comment
  143     if (it->find_first_of("#") == it->find_first_not_of(" \t"))
  144         continue;
  145 
  146     if (it->find(marker) != string::npos && 
  147         it->find(id) != string::npos) {
  148         line = *it;
  149         break;
  150     }
  151     }
  152 
  153     stringToTokens(line, sched);
  154     sched.resize(5);
  155     return true;
  156 }
  157 
  158 #else // TEST ->
  159 
  160 #include <stdio.h>
  161 #include <stdlib.h>
  162 #include <unistd.h>
  163 #include <errno.h>
  164 #include <string.h>
  165 
  166 #include <string>
  167 #include <iostream>
  168 
  169 using namespace std;
  170 
  171 #include "ecrontab.h"
  172 
  173 
  174 static char *thisprog;
  175 
  176 static char usage [] =
  177 " -a add or replace crontab line \n"
  178 " -d delete crontab line \n"
  179 " -s get scheduling \n"    
  180 " -c <string> check for unmanaged lines for string\n"
  181 ;
  182 static void
  183 Usage(void)
  184 {
  185     fprintf(stderr, "%s: usage:\n%s", thisprog, usage);
  186     exit(1);
  187 }
  188 
  189 static int     op_flags;
  190 #define OPT_MOINS 0x1
  191 #define OPT_a     0x2 
  192 #define OPT_d     0x4 
  193 #define OPT_w     0x8
  194 #define OPT_c     0x10
  195 #define OPT_s     0x20
  196 
  197 const string& marker("RCLCRON_RCLINDEX=");
  198 // Note of course the -w does not make sense for a cron entry
  199 const string& cmd0("recollindex -w ");
  200 const string& id("RECOLL_CONFDIR=\"/home/dockes/.recoll/\"");
  201 const string& sched("30 8 * 1 *");
  202 
  203 int main(int argc, char **argv)
  204 {
  205   thisprog = argv[0];
  206   argc--; argv++;
  207 
  208   string wt = "10";
  209   string cmd;
  210 
  211   while (argc > 0 && **argv == '-') {
  212     (*argv)++;
  213     if (!(**argv))
  214       /* Cas du "adb - core" */
  215       Usage();
  216     while (**argv)
  217       switch (*(*argv)++) {
  218       case 'a': op_flags |= OPT_a; break;
  219       case 'c': op_flags |= OPT_c; if (argc < 2)  Usage();
  220       cmd = *(++argv); argc--; 
  221       goto b1;
  222       case 'd': op_flags |= OPT_d; break;
  223       case 's': op_flags |= OPT_s; break;
  224       case 'w': op_flags |= OPT_w; if (argc < 2)  Usage();
  225       wt = *(++argv); argc--; 
  226       goto b1;
  227       
  228       default: Usage(); break;
  229       }
  230   b1: argc--; argv++;
  231   }
  232 
  233   if (argc != 0)
  234     Usage();
  235 
  236   string reason;
  237   bool status = false;
  238   
  239   if (op_flags & OPT_a) {
  240       cmd = cmd0 + wt;
  241       status = editCrontab(marker, id, sched, cmd, reason);
  242   } else if (op_flags & OPT_d) {
  243       status = editCrontab(marker, id, sched, "", reason);
  244   } else if (op_flags & OPT_s) {
  245       vector<string> sched;
  246       if (!(status = getCrontabSched(marker, id, sched))) {
  247       cerr << "getCrontabSched failed: " << reason << endl;
  248       exit(1);
  249       }
  250       cout << "sched vec size " << sched.size() << endl;
  251       cout << "mins " << sched[0] << " hours " << sched[1] <<
  252       " days of month " << sched[2] << " months " << sched[3] << 
  253       " days of week " << sched[4] << endl;
  254       exit(0);
  255       
  256   } else if (op_flags & OPT_c) {
  257       if ((status = checkCrontabUnmanaged(marker, cmd))) {
  258       cerr << "crontab has unmanaged lines for " << cmd << endl;
  259       exit(1);
  260       }
  261       exit(0);
  262   } else {
  263       Usage();
  264   }
  265   if (!status) {
  266       cerr << "editCrontab failed: " << reason << endl;
  267       exit(1);
  268   }
  269   exit(0);
  270 }
  271 #endif // TEST
  272