26 #include "libfossrepo.h" 27 #include "libfossscheduler.h" 34 #define FOSSREPO_CONF "/srv/fossology/repository" 37 #define FOSSGROUP "fossology" 42 char LibraryRepoBuildVersion[]=
"Library libfossrepo Build version: " COMMIT_HASH
".\n";
45 #define MAXHOSTNAMELEN 64 47 #define REPONAME "REPOSITORY" 58 #define REPCONFCHECK() { if (!*RepPath) fo_RepOpen(); } 76 if (S == NULL)
return (0);
77 for (i = 0; S[i] !=
'\0'; i++)
79 if (!isalnum(S[i]) && !strchr(
"@%_=+-", S[i]))
return (0);
98 if (S == NULL)
return (0);
99 if (S[0] ==
'.')
return (0);
100 for (i = 0; S[i] !=
'\0'; i++)
102 if (!isalnum(S[i]) && !strchr(
"@%_.=+-", S[i]))
return (0);
116 char* MyRepPath = NULL;
121 MyRepPath = (
char*) calloc(strlen(
RepPath) + 1, 1);
144 fprintf(stderr,
"ERROR: %s\n", error->message);
148 for (i = 0; i < length; i++)
151 if (entry[0] ==
'*' || strncmp(Type, entry, strlen(Type)) == 0)
179 GError* error = NULL;
186 for (i = 0; i < kl; i++)
189 for (j = 0; j < hl; j++)
192 char* remainder = NULL;
193 strtok_r(entry,
" ", &remainder);
194 start = strtok_r(NULL,
" ", &remainder);
195 end = strtok_r(NULL,
" ", &remainder);
197 if (strcmp(entry,
"*") == 0 || strcmp(entry, Type) == 0)
199 if ((strncasecmp(start, Filename, strlen(start)) <= 0) &&
200 (strncasecmp(end, Filename, strlen(end)) >= 0))
203 if (Match == MatchNum)
205 ret = (
char*) calloc(strlen(hosts[i]) + 1,
sizeof(char));
206 strcpy(ret, hosts[i]);
269 {Len += strlen(Host) + 1;}
270 if (!Host && (Which > 1))
276 if (Type) Len += strlen(Type) + 1;
281 Len = Len + 3 * RepDepth;
284 FilenameLen = strlen(Filename);
286 if (Ext) Len += 1 + strlen(Ext);
289 Path = (
char*) calloc(Len + 1, 1);
300 Len += strlen(Host) + 1;
306 Len += strlen(Type) + 1;
310 if (Host) free(Host);
313 if (FilenameLen < RepDepth * 2)
315 for (i = 0; i < FilenameLen; i++)
317 Path[Len++] = tolower(Filename[i]);
318 if (i % 2 == 1) Path[Len++] =
'/';
320 for (; i < RepDepth * 2; i++)
323 if (i % 2 == 1) Path[Len++] =
'/';
329 for (i = 0; i < RepDepth; i++)
331 Path[Len] = tolower(Filename[i * 2]);
332 Path[Len + 1] = tolower(Filename[i * 2 + 1]);
338 for (i = 0; Filename[i] !=
'\0'; i++)
340 Path[Len] = tolower(Filename[i]);
348 Len += strlen(Type) + 1;
366 char* Path, * AltPath;
371 if (!Path)
return (NULL);
373 if (!stat(Path, &Stat))
381 if (!AltPath)
return (Path);
383 if (!stat(AltPath, &Stat))
408 struct utimbuf Utime;
409 Utime.actime = Utime.modtime = time(NULL);
422 char Dir[FILENAME_MAX + 1];
430 memset(Dir,
'\0',
sizeof(Dir));
432 for (i = 1; Dir[i] !=
'\0'; i++)
442 rc = mkdir(Dir, 0770);
447 if (rc && (errno == EEXIST)) rc = 0;
451 fprintf(stderr,
"FATAL: 'mkdir %s' failed with rc=%d\n", Dir, rc);
468 char* FnameOld, * Fname;
473 if (!FnameOld || !Fname)
475 fprintf(stderr,
"ERROR: Bad repository name: type='%s' name='%s'\n",
479 rc = rename(FnameOld, Fname);
506 fprintf(stderr,
"ERROR: Invalid type '%s'\n", Type);
511 fprintf(stderr,
"ERROR: Invalid filename '%s'\n", Filename);
518 fprintf(stderr,
"ERROR: Unable to allocate path for '%s/%s'\n", Type, Filename);
521 if (!stat(Fname, &Stat)) rc = 1;
551 fprintf(stderr,
"ERROR: Invalid type '%s'\n", Type);
556 fprintf(stderr,
"ERROR: Invalid filename '%s'\n", Filename);
563 fprintf(stderr,
"ERROR: Unable to allocate path for '%s/%s'\n", Type, Filename);
566 if (stat(Fname, &Stat)) rc = errno;
588 fprintf(stderr,
"ERROR: Invalid type '%s'\n", Type);
593 fprintf(stderr,
"ERROR: Invalid filename '%s'\n", Filename);
600 fprintf(stderr,
"ERROR: Unable to allocate path for '%s/%s'\n", Type, Filename);
603 if (!stat(Fname, &Stat)) rc = unlink(Fname);
632 fprintf(stderr,
"ERROR: Invalid type '%s'\n", Type);
637 fprintf(stderr,
"ERROR: Invalid filename '%s'\n", Filename);
644 fprintf(stderr,
"ERROR: Unable to allocate path for '%s/%s'\n", Type, Filename);
648 F = fopen(Fname,
"rb");
671 fprintf(stderr,
"ERROR: Invalid type '%s'\n", Type);
676 fprintf(stderr,
"ERROR: Invalid filename '%s'\n", Filename);
683 fprintf(stderr,
"ERROR: Unable to allocate path for '%s/%s'\n", Type, Filename);
697 F = fopen(Fname,
"wb");
700 fprintf(stderr,
"ERROR: %s, in %s:%d, failed to open [%s]\n",
701 strerror(errno), __FILE__, __LINE__, Fname);
705 chmod(Fname, S_ISGID | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
760 fprintf(stderr,
"ERROR: Unable to open file for mmap (%s)\n", Fname);
768 fprintf(stderr,
"ERROR: Unable to stat file (%s)\n", Fname);
773 PageSize = getpagesize();
776 if (Stat.st_size > 0x7fffffff) Stat.st_size = 0x80000000;
781 if (M->
Mmap == MAP_FAILED)
783 fprintf(stderr,
"ERROR: Unable to mmap file (%s)\n", Fname);
806 if (!Fname)
return (NULL);
826 if (0 == strcmp(Type,
"files"))
828 chmod(Source, S_ISGID | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
845 if (!FoutPath)
return (0);
851 if (link(Source, FoutPath) == 0)
860 Fin = fopen(Source,
"rb");
863 fprintf(stderr,
"ERROR: Unable to open source file '%s'\n", Source);
866 setvbuf(Fin, vBuf, _IOFBF,
sizeof(vBuf));
871 fprintf(stderr,
"ERROR: Invalid -- type='%s' filename='%s'\n", Type, Filename);
876 LenIn = fread(Buf,1,
sizeof(Buf),Fin);
880 while(LenOut < LenIn)
882 i = fwrite(Buf+LenOut,1,LenIn - LenOut,Fout);
890 fprintf(stderr,
"ERROR: Write failed -- type='%s' filename='%s'\n",Type,Filename);
895 LenIn = fread(Buf,1,
sizeof(Buf),Fin);
934 GError* error = NULL;
946 Group = getgrnam(FOSSGROUP);
947 if (!Group)
return(0);
948 RepGroup = Group->gr_gid;
950 if ((Gid != RepGroup) && setegid(RepGroup))
959 char* repDepthStr =
fo_config_get(config,
"FOSSOLOGY",
"depth", &error);
962 fprintf(stderr,
"ERROR %s.%d: %s\n", __FILE__, __LINE__, error->message);
965 RepDepth = atoi(repDepthStr);
971 fprintf(stderr,
"ERROR %s.%d: %s\n", __FILE__, __LINE__, error->message);
992 int32_t nhosts, nlist, i, j;
993 char* gname =
"REPOSITORY";
996 GRegex* regex = NULL;
997 GMatchInfo*
match = NULL;
1004 return g_strdup(
"The fossology.conf file does not contain a \"REPOSITORY\" group.");
1015 regex = g_regex_new(
1016 "(\\*|gold|files|logs|license|test)\\s+([[:xdigit:]]+)\\s+([[:xdigit:]]+)$",
1019 for (i = 0; i < nhosts; i++)
1023 for (j = 0; j < nlist; j++)
1027 if (!g_regex_match(regex, curr, 0, &match))
1029 retval = g_strdup_printf(
"%s[] = %s", hosts[i], curr);
1033 begin_str = g_match_info_fetch(match, 2);
1034 end_str = g_match_info_fetch(match, 3);
1036 begin = strtoul(begin_str, NULL, 16);
1037 end = strtoul(end_str, NULL, 16);
1041 retval = g_strdup_printf(
"%s[] = %s", hosts[i], curr);
1047 g_match_info_free(match);
1051 g_regex_unref(regex);
FOSSology library to read config file.
FILE * fo_RepFwrite(char *Type, char *Filename)
Perform an fwrite. Also creates directories.
RepMmapStruct * fo_RepMmap(char *Type, char *Filename)
Perform a mmap.
Store the results of a regex match.
void _RepUpdateTime(char *File)
Update the last modified time of a file.
uint32_t MmapSize
Size of file mmap.
int fo_RepHostExist(char *Type, char *Host)
Determine if a host exists.
#define MAXLINE
Max length of a line.
char * fo_RepGetRepPath()
Determine the path for the repository's root.
char * fo_RepMkPathTmp(const char *Type, char *Filename, char *Ext, int Which)
Given a filename, construct the full path to the file.
char * fo_RepValidate(fo_conf *config)
validates the repository configuration information.
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, the error object is set and NULL is returned.
FILE * fo_RepFread(char *Type, char *Filename)
Perform an fopen for reading only.
char * fo_RepGetHost(char *Type, char *Filename)
Determine the host for a filename.
int fo_RepFclose(FILE *F)
Perform an fclose.
int _RepCheckType(const char *S)
Simple check to see if the string S is valid filename.
start($application)
start the application Assumes application is restartable via /etc/init.d/<script>. The application passed in should match the script name in /etc/init.d
RepPath($PfilePk, $Repo="files")
Given a pfile id, retrieve the pfile path.
uint32_t _MmapSize
Real size of mmap (set to page boundary)
RepMmapStruct * fo_RepMmapFile(char *Fname)
Perform a mmap on a regular file name.
FILE * fo_RepFwriteTmp(char *Type, char *Filename, char *Ext)
Perform an fwrite. Also creates directories.
int FileHandle
Handle from open()
#define REPONAME
Default repo name.
int _RepMkDirs(char *Fname)
Same as command-line "mkdir -p".
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.
int fo_RepOpenFull(fo_conf *config)
Loads common information from configuration files into ram.
unsigned char * Mmap
Memory pointer from mmap.
char * fo_RepMkPath(const char *Type, char *Filename)
Given a filename, construct the full path to the file.
void fo_RepMunmap(RepMmapStruct *M)
Perform a munmap.
char ** fo_config_key_set(fo_conf *conf, char *group, int *length)
Gets the set of key names for a particular group.
int fo_RepExist2(char *Type, char *Filename)
Determine if a file exists.
char * _RepGetHost(const char *Type, char *Filename, int MatchNum)
Determine the host for the tree.
int fo_RepOpen()
wrapper function for agents. Simply call fo_RepOpenFull() passing in the default system configuration...
void fo_RepClose()
Close and unmap the repository configuration file.
int fo_RepImport(char *Source, char *Type, char *Filename, int Link)
Import a file into the repository.
int fo_RepRenameTmp(char *Type, char *Filename, char *Ext)
Rename a temp file to a real file.
int _RepCheckString(char *S)
Simple check to see if the string is valid.
int fo_RepRemove(char *Type, char *Filename)
Delete a repository file.
int fo_RepExist(char *Type, char *Filename)
Determine if a file exists.