FOSSology  4.4.0
Open Source License Compliance by Open Source Software
libfossrepo.c File Reference

Repository access functions. All internal functions are prefixed by '_'. More...

#include "libfossrepo.h"
#include "libfossscheduler.h"
#include "fossconfig.h"
#include <sys/stat.h>
#include <glib.h>
Include dependency graph for libfossrepo.c:

Go to the source code of this file.

Macros

#define MAXHOSTNAMELEN   64
 Max host name length.
 
#define MAXLINE   1024
 Max length of a line.
 
#define REPONAME   "REPOSITORY"
 Default repo name.
 
#define GROUP   0
 Default group ID.
 
#define REPCONFCHECK()   { if (!*RepPath) fo_RepOpen(); }
 

Functions

int _RepCheckType (const char *S)
 Simple check to see if the string S is valid filename.
More...
 
int _RepCheckString (char *S)
 Simple check to see if the string is valid.
More...
 
char * fo_RepGetRepPath ()
 Determine the path for the repository's root. More...
 
int fo_RepHostExist (char *Type, char *Host)
 Determine if a host exists. More...
 
char * _RepGetHost (const char *Type, char *Filename, int MatchNum)
 Determine the host for the tree. More...
 
char * fo_RepGetHost (char *Type, char *Filename)
 Determine the host for a filename. More...
 
char * fo_RepMkPathTmp (const char *Type, char *Filename, char *Ext, int Which)
 Given a filename, construct the full path to the file. More...
 
char * fo_RepMkPath (const char *Type, char *Filename)
 Given a filename, construct the full path to the file. More...
 
void _RepUpdateTime (char *File)
 Update the last modified time of a file. More...
 
int _RepMkDirs (char *Fname)
 Same as command-line "mkdir -p". More...
 
int fo_RepRenameTmp (char *Type, char *Filename, char *Ext)
 Rename a temp file to a real file. More...
 
int fo_RepExist (char *Type, char *Filename)
 Determine if a file exists. More...
 
int fo_RepExist2 (char *Type, char *Filename)
 Determine if a file exists. More...
 
int fo_RepRemove (char *Type, char *Filename)
 Delete a repository file. More...
 
int fo_RepFclose (FILE *F)
 Perform an fclose. More...
 
FILE * fo_RepFread (char *Type, char *Filename)
 Perform an fopen for reading only. More...
 
FILE * fo_RepFwriteTmp (char *Type, char *Filename, char *Ext)
 Perform an fwrite. Also creates directories. More...
 
FILE * fo_RepFwrite (char *Type, char *Filename)
 Perform an fwrite. Also creates directories. More...
 
void fo_RepMunmap (RepMmapStruct *M)
 Perform a munmap. More...
 
RepMmapStructfo_RepMmapFile (char *Fname)
 Perform a mmap on a regular file name. More...
 
RepMmapStructfo_RepMmap (char *Type, char *Filename)
 Perform a mmap. More...
 
int fo_RepImport (char *Source, char *Type, char *Filename, int Link)
 Import a file into the repository. More...
 
void fo_RepClose ()
 Close and unmap the repository configuration file.
 
int fo_RepOpen ()
 wrapper function for agents. Simply call fo_RepOpenFull() passing in the default system configuration More...
 
int fo_RepOpenFull (fo_conf *config)
 Loads common information from configuration files into ram. More...
 
char * fo_RepValidate (fo_conf *config)
 validates the repository configuration information. More...
 

Variables

int RepDepth = 2
 
char RepPath [MAXLINE+1] = ""
 

Detailed Description

Repository access functions. All internal functions are prefixed by '_'.

Definition in file libfossrepo.c.

Function Documentation

◆ _RepCheckString()

int _RepCheckString ( char *  S)

Simple check to see if the string is valid.

Note
This is an internal function.

Valid strings only contain alphanumerics, and "@%_.=+-".
Used for types, hostnames, and filenames.

Parameters
Sstring to check
Returns
1=valid, 0=invalid.

Definition at line 83 of file libfossrepo.c.

◆ _RepCheckType()

int _RepCheckType ( const char *  S)

Simple check to see if the string S is valid filename.

Note
This is an internal function.

A valid name is composed of only alphanumerics, and "&%_=+-"
Used for types, hostnames, and filenames.
(Just like _RepCheckString, except dots are not allowed.)

Parameters
Sstring to check
Returns
1=valid, 0=invalid.

Definition at line 61 of file libfossrepo.c.

◆ _RepGetHost()

char* _RepGetHost ( const char *  Type,
char *  Filename,
int  MatchNum 
)

Determine the host for the tree.

Note
This is an internal only function.
Parameters
TypeType of data.
FilenameFilename to match.
MatchNumUsed to identify WHICH match to return. (MatchNum permits fallback paths.)
Returns
Allocates and returns string with hostname or NULL.

Definition at line 158 of file libfossrepo.c.

◆ _RepMkDirs()

int _RepMkDirs ( char *  Fname)

Same as command-line "mkdir -p".

Note
Internal only.
Parameters
Fnamefilename
Returns
0 on success, 1 on failure.

Definition at line 408 of file libfossrepo.c.

◆ _RepUpdateTime()

void _RepUpdateTime ( char *  File)

Update the last modified time of a file.

Every file access (read/write) should update the timestamp on the file. This allows us to determine when files are stale.

Note
Internal only function.
Parameters
Filefile name
Returns
none

Definition at line 394 of file libfossrepo.c.

◆ fo_RepExist()

int fo_RepExist ( char *  Type,
char *  Filename 
)

Determine if a file exists.

Parameters
TypeType of data.
FilenameThe file in question
Returns
1=exists, 0=not exists, -1 on error.
Test:
Test with standalone:
./repexist files 00000cb69c3c9c9fd15cadbf4652bd1552c349de.6caae94bdb579d7c9ada36726cf2e97f.776

Definition at line 486 of file libfossrepo.c.

◆ fo_RepExist2()

int fo_RepExist2 ( char *  Type,
char *  Filename 
)

Determine if a file exists.

If it does not exist, return an error code (errno). This is a replacement for fo_RepExist().

Parameters
Typeis the type of data.
Filename
Returns
0=exists, errno=not exists, -1 = internal errors. A message is also written to stderr for internal errors (bad inputs, etc).
Test:
Test with standalone:
./repexist files 00000cb69c3c9c9fd15cadbf4652bd1552c349de.6caae94bdb579d7c9ada36726cf2e97f.776

Definition at line 531 of file libfossrepo.c.

◆ fo_RepFclose()

int fo_RepFclose ( FILE *  F)

Perform an fclose.

Parameters
FFile handler
Returns
0 if success. On error, EOF is returned and global errno is set.

Definition at line 601 of file libfossrepo.c.

◆ fo_RepFread()

FILE* fo_RepFread ( char *  Type,
char *  Filename 
)

Perform an fopen for reading only.

Parameters
TypeType of data.
FilenameFile to open.
Returns
FILE pointer, or NULL if file does not exist.

Definition at line 613 of file libfossrepo.c.

◆ fo_RepFwrite()

FILE* fo_RepFwrite ( char *  Type,
char *  Filename 
)

Perform an fwrite. Also creates directories.

Same as fo_RepFwriteTmp() but without ext.

Parameters
TypeType of data.
FilenameFile to write
Returns
FILE pointer, or NULL if it fails.

Definition at line 710 of file libfossrepo.c.

◆ fo_RepFwriteTmp()

FILE* fo_RepFwriteTmp ( char *  Type,
char *  Filename,
char *  Ext 
)

Perform an fwrite. Also creates directories.

Parameters
TypeType of data.
FilenameFile to write to
ExtAn optional extension (for making temporary files).
Returns
FILE pointer, or NULL if it fails.

Definition at line 648 of file libfossrepo.c.

◆ fo_RepGetHost()

char* fo_RepGetHost ( char *  Type,
char *  Filename 
)

Determine the host for a filename.

Parameters
TypeType of data.
FilenameFilename to match.
Returns
Allocates and returns string with hostname or NULL.
Test:
Test with standalone:
./rephost files 00000cb69c3c9c9fd15cadbf4652bd1552c349de.6caae94bdb579d7c9ada36726cf2e97f.776

Definition at line 221 of file libfossrepo.c.

◆ fo_RepGetRepPath()

char* fo_RepGetRepPath ( )

Determine the path for the repository's root.

The RepPath is where all the repository mounts are located. The path should NOT end with a "/".

Returns
Allocates and returns string with the repo root path, or NULL.

Definition at line 102 of file libfossrepo.c.

◆ fo_RepHostExist()

int fo_RepHostExist ( char *  Type,
char *  Host 
)

Determine if a host exists.

Parameters
TypeThis is the repo type (files, gold, ununpack, ...)
HostHost to check
Returns
1=exists, 0=not exists, -1 on error.

Definition at line 120 of file libfossrepo.c.

◆ fo_RepImport()

int fo_RepImport ( char *  Source,
char *  Type,
char *  Filename,
int  Link 
)

Import a file into the repository.

This is a REALLY FAST copy.

Parameters
SourceSource filename
TypeType of data.
FilenameThe destination filename
Linktrue if this should be a hardlink instead of a copy
Returns
0=success, !0 for error.

Definition at line 812 of file libfossrepo.c.

◆ fo_RepMkPath()

char* fo_RepMkPath ( const char *  Type,
char *  Filename 
)

Given a filename, construct the full path to the file.

Parameters
TypeType of data.
Filenamefilename

This does NOT make the actual file or modify the file system!

Note
Caller must free the string!
This scans for alternate file locations, in case the file exists.
Returns
Allocates and returns a string.

Definition at line 352 of file libfossrepo.c.

◆ fo_RepMkPathTmp()

char* fo_RepMkPathTmp ( const char *  Type,
char *  Filename,
char *  Ext,
int  Which 
)

Given a filename, construct the full path to the file.

Parameters
TypeType of data.
FilenameFilename to construct
ExtAn optional extension (for making temporary files).
WhichUsed to identify WHICH match to return.

This does NOT make the actual file or modify the file system!

Note
Caller must free the string!
Test:
Test with standalone:
./reppath files 00000cb69c3c9c9fd15cadbf4652bd1552c349de.6caae94bdb579d7c9ada36726cf2e97f.776
Returns
Allocates and returns a string or NULL on error.

Definition at line 244 of file libfossrepo.c.

◆ fo_RepMmap()

RepMmapStruct* fo_RepMmap ( char *  Type,
char *  Filename 
)

Perform a mmap.

Parameters
TypeType of data.
FilenameThe filename to match.
Returns
An allocated struct RepMmap.
Note
This only works for READ-ONLY files!

Definition at line 786 of file libfossrepo.c.

◆ fo_RepMmapFile()

RepMmapStruct* fo_RepMmapFile ( char *  Fname)

Perform a mmap on a regular file name.

Parameters
Filename
Returns
filled RepMmapStruc, or NULL on error.

Definition at line 734 of file libfossrepo.c.

◆ fo_RepMunmap()

void fo_RepMunmap ( RepMmapStruct M)

Perform a munmap.

This frees the struct RepMmap.

Parameters
MRepMmapStruct pointer

Definition at line 721 of file libfossrepo.c.

◆ fo_RepOpen()

int fo_RepOpen ( )

wrapper function for agents. Simply call fo_RepOpenFull() passing in the default system configuration

Returns
1 on opened, 0 on failed.

Definition at line 908 of file libfossrepo.c.

◆ fo_RepOpenFull()

int fo_RepOpenFull ( fo_conf config)

Loads common information from configuration files into ram.

Parameters
configThe configuration to use
Returns
1 on opened, 0 on failed.

Definition at line 920 of file libfossrepo.c.

◆ fo_RepRemove()

int fo_RepRemove ( char *  Type,
char *  Filename 
)

Delete a repository file.

Parameters
TypeType of data.
FilenameFile to be deleted.
Returns
0=deleted, !0=error from unlink().
Note
This will LEAVE empty directories!

Definition at line 568 of file libfossrepo.c.

◆ fo_RepRenameTmp()

int fo_RepRenameTmp ( char *  Type,
char *  Filename,
char *  Ext 
)

Rename a temp file to a real file.

Parameters
TypeType of data.
FilenameFile to be renamed
ExtAn optional extension (for making temporary files).
Returns
0 on succes, !0 on error.

Definition at line 454 of file libfossrepo.c.

◆ fo_RepValidate()

char* fo_RepValidate ( fo_conf config)

validates the repository configuration information.

Checks that the repository entries in fossology.conf are correct. If this function does not return NULL, then the caller owns the return value.

Parameters
configthe configuration information
Returns
nothing if correct, the offending line if there was an error

Definition at line 977 of file libfossrepo.c.