FOSSology  4.4.0
Open Source License Compliance by Open Source Software
delagent.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2007-2013 Hewlett-Packard Development Company, L.P.
3  SPDX-FileCopyrightText: © 2015-2019 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
49 #include "delagent.h"
50 
51 #ifdef COMMIT_HASH_S
52 char BuildVersion[]="delagent build version: " VERSION_S " r(" COMMIT_HASH_S ").\n";
53 #else
54 char BuildVersion[]="delagent build version: NULL.\n";
55 #endif
56 
57 
58 /***********************************************
59  \brief Print agent usage for the user
60 
61  Command line options allow you to write the agent so it works
62  stand alone, in addition to working with the scheduler.
63  This simplifies code development and testing.
64  So if you have options, have a usage().
65  Here are some suggested options (in addition to the program
66  specific options you may already have).
67  \param Name Absolute path of the agent called by the user.
68  ***********************************************/
69 void usage (char *Name)
70 {
71  fprintf(stderr,"Usage: %s [options]\n",Name);
72  fprintf(stderr," List or delete uploads.\n");
73  fprintf(stderr," Options\n");
74  fprintf(stderr," -i :: Initialize the DB, then exit.\n");
75  fprintf(stderr," -u :: List uploads IDs.\n");
76  fprintf(stderr," -U # :: Delete upload ID.\n");
77  fprintf(stderr," -f :: List folder IDs.\n");
78  fprintf(stderr," -F # :: Delete folder ID and all uploads under this folder.\n");
79  fprintf(stderr," Folder '1' is the default folder. '-F 1' will delete\n");
80  fprintf(stderr," every upload and folder in the navigation tree.\n");
81  fprintf(stderr," use -P to indicate parent of the copied folder.\n");
82  fprintf(stderr," -s :: Run from the scheduler.\n");
83  fprintf(stderr," -T :: TEST -- do not update the DB or delete any files (just pretend)\n");
84  fprintf(stderr," -v :: Verbose (-vv for more verbose)\n");
85  fprintf(stderr," -c # :: Specify the directory for the system configuration\n");
86  fprintf(stderr," -V :: print the version info, then exit.\n");
87  fprintf(stderr," --user|-n # :: user name\n");
88  fprintf(stderr," --password|-p # :: password\n");
89 } /* usage() */
90 
98 void writeMessageAfterDelete(char *kind, long id, char *userName, int returnedCode)
99 {
100  if (0 == returnedCode)
101  {
102  fprintf(stdout, "The %s '%ld' is deleted by the user '%s'.\n", kind, id, userName);
103  }
104  else
105  {
106  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);
107  exitNow(returnedCode);
108  }
109 }
110 
151 int main (int argc, char *argv[])
152 {
153  int c;
154  int listProj=0, listFolder=0;
155  long delUpload=0, delFolder=0, delFolderParent=0;
156  int scheduler=0; /* should it run from the scheduler? */
157  int gotArg=0;
158  char *agentDesc = "Deletes upload. Other list/delete options available from the command line.";
159  char *commitHash;
160  char *version;
161  char agentRev[myBUFSIZ];
162  int optionIndex = 0;
163  char *userName = NULL;
164  char *password = NULL;
165  int userId = -1;
166  int userPerm = -1;
167  int returnedCode = 0;
168 
169  fo_scheduler_connect(&argc, argv, &pgConn);
170 
171  static struct option long_options[] =
172  {
173  {"user", required_argument, 0, 'n'},
174  {"password", required_argument, 0, 'p'},
175  {0, 0, 0, 0}
176  };
177 
178  while ((c = getopt_long (argc, argv, "n:p:ifF:lL:sTuU:P:vVc:h",
179  long_options, &optionIndex)) != -1)
180  {
181  switch (c)
182  {
183  case 'n':
184  userName = optarg;
185  break;
186  case 'p':
187  password = optarg;
188  break;
189  case 'i':
190  PQfinish(pgConn);
191  return(0);
192  case 'f':
193  listFolder=1;
194  gotArg=1;
195  break;
196  case 'F':
197  delFolder=atol(optarg);
198  gotArg=1;
199  break;
200  case 'P':
201  delFolderParent=atol(optarg);
202  gotArg=1;
203  break;
204  case 's':
205  scheduler=1;
206  gotArg=1;
207  break;
208  case 'T':
209  Test++;
210  break;
211  case 'u':
212  listProj=1;
213  gotArg=1;
214  break;
215  case 'U':
216  delUpload=atol(optarg);
217  gotArg=1;
218  break;
219  case 'v':
220  Verbose++;
221  break;
222  case 'c':
223  gotArg=1;
224  break; /* handled by fo_scheduler_connect() */
225  case 'V':
226  printf("%s", BuildVersion);
227  PQfinish(pgConn);
228  return(0);
229  default:
230  usage(argv[0]);
231  exitNow(-1);
232  }
233  }
234 
235  if (!gotArg)
236  {
237  usage(argv[0]);
238  exitNow(-1);
239  }
240 
241  if (scheduler != 1)
242  {
243  if (0 != authentication(userName, password, &userId, &userPerm))
244  {
245  LOG_FATAL("User name or password is invalid.\n");
246  exitNow(-1);
247  }
248 
249  commitHash = fo_sysconfig("delagent", "COMMIT_HASH");
250  version = fo_sysconfig("delagent", "VERSION");
251  sprintf(agentRev, "%s.%s", version, commitHash);
252  /* Get the Agent Key from the DB */
253  fo_GetAgentKey(pgConn, basename(argv[0]), 0, agentRev, agentDesc);
254 
255  if (listProj)
256  {
257  returnedCode = listUploads(userId, userPerm);
258  }
259  if (returnedCode < 0)
260  {
261  return returnedCode;
262  }
263  if (listFolder)
264  {
265  returnedCode = listFolders(userId, userPerm);
266  }
267  if (returnedCode < 0)
268  {
269  return returnedCode;
270  }
271 
272  alarm(60); /* from this point on, handle the alarm */
273  if (delUpload)
274  {
275  returnedCode = deleteUpload(delUpload, userId, userPerm);
276 
277  writeMessageAfterDelete("upload", delUpload, userName, returnedCode);
278  }
279  if (delFolder)
280  {
281  returnedCode = deleteFolder(delFolder, delFolderParent, userId, userPerm);
282 
283  writeMessageAfterDelete("folder", delFolder, userName, returnedCode);
284  }
285  }
286  else
287  {
288  /* process from the scheduler */
290  }
291 
292  exitNow(0);
293  return(returnedCode);
294 } /* main() */
PGconn * pgConn
Database connection.
Definition: adj2nest.c:86
char BuildVersion[]
Definition: buckets.c:68
void doSchedulerTasks()
process the jobs from scheduler
Definition: util.c:1092
int authentication(char *user, char *password, int *userId, int *userPerm)
if this account is valid
Definition: util.c:96
void exitNow(int exitVal)
Exit function. This does all cleanup and should be used instead of calling exit() or main() return.
Definition: util.c:1131
int Verbose
Verbose level.
Definition: util.c:19
int listUploads(int userId, int userPerm)
List every upload ID.
Definition: util.c:918
int Test
Definition: util.c:20
int listFolders(int userId, int userPerm)
List every folder.
Definition: util.c:875
int deleteUpload(long uploadId, int userId, int userPerm)
Given an upload ID, delete it.
Definition: util.c:285
int deleteFolder(long cFolder, long pFolder, int userId, int userPerm)
recursively delete a folder
Definition: util.c:972
int main(int argc, char *argv[])
main function for the delagent
Definition: delagent.c:151
void writeMessageAfterDelete(char *kind, long id, char *userName, int returnedCode)
Write message to user after success/failure.
Definition: delagent.c:98
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:158
char * fo_sysconfig(const char *sectionname, const char *variablename)
gets a system configuration variable from the configuration data.
void fo_scheduler_connect(int *argc, char **argv, PGconn **db_conn)
Establish a connection between an agent and the scheduler.
FUNCTION void usage(char *name)
Definition: usage.c:18