FOSSology  4.4.0
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

◆ AddToRepository()

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 1402 of file utils.c.

◆ CheckCommands()

void CheckCommands ( int  Show)

Make sure all commands are usable.

Parameters
ShowUnused
Returns
void but updates global CMD Status

Definition at line 567 of file utils.c.

◆ CopyFile()

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 440 of file utils.c.

◆ DBInsertPfile()

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 1114 of file utils.c.

◆ DBInsertUploadTree()

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 1286 of file utils.c.

◆ DebugContainerInfo()

void DebugContainerInfo ( ContainerInfo CI)

Print a ContainerInfo structure.

Parameters
CIContainerInfo struct to print

Definition at line 1085 of file utils.c.

◆ DisplayContainerInfo()

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 1457 of file utils.c.

◆ FindCmd()

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 850 of file utils.c.

◆ FreeDirList()

void FreeDirList ( dirlist DL)

Free a list of files in a directory list.

Parameters
DLdirectory list

Definition at line 951 of file utils.c.

◆ InitCmd()

void InitCmd ( )

Initialize the metahandler CMD table.

This ensures that:

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

Definition at line 108 of file utils.c.

◆ InitMagic()

int InitMagic ( )

Open and load Magic file Initializes global MagicCookie.

Returns
0 on success

Definition at line 715 of file utils.c.

◆ IsDebianSourceFile()

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 733 of file utils.c.

◆ IsDir()

int IsDir ( char *  Fname)

Given a filename, is it a directory?

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

Definition at line 319 of file utils.c.

◆ IsExe()

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 393 of file utils.c.

◆ IsFile()

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 335 of file utils.c.

◆ IsInflatedFile()

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 40 of file utils.c.

◆ MakeDirList()

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 970 of file utils.c.

◆ MkDir()

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 303 of file utils.c.

◆ MkDirs()

int MkDirs ( char *  Fname)

Same as command-line "mkdir -p".

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

Definition at line 248 of file utils.c.

◆ OctetType()

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 772 of file utils.c.

◆ ParentWait()

int ParentWait ( )

Wait for a child. Sets child status.

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

Definition at line 509 of file utils.c.

◆ PathCheck()

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. @parm DirPath Directory path.

Returns
new directory path

Definition at line 1662 of file utils.c.

◆ Prune()

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 217 of file utils.c.

◆ ReadLine()

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 356 of file utils.c.

◆ RemoveDir()

int RemoveDir ( char *  dirpath)

Remove all files under dirpath (rm -rf)

Parameters
dirpath
Returns
shell exit code of rm -rf

Definition at line 1641 of file utils.c.

◆ RemovePostfix()

void RemovePostfix ( char *  Name)

get rid of the postfix

For example: test.gz --> test

Parameters
[in,out]Nameinput file name

Definition at line 90 of file utils.c.

◆ RunCommand()

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 621 of file utils.c.

◆ SafeExit()

void SafeExit ( int  rc)

Close scheduler and database connections, then exit.

Parameters
rcexit code
Returns
no return, calls exit()

Definition at line 77 of file utils.c.

◆ SetDir()

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 1047 of file utils.c.

◆ SQLNoticeProcessor()

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 1772 of file utils.c.

◆ TaintString()

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 165 of file utils.c.

◆ TestSCMData()

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 1224 of file utils.c.

◆ Usage()

void Usage ( char *  Name,
char *  Version 
)

Display program usage.

Parameters
Nameprogram name
Versionprogram version

Definition at line 1723 of file utils.c.

Variable Documentation

◆ SCM_REGEX

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

regular expression to detect SCM data

Definition at line 27 of file utils.c.