General helper functions. More...
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>
#include <dlfcn.h>
#include <netdb.h>
#include <time.h>
#include <sys/types.h>
#include <grp.h>
#include <poll.h>
#include <pwd.h>
#include <apr_file_io.h>
#include <apr_md5.h>
#include <subversion-1/svn_config.h>
#include "global.h"
#include "waa.h"
#include "est_ops.h"
#include "options.h"
#include "interface.h"
#include "checksum.h"
#include "helper.h"
#include "cache.h"
#include <iconv.h>
Go to the source code of this file.
Defines | |
#define | STRING_LENGTH (4096) |
Functions | |
int | hlp___get_conv_handle (const char *from_charset, const char *to_charset, iconv_t *cd) |
Initializer for i18n operations. | |
int | hlp___do_convert (iconv_t cd, const char *from, char **to, int len) |
Charset convert function. | |
int | hlp___dummy_convert (const char *input, char **output, int len) |
Dummy converter function. | |
int | hlp__local2utf8 (const char *local_string, char **utf8_string, int len) |
-. | |
int | hlp__utf82local (const char *utf8_string, char **local_string, int len) |
-. | |
void | __attribute__ ((always_inline)) |
Small utility function to copy a system-defined struct stat into our own struct sstat_t - which is much smaller. | |
int | hlp__lstat (const char *fn, struct sstat_t *st) |
A wrapper for lstat(). | |
int | hlp__fstat (int fd, struct sstat_t *st) |
A wrapper for fstat(). | |
char * | hlp__pathcopy (char *dst, int *len,...) |
-. | |
int | hlp__parse_rev (char *stg, char **eos, svn_revnum_t *rev) |
-. | |
const char * | hlp__get_grname (gid_t gid, char *not_found) |
-. | |
const char * | hlp__get_uname (uid_t uid, char *not_found) |
-. | |
int | hlp__get_uid (char *user, uid_t *uid, apr_pool_t *pool) |
-. | |
int | hlp__get_gid (char *group, gid_t *gid, apr_pool_t *pool) |
-. | |
int | hlp__string_from_filep (FILE *input, char **string, char **eos, int flags) |
-. | |
int | hlp__safe_print (FILE *output, char *string, int maxlen) |
-. | |
int | hlp___encoder_waiter (struct encoder_t *encoder) |
select() loop for the encoder pipes. | |
svn_error_t * | hlp___encode_write (void *baton, const char *data, apr_size_t *len) |
Writer function for an encoder. | |
svn_error_t * | hlp___encode_read (void *baton, char *data, apr_size_t *len) |
Reader function for an encoder. | |
svn_error_t * | hlp___encode_close (void *baton) |
void | hlp___encode_filter_child (int pipe_in[2], int pipe_out[2], const char *path, const char *command) |
Helper function. | |
int | hlp__encode_filter (svn_stream_t *s_stream, const char *command, int is_writer, char *path, svn_stream_t **output, struct encoder_t **encoder_out, apr_pool_t *pool) |
-. | |
int | hlp__chrooter (void) |
Checks for the needed environment variables, and does the chroot()ing if necessary. | |
int | hlp___is_valid_env (char *env, char *path2cmp, int p2c_len, char **value, int *value_len) |
Is the given environment valid for substitution? Returns a boolean - 0 means not valid, everything else ok. | |
int | hlp__match_path_envs (struct estat *root) |
Here we simply (try to) match the (few) environment variables against the entries, and set pointers for matching paths; so for displaying them it suffices to walk the tree up until an entry with arg set is found. | |
int | hlp__format_path (struct estat *sts, char *wc_relative_path, char **output) |
Can be in several formats; see Displaying paths. | |
char * | hlp__rev_to_string (svn_revnum_t rev) |
-. | |
int | hlp__strncmp_uline_eq_dash (char *always_ul, char *other, int max) |
-. | |
int | hlp__is_special_property_name (const char *name) |
-. | |
int | hlp__stream_md5 (svn_stream_t *stream, unsigned char md5[APR_MD5_DIGESTSIZE]) |
-. | |
int | hlp__delay (time_t start, enum opt__delay_e which) |
Delays execution until the next second. | |
int | hlp__rename_to_unique (char *fn, char *extension, const char **unique_name, apr_pool_t *pool) |
-. | |
int | hlp__get_svn_config (apr_hash_t **config) |
-. | |
int | hlp__strnalloc (int len, char **dest, const char const *source) |
-. | |
int | hlp__strmnalloc (int len, char **dest, const char const *source,...) |
-. | |
int | hlp__calloc (void *output, size_t nmemb, size_t count) |
-. | |
int | hlp__realloc (void *output, size_t size) |
-. | |
char * | hlp__get_word (char *input, char **word_start) |
-. | |
int | hlp__compare_string_pointers (const void *a, const void *b) |
General helper functions.
Definition in file helper.c.
#define STRING_LENGTH (4096) |
Definition at line 733 of file helper.c.
Referenced by hlp__string_from_filep().
void __attribute__ | ( | (always_inline) | ) | [inline] |
int hlp___do_convert | ( | iconv_t | cd, | |
const char * | from, | |||
char ** | to, | |||
int | len | |||
) | [inline] |
Charset convert function.
Using a handle obtained with hlp___get_conv_handle() this function dynamically allocates some buffer space, and returns the converted data in it.
cd | A conversion handle | |
from | The source string | |
to | A pointer to the converted string. | |
len | The maximum number of input characters to translate. |
If from is NULL, to is returned as NULL.
A few buffers are used round-robin, so that the caller need not free anything and the maximum memory usage is limited. Normally only 1 or 2 buffers are "active", eg. file name for a symlink and its destination, or source and destination for apply_textdelta.
The destination string is always terminated with a \0.
Definition at line 87 of file helper.c.
References cch__add(), cch__entry_set(), cache_entry_t::data, DEBUGP, cache_t::entries, cache_entry_t::len, cache_t::lru, STOPIF, and STOPIF_CODE_ERR.
Referenced by hlp__local2utf8(), and hlp__utf82local().
int hlp___dummy_convert | ( | const char * | input, | |
char ** | output, | |||
int | len | |||
) |
Dummy converter function.
We need to honor the length parameter; but this function XOR the hlp___do_convert() run, so only one cache is active.
If the length is given as -1
, the original string is returned.
Definition at line 200 of file helper.c.
References cch__add(), and STOPIF.
Referenced by hlp__local2utf8(), and hlp__utf82local().
svn_error_t* hlp___encode_close | ( | void * | baton | ) |
Definition at line 1154 of file helper.c.
References encoder_t::child, cs__md5tohex_buffered(), DEBUGP, hlp___encode_write(), IF_FREE, encoder_t::is_writer, encoder_t::md5_ctx, encoder_t::orig, encoder_t::output_md5, encoder_t::pipe_in, RETURN_SVNERR, STOPIF_CODE_ERR, and STOPIF_SVNERR.
Referenced by hlp__encode_filter().
void hlp___encode_filter_child | ( | int | pipe_in[2], | |
int | pipe_out[2], | |||
const char * | path, | |||
const char * | command | |||
) |
Helper function.
Could be marked noreturn
.
Definition at line 1201 of file helper.c.
References FSVS_EXP_CURR_ENTRY, PATH_SEPARATOR, and STOPIF_CODE_ERR.
Referenced by hlp__encode_filter().
svn_error_t* hlp___encode_read | ( | void * | baton, | |
char * | data, | |||
apr_size_t * | len | |||
) |
Reader function for an encoder.
A svn_stream_t
reader gets as much data as it requested; a short read would be interpreted as EOF.
Definition at line 1030 of file helper.c.
References encoder_t::buffer, encoder_t::bytes_left, encoder_t::child, encoder_t::data_pos, DEBUGP, encoder_t::eof, hlp___encoder_waiter(), encoder_t::md5_ctx, encoder_t::orig, encoder_t::pipe_in, encoder_t::pipe_out, RETURN_SVNERR, STOPIF, STOPIF_CODE_ERR, and STOPIF_SVNERR.
Referenced by hlp__encode_filter().
svn_error_t* hlp___encode_write | ( | void * | baton, | |
const char * | data, | |||
apr_size_t * | len | |||
) |
Writer function for an encoder.
We have to write the full buffer through the pipe before returning - possibly in some chunks.
Definition at line 919 of file helper.c.
References encoder_t::buffer, encoder_t::bytes_left, encoder_t::child, encoder_t::data_pos, DEBUGP, encoder_t::eof, hlp___encoder_waiter(), encoder_t::md5_ctx, encoder_t::orig, encoder_t::pipe_in, encoder_t::pipe_out, RETURN_SVNERR, STOPIF, STOPIF_CODE_ERR, and STOPIF_SVNERR.
Referenced by hlp___encode_close(), and hlp__encode_filter().
int hlp___encoder_waiter | ( | struct encoder_t * | encoder | ) |
select() loop for the encoder pipes.
As we can always get/put data from/to the associated stream, the only rate limiter is the child process - how fast it can take/give data.
So we have to poll.
Definition at line 886 of file helper.c.
References encoder_t::is_writer, encoder_t::pipe_in, encoder_t::pipe_out, and STOPIF_CODE_ERR.
Referenced by hlp___encode_read(), and hlp___encode_write().
int hlp___get_conv_handle | ( | const char * | from_charset, | |
const char * | to_charset, | |||
iconv_t * | cd | |||
) |
Initializer for i18n operations.
from_charset | Source charset | |
to_charset | Destination charset. | |
cd | iconv-handle |
Definition at line 48 of file helper.c.
References STOPIF_CODE_ERR.
Referenced by hlp__local2utf8(), and hlp__utf82local().
int hlp___is_valid_env | ( | char * | env, | |
char * | path2cmp, | |||
int | p2c_len, | |||
char ** | value, | |||
int * | value_len | |||
) | [inline] |
Is the given environment valid for substitution? Returns a boolean - 0 means not valid, everything else ok.
It is valid, if
WC
p2c_len
equals -1
the length of the path in the environment variable is taken.If value_len is not NULL
, it get set to the length of the path in the environment variable.
Definition at line 1474 of file helper.c.
References PATH_SEPARATOR.
Referenced by hlp__format_path(), and hlp__match_path_envs().
int hlp__calloc | ( | void * | output, | |
size_t | nmemb, | |||
size_t | count | |||
) |
-.
Error returning calloc()
; uses (void**) output.
That is not defined in terms of hlp__alloc()
, because glibc might do some magic to get automagically 0-initialized memory (like mapping /dev/zero
).
Definition at line 1985 of file helper.c.
References STOPIF_CODE_ERR.
Referenced by cs__read_manber_hashes(), hsh__new(), ign___new_group(), ops__allocate(), url__allocate(), waa__copy_entries(), and waa__output_tree().
int hlp__chrooter | ( | void | ) |
Checks for the needed environment variables, and does the chroot()ing if necessary.
Chroot helper function.
See HOWTO: About running fsvs on older systems.
Definition at line 1363 of file helper.c.
References CHROOTER_CWD_ENV, CHROOTER_LIBS_ENV, CHROOTER_ROOT_ENV, DEBUGP, filename, and STOPIF_CODE_ERR.
Referenced by main().
int hlp__compare_string_pointers | ( | const void * | a, | |
const void * | b | |||
) |
Definition at line 2072 of file helper.c.
Referenced by log__receiver().
int hlp__delay | ( | time_t | start, | |
enum opt__delay_e | which | |||
) |
Delays execution until the next second.
Delay until time wraps.
Needed because of filesystem granularities; FSVS only stores seconds, not more.
Definition at line 1841 of file helper.c.
References DEBUGP, and OPT__DELAY.
Referenced by ci__work(), co__work(), delay__work(), rev__work(), and up__work().
int hlp__encode_filter | ( | svn_stream_t * | s_stream, | |
const char * | command, | |||
int | is_writer, | |||
char * | path, | |||
svn_stream_t ** | output, | |||
struct encoder_t ** | encoder_out, | |||
apr_pool_t * | pool | |||
) |
-.
Encode svn_stream_t
filter.
For is_writer data gets written into s_stream, gets piped as STDIN
into command, and the resulting STDOUT
can be read back with output; if is_writer is zero, data must be read from output to get processed.
After finishing the returned pointer *encoder_out can be used to retrieve the MD5.
*encoder_out must not be free()d
; delta editors that store data locally get driven by the subversion libraries, and the only place where we can be sure that it's no longer in use is when the streams get closed. Therefore the free()ing
has to be done in hlp___encode_close(); callers that want to get the final MD5 can set the encoder->output_md5
pointer to the destination address.
Definition at line 1293 of file helper.c.
References encoder_t::bytes_left, encoder_t::child, DEBUGP, encoder_t::eof, hlp___encode_close(), hlp___encode_filter_child(), hlp___encode_read(), hlp___encode_write(), hlp__alloc(), encoder_t::is_writer, encoder_t::md5_ctx, encoder_t::orig, encoder_t::output_md5, encoder_t::pipe_in, encoder_t::pipe_out, STOPIF, STOPIF_CODE_ERR, and STOPIF_ENOMEM.
Referenced by ci__nondir(), rev__get_text_to_stream(), and up__apply_textdelta().
int hlp__format_path | ( | struct estat * | sts, | |
char * | wc_relative_path, | |||
char ** | output | |||
) |
Can be in several formats; see Displaying paths.
Return a path that gets displayed for the user.
wc_relative_path
only if necessary - remove the parameter from the caller chains. Definition at line 1569 of file helper.c.
References estat::arg, BUG_ON, cch__entry_set(), DEBUGP, environ, ENVIRONMENT_START, hlp___is_valid_env(), hlp__pathcopy(), estat::name, OPT__PATH, estat::parent, PATH_ABSOLUTE, PATH_CACHEDENVIRON, PATH_FULLENVIRON, estat::path_len, PATH_PARMRELATIVE, PATH_SEPARATOR, PATH_WCRELATIVE, STOPIF, wc_path, and wc_path_len.
Referenced by cm___match(), df___repos_repos(), df__do_diff(), and st__print_status().
int hlp__get_gid | ( | char * | group, | |
gid_t * | gid, | |||
apr_pool_t * | pool | |||
) |
-.
Find a GID
by group name, cached.
Uses a simple hash function.
Definition at line 705 of file helper.c.
References cch__hash_add(), cch__hash_find(), and STOPIF.
Referenced by up__parse_prop().
const char* hlp__get_grname | ( | gid_t | gid, | |
char * | not_found | |||
) |
-.
Returns the name of the given group.
Has a few buffers for these operations.
The cache is statically allocated, as we cannot return ENOMEM
.
If we cannot store a cache during querying, we'll return the value, but forget that we already know it.
Definition at line 631 of file helper.c.
References CACHE_DEFAULT, cch__add(), cch__find(), and cache_t::max.
Referenced by ci___set_props(), df__do_diff(), and st__print_entry_info().
int hlp__get_svn_config | ( | apr_hash_t ** | config | ) |
-.
Reads the subversion config file(s), found by Configuration directory for the subversion libraries.
Caches the result, so that the configuration is only fetched a single time.
Definition at line 1911 of file helper.c.
References global_pool, OPT__CONFIG_DIR, and STOPIF_SVNERR.
Referenced by cb__init(), and url__open_session().
int hlp__get_uid | ( | char * | user, | |
uid_t * | uid, | |||
apr_pool_t * | pool | |||
) |
-.
Find a UID
by user name, cached.
Uses a simple hash function.
Definition at line 673 of file helper.c.
References cch__hash_add(), cch__hash_find(), and STOPIF.
Referenced by up__parse_prop().
const char* hlp__get_uname | ( | uid_t | uid, | |
char * | not_found | |||
) |
-.
Returns the name of the given user.
Has a few buffers for these operations; the cache is statically allocated, as we cannot return ENOMEM
.
If we cannot store a cache during querying, we'll return the value, but forget that we already know it.
Definition at line 655 of file helper.c.
References CACHE_DEFAULT, cch__add(), cch__find(), and cache_t::max.
Referenced by ci___set_props(), df__do_diff(), and st__print_entry_info().
char* hlp__get_word | ( | char * | input, | |
char ** | word_start | |||
) |
-.
Stores the first non-whitespace character position from input in word_start, and returns the next whitespace position in word_end.
Definition at line 2019 of file helper.c.
References hlp__skip_ws().
Referenced by ign___load_group(), and ops__load_1entry().
int hlp__is_special_property_name | ( | const char * | name | ) |
-.
name is a subversion internal property.
Definition at line 1790 of file helper.c.
Referenced by ci___send_user_props(), and up__parse_prop().
int hlp__local2utf8 | ( | const char * | local_string, | |
char ** | utf8_string, | |||
int | len | |||
) |
-.
Converts a string with local encoding to UTF8, suitable for storage in the repository.
If len ==-1
, a strlen()
is done.
Definition at line 227 of file helper.c.
References hlp___do_convert(), hlp___dummy_convert(), hlp___get_conv_handle(), local_codeset, and STOPIF.
Referenced by ci__nondir(), ci__work(), rev__get_props(), rev__get_text_to_stream(), sync___recurse(), and up__apply_textdelta().
int hlp__match_path_envs | ( | struct estat * | root | ) |
Here we simply (try to) match the (few) environment variables against the entries, and set pointers for matching paths; so for displaying them it suffices to walk the tree up until an entry with arg
set is found.
Distribute the environment variables on the loaded entries.
Definition at line 1508 of file helper.c.
References estat::arg, DEBUGP, environ, ENVIRONMENT_START, hlp___is_valid_env(), hlp__alloc(), estat::name, ops__traverse(), STOPIF, wc_path, and wc_path_len.
Referenced by waa__read_or_build_tree().
int hlp__parse_rev | ( | char * | stg, | |
char ** | eos, | |||
svn_revnum_t * | rev | |||
) |
-.
Parses a string to a revision number.
Normally a number; currently a special case is recogniced, namely HEAD
.
If the parameter eos
is not NULL
, it gets set to the character behind the parsed part, which is ignored. If it is NULL
, the string must end here.
Definition at line 591 of file helper.c.
References opt_target_revision, and STOPIF_CODE_ERR.
Referenced by main(), url__mark_todo(), and url__parse().
char* hlp__pathcopy | ( | char * | dst, | |
int * | len, | |||
... | ||||
) |
-.
A function like strcpy, but cleaning up paths.
dst | A target buffer | |
len | An optional length-saving field | |
... | A NULL -terminated list of pointers, which will be concatenated. |
This works like strcpy
, but removes sequences like /./
and //
.
If the first path has no PATH_SEPARATOR as first character, the start_path is used as beginning - so that always full paths are returned.
Used for cases where we cannot run realpath() (as this follows symlinks, something we don't want), but need to take care of some strange pathnames.
a//..//.///b/c//
.For people reading JoelOnSoftware and related pages - yes, this is cheating, but it works and is fast :-).
Definition at line 438 of file helper.c.
References DEBUGP, PATH_SEPARATOR, start_path, and start_path_len.
Referenced by cm___absolute_path(), hlp__format_path(), opt___normalized_path(), waa___get_path_md5(), waa__find_common_base2(), and waa__init().
int hlp__realloc | ( | void * | output, | |
size_t | size | |||
) |
-.
Reallocate the (void**) output.
Definition at line 2001 of file helper.c.
References STOPIF_CODE_ERR.
Referenced by cch__entry_set(), cs__read_manber_hashes(), dir__enumerator(), dir__sortbyname(), hlp__alloc(), ign__compile_pattern(), ign__new_pattern(), ops__free_marked(), ops__new_entries(), up__apply_textdelta(), url__allocate(), url__store_url_name(), waa__open(), waa__output_tree(), and waa__save_cwd().
int hlp__rename_to_unique | ( | char * | fn, | |
char * | extension, | |||
const char ** | unique_name, | |||
apr_pool_t * | pool | |||
) |
-.
Renames a local file to something like .mine.
We could either generate a name ourself, or just use this function - and have in mind that we open and close a file, just to overwrite it immediately.
But by using that function we get the behaviour that subversion users already know.
Definition at line 1864 of file helper.c.
References DEBUGP, PATH_SEPARATOR, STOPIF, and STOPIF_SVNERR.
Referenced by rev___undo_change().
char* hlp__rev_to_string | ( | svn_revnum_t | rev | ) |
-.
Returns a string describing the revision number.
Can be non-numeric, like HEAD
.
Definition at line 1735 of file helper.c.
References BUG_ON.
Referenced by log__work(), rev___revert_to_base(), rev__get_text_to_stream(), url___dump(), url__iterator2(), and url__parse().
int hlp__safe_print | ( | FILE * | output, | |
char * | string, | |||
int | maxlen | |||
) |
-.
Print the given data to output, safely converting special characters to codes like \x1e
.
Some special values are recogniced - eg. \r
, \n
.
Definition at line 829 of file helper.c.
References STOPIF_CODE_EPIPE.
Referenced by prp__l_work().
int hlp__stream_md5 | ( | svn_stream_t * | stream, | |
unsigned char | md5[APR_MD5_DIGESTSIZE] | |||
) |
-.
Reads all data from stream and drops it.
md5, if not NULL
, must point to at least MD5_DIGEST_LENGTH bytes.
Definition at line 1805 of file helper.c.
References DEBUGP, hlp__alloc(), estat::md5, STOPIF, and STOPIF_SVNERR.
Referenced by ci__nondir().
int hlp__string_from_filep | ( | FILE * | input, | |
char ** | string, | |||
char ** | eos, | |||
int | flags | |||
) |
-.
Reads a string from the given input
into the (self-managed) buffer string, removes \r and/or \n at the end, and depending on flags strips whitespace or comments.
Returns 0 for success, EOF
for no more data.
Empty lines (only whitespace) are ignored (but counted).
If no_ws is set, the returned pointer has whitespace at beginning and end removed; \r
and \n
at the end are always removed.
Only a single statically allocated buffer is used, so the line has to be copied if needed over several invocations.
eos is set to the last non-whitespace character in the line.
Definition at line 746 of file helper.c.
References DEBUGP, hlp__alloc(), hlp__skip_ws(), SFF_COMMENT, SFF_GET_LINENUM, SFF_RESET_LINENUM, SFF_WHITESPACE, STOPIF, and STRING_LENGTH.
Referenced by cm__work(), ign___load_group(), ign__work(), opt__load_settings(), url__load_list(), and url__work().
int hlp__strmnalloc | ( | int | len, | |
char ** | dest, | |||
const char const * | source, | |||
... | ||||
) |
-.
Like hlp__strnalloc
, but concatenates strings until a NULL
is found.
Definition at line 1949 of file helper.c.
References BUG_ON, hlp__alloc(), and STOPIF.
Referenced by ci__getmsg(), ign___init_pattern_into(), ign___load_group(), log__receiver(), and log__work().
int hlp__strnalloc | ( | int | len, | |
char ** | dest, | |||
const char const * | source | |||
) |
-.
Allocates a buffer in *dest, and copies source.
If source is not NULL
len bytes are copied. The buffer is always \0
terminated.
Definition at line 1934 of file helper.c.
References hlp__alloc(), and STOPIF.
Referenced by ci__work(), cm___get_base_source(), hlp__strdup(), ign___init_pattern_into(), ign___load_group(), log__work(), opt___store_string(), url__open_session(), url__parse(), waa__find_common_base2(), and waa__set_working_copy().
int hlp__strncmp_uline_eq_dash | ( | char * | always_ul, | |
char * | other, | |||
int | max | |||
) |
-.
Function to compare two strings for max bytes, but treating '-'
and '_'
as equal.
If max<0, the comparision is done until the \0
. max is the maximum number of characters to compare; the result is always equal
(==0
), if max==0
.
Not useable for lesser/greater compares.
Definition at line 1762 of file helper.c.
Referenced by opt__parse().
int hlp__utf82local | ( | const char * | utf8_string, | |
char ** | local_string, | |||
int | len | |||
) |
-.
Converts an UTF8 string to local encoding, to make it printable on the current console.
If len ==-1
, a strlen()
is done.
Definition at line 255 of file helper.c.
References hlp___do_convert(), hlp___dummy_convert(), hlp___get_conv_handle(), local_codeset, and STOPIF.
Referenced by cb___delete_entry(), cb__add_entry(), log__receiver(), sync___recurse(), up__handle_special(), and up__parse_prop().