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 gotArg=0;
157  char *agentDesc = "Deletes upload. Other list/delete options available from the command line.";
158  char *commitHash;
159  char *version;
160  char agentRev[myBUFSIZ];
161  int optionIndex = 0;
162  char *userName = NULL;
163  char *password = NULL;
164  int userId = -1;
165  int userPerm = -1;
166  int returnedCode = 0;
167 
168  fo_scheduler_connect(&argc, argv, &pgConn);
169 
170  static struct option long_options[] =
171  {
172  {"user", required_argument, 0, 'n'},
173  {"password", required_argument, 0, 'p'},
174  {0, 0, 0, 0}
175  };
176 
177  while ((c = getopt_long (argc, argv, "n:p:ifF:lL:sTuU:P:vVc:h",
178  long_options, &optionIndex)) != -1)
179  {
180  switch (c)
181  {
182  case 'n':
183  userName = optarg;
184  break;
185  case 'p':
186  password = optarg;
187  break;
188  case 'i':
189  PQfinish(pgConn);
190  return(0);
191  case 'f':
192  listFolder=1;
193  gotArg=1;
194  break;
195  case 'F':
196  delFolder=atol(optarg);
197  gotArg=1;
198  break;
199  case 'P':
200  delFolderParent=atol(optarg);
201  gotArg=1;
202  break;
203  case 's':
204  Scheduler=1;
205  gotArg=1;
206  break;
207  case 'T':
208  Test++;
209  break;
210  case 'u':
211  listProj=1;
212  gotArg=1;
213  break;
214  case 'U':
215  delUpload=atol(optarg);
216  gotArg=1;
217  break;
218  case 'v':
219  Verbose++;
220  break;
221  case 'c':
222  gotArg=1;
223  break; /* handled by fo_scheduler_connect() */
224  case 'V':
225  printf("%s", BuildVersion);
226  PQfinish(pgConn);
227  return(0);
228  default:
229  usage(argv[0]);
230  exitNow(-1);
231  }
232  }
233 
234  if (!gotArg)
235  {
236  usage(argv[0]);
237  exitNow(-1);
238  }
239 
240  if (Scheduler != 1)
241  {
242  if (0 != authentication(userName, password, &userId, &userPerm))
243  {
244  LOG_FATAL("User name or password is invalid.\n");
245  exitNow(-1);
246  }
247 
248  commitHash = fo_sysconfig("delagent", "COMMIT_HASH");
249  version = fo_sysconfig("delagent", "VERSION");
250  sprintf(agentRev, "%s.%s", version, commitHash);
251  /* Get the Agent Key from the DB */
252  fo_GetAgentKey(pgConn, basename(argv[0]), 0, agentRev, agentDesc);
253 
254  if (listProj)
255  {
256  returnedCode = listUploads(userId, userPerm);
257  }
258  if (returnedCode < 0)
259  {
260  return returnedCode;
261  }
262  if (listFolder)
263  {
264  returnedCode = listFolders(userId, userPerm);
265  }
266  if (returnedCode < 0)
267  {
268  return returnedCode;
269  }
270 
271  alarm(60); /* from this point on, handle the alarm */
272  if (delUpload)
273  {
274  returnedCode = deleteUpload(delUpload, userId, userPerm);
275 
276  writeMessageAfterDelete("upload", delUpload, userName, returnedCode);
277  }
278  if (delFolder)
279  {
280  returnedCode = deleteFolder(delFolder, delFolderParent, userId, userPerm);
281 
282  writeMessageAfterDelete("folder", delFolder, userName, returnedCode);
283  }
284  }
285  else
286  {
287  /* process from the scheduler */
289  }
290 
291  exitNow(0);
292  return(returnedCode);
293 } /* 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:1138
int authentication(char *user, char *password, int *userId, int *userPerm)
if this account is valid
Definition: util.c:129
void exitNow(int exitVal)
Exit function. This does all cleanup and should be used instead of calling exit() or main() return.
Definition: util.c:1177
int Verbose
Verbose level.
Definition: util.c:19
int listUploads(int userId, int userPerm)
List every upload ID.
Definition: util.c:964
int Test
Definition: util.c:20
int Scheduler
Definition: util.c:21
int listFolders(int userId, int userPerm)
List every folder.
Definition: util.c:921
int deleteUpload(long uploadId, int userId, int userPerm)
Given an upload ID, delete it.
Definition: util.c:318
int deleteFolder(long cFolder, long pFolder, int userId, int userPerm)
recursively delete a folder
Definition: util.c:1018
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