libalpm
Arch Linux Package Manager Library
Functions
util.c File Reference
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <dirent.h>
#include <time.h>
#include <syslog.h>
#include <errno.h>
#include <limits.h>
#include <sys/wait.h>
#include <locale.h>
#include <fnmatch.h>
#include <archive.h>
#include <archive_entry.h>
#include "md5.h"
#include "sha2.h"
#include "util.h"
#include "log.h"
#include "alpm.h"
#include "alpm_list.h"
#include "handle.h"
#include "trans.h"
Include dependency graph for lib/libalpm/util.c:

Go to the source code of this file.

Functions

char * strsep (char **str, const char *delims)
 Extracts tokens from a string.
int _alpm_makepath (const char *path)
int _alpm_makepath_mode (const char *path, mode_t mode)
 Creates a directory, including parents if needed, similar to 'mkdir -p'.
int _alpm_copyfile (const char *src, const char *dest)
 Copies a file.
size_t _alpm_strip_newline (char *str)
 Trim trailing newlines from a string (if any exist).
int _alpm_open_archive (alpm_handle_t *handle, const char *path, struct stat *buf, struct archive **archive, alpm_errno_t error)
 Open an archive for reading and perform the necessary boilerplate.
int _alpm_unpack_single (alpm_handle_t *handle, const char *archive, const char *prefix, const char *filename)
 Unpack a specific file in an archive.
int _alpm_unpack (alpm_handle_t *handle, const char *path, const char *prefix, alpm_list_t *list, int breakfirst)
 Unpack a list of files in an archive.
int _alpm_rmrf (const char *path)
 Recursively removes a path similar to 'rm -rf'.
ssize_t _alpm_files_in_directory (alpm_handle_t *handle, const char *path, int full_count)
 Determine if there are files in a directory.
int _alpm_logaction (alpm_handle_t *handle, const char *fmt, va_list args)
 Write formatted message to log.
int _alpm_run_chroot (alpm_handle_t *handle, const char *cmd, char *const argv[])
 Execute a command with arguments in a chroot.
int _alpm_ldconfig (alpm_handle_t *handle)
 Run ldconfig in a chroot.
int _alpm_str_cmp (const void *s1, const void *s2)
 Helper function for comparing strings using the alpm "compare func" signature.
char * _alpm_filecache_find (alpm_handle_t *handle, const char *filename)
 Find a filename in a registered alpm cachedir.
const char * _alpm_filecache_setup (alpm_handle_t *handle)
 Check the alpm cachedirs for existance and find a writable one.
int _alpm_lstat (const char *path, struct stat *buf)
 lstat wrapper that treats /path/dirsymlink/ the same as /path/dirsymlink.
char * alpm_compute_md5sum (const char *filename)
char * alpm_compute_sha256sum (const char *filename)
int _alpm_test_checksum (const char *filepath, const char *expected, enum _alpm_csum type)
 Calculates a file's MD5 or SHA2 digest and compares it to an expected value.
int _alpm_archive_fgets (struct archive *a, struct archive_read_buffer *b)
 TODO.
int _alpm_splitname (const char *target, char **name, char **version, unsigned long *name_hash)
 Parse a full package specifier.
unsigned long _alpm_hash_sdbm (const char *str)
 Hash the given string to an unsigned long value.
off_t _alpm_strtoofft (const char *line)
 Convert a string to a file offset.
alpm_time_t _alpm_parsedate (const char *line)
 Parses a date into an alpm_time_t struct.
int _alpm_access (alpm_handle_t *handle, const char *dir, const char *file, int amode)
 Wrapper around access() which takes a dir and file argument separately and generates an appropriate error message.
int _alpm_fnmatch (const void *pattern, const void *string)
 Checks whether a string matches a shell wildcard pattern.
char * strndup (const char *s, size_t n)
 Copies a string.

Function Documentation

int _alpm_access ( alpm_handle_t handle,
const char *  dir,
const char *  file,
int  amode 
)

Wrapper around access() which takes a dir and file argument separately and generates an appropriate error message.

If dir is NULL file will be treated as the whole path.

Parameters:
handlean alpm handle
dirdirectory path ending with and slash
filefilename
amodeaccess mode as described in access()
Returns:
int value returned by access()

Definition at line 1235 of file lib/libalpm/util.c.

References _alpm_log(), ALPM_ERR_MEMORY, ALPM_LOG_DEBUG, CALLOC, and RET_ERR.

Referenced by _alpm_gpgme_checksig(), _alpm_pkg_validate_internal(), _alpm_process_siglist(), and _alpm_runscriptlet().

Here is the call graph for this function:

Here is the caller graph for this function:

int _alpm_archive_fgets ( struct archive *  a,
struct archive_read_buffer b 
)
int _alpm_copyfile ( const char *  src,
const char *  dest 
)

Copies a file.

Parameters:
srcfile path to copy from
destfile path to copy to
Returns:
0 on success, 1 on error

Definition at line 142 of file lib/libalpm/util.c.

References ALPM_BUFFER_SIZE, CLOSE, MALLOC, and OPEN.

Referenced by _alpm_runscriptlet().

Here is the caller graph for this function:

char* _alpm_filecache_find ( alpm_handle_t handle,
const char *  filename 
)

Find a filename in a registered alpm cachedir.

Parameters:
handlethe context handle
filenamename of file to find
Returns:
malloced path of file, NULL if not found

Definition at line 689 of file lib/libalpm/util.c.

References _alpm_log(), ALPM_LOG_DEBUG, __alpm_handle_t::cachedirs, __alpm_list_t::data, and __alpm_list_t::next.

Referenced by alpm_fetch_pkgurl(), and alpm_pkg_checkmd5sum().

Here is the call graph for this function:

Here is the caller graph for this function:

const char* _alpm_filecache_setup ( alpm_handle_t handle)

Check the alpm cachedirs for existance and find a writable one.

If no valid cache directory can be found, use /tmp.

Parameters:
handlethe context handle
Returns:
pointer to a writable cache directory.

Definition at line 715 of file lib/libalpm/util.c.

References _, _alpm_log(), _alpm_makepath(), ALPM_LOG_DEBUG, ALPM_LOG_WARNING, alpm_option_add_cachedir(), __alpm_handle_t::cachedirs, __alpm_list_t::data, __alpm_list_t::next, and __alpm_list_t::prev.

Referenced by alpm_fetch_pkgurl().

Here is the call graph for this function:

Here is the caller graph for this function:

ssize_t _alpm_files_in_directory ( alpm_handle_t handle,
const char *  path,
int  full_count 
)

Determine if there are files in a directory.

Parameters:
handlethe context handle
paththe full absolute directory path
full_countwhether to return an exact count of files
Returns:
a file count if full_count is != 0, else >0 if directory has contents, 0 if no contents, and -1 on error

Definition at line 453 of file lib/libalpm/util.c.

References _alpm_log(), and ALPM_LOG_DEBUG.

Here is the call graph for this function:

int _alpm_fnmatch ( const void *  pattern,
const void *  string 
)

Checks whether a string matches a shell wildcard pattern.

Wrapper around fnmatch.

Parameters:
patternpattern to match aganist
stringstring to check against pattern
Returns:
0 if string matches pattern, non-zero if they don't match and on error

Definition at line 1282 of file lib/libalpm/util.c.

Referenced by _alpm_pkg_should_ignore().

Here is the caller graph for this function:

unsigned long _alpm_hash_sdbm ( const char *  str)

Hash the given string to an unsigned long value.

This is the standard sdbm hashing algorithm.

Parameters:
strstring to hash
Returns:
the hash value of the given string

Definition at line 1144 of file lib/libalpm/util.c.

Referenced by _alpm_pkg_find(), _alpm_pkghash_find(), _alpm_splitdep(), and _alpm_splitname().

Here is the caller graph for this function:

int _alpm_ldconfig ( alpm_handle_t handle)

Run ldconfig in a chroot.

Parameters:
handlethe context handle
Returns:
0 on success, 1 on error

Definition at line 654 of file lib/libalpm/util.c.

References _alpm_log(), _alpm_run_chroot(), ALPM_LOG_DEBUG, and __alpm_handle_t::root.

Referenced by _alpm_remove_packages(), and _alpm_upgrade_packages().

Here is the call graph for this function:

Here is the caller graph for this function:

int _alpm_logaction ( alpm_handle_t handle,
const char *  fmt,
va_list  args 
)

Write formatted message to log.

Parameters:
handlethe context handle
formatformatted string to write out
argsformatting arguments
Returns:
0 or number of characters written on success, vfprintf return value on error

Definition at line 494 of file lib/libalpm/util.c.

References __alpm_handle_t::logstream, and __alpm_handle_t::usesyslog.

Referenced by alpm_logaction().

Here is the caller graph for this function:

int _alpm_lstat ( const char *  path,
struct stat *  buf 
)

lstat wrapper that treats /path/dirsymlink/ the same as /path/dirsymlink.

Linux lstat follows POSIX semantics and still performs a dereference on the first, and for uses of lstat in libalpm this is not what we want.

Parameters:
pathpath to file to lstat
bufstructure to fill with stat information
Returns:
the return code from lstat

Definition at line 768 of file lib/libalpm/util.c.

Referenced by _alpm_db_find_fileconflicts(), and _alpm_rmrf().

Here is the caller graph for this function:

int _alpm_makepath ( const char *  path)

Definition at line 92 of file lib/libalpm/util.c.

References _alpm_makepath_mode().

Referenced by _alpm_filecache_setup(), and _alpm_handle_lock().

Here is the call graph for this function:

Here is the caller graph for this function:

int _alpm_makepath_mode ( const char *  path,
mode_t  mode 
)

Creates a directory, including parents if needed, similar to 'mkdir -p'.

Parameters:
pathdirectory path to create
modepermission mode for created directories
Returns:
0 on success, 1 on error

Definition at line 102 of file lib/libalpm/util.c.

References strsep().

Referenced by _alpm_makepath(), and _alpm_runscriptlet().

Here is the call graph for this function:

Here is the caller graph for this function:

int _alpm_open_archive ( alpm_handle_t handle,
const char *  path,
struct stat *  buf,
struct archive **  archive,
alpm_errno_t  error 
)

Open an archive for reading and perform the necessary boilerplate.

This takes care of creating the libarchive 'archive' struct, setting up compression and format options, opening a file descriptor, setting up the buffer size, and performing a stat on the path once opened. On error, no file descriptor is opened, and the archive pointer returned will be set to NULL.

Parameters:
handlethe context handle
paththe path of the archive to open
bufspace for a stat buffer for the given path
archivepointer to place the created archive object
errorerror code to set on failure to open archive
Returns:
-1 on failure, >=0 file descriptor on success

Definition at line 225 of file lib/libalpm/util.c.

References _, _alpm_log(), ALPM_BUFFER_SIZE, ALPM_ERR_LIBARCHIVE, ALPM_LOG_DEBUG, ALPM_LOG_ERROR, CLOSE, OPEN, and RET_ERR.

Referenced by _alpm_pkg_load_internal(), and _alpm_unpack().

Here is the call graph for this function:

Here is the caller graph for this function:

alpm_time_t _alpm_parsedate ( const char *  line)

Parses a date into an alpm_time_t struct.

Parameters:
linedate to parse
Returns:
time struct on success, 0 on error

Definition at line 1193 of file lib/libalpm/util.c.

int _alpm_rmrf ( const char *  path)

Recursively removes a path similar to 'rm -rf'.

Parameters:
pathpath to remove
Returns:
0 on success, number of paths that could not be removed on error

Definition at line 404 of file lib/libalpm/util.c.

References _alpm_lstat(), and _alpm_rmrf().

Referenced by _alpm_local_db_remove(), and _alpm_rmrf().

Here is the call graph for this function:

Here is the caller graph for this function:

int _alpm_run_chroot ( alpm_handle_t handle,
const char *  cmd,
char *const  argv[] 
)

Execute a command with arguments in a chroot.

Parameters:
handlethe context handle
cmdcommand to execute
argvarguments to pass to cmd
Returns:
0 on success, 1 on error

Definition at line 531 of file lib/libalpm/util.c.

References _, _alpm_log(), ALPM_EVENT_SCRIPTLET_INFO, ALPM_LOG_DEBUG, ALPM_LOG_ERROR, alpm_logaction(), CLOSE, EVENT, OPEN, and __alpm_handle_t::root.

Referenced by _alpm_ldconfig(), and _alpm_runscriptlet().

Here is the call graph for this function:

Here is the caller graph for this function:

int _alpm_splitname ( const char *  target,
char **  name,
char **  version,
unsigned long *  name_hash 
)

Parse a full package specifier.

Parameters:
targetpackage specifier to parse, such as: "pacman-4.0.1-2", "pacman-4.01-2/", or "pacman-4.0.1-2/desc"
nameto hold package name
versionto hold package version
name_hashto hold package name hash
Returns:
0 on success, -1 on error

Definition at line 1087 of file lib/libalpm/util.c.

References _alpm_hash_sdbm(), FREE, and STRNDUP.

Here is the call graph for this function:

int _alpm_str_cmp ( const void *  s1,
const void *  s2 
)

Helper function for comparing strings using the alpm "compare func" signature.

Parameters:
s1first string to be compared
s2second string to be compared
Returns:
0 if strings are equal, positive int if first unequal character has a greater value in s1, negative if it has a greater value in s2

Definition at line 679 of file lib/libalpm/util.c.

Referenced by alpm_pkg_compute_requiredby().

Here is the caller graph for this function:

size_t _alpm_strip_newline ( char *  str)

Trim trailing newlines from a string (if any exist).

Parameters:
stra single line of text
Returns:
the length of the trimmed string

Definition at line 195 of file lib/libalpm/util.c.

off_t _alpm_strtoofft ( const char *  line)

Convert a string to a file offset.

This parses bare positive integers only.

Parameters:
linestring to convert
Returns:
off_t on success, -1 on error

Definition at line 1164 of file lib/libalpm/util.c.

Referenced by _alpm_delta_parse().

Here is the caller graph for this function:

int _alpm_test_checksum ( const char *  filepath,
const char *  expected,
enum _alpm_csum  type 
)

Calculates a file's MD5 or SHA2 digest and compares it to an expected value.

Parameters:
filepathpath of the file to check
expectedhash value to compare against
typedigest type to use
Returns:
0 if file matches the expected hash, 1 if they do not match, -1 on error

Definition at line 951 of file lib/libalpm/util.c.

References alpm_compute_md5sum(), alpm_compute_sha256sum(), ALPM_CSUM_MD5, ALPM_CSUM_SHA256, and FREE.

Referenced by _alpm_pkg_validate_internal(), and alpm_pkg_checkmd5sum().

Here is the call graph for this function:

Here is the caller graph for this function:

int _alpm_unpack ( alpm_handle_t handle,
const char *  path,
const char *  prefix,
alpm_list_t list,
int  breakfirst 
)

Unpack a list of files in an archive.

Parameters:
handlethe context handle
paththe archive to unpack
prefixwhere to extract the files
lista list of files within the archive to unpack or NULL for all
breakfirstbreak after the first entry found
Returns:
0 on success, 1 on failure

Definition at line 303 of file lib/libalpm/util.c.

References _, _alpm_log(), _alpm_open_archive(), ALPM_ERR_PKG_OPEN, alpm_list_find_str(), ALPM_LOG_DEBUG, ALPM_LOG_ERROR, ALPM_LOG_WARNING, CLOSE, and OPEN.

Referenced by _alpm_unpack_single().

Here is the call graph for this function:

Here is the caller graph for this function:

int _alpm_unpack_single ( alpm_handle_t handle,
const char *  archive,
const char *  prefix,
const char *  filename 
)

Unpack a specific file in an archive.

Parameters:
handlethe context handle
archivethe archive to unpack
prefixwhere to extract the files
filenamea file within the archive to unpack
Returns:
0 on success, 1 on failure

Definition at line 281 of file lib/libalpm/util.c.

References _alpm_unpack(), alpm_list_add(), and alpm_list_free().

Referenced by _alpm_runscriptlet().

Here is the call graph for this function:

Here is the caller graph for this function:

char* alpm_compute_md5sum ( const char *  filename)

Definition at line 911 of file lib/libalpm/util.c.

References ASSERT, and md5_file().

Referenced by _alpm_test_checksum().

Here is the call graph for this function:

Here is the caller graph for this function:

char* alpm_compute_sha256sum ( const char *  filename)

Definition at line 930 of file lib/libalpm/util.c.

References ASSERT, and sha2_file().

Referenced by _alpm_test_checksum().

Here is the call graph for this function:

Here is the caller graph for this function:

char* strndup ( const char *  s,
size_t  n 
)

Copies a string.

Returned string needs to be freed

Parameters:
sstring to be copied
nmaximum number of characters to copy
Returns:
pointer to the new string on success, NULL on error

Definition at line 1307 of file lib/libalpm/util.c.

Referenced by strsplit().

Here is the caller graph for this function:

char* strsep ( char **  str,
const char *  delims 
)

Extracts tokens from a string.

Replaces strset which is not portable (missing on Solaris). Copyright (c) 2001 by François Gouget <fgouget_at_codeweavers.com> Modifies str to point to the first character after the token if one is found, or NULL if one is not.

Parameters:
strstring containing delimited tokens to parse
delimcharacter delimiting tokens in str
Returns:
pointer to the first token in str if str is not NULL, NULL if str is NULL

Definition at line 68 of file lib/libalpm/util.c.

Referenced by _alpm_makepath_mode().

Here is the caller graph for this function: