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>
47 #define S_ISREG(mode) ((mode)&_S_IFREG)
51 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
54 #endif /* G_OS_WIN32 */
68 * @filename: a filename to test
69 * @test: bitfield of #GFileTest flags
71 * Returns TRUE if any of the tests in the bitfield @test are
72 * TRUE. For example, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)
73 * will return TRUE if the file exists; the check whether it's
74 * a directory doesn't matter since the existence test is TRUE.
75 * With the current set of available tests, there's no point
76 * passing in more than one test at a time.
78 * Return value: whether a test was TRUE
81 g_file_test (const gchar *filename,
84 if (test & G_FILE_TEST_EXISTS)
85 return (access (filename, F_OK) == 0);
86 else if (test & G_FILE_TEST_IS_EXECUTABLE)
87 return (access (filename, X_OK) == 0);
92 if (stat (filename, &s) < 0)
95 if ((test & G_FILE_TEST_IS_REGULAR) &&
98 else if ((test & G_FILE_TEST_IS_DIR) &&
101 else if ((test & G_FILE_TEST_IS_SYMLINK) &&
110 g_file_error_quark (void)
114 q = g_quark_from_static_string ("g-file-error-quark");
120 g_file_error_from_errno (gint en)
126 return G_FILE_ERROR_EXIST;
132 return G_FILE_ERROR_ISDIR;
138 return G_FILE_ERROR_ACCES;
144 return G_FILE_ERROR_NAMETOOLONG;
150 return G_FILE_ERROR_NOENT;
156 return G_FILE_ERROR_NOTDIR;
162 return G_FILE_ERROR_NXIO;
168 return G_FILE_ERROR_NODEV;
174 return G_FILE_ERROR_ROFS;
180 return G_FILE_ERROR_TXTBSY;
186 return G_FILE_ERROR_FAULT;
192 return G_FILE_ERROR_LOOP;
198 return G_FILE_ERROR_NOSPC;
204 return G_FILE_ERROR_NOMEM;
210 return G_FILE_ERROR_MFILE;
216 return G_FILE_ERROR_NFILE;
222 return G_FILE_ERROR_BADF;
228 return G_FILE_ERROR_INVAL;
234 return G_FILE_ERROR_PIPE;
240 return G_FILE_ERROR_AGAIN;
246 return G_FILE_ERROR_INTR;
252 return G_FILE_ERROR_IO;
258 return G_FILE_ERROR_PERM;
263 return G_FILE_ERROR_FAILED;
269 get_contents_stdio (const gchar *filename,
279 g_assert (f != NULL);
281 str = g_string_new ("");
285 bytes = fread (buf, 1, 2048, f);
291 g_file_error_from_errno (errno),
292 _("Error reading file '%s': %s"),
293 filename, strerror (errno));
295 g_string_free (str, TRUE);
300 g_string_append_len (str, buf, bytes);
308 *contents = g_string_free (str, FALSE);
316 get_contents_regfile (const gchar *filename,
317 struct stat *stat_buf,
327 size = stat_buf->st_size;
329 buf = g_new (gchar, size + 1);
332 while (bytes_read < size)
336 rc = read (fd, buf + bytes_read, size - bytes_read);
348 g_file_error_from_errno (errno),
349 _("Failed to read from file '%s': %s"),
350 filename, strerror (errno));
361 buf[bytes_read] = '\0';
364 *length = bytes_read;
372 get_contents_posix (const gchar *filename,
377 struct stat stat_buf;
380 fd = open (filename, O_RDONLY);
386 g_file_error_from_errno (errno),
387 _("Failed to open file '%s': %s"),
388 filename, strerror (errno));
393 /* I don't think this will ever fail, aside from ENOMEM, but. */
394 if (fstat (fd, &stat_buf) < 0)
400 g_file_error_from_errno (errno),
401 _("Failed to get attributes of file '%s': fstat() failed: %s"),
402 filename, strerror (errno));
407 if (stat_buf.st_size > 0 && S_ISREG (stat_buf.st_mode))
409 return get_contents_regfile (filename,
420 f = fdopen (fd, "r");
426 g_file_error_from_errno (errno),
427 _("Failed to open file '%s': fdopen() failed: %s"),
428 filename, strerror (errno));
433 return get_contents_stdio (filename, f, contents, length, error);
437 #else /* G_OS_WIN32 */
440 get_contents_win32 (const gchar *filename,
447 /* I guess you want binary mode; maybe you want text sometimes? */
448 f = fopen (filename, "rb");
454 g_file_error_from_errno (errno),
455 _("Failed to open file '%s': %s"),
456 filename, strerror (errno));
461 return get_contents_stdio (filename, f, contents, length, error);
467 * g_file_get_contents:
468 * @filename: a file to read contents from
469 * @contents: location to store an allocated string
470 * @length: location to store length in bytes of the contents
471 * @error: return location for a #GError
473 * Reads an entire file into allocated memory, with good error
474 * checking. If @error is set, FALSE is returned, and @contents is set
475 * to NULL. If TRUE is returned, @error will not be set, and @contents
476 * will be set to the file contents. The string stored in @contents
477 * will be nul-terminated, so for text files you can pass NULL for the
478 * @length argument. The error domain is #G_FILE_ERROR. Possible
479 * error codes are those in the #GFileError enumeration.
481 * FIXME currently crashes if the file is too big to fit in memory;
482 * should probably use g_try_malloc() when we have that function.
484 * Return value: TRUE on success, FALSE if error is set
487 g_file_get_contents (const gchar *filename,
492 g_return_val_if_fail (filename != NULL, FALSE);
493 g_return_val_if_fail (contents != NULL, FALSE);
500 return get_contents_win32 (filename, contents, length, error);
502 return get_contents_posix (filename, contents, length, error);
507 * mkstemp() implementation is from the GNU C library.
508 * Copyright (C) 1991,92,93,94,95,96,97,98,99 Free Software Foundation, Inc.
512 * @tmpl: template filename
514 * Open a temporary file. See "man mkstemp" on most UNIX-like systems.
515 * This is a portability wrapper, which simply calls mkstemp() on systems
516 * that have it, and implements it in GLib otherwise.
518 * The parameter is a string that should match the rules for mktemp, i.e.
519 * end in "XXXXXX". The X string will be modified to form the name
520 * of a file that didn't exist.
522 * Return value: A file handle (as from open()) to the file
523 * opened for reading and writing. The file is opened in binary mode
524 * on platforms where there is a difference. The file handle should be
525 * 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 * Return value: A file handle (as from open()) to the file
609 * opened for reading and writing. The file is opened in binary mode
610 * on platforms where there is a difference. The file handle should be
611 * closed with close(). In case of errors, -1 is returned and
612 * @error will be set.
615 g_file_open_tmp (const char *tmpl,
627 if (strchr (tmpl, G_DIR_SEPARATOR))
632 _("Template '%s' illegal, should not contain a '%s'"),
633 tmpl, G_DIR_SEPARATOR_S);
638 if (strlen (tmpl) < 6 ||
639 strcmp (tmpl + strlen (tmpl) - 6, "XXXXXX") != 0)
644 _("Template '%s' doesn end with XXXXXX"),
649 tmpdir = g_get_tmp_dir ();
651 if (tmpdir [strlen (tmpdir) - 1] == G_DIR_SEPARATOR)
654 sep = G_DIR_SEPARATOR_S;
656 fulltemplate = g_strconcat (tmpdir, sep, tmpl, NULL);
658 retval = g_mkstemp (fulltemplate);
664 g_file_error_from_errno (errno),
665 _("Failed to create file '%s': %s"),
666 fulltemplate, strerror (errno));
667 g_free (fulltemplate);
672 *name_used = fulltemplate;
674 g_free (fulltemplate);