FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
main.c
Go to the documentation of this file.
1 /***************************************************************
2  Copyright (C) 2011-2013 Hewlett-Packard Development Company, L.P.
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public License
6  version 2 as published by the Free Software Foundation.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License along
14  with this program; if not, write to the Free Software Foundation, Inc.,
15  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 
17  ***************************************************************/
18 
25 #define _GNU_SOURCE
26 #include "wget_agent.h"
27 
28 #ifdef COMMIT_HASH_S
29 char BuildVersion[]="wget_agent build version: " VERSION_S " r(" COMMIT_HASH_S ").\n";
30 #else
31 char BuildVersion[]="wget_agent build version: NULL.\n";
32 #endif
33 
107 int main (int argc, char *argv[])
108 {
109  int arg;
110  char *Parm = NULL;
111  char *TempFileDir=NULL;
112  int c;
113  int InitFlag=0;
114  int CmdlineFlag = 0; /* run from command line flag, 1 yes, 0 not */
115  int user_pk;
116  char *agent_desc = "Network downloader. Uses wget(1).";
117 
118  memset(GlobalTempFile,'\0',STRMAX);
119  memset(GlobalURL,'\0',URLMAX);
120  memset(GlobalParam,'\0',STRMAX);
121  memset(GlobalType,'\0',STRMAX);
122  GlobalUploadKey = -1;
123  int upload_pk = 0; // the upload primary key
124  //int Agent_pk;
125  char *COMMIT_HASH;
126  char *VERSION;
127  char agent_rev[STRMAX];
128 
129  /* open the connection to the scheduler and configuration */
130  fo_scheduler_connect(&argc, argv, &pgConn);
131 
132  /* Process command-line */
133  while((c = getopt(argc,argv,"d:Gg:ik:A:R:l:Cc:Vvh")) != -1)
134  {
135  switch(c)
136  {
137  case 'd':
138  TempFileDir = PathCheck(optarg);
139  break;
140  case 'g':
141  {
142  struct group *SG;
143  SG = getgrnam(optarg);
144  if (SG) ForceGroup = SG->gr_gid;
145  }
146  break;
147  case 'G':
149  break;
150  case 'i':
151  InitFlag=1;
152  break;
153  case 'k':
154  GlobalUploadKey = atol(optarg);
155  if (!GlobalTempFile[0])
156  strcpy(GlobalTempFile,"wget.default_download");
157  break;
158  case 'A':
159  strncat(GlobalParam, " -A ", STRMAX - strlen(GlobalParam) -1);
160  strncat(GlobalParam, optarg, STRMAX - strlen(GlobalParam) -1);
161  break;
162  case 'R':
163  strncat(GlobalParam, " -R ", STRMAX - strlen(GlobalParam) -1);
164  strncat(GlobalParam, optarg, STRMAX - strlen(GlobalParam) -1);
165  break;
166  case 'l':
167  strncat(GlobalParam, " -l ", STRMAX - strlen(GlobalParam) -1);
168  strncat(GlobalParam, optarg, STRMAX - strlen(GlobalParam) -1);
169  break;
170  case 'c': break; /* handled by fo_scheduler_connect() */
171  case 'C':
172  CmdlineFlag = 1;
173  break;
174  case 'v':
175  agent_verbose++; // global agent verbose flag.
176  break;
177  case 'V':
178  printf("%s", BuildVersion);
179  SafeExit(0);
180  default:
181  Usage(argv[0]);
182  SafeExit(-1);
183  }
184  }
185  if (argc - optind > 1)
186  {
187  Usage(argv[0]);
188  SafeExit(-1);
189  }
190 
191  /* When initializing the DB, don't do anything else */
192  if (InitFlag)
193  {
194  if (pgConn) PQfinish(pgConn);
195  SafeExit(0);
196  }
197 
198  COMMIT_HASH = fo_sysconfig("wget_agent", "COMMIT_HASH");
199  VERSION = fo_sysconfig("wget_agent", "VERSION");
200  sprintf(agent_rev, "%s.%s", VERSION, COMMIT_HASH);
201  /* Get the Agent Key from the DB */
202  fo_GetAgentKey(pgConn, basename(argv[0]), GlobalUploadKey, agent_rev, agent_desc);
203 
204  /* get proxy */
205  GetProxy();
206 
207  /* Run from the command-line (for testing) */
208  for(arg=optind; arg < argc; arg++)
209  {
210  memset(GlobalURL,'\0',sizeof(GlobalURL));
211  strncpy(GlobalURL,argv[arg],sizeof(GlobalURL));
212  /* If the file contains "://" then assume it is a URL.
213  Else, assume it is a file. */
214  LOG_VERBOSE0("Command-line: %s",GlobalURL);
215  if (strstr(GlobalURL,"://"))
216  {
218  LOG_VERBOSE0("It's a URL");
219  if (GetURL(GlobalTempFile,GlobalURL,TempFileDir) != 0)
220  {
221  LOG_FATAL("Download of %s failed.",GlobalURL);
222  SafeExit(21);
223  }
224  if (GlobalUploadKey != -1) { DBLoadGold(); }
225  unlink(GlobalTempFile);
226  }
227  else /* must be a file */
228  {
229  LOG_VERBOSE0("It's a file -- GlobalUploadKey = %ld",GlobalUploadKey);
230  if (GlobalUploadKey != -1)
231  {
232  memcpy(GlobalTempFile,GlobalURL,STRMAX);
233  DBLoadGold();
234  }
235  }
236  }
237 
238  /* Run from scheduler! */
239  if (0 == CmdlineFlag)
240  {
241  user_pk = fo_scheduler_userID(); /* get user_pk for user who queued the agent */
242  while(fo_scheduler_next())
243  {
244  Parm = fo_scheduler_current(); /* get piece of information, including upload_pk, downloadfile url, and parameters */
245  if (Parm && Parm[0])
246  {
248  /* set globals: uploadpk, downloadfile url, parameters */
249  SetEnv(Parm,TempFileDir);
250  upload_pk = GlobalUploadKey;
251 
252  /* Check Permissions */
253  if (GetUploadPerm(pgConn, upload_pk, user_pk) < PERM_WRITE)
254  {
255  LOG_ERROR("You have no update permissions on upload %d", upload_pk);
256  continue;
257  }
258 
259  char TempDir[STRMAX];
260  memset(TempDir,'\0',STRMAX);
261  snprintf(TempDir, STRMAX-1, "%s/wget", TempFileDir); // /var/local/lib/fossology/agents/wget
262  struct stat Status = {};
263 
264  if (GlobalType[0])
265  {
266  if (GetVersionControl() == 0)
267  {
268  DBLoadGold();
269  unlink(GlobalTempFile);
270  }
271  else
272  {
273  LOG_FATAL("upload %ld File retrieval failed: uploadpk=%ld tempfile=%s URL=%s Type=%s",
275  SafeExit(23);
276  }
277  }
278  else if (strstr(GlobalURL, "*") || stat(GlobalURL, &Status) == 0)
279  {
280  if (!Archivefs(GlobalURL, GlobalTempFile, TempFileDir, Status))
281  {
282  LOG_FATAL("Failed to archive. GlobalURL, GlobalTempFile, TempFileDir are: %s, %s, %s, "
283  "Mode is: %lo (octal)\n", GlobalURL, GlobalTempFile, TempFileDir, (unsigned long) Status.st_mode);
284  SafeExit(50);
285  }
286  DBLoadGold();
287  unlink(GlobalTempFile);
288  }
289  else
290  {
291  if (GetURL(GlobalTempFile,GlobalURL,TempDir) == 0)
292  {
293  DBLoadGold();
294  unlink(GlobalTempFile);
295  }
296  else
297  {
298  LOG_FATAL("upload %ld File retrieval failed: uploadpk=%ld tempfile=%s URL=%s",
300  SafeExit(22);
301  }
302  }
303  }
304  }
305  } /* if run from scheduler */
306 
307  SafeExit(0);
308  exit(0); /* to prevent compiler warning */
309 } /* main() */
310 
char * PathCheck(char *DirPath)
Check if path contains a "%U" or "%H". If so, substitute a unique ID for U.
Definition: utils.c:1672
char BuildVersion[]
Definition: buckets.c:79
gid_t ForceGroup
Set to group id to be used for download files.
Definition: wget_agent.c:43
int Archivefs(char *Path, char *TempFile, char *TempFileDir, struct stat Status)
Copy downloaded files to temporary directory.
Definition: wget_agent.c:831
PGconn * pgConn
Database connection.
Definition: adj2nest.c:98
long GlobalUploadKey
Input for this system.
Definition: wget_agent.c:35
char GlobalParam[STRMAX]
Additional parameters.
Definition: wget_agent.c:39
char * fo_scheduler_current()
Get the last read string from the scheduler.
int GetURL(char *TempFile, char *URL, char *TempFileDir)
Do the wget.
Definition: wget_agent.c:340
void fo_scheduler_connect(int *argc, char **argv, PGconn **db_conn)
Establish a connection between an agent and the scheduler.
char GlobalType[STRMAX]
Type of download (FILE/version control)
Definition: wget_agent.c:38
int agent_verbose
Common verbose flags for the agents, this is used so that the scheduler can change the verbose level ...
void GetProxy()
Get proxy from fossology.conf.
Definition: wget_agent.c:928
char GlobalURL[URLMAX]
URL to download.
Definition: wget_agent.c:37
void SetEnv(char *S, char *TempFileDir)
Convert input pairs into globals.
Definition: wget_agent.c:695
void DBLoadGold()
Insert a file into the database and repository.
Definition: wget_agent.c:93
char GlobalTempFile[STRMAX]
Temp file to be used.
Definition: wget_agent.c:36
int fo_scheduler_userID()
Gets the id of the user that created the job that the agent is running.
Usage()
Print Usage statement.
Definition: fo_dbcheck.php:75
int GetVersionControl()
Get source code from version control system.
Definition: wget_agent.c:578
int main(int argc, char *argv[])
main function for the pkgagent
Definition: main.c:88
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
char * fo_scheduler_next()
Get the next data to process from the scheduler.
int GlobalImportGold
Set to 0 to not store file in gold repository.
Definition: wget_agent.c:42
void fo_scheduler_heart(int i)
This function must be called by agents to let the scheduler know they are alive and how many items th...
FUNCTION int GetUploadPerm(PGconn *pgConn, long UploadPk, int user_pk)
Get users permission to this upload.
Definition: libfossagent.c:385
const char * upload_pk
Definition: sqlstatements.h:93
void SafeExit(int rc)
Close scheduler and database connections, then exit.
Definition: utils.c:88
char * fo_sysconfig(const char *sectionname, const char *variablename)
gets a system configuration variable from the configuration data.
#define PERM_WRITE
Read-Write permission.
Definition: libfossology.h:45