Compare commits

...

3 Commits

Author SHA1 Message Date
e029aae1d3
Import xwrite(), cstr_set(), resolve_..._template()
From degesch and json-rpc-shell.
2020-10-10 04:31:52 +02:00
b9457c321f
Rename cstr_transform() argument
It does not always have to be tolower().
2020-10-10 04:30:19 +02:00
2201becca4
Mark some issues 2020-10-10 04:29:41 +02:00

View File

@ -731,6 +731,21 @@ set_blocking (int fd, bool blocking)
return prev; return prev;
} }
static bool
xwrite (int fd, const char *data, size_t len, struct error **e)
{
size_t written = 0;
while (written < len)
{
ssize_t res = write (fd, data + written, len - written);
if (res >= 0)
written += res;
else if (errno != EINTR)
return error_set (e, "%s", strerror (errno));
}
return true;
}
static void static void
xclose (int fd) xclose (int fd)
{ {
@ -1357,6 +1372,7 @@ struct poller_idle
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// The heap could definitely be made faster but we'll prefer simplicity
struct poller_timers struct poller_timers
{ {
struct poller_timer **heap; ///< Min-heap of timers struct poller_timer **heap; ///< Min-heap of timers
@ -2923,6 +2939,13 @@ base64_encode (const void *data, size_t len, struct str *output)
// --- Utilities --------------------------------------------------------------- // --- Utilities ---------------------------------------------------------------
static void
cstr_set (char **s, char *new)
{
free (*s);
*s = new;
}
static void static void
cstr_split (const char *s, const char *delimiters, bool ignore_empty, cstr_split (const char *s, const char *delimiters, bool ignore_empty,
struct strv *out) struct strv *out)
@ -2952,10 +2975,10 @@ cstr_strip_in_place (char *s, const char *stripped_chars)
} }
static void static void
cstr_transform (char *s, int (*tolower) (int c)) cstr_transform (char *s, int (*xform) (int c))
{ {
for (; *s; s++) for (; *s; s++)
*s = tolower (*s); *s = xform (*s);
} }
static char * static char *
@ -3002,6 +3025,7 @@ iconv_xstrdup (iconv_t conv, char *in, size_t in_len, size_t *out_len)
char *in_ptr = in; char *in_ptr = in;
if (in_len == (size_t) -1) if (in_len == (size_t) -1)
// XXX: out_len will be one character longer than the string!
in_len = strlen (in) + 1; in_len = strlen (in) + 1;
while (iconv (conv, (char **) &in_ptr, &in_len, while (iconv (conv, (char **) &in_ptr, &in_len,
@ -3261,16 +3285,8 @@ resolve_relative_data_filename (const char *filename)
} }
static char * static char *
resolve_relative_runtime_filename (const char *filename) resolve_relative_runtime_filename_finish (struct str path)
{ {
struct str path = str_make ();
const char *runtime_dir = getenv ("XDG_RUNTIME_DIR");
if (runtime_dir && *runtime_dir == '/')
str_append (&path, runtime_dir);
else
get_xdg_home_dir (&path, "XDG_DATA_HOME", ".local/share");
str_append_printf (&path, "/%s/%s", PROGRAM_NAME, filename);
// Try to create the file's ancestors; // Try to create the file's ancestors;
// typically the user will want to immediately create a file in there // typically the user will want to immediately create a file in there
const char *last_slash = strrchr (path.str, '/'); const char *last_slash = strrchr (path.str, '/');
@ -3283,6 +3299,41 @@ resolve_relative_runtime_filename (const char *filename)
return str_steal (&path); return str_steal (&path);
} }
static char *
resolve_relative_runtime_filename (const char *filename)
{
struct str path = str_make ();
const char *runtime_dir = getenv ("XDG_RUNTIME_DIR");
if (runtime_dir && *runtime_dir == '/')
str_append (&path, runtime_dir);
else
get_xdg_home_dir (&path, "XDG_DATA_HOME", ".local/share");
str_append_printf (&path, "/%s/%s", PROGRAM_NAME, filename);
return resolve_relative_runtime_filename_finish (path);
}
/// This differs from resolve_relative_runtime_filename() in that we expect
/// the filename to be something like a pattern for mkstemp(), so the resulting
/// path can reside in a system-wide directory with no risk of a conflict.
/// However, we have to take care about permissions. Do we even need this?
static char *
resolve_relative_runtime_template (const char *template)
{
struct str path = str_make ();
const char *runtime_dir = getenv ("XDG_RUNTIME_DIR");
const char *tmpdir = getenv ("TMPDIR");
if (runtime_dir && *runtime_dir == '/')
str_append_printf (&path, "%s/%s", runtime_dir, PROGRAM_NAME);
else if (tmpdir && *tmpdir == '/')
str_append_printf (&path, "%s/%s.%d", tmpdir, PROGRAM_NAME, geteuid ());
else
str_append_printf (&path, "/tmp/%s.%d", PROGRAM_NAME, geteuid ());
str_append_printf (&path, "/%s", template);
return resolve_relative_runtime_filename_finish (path);
}
static char * static char *
try_expand_tilde (const char *filename) try_expand_tilde (const char *filename)
{ {