#include "config.h"
#include "glib.h"
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#ifdef G_OS_WIN32
#include <io.h>
#ifndef F_OK
#define F_OK 0
#define W_OK 2
#define R_OK 4
#endif
#ifndef S_ISREG
#define S_ISREG(mode) ((mode)&_S_IFREG)
#endif
#ifndef S_ISDIR
#define S_ISDIR(mode) ((mode)&_S_IFDIR)
#endif
#endif
#ifndef S_ISLNK
#define S_ISLNK(x) 0
#endif
#ifndef O_BINARY
#define O_BINARY 0
#endif
#include "glibintl.h"
#ifdef G_OS_WIN32
#define G_IS_DIR_SEPARATOR(c) (c == G_DIR_SEPARATOR || c == '/')
#else
#define G_IS_DIR_SEPARATOR(c) (c == G_DIR_SEPARATOR)
#endif
gboolean
g_file_test (const gchar *filename,
GFileTest test)
{
if ((test & G_FILE_TEST_EXISTS) && (access (filename, F_OK) == 0))
return TRUE;
#ifndef G_OS_WIN32
if ((test & G_FILE_TEST_IS_EXECUTABLE) && (access (filename, X_OK) == 0))
{
if (getuid () != 0)
return TRUE;
}
else
test &= ~G_FILE_TEST_IS_EXECUTABLE;
#endif
if (test & G_FILE_TEST_IS_SYMLINK)
{
#ifdef G_OS_WIN32
#else
struct stat s;
if ((lstat (filename, &s) == 0) && S_ISLNK (s.st_mode))
return TRUE;
#endif
}
if (test & (G_FILE_TEST_IS_REGULAR |
G_FILE_TEST_IS_DIR |
G_FILE_TEST_IS_EXECUTABLE))
{
struct stat s;
if (stat (filename, &s) == 0)
{
if ((test & G_FILE_TEST_IS_REGULAR) && S_ISREG (s.st_mode))
return TRUE;
if ((test & G_FILE_TEST_IS_DIR) && S_ISDIR (s.st_mode))
return TRUE;
#ifndef G_OS_WIN32
if ((test & G_FILE_TEST_IS_EXECUTABLE) &&
((s.st_mode & S_IXOTH) ||
(s.st_mode & S_IXUSR) ||
(s.st_mode & S_IXGRP)))
return TRUE;
#else
if ((test & G_FILE_TEST_IS_EXECUTABLE) &&
(s.st_mode & _S_IEXEC))
return TRUE;
#endif
}
}
return FALSE;
}
GQuark
g_file_error_quark (void)
{
static GQuark q = 0;
if (q == 0)
q = g_quark_from_static_string ("g-file-error-quark");
return q;
}
GFileError
g_file_error_from_errno (gint err_no)
{
switch (err_no)
{
#ifdef EEXIST
case EEXIST:
return G_FILE_ERROR_EXIST;
break;
#endif
#ifdef EISDIR
case EISDIR:
return G_FILE_ERROR_ISDIR;
break;
#endif
#ifdef EACCES
case EACCES:
return G_FILE_ERROR_ACCES;
break;
#endif
#ifdef ENAMETOOLONG
case ENAMETOOLONG:
return G_FILE_ERROR_NAMETOOLONG;
break;
#endif
#ifdef ENOENT
case ENOENT:
return G_FILE_ERROR_NOENT;
break;
#endif
#ifdef ENOTDIR
case ENOTDIR:
return G_FILE_ERROR_NOTDIR;
break;
#endif
#ifdef ENXIO
case ENXIO:
return G_FILE_ERROR_NXIO;
break;
#endif
#ifdef ENODEV
case ENODEV:
return G_FILE_ERROR_NODEV;
break;
#endif
#ifdef EROFS
case EROFS:
return G_FILE_ERROR_ROFS;
break;
#endif
#ifdef ETXTBSY
case ETXTBSY:
return G_FILE_ERROR_TXTBSY;
break;
#endif
#ifdef EFAULT
case EFAULT:
return G_FILE_ERROR_FAULT;
break;
#endif
#ifdef ELOOP
case ELOOP:
return G_FILE_ERROR_LOOP;
break;
#endif
#ifdef ENOSPC
case ENOSPC:
return G_FILE_ERROR_NOSPC;
break;
#endif
#ifdef ENOMEM
case ENOMEM:
return G_FILE_ERROR_NOMEM;
break;
#endif
#ifdef EMFILE
case EMFILE:
return G_FILE_ERROR_MFILE;
break;
#endif
#ifdef ENFILE
case ENFILE:
return G_FILE_ERROR_NFILE;
break;
#endif
#ifdef EBADF
case EBADF:
return G_FILE_ERROR_BADF;
break;
#endif
#ifdef EINVAL
case EINVAL:
return G_FILE_ERROR_INVAL;
break;
#endif
#ifdef EPIPE
case EPIPE:
return G_FILE_ERROR_PIPE;
break;
#endif
#ifdef EAGAIN
case EAGAIN:
return G_FILE_ERROR_AGAIN;
break;
#endif
#ifdef EINTR
case EINTR:
return G_FILE_ERROR_INTR;
break;
#endif
#ifdef EIO
case EIO:
return G_FILE_ERROR_IO;
break;
#endif
#ifdef EPERM
case EPERM:
return G_FILE_ERROR_PERM;
break;
#endif
default:
return G_FILE_ERROR_FAILED;
break;
}
}
static gboolean
get_contents_stdio (const gchar *filename,
FILE *f,
gchar **contents,
gsize *length,
GError **error)
{
gchar buf[2048];
size_t bytes;
char *str;
size_t total_bytes;
size_t total_allocated;
g_assert (f != NULL);
#define STARTING_ALLOC 64
total_bytes = 0;
total_allocated = STARTING_ALLOC;
str = g_malloc (STARTING_ALLOC);
while (!feof (f))
{
bytes = fread (buf, 1, 2048, f);
while ((total_bytes + bytes + 1) > total_allocated)
{
total_allocated *= 2;
str = g_try_realloc (str, total_allocated);
if (str == NULL)
{
g_set_error (error,
G_FILE_ERROR,
G_FILE_ERROR_NOMEM,
_("Could not allocate %lu bytes to read file \"%s\""),
(gulong) total_allocated, filename);
goto error;
}
}
if (ferror (f))
{
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (errno),
_("Error reading file '%s': %s"),
filename, g_strerror (errno));
goto error;
}
memcpy (str + total_bytes, buf, bytes);
total_bytes += bytes;
}
fclose (f);
str[total_bytes] = '\0';
if (length)
*length = total_bytes;
*contents = str;
return TRUE;
error:
g_free (str);
fclose (f);
return FALSE;
}
#ifndef G_OS_WIN32
static gboolean
get_contents_regfile (const gchar *filename,
struct stat *stat_buf,
gint fd,
gchar **contents,
gsize *length,
GError **error)
{
gchar *buf;
size_t bytes_read;
size_t size;
size_t alloc_size;
size = stat_buf->st_size;
alloc_size = size + 1;
buf = g_try_malloc (alloc_size);
if (buf == NULL)
{
g_set_error (error,
G_FILE_ERROR,
G_FILE_ERROR_NOMEM,
_("Could not allocate %lu bytes to read file \"%s\""),
(gulong) alloc_size, filename);
goto error;
}
bytes_read = 0;
while (bytes_read < size)
{
gssize rc;
rc = read (fd, buf + bytes_read, size - bytes_read);
if (rc < 0)
{
if (errno != EINTR)
{
g_free (buf);
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (errno),
_("Failed to read from file '%s': %s"),
filename, g_strerror (errno));
goto error;
}
}
else if (rc == 0)
break;
else
bytes_read += rc;
}
buf[bytes_read] = '\0';
if (length)
*length = bytes_read;
*contents = buf;
close (fd);
return TRUE;
error:
close (fd);
return FALSE;
}
static gboolean
get_contents_posix (const gchar *filename,
gchar **contents,
gsize *length,
GError **error)
{
struct stat stat_buf;
gint fd;
fd = open (filename, O_RDONLY|O_BINARY);
if (fd < 0)
{
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (errno),
_("Failed to open file '%s': %s"),
filename, g_strerror (errno));
return FALSE;
}
if (fstat (fd, &stat_buf) < 0)
{
close (fd);
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (errno),
_("Failed to get attributes of file '%s': fstat() failed: %s"),
filename, g_strerror (errno));
return FALSE;
}
if (stat_buf.st_size > 0 && S_ISREG (stat_buf.st_mode))
{
return get_contents_regfile (filename,
&stat_buf,
fd,
contents,
length,
error);
}
else
{
FILE *f;
f = fdopen (fd, "r");
if (f == NULL)
{
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (errno),
_("Failed to open file '%s': fdopen() failed: %s"),
filename, g_strerror (errno));
return FALSE;
}
return get_contents_stdio (filename, f, contents, length, error);
}
}
#else
static gboolean
get_contents_win32 (const gchar *filename,
gchar **contents,
gsize *length,
GError **error)
{
FILE *f;
f = fopen (filename, "rb");
if (f == NULL)
{
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (errno),
_("Failed to open file '%s': %s"),
filename, g_strerror (errno));
return FALSE;
}
return get_contents_stdio (filename, f, contents, length, error);
}
#endif
gboolean
g_file_get_contents (const gchar *filename,
gchar **contents,
gsize *length,
GError **error)
{
g_return_val_if_fail (filename != NULL, FALSE);
g_return_val_if_fail (contents != NULL, FALSE);
*contents = NULL;
if (length)
*length = 0;
#ifdef G_OS_WIN32
return get_contents_win32 (filename, contents, length, error);
#else
return get_contents_posix (filename, contents, length, error);
#endif
}
gint
g_mkstemp (gchar *tmpl)
{
#ifdef HAVE_MKSTEMP
return mkstemp (tmpl);
#else
int len;
char *XXXXXX;
int count, fd;
static const char letters[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
static const int NLETTERS = sizeof (letters) - 1;
glong value;
GTimeVal tv;
static int counter = 0;
len = strlen (tmpl);
if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
return -1;
XXXXXX = &tmpl[len - 6];
g_get_current_time (&tv);
value = (tv.tv_usec ^ tv.tv_sec) + counter++;
for (count = 0; count < 100; value += 7777, ++count)
{
glong v = value;
XXXXXX[0] = letters[v % NLETTERS];
v /= NLETTERS;
XXXXXX[1] = letters[v % NLETTERS];
v /= NLETTERS;
XXXXXX[2] = letters[v % NLETTERS];
v /= NLETTERS;
XXXXXX[3] = letters[v % NLETTERS];
v /= NLETTERS;
XXXXXX[4] = letters[v % NLETTERS];
v /= NLETTERS;
XXXXXX[5] = letters[v % NLETTERS];
fd = open (tmpl, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0600);
if (fd >= 0)
return fd;
else if (errno != EEXIST)
return -1;
}
return -1;
#endif
}
gint
g_file_open_tmp (const gchar *tmpl,
gchar **name_used,
GError **error)
{
int retval;
const char *tmpdir;
char *sep;
char *fulltemplate;
const char *slash;
if (tmpl == NULL)
tmpl = ".XXXXXX";
if ((slash = strchr (tmpl, G_DIR_SEPARATOR)) != NULL
#ifdef G_OS_WIN32
|| (strchr (tmpl, '/') != NULL && (slash = "/"))
#endif
)
{
char c[2];
c[0] = *slash;
c[1] = '\0';
g_set_error (error,
G_FILE_ERROR,
G_FILE_ERROR_FAILED,
_("Template '%s' invalid, should not contain a '%s'"),
tmpl, c);
return -1;
}
if (strlen (tmpl) < 6 ||
strcmp (tmpl + strlen (tmpl) - 6, "XXXXXX") != 0)
{
g_set_error (error,
G_FILE_ERROR,
G_FILE_ERROR_FAILED,
_("Template '%s' doesn't end with XXXXXX"),
tmpl);
return -1;
}
tmpdir = g_get_tmp_dir ();
if (G_IS_DIR_SEPARATOR (tmpdir [strlen (tmpdir) - 1]))
sep = "";
else
sep = G_DIR_SEPARATOR_S;
fulltemplate = g_strconcat (tmpdir, sep, tmpl, NULL);
retval = g_mkstemp (fulltemplate);
if (retval == -1)
{
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (errno),
_("Failed to create file '%s': %s"),
fulltemplate, g_strerror (errno));
g_free (fulltemplate);
return -1;
}
if (name_used)
*name_used = fulltemplate;
else
g_free (fulltemplate);
return retval;
}
static gchar *
g_build_pathv (const gchar *separator,
const gchar *first_element,
va_list args)
{
GString *result;
gint separator_len = strlen (separator);
gboolean is_first = TRUE;
gboolean have_leading = FALSE;
const gchar *single_element = NULL;
const gchar *next_element;
const gchar *last_trailing = NULL;
result = g_string_new (NULL);
next_element = first_element;
while (TRUE)
{
const gchar *element;
const gchar *start;
const gchar *end;
if (next_element)
{
element = next_element;
next_element = va_arg (args, gchar *);
}
else
break;
if (!*element)
continue;
start = element;
if (separator_len)
{
while (start &&
strncmp (start, separator, separator_len) == 0)
start += separator_len;
}
end = start + strlen (start);
if (separator_len)
{
while (end >= start + separator_len &&
strncmp (end - separator_len, separator, separator_len) == 0)
end -= separator_len;
last_trailing = end;
while (last_trailing >= element + separator_len &&
strncmp (last_trailing - separator_len, separator, separator_len) == 0)
last_trailing -= separator_len;
if (!have_leading)
{
if (last_trailing <= start)
single_element = element;
g_string_append_len (result, element, start - element);
have_leading = TRUE;
}
else
single_element = NULL;
}
if (end == start)
continue;
if (!is_first)
g_string_append (result, separator);
g_string_append_len (result, start, end - start);
is_first = FALSE;
}
if (single_element)
{
g_string_free (result, TRUE);
return g_strdup (single_element);
}
else
{
if (last_trailing)
g_string_append (result, last_trailing);
return g_string_free (result, FALSE);
}
}
gchar *
g_build_path (const gchar *separator,
const gchar *first_element,
...)
{
gchar *str;
va_list args;
g_return_val_if_fail (separator != NULL, NULL);
va_start (args, first_element);
str = g_build_pathv (separator, first_element, args);
va_end (args);
return str;
}
gchar *
g_build_filename (const gchar *first_element,
...)
{
#ifndef G_OS_WIN32
gchar *str;
va_list args;
va_start (args, first_element);
str = g_build_pathv (G_DIR_SEPARATOR_S, first_element, args);
va_end (args);
return str;
#else
va_list args;
GString *result;
gboolean is_first = TRUE;
gboolean have_leading = FALSE;
const gchar *single_element = NULL;
const gchar *next_element;
const gchar *last_trailing = NULL;
gchar current_separator = '\\';
va_start (args, first_element);
result = g_string_new (NULL);
next_element = first_element;
while (TRUE)
{
const gchar *element;
const gchar *start;
const gchar *end;
if (next_element)
{
element = next_element;
next_element = va_arg (args, gchar *);
}
else
break;
if (!*element)
continue;
start = element;
if (TRUE)
{
while (start &&
(*start == '\\' || *start == '/'))
{
current_separator = *start;
start++;
}
}
end = start + strlen (start);
if (TRUE)
{
while (end >= start + 1 &&
(end[-1] == '\\' || end[-1] == '/'))
{
current_separator = end[-1];
end--;
}
last_trailing = end;
while (last_trailing >= element + 1 &&
(last_trailing[-1] == '\\' || last_trailing[-1] == '/'))
last_trailing--;
if (!have_leading)
{
if (last_trailing <= start)
single_element = element;
g_string_append_len (result, element, start - element);
have_leading = TRUE;
}
else
single_element = NULL;
}
if (end == start)
continue;
if (!is_first)
g_string_append_len (result, ¤t_separator, 1);
g_string_append_len (result, start, end - start);
is_first = FALSE;
}
va_end (args);
if (single_element)
{
g_string_free (result, TRUE);
return g_strdup (single_element);
}
else
{
if (last_trailing)
g_string_append (result, last_trailing);
return g_string_free (result, FALSE);
}
#endif
}
gchar *
g_file_read_link (const gchar *filename,
GError **error)
{
#ifdef HAVE_READLINK
gchar *buffer;
guint size;
gint read_size;
size = 256;
buffer = g_malloc (size);
while (TRUE)
{
read_size = readlink (filename, buffer, size);
if (read_size < 0) {
g_free (buffer);
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (errno),
_("Failed to read the symbolic link '%s': %s"),
filename, g_strerror (errno));
return NULL;
}
if (read_size < size)
{
buffer[read_size] = 0;
return buffer;
}
size *= 2;
buffer = g_realloc (buffer, size);
}
#else
g_set_error (error,
G_FILE_ERROR,
G_FILE_ERROR_INVAL,
_("Symbolic links not supported"));
return NULL;
#endif
}