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 fd = open (filename, O_RDONLY);
387 g_file_error_from_errno (errno),
388 _("Failed to open file '%s': %s"),
389 filename, strerror (errno));
394 /* I don't think this will ever fail, aside from ENOMEM, but. */
395 if (fstat (fd, &stat_buf) < 0)
401 g_file_error_from_errno (errno),
402 _("Failed to get attributes of file '%s': fstat() failed: %s"),
403 filename, strerror (errno));
408 if (stat_buf.st_size > 0 && S_ISREG (stat_buf.st_mode))
410 return get_contents_regfile (filename,
421 f = fdopen (fd, "r");
427 g_file_error_from_errno (errno),
428 _("Failed to open file '%s': fdopen() failed: %s"),
429 filename, strerror (errno));
434 return get_contents_stdio (filename, f, contents, length, error);
438 #else /* G_OS_WIN32 */
441 get_contents_win32 (const gchar *filename,
448 /* I guess you want binary mode; maybe you want text sometimes? */
449 f = fopen (filename, "rb");
455 g_file_error_from_errno (errno),
456 _("Failed to open file '%s': %s"),
457 filename, strerror (errno));
462 return get_contents_stdio (filename, f, contents, length, error);
468 * g_file_get_contents:
469 * @filename: a file to read contents from
470 * @contents: location to store an allocated string
471 * @length: location to store length in bytes of the contents
472 * @error: return location for a #GError
474 * Reads an entire file into allocated memory, with good error
475 * checking. If @error is set, FALSE is returned, and @contents is set
476 * to NULL. If TRUE is returned, @error will not be set, and @contents
477 * will be set to the file contents. The string stored in @contents
478 * will be nul-terminated, so for text files you can pass NULL for the
479 * @length argument. The error domain is #G_FILE_ERROR. Possible
480 * error codes are those in the #GFileError enumeration.
482 * FIXME currently crashes if the file is too big to fit in memory;
483 * should probably use g_try_malloc() when we have that function.
485 * Return value: TRUE on success, FALSE if error is set
488 g_file_get_contents (const gchar *filename,
493 g_return_val_if_fail (filename != NULL, FALSE);
494 g_return_val_if_fail (contents != NULL, FALSE);
501 return get_contents_win32 (filename, contents, length, error);
503 return get_contents_posix (filename, contents, length, error);
508 * mkstemp() implementation is from the GNU C library.
509 * Copyright (C) 1991,92,93,94,95,96,97,98,99 Free Software Foundation, Inc.
513 * @tmpl: template filename
515 * Open a temporary file. See "man mkstemp" on most UNIX-like systems.
516 * This is a portability wrapper, which simply calls mkstemp() on systems
517 * that have it, and implements it in GLib otherwise.
519 * The parameter is a string that should match the rules for mktemp, i.e.
520 * end in "XXXXXX". The X string will be modified to form the name
521 * of a file that didn't exist.
523 * Return value: A file handle (as from open()) to the file
524 * opened for reading and writing. The file is opened in binary mode
525 * on platforms where there is a difference. The file handle should be
526 * closed with close(). In case of errors, -1 is returned.
529 g_mkstemp (char *tmpl)
532 return mkstemp (tmpl);
537 static const char letters[] =
538 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
539 static const int NLETTERS = sizeof (letters) - 1;
542 static int counter = 0;
545 if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
548 /* This is where the Xs start. */
549 XXXXXX = &tmpl[len - 6];
551 /* Get some more or less random data. */
552 g_get_current_time (&tv);
553 value = (tv.tv_usec ^ tv.tv_sec) + counter++;
555 for (count = 0; count < 100; value += 7777, ++count)
559 /* Fill in the random bits. */
560 XXXXXX[0] = letters[v % NLETTERS];
562 XXXXXX[1] = letters[v % NLETTERS];
564 XXXXXX[2] = letters[v % NLETTERS];
566 XXXXXX[3] = letters[v % NLETTERS];
568 XXXXXX[4] = letters[v % NLETTERS];
570 XXXXXX[5] = letters[v % NLETTERS];
572 fd = open (tmpl, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0600);
576 else if (errno != EEXIST)
577 /* Any other error will apply also to other names we might
578 * try, and there are 2^32 or so of them, so give up now.
583 /* We got out of the loop because we ran out of combinations to try. */
590 * @tmpl: Template for file name, as in g_mkstemp, basename only
591 * @name_used: location to store actual name used
592 * @error: return location for a #GError
594 * Opens a file for writing in the preferred directory for temporary
595 * files (as returned by g_get_tmp_dir()).
597 * @tmpl should be a string ending with six 'X' characters, as the
598 * parameter to g_mkstemp() (or mkstemp()). However, unlike these
599 * functions, the template should only be a basename, no directory
600 * components are allowed. If template is NULL, a default template is
603 * Note that in contrast to g_mkstemp() (and mkstemp()) @tmpl is not
604 * modified, and might thus be a read-only literal string.
606 * The actual name used is returned in @name_used if non-NULL. This
607 * string should be freed with g_free when not needed any longer.
609 * Return value: A file handle (as from open()) to the file
610 * opened for reading and writing. The file is opened in binary mode
611 * on platforms where there is a difference. The file handle should be
612 * closed with close(). In case of errors, -1 is returned and
613 * @error will be set.
616 g_file_open_tmp (const char *tmpl,
628 if (strchr (tmpl, G_DIR_SEPARATOR)
630 || strchr (tmpl, '/')
637 _("Template '%s' illegal, should not contain a '%s'"),
638 tmpl, G_DIR_SEPARATOR_S);
643 if (strlen (tmpl) < 6 ||
644 strcmp (tmpl + strlen (tmpl) - 6, "XXXXXX") != 0)
649 _("Template '%s' doesn end with XXXXXX"),
654 tmpdir = g_get_tmp_dir ();
656 if (tmpdir [strlen (tmpdir) - 1] == G_DIR_SEPARATOR)
659 sep = G_DIR_SEPARATOR_S;
661 fulltemplate = g_strconcat (tmpdir, sep, tmpl, NULL);
663 retval = g_mkstemp (fulltemplate);
669 g_file_error_from_errno (errno),
670 _("Failed to create file '%s': %s"),
671 fulltemplate, strerror (errno));
672 g_free (fulltemplate);
677 *name_used = fulltemplate;
679 g_free (fulltemplate);