14 #include "libfossrepo.h"
15 #include "libfossscheduler.h"
22 #define FOSSREPO_CONF "/srv/fossology/repository"
25 #define FOSSGROUP "fossology"
30 char LibraryRepoBuildVersion[]=
"Library libfossrepo Build version: " COMMIT_HASH
".\n";
33 #define MAXHOSTNAMELEN 64
35 #define REPONAME "REPOSITORY"
46 #define REPCONFCHECK() { if (!*RepPath) fo_RepOpen(); }
64 if (S == NULL)
return (0);
65 for (i = 0; S[i] !=
'\0'; i++)
67 if (!isalnum(S[i]) && !strchr(
"@%_=+-", S[i]))
return (0);
86 if (S == NULL)
return (0);
87 if (S[0] ==
'.')
return (0);
88 for (i = 0; S[i] !=
'\0'; i++)
90 if (!isalnum(S[i]) && !strchr(
"@%_.=+-", S[i]))
return (0);
104 char* MyRepPath = NULL;
109 MyRepPath = (
char*) calloc(strlen(
RepPath) + 1, 1);
132 fprintf(stderr,
"ERROR: %s\n", error->message);
136 for (i = 0; i < length; i++)
139 if (entry[0] ==
'*' || strncmp(Type, entry, strlen(Type)) == 0)
167 GError* error = NULL;
174 for (i = 0; i < kl; i++)
177 for (j = 0; j < hl; j++)
180 char* remainder = NULL;
181 strtok_r(entry,
" ", &remainder);
182 start = strtok_r(NULL,
" ", &remainder);
183 end = strtok_r(NULL,
" ", &remainder);
185 if (strcmp(entry,
"*") == 0 || strcmp(entry, Type) == 0)
188 (strncasecmp(end,
Filename, strlen(end)) >= 0))
191 if (
Match == MatchNum)
193 ret = (
char*) calloc(strlen(hosts[i]) + 1,
sizeof(char));
194 strcpy(ret, hosts[i]);
257 {Len += strlen(Host) + 1;}
258 if (!Host && (Which > 1))
264 if (Type) Len += strlen(Type) + 1;
269 Len = Len + 3 * RepDepth;
274 if (Ext) Len += 1 + strlen(Ext);
277 Path = (
char*) calloc(Len + 1, 1);
288 Len += strlen(Host) + 1;
294 Len += strlen(Type) + 1;
298 if (Host) free(Host);
301 if (FilenameLen < RepDepth * 2)
303 for (i = 0; i < FilenameLen; i++)
306 if (i % 2 == 1) Path[Len++] =
'/';
308 for (; i < RepDepth * 2; i++)
311 if (i % 2 == 1) Path[Len++] =
'/';
317 for (i = 0; i < RepDepth; i++)
319 Path[Len] = tolower(
Filename[i * 2]);
320 Path[Len + 1] = tolower(
Filename[i * 2 + 1]);
326 for (i = 0;
Filename[i] !=
'\0'; i++)
336 Len += strlen(Type) + 1;
354 char* Path, * AltPath;
359 if (!Path)
return (NULL);
361 if (!stat(Path, &Stat))
369 if (!AltPath)
return (Path);
371 if (!stat(AltPath, &Stat))
396 struct utimbuf Utime;
397 Utime.actime = Utime.modtime = time(NULL);
410 char Dir[FILENAME_MAX + 1];
418 memset(Dir,
'\0',
sizeof(Dir));
420 for (i = 1; Dir[i] !=
'\0'; i++)
430 rc = mkdir(Dir, 0770);
435 if (rc && (errno == EEXIST)) rc = 0;
439 fprintf(stderr,
"FATAL: 'mkdir %s' failed with rc=%d\n", Dir, rc);
456 char* FnameOld, * Fname;
461 if (!FnameOld || !Fname)
463 fprintf(stderr,
"ERROR: Bad repository name: type='%s' name='%s'\n",
467 rc = rename(FnameOld, Fname);
494 fprintf(stderr,
"ERROR: Invalid type '%s'\n", Type);
499 fprintf(stderr,
"ERROR: Invalid filename '%s'\n",
Filename);
506 fprintf(stderr,
"ERROR: Unable to allocate path for '%s/%s'\n", Type,
Filename);
509 if (!stat(Fname, &Stat)) rc = 1;
539 fprintf(stderr,
"ERROR: Invalid type '%s'\n", Type);
544 fprintf(stderr,
"ERROR: Invalid filename '%s'\n",
Filename);
551 fprintf(stderr,
"ERROR: Unable to allocate path for '%s/%s'\n", Type,
Filename);
554 if (stat(Fname, &Stat)) rc = errno;
576 fprintf(stderr,
"ERROR: Invalid type '%s'\n", Type);
581 fprintf(stderr,
"ERROR: Invalid filename '%s'\n",
Filename);
588 fprintf(stderr,
"ERROR: Unable to allocate path for '%s/%s'\n", Type,
Filename);
591 if (!stat(Fname, &Stat)) rc = unlink(Fname);
620 fprintf(stderr,
"ERROR: Invalid type '%s'\n", Type);
625 fprintf(stderr,
"ERROR: Invalid filename '%s'\n",
Filename);
632 fprintf(stderr,
"ERROR: Unable to allocate path for '%s/%s'\n", Type,
Filename);
636 F = fopen(Fname,
"rb");
659 fprintf(stderr,
"ERROR: Invalid type '%s'\n", Type);
664 fprintf(stderr,
"ERROR: Invalid filename '%s'\n",
Filename);
671 fprintf(stderr,
"ERROR: Unable to allocate path for '%s/%s'\n", Type,
Filename);
685 F = fopen(Fname,
"wb");
688 fprintf(stderr,
"ERROR: %s, in %s:%d, failed to open [%s]\n",
689 strerror(errno), __FILE__, __LINE__, Fname);
693 chmod(Fname, S_ISGID | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
748 fprintf(stderr,
"ERROR: Unable to open file for mmap (%s)\n", Fname);
756 fprintf(stderr,
"ERROR: Unable to stat file (%s)\n", Fname);
761 PageSize = getpagesize();
764 if (Stat.st_size > 0x7fffffff) Stat.st_size = 0x80000000;
769 if (M->
Mmap == MAP_FAILED)
771 fprintf(stderr,
"ERROR: Unable to mmap file (%s)\n", Fname);
794 if (!Fname)
return (NULL);
814 if (0 == strcmp(Type,
"files"))
816 chmod(Source, S_ISGID | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
833 if (!FoutPath)
return (0);
839 if (link(Source, FoutPath) == 0)
848 Fin = fopen(Source,
"rb");
851 fprintf(stderr,
"ERROR: Unable to open source file '%s'\n", Source);
854 setvbuf(Fin, vBuf, _IOFBF,
sizeof(vBuf));
859 fprintf(stderr,
"ERROR: Invalid -- type='%s' filename='%s'\n", Type,
Filename);
864 LenIn = fread(Buf,1,
sizeof(Buf),Fin);
868 while(LenOut < LenIn)
870 i = fwrite(Buf+LenOut,1,LenIn - LenOut,Fout);
878 fprintf(stderr,
"ERROR: Write failed -- type='%s' filename='%s'\n",Type,
Filename);
883 LenIn = fread(Buf,1,
sizeof(Buf),Fin);
922 GError* error = NULL;
934 Group = getgrnam(FOSSGROUP);
935 if (!Group)
return(0);
936 RepGroup = Group->gr_gid;
938 if ((Gid != RepGroup) && setegid(RepGroup))
947 char* repDepthStr =
fo_config_get(config,
"FOSSOLOGY",
"depth", &error);
950 fprintf(stderr,
"ERROR %s.%d: %s\n", __FILE__, __LINE__, error->message);
953 RepDepth = atoi(repDepthStr);
959 fprintf(stderr,
"ERROR %s.%d: %s\n", __FILE__, __LINE__, error->message);
980 int32_t nhosts, nlist, i, j;
981 char* gname =
"REPOSITORY";
984 GRegex* regex = NULL;
985 GMatchInfo*
match = NULL;
992 return g_strdup(
"The fossology.conf file does not contain a \"REPOSITORY\" group.");
1003 regex = g_regex_new(
1004 "(\\*|gold|files|logs|license|test)\\s+([[:xdigit:]]+)\\s+([[:xdigit:]]+)$",
1007 for (i = 0; i < nhosts; i++)
1011 for (j = 0; j < nlist; j++)
1015 if (!g_regex_match(regex, curr, 0, &
match))
1017 retval = g_strdup_printf(
"%s[] = %s", hosts[i], curr);
1021 begin_str = g_match_info_fetch(
match, 2);
1022 end_str = g_match_info_fetch(
match, 3);
1024 begin = strtoul(begin_str, NULL, 16);
1025 end = strtoul(end_str, NULL, 16);
1029 retval = g_strdup_printf(
"%s[] = %s", hosts[i], curr);
1035 g_match_info_free(
match);
1039 g_regex_unref(regex);
RepPath($PfilePk, $Repo="files")
Given a pfile id, retrieve the pfile path.
char * fo_config_get_list(fo_conf *conf, char *group, char *key, int idx, GError **error)
int fo_config_list_length(fo_conf *conf, char *group, char *key, GError **error)
Gets the length of the list associated with a particular list key.
char * fo_config_get(fo_conf *conf, const char *group, const char *key, GError **error)
Gets an element based on its group name and key name. If the group or key is not found,...
char ** fo_config_key_set(fo_conf *conf, char *group, int *length)
Gets the set of key names for a particular group.
FOSSology library to read config file.
#define REPONAME
Default repo name.
FILE * fo_RepFwrite(char *Type, char *Filename)
Perform an fwrite. Also creates directories.
char * fo_RepMkPath(const char *Type, char *Filename)
Given a filename, construct the full path to the file.
#define MAXLINE
Max length of a line.
int fo_RepOpenFull(fo_conf *config)
Loads common information from configuration files into ram.
int fo_RepExist2(char *Type, char *Filename)
Determine if a file exists.
int fo_RepImport(char *Source, char *Type, char *Filename, int Link)
Import a file into the repository.
int fo_RepFclose(FILE *F)
Perform an fclose.
char * fo_RepGetRepPath()
Determine the path for the repository's root.
FILE * fo_RepFread(char *Type, char *Filename)
Perform an fopen for reading only.
char * fo_RepValidate(fo_conf *config)
validates the repository configuration information.
int fo_RepOpen()
wrapper function for agents. Simply call fo_RepOpenFull() passing in the default system configuration
int _RepCheckString(char *S)
Simple check to see if the string is valid.
int fo_RepHostExist(char *Type, char *Host)
Determine if a host exists.
char * _RepGetHost(const char *Type, char *Filename, int MatchNum)
Determine the host for the tree.
RepMmapStruct * fo_RepMmap(char *Type, char *Filename)
Perform a mmap.
int fo_RepExist(char *Type, char *Filename)
Determine if a file exists.
char * fo_RepGetHost(char *Type, char *Filename)
Determine the host for a filename.
char * fo_RepMkPathTmp(const char *Type, char *Filename, char *Ext, int Which)
Given a filename, construct the full path to the file.
int fo_RepRemove(char *Type, char *Filename)
Delete a repository file.
FILE * fo_RepFwriteTmp(char *Type, char *Filename, char *Ext)
Perform an fwrite. Also creates directories.
void fo_RepClose()
Close and unmap the repository configuration file.
int _RepCheckType(const char *S)
Simple check to see if the string S is valid filename.
void fo_RepMunmap(RepMmapStruct *M)
Perform a munmap.
RepMmapStruct * fo_RepMmapFile(char *Fname)
Perform a mmap on a regular file name.
int fo_RepRenameTmp(char *Type, char *Filename, char *Ext)
Rename a temp file to a real file.
void _RepUpdateTime(char *File)
Update the last modified time of a file.
int _RepMkDirs(char *Fname)
Same as command-line "mkdir -p".
start($application)
start the application Assumes application is restartable via /etc/init.d/<script>....
unsigned char * Mmap
Memory pointer from mmap.
int FileHandle
Handle from open()
uint32_t MmapSize
Size of file mmap.
uint32_t _MmapSize
Real size of mmap (set to page boundary)
Store the results of a regex match.