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.
33 #include <sys/types.h>
48 #define S_ISREG(mode) ((mode)&_S_IFREG)
52 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
55 #endif /* G_OS_WIN32 */
69 * @filename: a filename to test
70 * @test: bitfield of #GFileTest flags
72 * Returns TRUE if any of the tests in the bitfield @test are
73 * TRUE. For example, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)
74 * will return TRUE if the file exists; the check whether it's
75 * a directory doesn't matter since the existence test is TRUE.
76 * With the current set of available tests, there's no point
77 * passing in more than one test at a time.
79 * Return value: whether a test was TRUE
82 g_file_test (const gchar *filename,
85 if (test & G_FILE_TEST_EXISTS)
86 return (access (filename, F_OK) == 0);
87 else if (test & G_FILE_TEST_IS_EXECUTABLE)
88 return (access (filename, X_OK) == 0);
93 if (stat (filename, &s) < 0)
96 if ((test & G_FILE_TEST_IS_REGULAR) &&
99 else if ((test & G_FILE_TEST_IS_DIR) &&
102 else if ((test & G_FILE_TEST_IS_SYMLINK) &&
111 g_file_error_quark (void)
115 q = g_quark_from_static_string ("g-file-error-quark");
121 g_file_error_from_errno (gint en)
127 return G_FILE_ERROR_EXIST;
133 return G_FILE_ERROR_ISDIR;
139 return G_FILE_ERROR_ACCES;
145 return G_FILE_ERROR_NAMETOOLONG;
151 return G_FILE_ERROR_NOENT;
157 return G_FILE_ERROR_NOTDIR;
163 return G_FILE_ERROR_NXIO;
169 return G_FILE_ERROR_NODEV;
175 return G_FILE_ERROR_ROFS;
181 return G_FILE_ERROR_TXTBSY;
187 return G_FILE_ERROR_FAULT;
193 return G_FILE_ERROR_LOOP;
199 return G_FILE_ERROR_NOSPC;
205 return G_FILE_ERROR_NOMEM;
211 return G_FILE_ERROR_MFILE;
217 return G_FILE_ERROR_NFILE;
223 return G_FILE_ERROR_BADF;
229 return G_FILE_ERROR_INVAL;
235 return G_FILE_ERROR_PIPE;
241 return G_FILE_ERROR_AGAIN;
247 return G_FILE_ERROR_INTR;
253 return G_FILE_ERROR_IO;
259 return G_FILE_ERROR_PERM;
264 return G_FILE_ERROR_FAILED;
270 get_contents_stdio (const gchar *filename,
280 g_assert (f != NULL);
282 str = g_string_new ("");
286 bytes = fread (buf, 1, 2048, f);
292 g_file_error_from_errno (errno),
293 _("Error reading file '%s': %s"),
294 filename, strerror (errno));
296 g_string_free (str, TRUE);
301 g_string_append_len (str, buf, bytes);
309 *contents = g_string_free (str, FALSE);
317 get_contents_regfile (const gchar *filename,
318 struct stat *stat_buf,
328 size = stat_buf->st_size;
330 buf = g_new (gchar, size + 1);
333 while (bytes_read < size)
337 rc = read (fd, buf + bytes_read, size - bytes_read);
349 g_file_error_from_errno (errno),
350 _("Failed to read from file '%s': %s"),
351 filename, strerror (errno));
362 buf[bytes_read] = '\0';
365 *length = bytes_read;
373 get_contents_posix (const gchar *filename,
378 struct stat stat_buf;
381 /* O_BINARY useful on Cygwin */
382 fd = open (filename, O_RDONLY|O_BINARY);
388 g_file_error_from_errno (errno),
389 _("Failed to open file '%s': %s"),
390 filename, strerror (errno));
395 /* I don't think this will ever fail, aside from ENOMEM, but. */
396 if (fstat (fd, &stat_buf) < 0)
402 g_file_error_from_errno (errno),
403 _("Failed to get attributes of file '%s': fstat() failed: %s"),
404 filename, strerror (errno));
409 if (stat_buf.st_size > 0 && S_ISREG (stat_buf.st_mode))
411 return get_contents_regfile (filename,
422 f = fdopen (fd, "r");
428 g_file_error_from_errno (errno),
429 _("Failed to open file '%s': fdopen() failed: %s"),
430 filename, strerror (errno));
435 return get_contents_stdio (filename, f, contents, length, error);
439 #else /* G_OS_WIN32 */
442 get_contents_win32 (const gchar *filename,
449 /* I guess you want binary mode; maybe you want text sometimes? */
450 f = fopen (filename, "rb");
456 g_file_error_from_errno (errno),
457 _("Failed to open file '%s': %s"),
458 filename, strerror (errno));
463 return get_contents_stdio (filename, f, contents, length, error);
469 * g_file_get_contents:
470 * @filename: a file to read contents from
471 * @contents: location to store an allocated string
472 * @length: location to store length in bytes of the contents
473 * @error: return location for a #GError
475 * Reads an entire file into allocated memory, with good error
476 * checking. If @error is set, FALSE is returned, and @contents is set
477 * to NULL. If TRUE is returned, @error will not be set, and @contents
478 * will be set to the file contents. The string stored in @contents
479 * will be nul-terminated, so for text files you can pass NULL for the
480 * @length argument. The error domain is #G_FILE_ERROR. Possible
481 * error codes are those in the #GFileError enumeration.
483 * FIXME currently crashes if the file is too big to fit in memory;
484 * should probably use g_try_malloc() when we have that function.
486 * Return value: TRUE on success, FALSE if error is set
489 g_file_get_contents (const gchar *filename,
494 g_return_val_if_fail (filename != NULL, FALSE);
495 g_return_val_if_fail (contents != NULL, FALSE);
502 return get_contents_win32 (filename, contents, length, error);
504 return get_contents_posix (filename, contents, length, error);
509 * mkstemp() implementation is from the GNU C library.
510 * Copyright (C) 1991,92,93,94,95,96,97,98,99 Free Software Foundation, Inc.
514 * @tmpl: template filename
516 * Open a temporary file. See "man mkstemp" on most UNIX-like systems.
517 * This is a portability wrapper, which simply calls mkstemp() on systems
518 * that have it, and implements it in GLib otherwise.
520 * The parameter is a string that should match the rules for mktemp, i.e.
521 * end in "XXXXXX". The X string will be modified to form the name
522 * of a file that didn't exist.
524 * Return value: A file handle (as from open()) to the file
525 * opened for reading and writing. The file is opened in binary mode
526 * on platforms where there is a difference. The file handle should be
527 * closed with close(). In case of errors, -1 is returned.
530 g_mkstemp (char *tmpl)
533 return mkstemp (tmpl);
538 static const char letters[] =
539 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
540 static const int NLETTERS = sizeof (letters) - 1;
543 static int counter = 0;
546 if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
549 /* This is where the Xs start. */
550 XXXXXX = &tmpl[len - 6];
552 /* Get some more or less random data. */
553 g_get_current_time (&tv);
554 value = (tv.tv_usec ^ tv.tv_sec) + counter++;
556 for (count = 0; count < 100; value += 7777, ++count)
560 /* Fill in the random bits. */
561 XXXXXX[0] = letters[v % NLETTERS];
563 XXXXXX[1] = letters[v % NLETTERS];
565 XXXXXX[2] = letters[v % NLETTERS];
567 XXXXXX[3] = letters[v % NLETTERS];
569 XXXXXX[4] = letters[v % NLETTERS];
571 XXXXXX[5] = letters[v % NLETTERS];
573 fd = open (tmpl, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0600);
577 else if (errno != EEXIST)
578 /* Any other error will apply also to other names we might
579 * try, and there are 2^32 or so of them, so give up now.
584 /* We got out of the loop because we ran out of combinations to try. */
591 * @tmpl: Template for file name, as in g_mkstemp, basename only
592 * @name_used: location to store actual name used
593 * @error: return location for a #GError
595 * Opens a file for writing in the preferred directory for temporary
596 * files (as returned by g_get_tmp_dir()).
598 * @tmpl should be a string ending with six 'X' characters, as the
599 * parameter to g_mkstemp() (or mkstemp()). However, unlike these
600 * functions, the template should only be a basename, no directory
601 * components are allowed. If template is NULL, a default template is
604 * Note that in contrast to g_mkstemp() (and mkstemp()) @tmpl is not
605 * modified, and might thus be a read-only literal string.
607 * The actual name used is returned in @name_used if non-NULL. This
608 * string should be freed with g_free when not needed any longer.
610 * Return value: A file handle (as from open()) to the file
611 * opened for reading and writing. The file is opened in binary mode
612 * on platforms where there is a difference. The file handle should be
613 * closed with close(). In case of errors, -1 is returned and
614 * @error will be set.
617 g_file_open_tmp (const char *tmpl,
629 if (strchr (tmpl, G_DIR_SEPARATOR)
631 || strchr (tmpl, '/')
638 _("Template '%s' illegal, should not contain a '%s'"),
639 tmpl, G_DIR_SEPARATOR_S);
644 if (strlen (tmpl) < 6 ||
645 strcmp (tmpl + strlen (tmpl) - 6, "XXXXXX") != 0)
650 _("Template '%s' doesn't end with XXXXXX"),
655 tmpdir = g_get_tmp_dir ();
657 if (tmpdir [strlen (tmpdir) - 1] == G_DIR_SEPARATOR)
660 sep = G_DIR_SEPARATOR_S;
662 fulltemplate = g_strconcat (tmpdir, sep, tmpl, NULL);
664 retval = g_mkstemp (fulltemplate);
670 g_file_error_from_errno (errno),
671 _("Failed to create file '%s': %s"),
672 fulltemplate, strerror (errno));
673 g_free (fulltemplate);
678 *name_used = fulltemplate;
680 g_free (fulltemplate);