FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
delagent.c
Go to the documentation of this file.
1 /********************************************************
2  Copyright (C) 2007-2013 Hewlett-Packard Development Company, L.P.
3  Copyright (C) 2015-2019 Siemens AG
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  version 2 as published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  ********************************************************/
18 
60 #include "delagent.h"
61 
62 #ifdef COMMIT_HASH_S
63 char BuildVersion[]="delagent build version: " VERSION_S " r(" COMMIT_HASH_S ").\n";
64 #else
65 char BuildVersion[]="delagent build version: NULL.\n";
66 #endif
67 
68 
69 /***********************************************
70  \brief Print agent usage for the user
71 
72  Command line options allow you to write the agent so it works
73  stand alone, in addition to working with the scheduler.
74  This simplifies code development and testing.
75  So if you have options, have a usage().
76  Here are some suggested options (in addition to the program
77  specific options you may already have).
78  \param Name Absolute path of the agent called by the user.
79  ***********************************************/
80 void usage (char *Name)
81 {
82  fprintf(stderr,"Usage: %s [options]\n",Name);
83  fprintf(stderr," List or delete uploads.\n");
84  fprintf(stderr," Options\n");
85  fprintf(stderr," -i :: Initialize the DB, then exit.\n");
86  fprintf(stderr," -u :: List uploads IDs.\n");
87  fprintf(stderr," -U # :: Delete upload ID.\n");
88  fprintf(stderr," -f :: List folder IDs.\n");
89  fprintf(stderr," -F # :: Delete folder ID and all uploads under this folder.\n");
90  fprintf(stderr," Folder '1' is the default folder. '-F 1' will delete\n");
91  fprintf(stderr," every upload and folder in the navigation tree.\n");
92  fprintf(stderr," use -P to indicate parent of the copied folder.\n");
93  fprintf(stderr," -s :: Run from the scheduler.\n");
94  fprintf(stderr," -T :: TEST -- do not update the DB or delete any files (just pretend)\n");
95  fprintf(stderr," -v :: Verbose (-vv for more verbose)\n");
96  fprintf(stderr," -c # :: Specify the directory for the system configuration\n");
97  fprintf(stderr," -V :: print the version info, then exit.\n");
98  fprintf(stderr," --user|-n # :: user name\n");
99  fprintf(stderr," --password|-p # :: password\n");
100 } /* usage() */
101 
109 void writeMessageAfterDelete(char *kind, long id, char *userName, int returnedCode)
110 {
111  if (0 == returnedCode)
112  {
113  fprintf(stdout, "The %s '%ld' is deleted by the user '%s'.\n", kind, id, userName);
114  }
115  else
116  {
117  fprintf(stdout, "Deletion failed: user '%s' does not have the permsssion to delete the %s '%ld', or the %s '%ld' does not exist.\n", userName, kind, id, kind, id);
118  exitNow(returnedCode);
119  }
120 }
121 
162 int main (int argc, char *argv[])
163 {
164  int c;
165  int listProj=0, listFolder=0;
166  long delUpload=0, delFolder=0, delFolderParent=0;
167  int scheduler=0; /* should it run from the scheduler? */
168  int gotArg=0;
169  char *agentDesc = "Deletes upload. Other list/delete options available from the command line.";
170  char *commitHash;
171  char *version;
172  char agentRev[myBUFSIZ];
173  int optionIndex = 0;
174  char *userName = NULL;
175  char *password = NULL;
176  int userId = -1;
177  int userPerm = -1;
178  int returnedCode = 0;
179 
180  fo_scheduler_connect(&argc, argv, &pgConn);
181 
182  static struct option long_options[] =
183  {
184  {"user", required_argument, 0, 'n'},
185  {"password", required_argument, 0, 'p'},
186  {0, 0, 0, 0}
187  };
188 
189  while ((c = getopt_long (argc, argv, "n:p:ifF:lL:sTuU:P:vVc:h",
190  long_options, &optionIndex)) != -1)
191  {
192  switch (c)
193  {
194  case 'n':
195  userName = optarg;
196  break;
197  case 'p':
198  password = optarg;
199  break;
200  case 'i':
201  PQfinish(pgConn);
202  return(0);
203  case 'f':
204  listFolder=1;
205  gotArg=1;
206  break;
207  case 'F':
208  delFolder=atol(optarg);
209  gotArg=1;
210  break;
211  case 'P':
212  delFolderParent=atol(optarg);
213  gotArg=1;
214  break;
215  case 's':
216  scheduler=1;
217  gotArg=1;
218  break;
219  case 'T':
220  Test++;
221  break;
222  case 'u':
223  listProj=1;
224  gotArg=1;
225  break;
226  case 'U':
227  delUpload=atol(optarg);
228  gotArg=1;
229  break;
230  case 'v':
231  Verbose++;
232  break;
233  case 'c':
234  gotArg=1;
235  break; /* handled by fo_scheduler_connect() */
236  case 'V':
237  printf("%s", BuildVersion);
238  PQfinish(pgConn);
239  return(0);
240  default:
241  usage(argv[0]);
242  exitNow(-1);
243  }
244  }
245 
246  if (!gotArg)
247  {
248  usage(argv[0]);
249  exitNow(-1);
250  }
251 
252  if (scheduler != 1)
253  {
254  if (0 != authentication(userName, password, &userId, &userPerm))
255  {
256  LOG_FATAL("User name or password is invalid.\n");
257  exitNow(-1);
258  }
259 
260  commitHash = fo_sysconfig("delagent", "COMMIT_HASH");
261  version = fo_sysconfig("delagent", "VERSION");
262  sprintf(agentRev, "%s.%s", version, commitHash);
263  /* Get the Agent Key from the DB */
264  fo_GetAgentKey(pgConn, basename(argv[0]), 0, agentRev, agentDesc);
265 
266  if (listProj)
267  {
268  returnedCode = listUploads(userId, userPerm);
269  }
270  if (returnedCode < 0)
271  {
272  return returnedCode;
273  }
274  if (listFolder)
275  {
276  returnedCode = listFolders(userId, userPerm);
277  }
278  if (returnedCode < 0)
279  {
280  return returnedCode;
281  }
282 
283  alarm(60); /* from this point on, handle the alarm */
284  if (delUpload)
285  {
286  returnedCode = deleteUpload(delUpload, userId, userPerm);
287 
288  writeMessageAfterDelete("upload", delUpload, userName, returnedCode);
289  }
290  if (delFolder)
291  {
292  returnedCode = deleteFolder(delFolder, delFolderParent, userId, userPerm);
293 
294  writeMessageAfterDelete("folder", delFolder, userName, returnedCode);
295  }
296  }
297  else
298  {
299  /* process from the scheduler */
301  }
302 
303  exitNow(0);
304  return(returnedCode);
305 } /* main() */
int Test
Definition: util.c:29
int deleteFolder(long cFolder, long pFolder, int userId, int userPerm)
recursively delete a folder
Definition: util.c:940
char BuildVersion[]
Definition: buckets.c:79
PGconn * pgConn
Database connection.
Definition: adj2nest.c:98
void doSchedulerTasks()
process the jobs from scheduler
Definition: util.c:1060
int Verbose
Verbose level.
Definition: util.c:28
void fo_scheduler_connect(int *argc, char **argv, PGconn **db_conn)
Establish a connection between an agent and the scheduler.
void writeMessageAfterDelete(char *kind, long id, char *userName, int returnedCode)
Write message to user after success/failure.
Definition: delagent.c:109
int deleteUpload(long uploadId, int userId, int userPerm)
Given an upload ID, delete it.
Definition: util.c:289
FUNCTION int fo_GetAgentKey(PGconn *pgConn, const char *agent_name, long Upload_pk, const char *rev, const char *agent_desc)
Get the latest enabled agent key (agent_pk) from the database.
Definition: libfossagent.c:172
FUNCTION void usage(char *name)
Definition: usage.c:30
int main(int argc, char *argv[])
main function for the delagent
Definition: delagent.c:162
int listFolders(int userId, int userPerm)
List every folder.
Definition: util.c:843
void exitNow(int exitVal)
Exit function. This does all cleanup and should be used instead of calling exit() or main() return...
Definition: util.c:1099
int authentication(char *user, char *password, int *userId, int *userPerm)
if this account is valid
Definition: util.c:105
int listUploads(int userId, int userPerm)
List every upload ID.
Definition: util.c:886
char * fo_sysconfig(const char *sectionname, const char *variablename)
gets a system configuration variable from the configuration data.