FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
utils.c File Reference

Contains all utility functions used by FOSSology. More...

#include "ununpack.h"
#include "externs.h"
#include "regex.h"
Include dependency graph for utils.c:

Go to the source code of this file.

Enumerations

enum  BITS { BITS_PROJECT = 27, BITS_ARTIFACT = 28, BITS_CONTAINER = 29 }
 File mode BITS.
 

Functions

int IsInflatedFile (char *FileName, int InflateSize)
 Test if the file is a compression bomb. More...
 
void SafeExit (int rc)
 Close scheduler and database connections, then exit. More...
 
void RemovePostfix (char *Name)
 get rid of the postfix More...
 
void InitCmd ()
 Initialize the metahandler CMD table. More...
 
int TaintString (char *Dest, int DestLen, char *Src, int ProtectQuotes, char *Replace)
 Protect strings intelligently. More...
 
int Prune (char *Fname, struct stat Stat)
 Given a filename and its stat, prune it. More...
 
int MkDirs (char *Fname)
 Same as command-line "mkdir -p". More...
 
int MkDir (char *Fname)
 Smart mkdir. More...
 
int IsDir (char *Fname)
 Given a filename, is it a directory? More...
 
int IsFile (char *Fname, int Link)
 Given a filename, is it a file? More...
 
int ReadLine (FILE *Fin, char *Line, int MaxLine)
 Read a command from a stream. More...
 
int IsExe (char *Exe, int Quiet)
 Check if the executable exists. More...
 
int CopyFile (char *Src, char *Dst)
 Copy a file. For speed: mmap and save. More...
 
int ParentWait ()
 Wait for a child. Sets child status. More...
 
void CheckCommands (int Show)
 Make sure all commands are usable. More...
 
int RunCommand (char *Cmd, char *CmdPre, char *File, char *CmdPost, char *Out, char *Where)
 Try a command and return command code. More...
 
int InitMagic ()
 Open and load Magic file Initializes global MagicCookie. More...
 
int IsDebianSourceFile (char *Filename)
 Read file to see if it is a Debian source file. More...
 
void OctetType (char *Filename, char *TypeBuf)
 Figure out the real type of "octet" files in case we can unarchive them. More...
 
int FindCmd (char *Filename)
 Given a file name, determine the type of extraction command. This uses Magic. More...
 
void FreeDirList (dirlist *DL)
 Free a list of files in a directory list. More...
 
dirlistMakeDirList (char *Fullname)
 Create a list of files in a directory. More...
 
void SetDir (char *Dest, int DestLen, char *Smain, char *Sfile)
 Set a destination directory name. More...
 
void DebugContainerInfo (ContainerInfo *CI)
 Print a ContainerInfo structure. More...
 
int DBInsertPfile (ContainerInfo *CI, char *Fuid)
 Insert a Pfile record. Sets the pfile_pk in CI. More...
 
int TestSCMData (char *sourcefilename)
 Search for SCM data in the filename. More...
 
int DBInsertUploadTree (ContainerInfo *CI, int Mask)
 Insert an UploadTree record. More...
 
int AddToRepository (ContainerInfo *CI, char *Fuid, int Mask)
 Add a ContainerInfo record to the repository AND to the database. More...
 
int DisplayContainerInfo (ContainerInfo *CI, int Cmd)
 Print what can be printed in XML. More...
 
int RemoveDir (char *dirpath)
 Remove all files under dirpath (rm -rf) More...
 
char * PathCheck (char *DirPath)
 Check if path contains a "%U" or "%H". If so, substitute a unique ID for U. More...
 
void deleteTmpFiles (char *dir)
 
void Usage (char *Name, char *Version)
 Display program usage. More...
 
void SQLNoticeProcessor (void *arg, const char *message)
 Dummy postgresql notice processor. This prevents Notices from being written to stderr. More...
 

Variables

const char * SCM_REGEX = "/\\.git|\\.hg|\\.bzr|CVS/ROOT|\\.svn/"
 

Detailed Description

Contains all utility functions used by FOSSology.

Definition in file utils.c.

Function Documentation

int AddToRepository ( ContainerInfo CI,
char *  Fuid,
int  Mask 
)

Add a ContainerInfo record to the repository AND to the database.

This modifies the CI record's pfile and ufile indexes!

Parameters
CI
Fuidsha1.md5.sha256.size
Maskfile mode mask
Returns
1 if added, 0 if already exists!

Definition at line 1412 of file utils.c.

void CheckCommands ( int  Show)

Make sure all commands are usable.

Parameters
ShowUnused
Returns
void but updates global CMD Status

Definition at line 578 of file utils.c.

int CopyFile ( char *  Src,
char *  Dst 
)

Copy a file. For speed: mmap and save.

Parameters
SrcSource file path
[out]DstDestination file path
Returns
0 if copy worked, 1 if failed.

Definition at line 451 of file utils.c.

int DBInsertPfile ( ContainerInfo CI,
char *  Fuid 
)

Insert a Pfile record. Sets the pfile_pk in CI.

Parameters
CI
Fuidstring of sha1.md5.size
Returns
1 if record exists, 0 if record does not exist.

< Temporary storage for mimetype fk from DB

< Temporary storage for pfile_sha256 from DB

Definition at line 1124 of file utils.c.

int DBInsertUploadTree ( ContainerInfo CI,
int  Mask 
)

Insert an UploadTree record.

If the tree is a duplicate, then we need to replicate all of the uploadtree records for the tree. This uses Upload_Pk.

Parameters
CI
Maskmask file mode for ufile_mode
Returns
1 if tree exists for some other project (duplicate) and 0 if tree does not exist.

Definition at line 1296 of file utils.c.

void DebugContainerInfo ( ContainerInfo CI)

Print a ContainerInfo structure.

Parameters
CIContainerInfo struct to print

Definition at line 1095 of file utils.c.

int DisplayContainerInfo ( ContainerInfo CI,
int  Cmd 
)

Print what can be printed in XML.

Parameters
CI
CmdCommand used to create this file (parent) CI->Cmd = command to be used ON this file (child)
Returns
1 if item is unique, 0 if duplicate.

Definition at line 1467 of file utils.c.

int FindCmd ( char *  Filename)

Given a file name, determine the type of extraction command. This uses Magic.

Returns
index to command-type, or -1 on error.

Definition at line 860 of file utils.c.

void FreeDirList ( dirlist DL)

Free a list of files in a directory list.

Parameters
DLdirectory list

Definition at line 961 of file utils.c.

void InitCmd ( )

Initialize the metahandler CMD table.

This ensures that:

  • Every mimetype is loaded
  • Every mimetype has an DBindex.

Definition at line 119 of file utils.c.

int InitMagic ( )

Open and load Magic file Initializes global MagicCookie.

Returns
0 on success

Definition at line 725 of file utils.c.

int IsDebianSourceFile ( char *  Filename)

Read file to see if it is a Debian source file.

Assumes that all Debian source files have a .dsc filename extension.

Parameters
FilenameFile to open
Returns
1 if Filename is a Debian source file, else 0

Definition at line 743 of file utils.c.

int IsDir ( char *  Fname)

Given a filename, is it a directory?

Parameters
Fnamefile name
Returns
1=yes, 0=no.

Definition at line 330 of file utils.c.

int IsExe ( char *  Exe,
int  Quiet 
)

Check if the executable exists.

(Like the command-line "which" but without returning the path.)

Note
This should only be used on relative path executables.
Parameters
ExeExecutable file name
QuietIf true, do not write warning on file not found
Returns
1 if exists, 0 if does not exist.

Definition at line 404 of file utils.c.

int IsFile ( char *  Fname,
int  Link 
)

Given a filename, is it a file?

Parameters
FnamePath of file to check
LinkTrue if should it follow symbolic links
Returns
1=yes, 0=no.

Definition at line 346 of file utils.c.

int IsInflatedFile ( char *  FileName,
int  InflateSize 
)

Test if the file is a compression bomb.

If the size of FileName is a factor of InflateSize more than the size of the directory containing it, then it is a bomb.

Parameters
FileNamepathname to file
InflateSizeInflation factor.
Returns
1 on is one inflated file, 0 on is not

Definition at line 51 of file utils.c.

dirlist* MakeDirList ( char *  Fullname)

Create a list of files in a directory.

Parameters
FullnamePath to top level directory.
Returns
the directory list

Definition at line 980 of file utils.c.

int MkDir ( char *  Fname)

Smart mkdir.

If mkdir fails, then try running MkDirs.

Parameters
Fnamefile name
Returns
0 on success, 1 on failure.

Definition at line 314 of file utils.c.

int MkDirs ( char *  Fname)

Same as command-line "mkdir -p".

Parameters
Fnamefile name
Returns
0 on success, 1 on failure.

Definition at line 259 of file utils.c.

void OctetType ( char *  Filename,
char *  TypeBuf 
)

Figure out the real type of "octet" files in case we can unarchive them.

Parameters
Filename
Staticbuffer to return with new Type

Definition at line 782 of file utils.c.

int ParentWait ( )

Wait for a child. Sets child status.

Returns
the queue record, or -1 if no more children.

Definition at line 520 of file utils.c.

char* PathCheck ( char *  DirPath)

Check if path contains a "%U" or "%H". If so, substitute a unique ID for U.

This substitution parameter must be at the end of the DirPath. Substitute hostname for H. DirPath Directory path.

Returns
new directory path

Definition at line 1672 of file utils.c.

int Prune ( char *  Fname,
struct stat  Stat 
)

Given a filename and its stat, prune it.

  • Remove anything that is not a regular file or directory
  • Remove files when hard-link count > 1 (duplicate search)
  • Remove zero-length files
    Returns
    1=pruned, 0=no change.

Definition at line 228 of file utils.c.

int ReadLine ( FILE *  Fin,
char *  Line,
int  MaxLine 
)

Read a command from a stream.

Read a line each time from one file.

If the line is empty, then try again.

Parameters
FinInput file pointer
[out]LineOutput line buffer
MaxLineMax line length
Returns
line length, or -1 of EOF.

Definition at line 367 of file utils.c.

int RemoveDir ( char *  dirpath)

Remove all files under dirpath (rm -rf)

Parameters
dirpath
Returns
shell exit code of rm -rf

Definition at line 1651 of file utils.c.

void RemovePostfix ( char *  Name)

get rid of the postfix

For example: test.gz --> test

Parameters
[in,out]Nameinput file name

Definition at line 101 of file utils.c.

int RunCommand ( char *  Cmd,
char *  CmdPre,
char *  File,
char *  CmdPost,
char *  Out,
char *  Where 
)

Try a command and return command code.

Command becomes:

  • `Cmd CmdPre 'File' CmdPost Out`
  • If there is a s, then that becomes Where.
    Parameters
    Cmd
    CmdPre
    File
    CmdPost
    Out
    Where
    Returns
    -1 if command could not run.

Definition at line 631 of file utils.c.

void SafeExit ( int  rc)

Close scheduler and database connections, then exit.

Parameters
rcexit code
Returns
no return, calls exit()

Definition at line 88 of file utils.c.

void SetDir ( char *  Dest,
int  DestLen,
char *  Smain,
char *  Sfile 
)

Set a destination directory name.

This will concatenate Smain and Sfile, but remove and terminating filename.

Parameters
[in,out]Destreturned directory name
DestLensize of Dest
Smainmain extraction directory (may be null)
Sfilefilename

NOTE: Someone that embeds "../" within the path can still climb out!

Definition at line 1057 of file utils.c.

void SQLNoticeProcessor ( void *  arg,
const char *  message 
)

Dummy postgresql notice processor. This prevents Notices from being written to stderr.

Parameters
argunused
messageunused

Definition at line 1782 of file utils.c.

int TaintString ( char *  Dest,
int  DestLen,
char *  Src,
int  ProtectQuotes,
char *  Replace 
)

Protect strings intelligently.

Prevents filenames containing ' or % or \ from screwing up system() and snprintf(). Even supports a "%s".

Note
s is assumed to be in single quotes!
Parameters
[in,out]DestDestination to store tainted string
DestLenLength of Dest
SrcSource string
ProtectQuotesSet to protect quotes for shell
ReplaceString to replace with
Returns
0 on success, 1 on overflow.

Definition at line 176 of file utils.c.

int TestSCMData ( char *  sourcefilename)

Search for SCM data in the filename.

SCM data is one of these: Git (.git)Data(char *FileName) Mercurial (.hg) Bazaar (.bzr) CVS (CVS/Root) Subversion (.svn)

Parameters
sourcefilename
Returns
1 if SCM data is found

Definition at line 1234 of file utils.c.

void Usage ( char *  Name,
char *  Version 
)

Display program usage.

Parameters
Nameprogram name
Versionprogram version

Definition at line 1733 of file utils.c.

Variable Documentation

const char* SCM_REGEX = "/\\.git|\\.hg|\\.bzr|CVS/ROOT|\\.svn/"

regular expression to detect SCM data

Definition at line 38 of file utils.c.