diff --git a/src/demomod/agent/Makefile b/src/demomod/agent/Makefile index 5a216686..de5316b6 100644 --- a/src/demomod/agent/Makefile +++ b/src/demomod/agent/Makefile @@ -1,57 +1,55 @@ ##################################################################### # Copyright (C) 2013 Hewlett-Packard Development Company, L.P. +# Copyright (C) 2016 Siemens AG # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # version 2 as published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. ###################################################################### TOP=../../.. VARS=$(TOP)/Makefile.conf include $(VARS) -DEF=-DPROJECTSTATEDIR='"$(PROJECTSTATEDIR)"' -DDATADIR='"$(DATADIR)"' -LOCAL_CFLAGS= $(DEF) $(DEFS) +CFLAGS_LOCAL = $(FO_CFLAGS) -Werror +CFLAGS_LOCALO = $(FO_CFLAGS) EXE=demomod OBJS=usage.o utils.o process.o HDRS=demomod.h all: $(EXE) @echo "make demomod agent" -$(EXE): demomod.c $(FOLIB) $(OBJS) $(HDRS) - $(CC) demomod.c $(OBJS) -lpq $(LOCAL_CFLAGS) -o $@ +demomod: demomod.o $(OBJS) + $(CC) demomod.o $(OBJS) $(CFLAGS_LOCAL) $(FO_LDFLAGS) -o $@ -usage.o: $(HDRS) usage.c - $(CC) -c $(LOCAL_CFLAGS) usage.c +demomod.o: demomod.c $(HDRS) $(DB) $(REPO) $(AGENTLIB) $(VARS) + $(CC) -c $< $(CFLAGS_LOCALO) $(DEFS) -utils.o: $(HDRS) utils.c - $(CC) -c $(LOCAL_CFLAGS) utils.c - -process.o: $(HDRS) process.c - $(CC) -c $(LOCAL_CFLAGS) process.c +$(OBJS): %.o: %.c $(HDRS) $(DB) $(VARS) + $(CC) -c $< $(CFLAGS_DBO) $(FO_CFLAGS) $(FOLIB): $(MAKE) -C $(FOLIBDIR) install: all @echo "make demomod agent install" $(INSTALL_PROGRAM) $(EXE) $(DESTDIR)$(MODDIR)/$(EXE)/agent/$(EXE) uninstall: @echo "make demomod agent uninstall" rm -rf $(DESTDIR)$(MODDIR)/$(EXE)/agent clean: rm -f $(EXE) *.o core -.PHONY: all install uninstall clean test $(FOLIB) coverage +.PHONY: all install uninstall clean $(FOLIB) diff --git a/src/demomod/agent/demomod.c b/src/demomod/agent/demomod.c index e79558e1..e4c3c0e6 100644 --- a/src/demomod/agent/demomod.c +++ b/src/demomod/agent/demomod.c @@ -1,185 +1,185 @@ /*************************************************************** Copyright (C) 2013 Hewlett-Packard Development Company, L.P. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - ***************************************************************/ +***************************************************************/ /** - \file demomod.c - \brief FOSSology demonstration agent + \file demomod.c + \brief FOSSology demonstration agent - This is the agent for the demomod FOSSology module. - The purpose of this is to show the structure of an agent. So, by design, - it does very little. - - Given an upload_pk, this agent simply reads the first 32 bytes of each file and records - them in the demomod table. + This is the agent for the demomod FOSSology module. + The purpose of this is to show the structure of an agent. So, by design, + it does very little. - The way permissions work on this agent is that if it runs from the scheduler, - the normal fossology upload permissions are checked. However, if demomod agent runs from - the command line, no permissions are checked and the output is to stdout, not the database. - */ + Given an upload_pk, this agent simply reads the first 32 bytes of each file and records + them in the demomod table. + + The way permissions work on this agent is that if it runs from the scheduler, + the normal fossology upload permissions are checked. However, if demomod agent runs from + the command line, no permissions are checked and the output is to stdout, not the database. +*/ #include "demomod.h" #ifdef COMMIT_HASH_S char BuildVersion[]="demomod build version: " VERSION_S " r(" COMMIT_HASH_S ").\n"; #else char BuildVersion[]="demomod build version: NULL.\n"; #endif /********** Globals *************/ psqlCopy_t psqlcpy = 0; // fo_sqlCopy struct used for fast data insertion PGconn *pgConn = 0; // database connection /****************************************************/ -int main(int argc, char **argv) +int main(int argc, char **argv) { char *agentDesc = "demomod demonstration module"; char *AgentARSName = "demomod_ars"; int cmdopt; PGresult *ars_result; char sqlbuf[512]; int agent_pk = 0; int user_pk = 0; char *COMMIT_HASH; char *VERSION; char agent_rev[myBUFSIZ]; int upload_pk = 0; int ars_pk = 0; int Unused = 0; int i; FileResult_t FileResult; /* connect to the scheduler */ fo_scheduler_connect(&argc, argv, &pgConn); user_pk = fo_scheduler_userID(); /* get user_pk for user who queued the agent */ - /* get agent pk + /* get agent pk * Note, if GetAgentKey fails, this process will exit. */ COMMIT_HASH = fo_sysconfig("demomod", "COMMIT_HASH"); VERSION = fo_sysconfig("demomod", "VERSION"); snprintf(agent_rev, sizeof(agent_rev), "%s.%s", VERSION, COMMIT_HASH); agent_pk = fo_GetAgentKey(pgConn, basename(argv[0]), Unused, agent_rev, agentDesc); /* command line options */ - while ((cmdopt = getopt(argc, argv, "ivVc:")) != -1) + while ((cmdopt = getopt(argc, argv, "ivVc:")) != -1) { - switch (cmdopt) + switch (cmdopt) { - case 'i': /* "Initialize" */ - ExitNow(0); - case 'v': /* verbose output for debugging */ - agent_verbose++; // global agent verbose flag. Can be changed in running agent by the scheduler on each fo_scheduler_next() call - break; - case 'V': /* print version info */ - printf("%s", BuildVersion); - ExitNow(0); - case 'c': break; /* handled by fo_scheduler_connect() */ - default: - Usage(argv[0]); - ExitNow(-1); + case 'i': /* "Initialize" */ + ExitNow(0); + case 'v': /* verbose output for debugging */ + agent_verbose++; // global agent verbose flag. Can be changed in running agent by the scheduler on each fo_scheduler_next() call + break; + case 'V': /* print version info */ + printf("%s", BuildVersion); + ExitNow(0); + case 'c': break; /* handled by fo_scheduler_connect() */ + default: + Usage(argv[0]); + ExitNow(-1); } } if (optind >= argc) // if this is true then files weren't specified on the command line, so use the scheduler { /* make sure the demomod and demomod_ars tables exists */ CheckTable(AgentARSName); user_pk = fo_scheduler_userID(); /* get user_pk for user who queued the agent */ /* The following is used for debugging only! * Once the user_pk is set, you can test this agent as if it were run from the scheduler by * echo 158 | ./demomod -c /usr/local/etc/fossology/ * where 158 is the upload_pk */ //user_pk=2; // TODO: REMOVE THIS FROM THE PRODUCTION CODE! /* It isn't necessary to use a loop here because this agent only reads one item * from the scheduler, the upload_pk. However, it is possible to queue jobs such - * that the scheduler will pass multiple data items into an agent, so I'm just + * that the scheduler will pass multiple data items into an agent, so I'm just * going to use a 'while' even though for demomod it will only run once. */ while(fo_scheduler_next()) { upload_pk = atoi(fo_scheduler_current()); LOG_VERBOSE("demomod upload_pk is %d\n", upload_pk); - if (upload_pk ==0) + if (upload_pk ==0) { LOG_ERROR("demomod was passed a zero upload_pk. This is an invalid key."); ExitNow(-2); } /* Check Permissions */ if (GetUploadPerm(pgConn, upload_pk, user_pk) < PERM_WRITE) { LOG_ERROR("You do not have write permission on upload %d", upload_pk); ExitNow(-3); } /* * check if demomod has already been run on this upload. */ snprintf(sqlbuf, sizeof(sqlbuf), - "select ars_pk from %s,agent where agent_pk=agent_fk and ars_success=true \ + "select ars_pk from %s,agent where agent_pk=agent_fk and ars_success=true \ and upload_fk='%d' and agent_fk='%d'", - AgentARSName, upload_pk, agent_pk); + AgentARSName, upload_pk, agent_pk); ars_result = PQexec(pgConn, sqlbuf); if (fo_checkPQresult(pgConn, ars_result, sqlbuf, __FILE__, __LINE__)) break; if (PQntuples(ars_result) > 0) { LOG_WARNING("Ignoring requested demomod scan of upload %d - Results are already in the database.\n",upload_pk); PQclear(ars_result); continue; } PQclear(ars_result); /* Record scan start in the agent ars table, this is the agent audit trail. */ ars_pk = fo_WriteARS(pgConn, ars_pk, upload_pk, agent_pk, AgentARSName, 0, 0); /* Create the sql copy structure for the demomod table. * The fo_sqlCopy functions are used to batch copy records into the database. This is * much faster than single inserts. * This creates a struct with buffer size of 100,000 bytes, and three columns */ psqlcpy = fo_sqlCopyCreate(pgConn, "demomod", 100000, 3, "pfile_fk", "agent_fk", "firstbytes" ); if (!psqlcpy) ExitNow(-4); /* process the upload_pk */ if(ProcessUpload(upload_pk, agent_pk) != 0) ExitNow(-5); /* Flush the sqlCopy buffer */ fo_sqlCopyDestroy(psqlcpy, 1); /* Record scan success in ars table. */ fo_WriteARS(pgConn, ars_pk, upload_pk, agent_pk, AgentARSName, 0, 1); } } else { /* loop through files on the command line */ for (i=optind; iBuf, sizeof(char), sizeof(FileResult->Buf), fin); /* Convert to a hex string and save in the FileResult */ Char2Hex(FileResult->Buf, DataSize, FileResult->HexStr); /* Close the file */ fclose(fin); /* Return Success */ return 0; } /** * @brief Process a single upload - read the first 32 bytes in each file * @param upload_pk * @param agent_fk version of the agent that is processing this upload * * @returns 0 if success, may also write fatal error to stderr. */ FUNCTION int ProcessUpload(int upload_pk, int agent_fk) { PGresult* result; // the result of the database access int i; int rv; // generic return value int numrows; // generic return value int pfile_pk; char *FilePath; // complete path to file in the repository char *uploadtree_tablename; char LastChar; char sqlbuf[1024]; char FileName[128]; char DataBuf[128]; char *RepoArea = "files"; // char *ufile_mode = "30000000"; // This mode is for artifacts and containers (which will be excluded) char *ufile_mode = "10000000"; // This mode is for artifacts only (which will be excluded) FileResult_t FileResult; /* Select each upload filename (repository filename) that hasn't been processed by this agent yet */ char* SelectFilename_sql = "\ SELECT pfile_pk, pfile_sha1 || '.' || pfile_md5 || '.' || pfile_size AS pfilename \ FROM ( SELECT distinct(pfile_fk) AS PF FROM uploadtree \ WHERE upload_fk = %d and (ufile_mode&x'%s'::int)=0 \ ) AS SS \ left outer join demomod on (PF = pfile_fk ) \ inner join pfile on (PF = pfile_pk) \ WHERE demomod_pk IS null or agent_fk <> %d"; char* SelectFilename2_sql = "\ SELECT pfile_pk, pfile_sha1 || '.' || pfile_md5 || '.' || pfile_size AS pfilename \ FROM ( SELECT distinct(pfile_fk) AS PF FROM %s \ WHERE (ufile_mode&x'%s'::int)=0 \ ) AS SS \ left outer join demomod on (PF = pfile_fk ) \ inner join pfile on (PF = pfile_pk) \ WHERE demomod_pk IS null or agent_fk <> %d"; /* Find the correct uploadtree table name */ uploadtree_tablename = GetUploadtreeTableName(pgConn, upload_pk); if (!uploadtree_tablename) { LOG_FATAL("demomod passed invalid upload, upload_pk = %d", upload_pk); return(-110); -} + } /* If the last character of the uploadtree_tablename is a digit, then we don't need upload_fk * in the query (because the table only has that uplaod). */ LastChar = uploadtree_tablename[strlen(uploadtree_tablename)-1]; if (LastChar >= '0' && LastChar <= '9') { snprintf(sqlbuf, sizeof(sqlbuf), SelectFilename2_sql, uploadtree_tablename, ufile_mode, agent_fk); } else { snprintf(sqlbuf, sizeof(sqlbuf), SelectFilename_sql, upload_pk, ufile_mode, agent_fk); } free(uploadtree_tablename); /* retrieve the records to process */ result = PQexec(pgConn, sqlbuf); if (fo_checkPQresult(pgConn, result, sqlbuf, __FILE__, __LINE__)) ExitNow(-100); numrows = PQntuples(result); /* process all files in this upload */ for (i=0; i _("Demo Hello World"), + self::MENU_LIST => "Help::Demo::Hello World", + self::PERMISSION => Auth::PERM_READ + )); + } /** * \brief Find out who I am from my user record. * \returns user name */ - function WhoAmI() + protected function WhoAmI() { - $user_pk = $_SESSION['UserId']; + $user_pk = Auth::getUserId(); if (empty($user_pk)) { - /* Note that this message is localized. This is a good practice for as - * much of your UI text as possible. - */ return _("You are not logged in"); } - /* The user's name is stored in $_SESSION[User] after they login. - * But to demonstrate a database call, I'll get the name from the - * user's primary key (in $_SESSION[UserId]). - */ - $UserRow = GetSingleRec('users', "where user_pk='$_SESSION[UserId]'"); - return $UserRow['user_name']; - } // end of WhoAmI() + $userDao = $this->getObject('dao.user'); + return $userDao->getUserName($user_pk); + } /** - * \brief Generate output. + * \brief Generate response. */ - function Output() + protected function handle(Request $request) { - /* make sure this plugin hasn't been turned off */ - if ($this->State != PLUGIN_STATE_READY) { return; } - $UserName = $this->WhoAmI(); $Hello = _("Hello"); $OutBuf = "

$Hello $UserName

"; $OutBuf .= _("Wasn't that easy?"); - print($OutBuf); - return; - } // Output() + return $this->render('include/base.html.twig', $this->mergeWithDefault(array('message' => $OutBuf))); + } -}; +} -$NewPlugin = new demohello; -$NewPlugin->Initialize(); -?> +register_plugin(new DemoHello()); diff --git a/src/readmeoss/agent/readmeoss.php b/src/readmeoss/agent/readmeoss.php index a9815891..b6dbadcf 100644 --- a/src/readmeoss/agent/readmeoss.php +++ b/src/readmeoss/agent/readmeoss.php @@ -1,151 +1,149 @@ cpClearedGetter = new XpClearedGetter("copyright", "statement", false); $this->licenseClearedGetter = new LicenseClearedGetter(); parent::__construct(README_AGENT_NAME, AGENT_VERSION, AGENT_REV); $this->uploadDao = $this->container->get('dao.upload'); $this->agentSpecifLongOptions[] = self::UPLOAD_ADDS.':'; } /** * @todo without wrapper */ function processUploadId($uploadId) { $groupId = $this->groupId; $args = $this->args; $this->additionalUploadIds = array_key_exists(self::UPLOAD_ADDS,$args) ? explode(',',$args[self::UPLOAD_ADDS]) : array(); $uploadIds = $this->additionalUploadIds; array_unshift($uploadIds, $uploadId); $this->heartbeat(0); $licenseStmts = array(); $copyrightStmts = array(); - + foreach($uploadIds as $addUploadId) { if (!$this->uploadDao->isAccessible($addUploadId, $groupId)) { continue; } $moreLicenses = $this->licenseClearedGetter->getCleared($addUploadId, $groupId); $licenseStmts = array_merge($licenseStmts, $moreLicenses['statements']); $this->heartbeat(count($moreLicenses['statements'])); $moreCopyrights = $this->cpClearedGetter->getCleared($addUploadId, $groupId); $copyrightStmts = array_merge($copyrightStmts, $moreCopyrights['statements']); $this->heartbeat(count($moreCopyrights['statements'])); } $contents = array('licenses'=>$licenseStmts, 'copyrights'=>$copyrightStmts ); $this->writeReport($contents, $uploadId); return true; } private function writeReport($contents, $uploadId) { global $SysConf; $packageName = $this->uploadDao->getUpload($uploadId)->getFilename(); $fileBase = $SysConf['FOSSOLOGY']['path']."/report/"; $fileName = $fileBase. "ReadMe_OSS_".$packageName.'_'.time().".txt" ; foreach($this->additionalUploadIds as $addUploadId) { $packageName .= ', ' . $this->uploadDao->getUpload($addUploadId)->getFilename(); } if(!is_dir($fileBase)) { mkdir($fileBase, 0777, true); } umask(0133); $message = $this->generateReport($contents, $packageName); file_put_contents($fileName, $message); $this->updateReportTable($uploadId, $this->jobId, $fileName); } private function updateReportTable($uploadId, $jobId, $filename){ $this->dbManager->insertTableRow('reportgen', array('upload_fk'=>$uploadId, 'job_fk'=>$jobId, 'filepath'=>$filename), __METHOD__); } private function generateReport($contents, $packageName) { $separator1 = "======================================================================================================================="; $separator2 = "-----------------------------------------------------------------------------------------------------------------------"; $break = "\r\n\r\n"; $output = $separator1 . $break . $packageName . $break; foreach($contents['licenses'] as $licenseStatement){ $output .= $licenseStatement['text'] . $break; $output .= $separator2 . $break; } $copyrights = ""; foreach($contents['copyrights'] as $copyrightStatement){ $copyrights .= $copyrightStatement['content']."\r\n"; } if(empty($copyrights)){ $output .= ""; $output .= $break; $output .= ""; }else{ - $output .= "Copyright notices"; - $output .= $break; - $output .= $copyrights; + $output .= "Copyright notices"; + $output .= $break; + $output .= $copyrights; } return $output; } } $agent = new ReadmeOssAgent(); $agent->scheduler_connect(); $agent->run_scheduler_event_loop(); $agent->scheduler_disconnect(0); diff --git a/src/readmeoss/agent/version.php.in b/src/readmeoss/agent/version.php.in index 1df2fd86..8ca62c52 100644 --- a/src/readmeoss/agent/version.php.in +++ b/src/readmeoss/agent/version.php.in @@ -1,3 +1,4 @@