1 /* gfileutils.c - File utility functions
3 * Copyright 2000 Red Hat, Inc.
5 * GLib is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * GLib is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with GLib; see the file COPYING.LIB. If not,
17 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
32 #include <sys/types.h>
46 #define S_ISREG(mode) ((mode)&_S_IFREG)
50 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
53 #endif /* G_OS_WIN32 */
67 * @filename: a filename to test
68 * @test: bitfield of #GFileTest flags
70 * Returns TRUE if any of the tests in the bitfield @test are
71 * TRUE. For example, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)
72 * will return TRUE if the file exists; the check whether it's
73 * a directory doesn't matter since the existence test is TRUE.
74 * With the current set of available tests, there's no point
75 * passing in more than one test at a time.
77 * Return value: whether a test was TRUE
80 g_file_test (const gchar *filename,
83 if (test & G_FILE_TEST_EXISTS)
84 return (access (filename, F_OK) == 0);
85 else if (test & G_FILE_TEST_IS_EXECUTABLE)
86 return (access (filename, X_OK) == 0);
91 if (stat (filename, &s) < 0)
94 if ((test & G_FILE_TEST_IS_REGULAR) &&
97 else if ((test & G_FILE_TEST_IS_DIR) &&
100 else if ((test & G_FILE_TEST_IS_SYMLINK) &&
109 g_file_error_quark (void)
113 q = g_quark_from_static_string ("g-file-error-quark");
119 g_file_error_from_errno (gint en)
125 return G_FILE_ERROR_EXIST;
131 return G_FILE_ERROR_ISDIR;
137 return G_FILE_ERROR_ACCES;
143 return G_FILE_ERROR_NAMETOOLONG;
149 return G_FILE_ERROR_NOENT;
155 return G_FILE_ERROR_NOTDIR;
161 return G_FILE_ERROR_NXIO;
167 return G_FILE_ERROR_NODEV;
173 return G_FILE_ERROR_ROFS;
179 return G_FILE_ERROR_TXTBSY;
185 return G_FILE_ERROR_FAULT;
191 return G_FILE_ERROR_LOOP;
197 return G_FILE_ERROR_NOSPC;
203 return G_FILE_ERROR_NOMEM;
209 return G_FILE_ERROR_MFILE;
215 return G_FILE_ERROR_NFILE;
221 return G_FILE_ERROR_BADF;
227 return G_FILE_ERROR_INVAL;
233 return G_FILE_ERROR_PIPE;
239 return G_FILE_ERROR_AGAIN;
245 return G_FILE_ERROR_INTR;
251 return G_FILE_ERROR_IO;
257 return G_FILE_ERROR_PERM;
262 return G_FILE_ERROR_FAILED;
268 get_contents_stdio (const gchar *filename,
278 g_assert (f != NULL);
280 str = g_string_new ("");
284 bytes = fread (buf, 1, 2048, f);
290 g_file_error_from_errno (errno),
291 _("Error reading file '%s': %s"),
292 filename, strerror (errno));
294 g_string_free (str, TRUE);
299 g_string_append_len (str, buf, bytes);
307 *contents = g_string_free (str, FALSE);
315 get_contents_regfile (const gchar *filename,
316 struct stat *stat_buf,
326 size = stat_buf->st_size;
328 buf = g_new (gchar, size + 1);
331 while (bytes_read < size)
335 rc = read (fd, buf + bytes_read, size - bytes_read);
347 g_file_error_from_errno (errno),
348 _("Failed to read from file '%s': %s"),
349 filename, strerror (errno));
360 buf[bytes_read] = '\0';
363 *length = bytes_read;
371 get_contents_posix (const gchar *filename,
376 struct stat stat_buf;
379 fd = open (filename, O_RDONLY);
385 g_file_error_from_errno (errno),
386 _("Failed to open file '%s': %s"),
387 filename, strerror (errno));
392 /* I don't think this will ever fail, aside from ENOMEM, but. */
393 if (fstat (fd, &stat_buf) < 0)
399 g_file_error_from_errno (errno),
400 _("Failed to get attributes of file '%s': fstat() failed: %s"),
401 filename, strerror (errno));
406 if (stat_buf.st_size > 0 && S_ISREG (stat_buf.st_mode))
408 return get_contents_regfile (filename,
419 f = fdopen (fd, "r");
425 g_file_error_from_errno (errno),
426 _("Failed to open file '%s': fdopen() failed: %s"),
427 filename, strerror (errno));
432 return get_contents_stdio (filename, f, contents, length, error);
436 #else /* G_OS_WIN32 */
439 get_contents_win32 (const gchar *filename,
446 /* I guess you want binary mode; maybe you want text sometimes? */
447 f = fopen (filename, "rb");
453 g_file_error_from_errno (errno),
454 _("Failed to open file '%s': %s"),
455 filename, strerror (errno));
460 return get_contents_stdio (filename, f, contents, length, error);
466 * g_file_get_contents:
467 * @filename: a file to read contents from
468 * @contents: location to store an allocated string
469 * @length: location to store length in bytes of the contents
470 * @error: return location for a #GError
472 * Reads an entire file into allocated memory, with good error
473 * checking. If @error is set, FALSE is returned, and @contents is set
474 * to NULL. If TRUE is returned, @error will not be set, and @contents
475 * will be set to the file contents. The string stored in @contents
476 * will be nul-terminated, so for text files you can pass NULL for the
477 * @length argument. The error domain is #G_FILE_ERROR. Possible
478 * error codes are those in the #GFileError enumeration.
480 * FIXME currently crashes if the file is too big to fit in memory;
481 * should probably use g_try_malloc() when we have that function.
483 * Return value: TRUE on success, FALSE if error is set
486 g_file_get_contents (const gchar *filename,
491 g_return_val_if_fail (filename != NULL, FALSE);
492 g_return_val_if_fail (contents != NULL, FALSE);
499 return get_contents_win32 (filename, contents, length, error);
501 return get_contents_posix (filename, contents, length, error);
506 * mkstemp() implementation is from the GNU C library.
507 * Copyright (C) 1991,92,93,94,95,96,97,98,99 Free Software Foundation, Inc.
511 * @tmpl: template filename
513 * Open a temporary file. See "man mkstemp" on most UNIX-like systems.
514 * This is a portability wrapper, which simply calls mkstemp() on systems
515 * that have it, and implements it in GLib otherwise.
517 * The parameter is a string that should match the rules for mktemp, i.e.
518 * end in "XXXXXX". The X string will be modified to form the name
519 * of a file that didn't exist.
521 * Return value: A file handle (as from open()) to the file
522 * opened for reading and writing. The file is opened in binary mode
523 * on platforms where there is a difference. The file handle should be
524 * closed with close(). In case of errors, -1 is returned.
528 g_mkstemp (char *tmpl)
531 return mkstemp (tmpl);
536 static const char letters[] =
537 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
538 static const int NLETTERS = sizeof (letters) - 1;
541 static int counter = 0;
544 if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
547 /* This is where the Xs start. */
548 XXXXXX = &tmpl[len - 6];
550 /* Get some more or less random data. */
551 g_get_current_time (&tv);
552 value = (tv.tv_usec ^ tv.tv_sec) + counter++;
554 for (count = 0; count < 100; value += 7777, ++count)
558 /* Fill in the random bits. */
559 XXXXXX[0] = letters[v % NLETTERS];
561 XXXXXX[1] = letters[v % NLETTERS];
563 XXXXXX[2] = letters[v % NLETTERS];
565 XXXXXX[3] = letters[v % NLETTERS];
567 XXXXXX[4] = letters[v % NLETTERS];
569 XXXXXX[5] = letters[v % NLETTERS];
571 fd = open (tmpl, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0600);
575 else if (errno != EEXIST)
576 /* Any other error will apply also to other names we might
577 * try, and there are 2^32 or so of them, so give up now.
582 /* We got out of the loop because we ran out of combinations to try. */
589 * @tmpl: Template for file name, as in g_mkstemp, basename only
590 * @name_used: location to store actual name used
591 * @error: return location for a #GError
593 * Opens a file for writing in the preferred directory for temporary
594 * files (as returned by g_get_tmp_dir()).
596 * @tmpl should be a string ending with six 'X' characters, as the
597 * parameter to g_mkstemp() (or mkstemp()). However, unlike these
598 * functions, the template should only be a basename, no directory
599 * components are allowed. If template is NULL, a default template is
602 * Note that in contrast to g_mkstemp() (and mkstemp()) @tmpl is not
603 * modified, and might thus be a read-only literal string.
605 * The actual name used is returned in @name_used if non-NULL. This
606 * string should be freed with g_free when not needed any longer.
608 * If some error occurs, @error is set, and -1 is returned. Otherwise,
609 * the file descriptor to a file opened for reading and writing with
610 * g_mkstemp() is returned.
613 g_file_open_tmp (const char *tmpl,
625 if (strchr (tmpl, G_DIR_SEPARATOR))
630 _("Template '%s' illegal, should not contain a '%s'"),
631 tmpl, G_DIR_SEPARATOR_S);
636 if (strlen (tmpl) < 6 ||
637 strcmp (tmpl + strlen (tmpl) - 6, "XXXXXX") != 0)
642 _("Template '%s' doesn end with XXXXXX"),
647 tmpdir = g_get_tmp_dir ();
649 if (tmpdir [strlen (tmpdir) - 1] == G_DIR_SEPARATOR)
652 sep = G_DIR_SEPARATOR_S;
654 fulltemplate = g_strconcat (tmpdir, sep, tmpl, NULL);
656 retval = g_mkstemp (fulltemplate);
662 g_file_error_from_errno (errno),
663 _("Failed to create file '%s': %s"),
664 fulltemplate, strerror (errno));
665 g_free (fulltemplate);
670 *name_used = fulltemplate;
672 g_free (fulltemplate);