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);
508 * Open a temporary file
510 * The parameter is a string that should match the rules for mktemp, i.e.
511 * end in "XXXXXX". The X string will be modified to form the name
512 * of a file that didn't exist.
514 * Return value: A file handle (as from open()) to the file file
515 * opened for reading and writing. The file is opened in binary mode
516 * on platforms where there is a difference. The file handle should be
517 * closed with close(). In case of errors, -1 is returned.
519 * From the GNU C library.
520 * Copyright (C) 1991,92,93,94,95,96,97,98,99 Free Software Foundation, Inc.
523 g_mkstemp (char *tmpl)
526 return mkstemp (tmpl);
531 static const char letters[] =
532 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
537 if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
540 /* This is where the Xs start. */
541 XXXXXX = &tmpl[len - 6];
543 /* Get some more or less random data. */
544 g_get_current_time (&tv);
545 value = tv.tv_usec ^ tv.tv_sec;
547 for (count = 0; count < 100; value += 7777, ++count)
551 /* Fill in the random bits. */
552 XXXXXX[0] = letters[v % 62];
554 XXXXXX[1] = letters[v % 62];
556 XXXXXX[2] = letters[v % 62];
558 XXXXXX[3] = letters[v % 62];
560 XXXXXX[4] = letters[v % 62];
562 XXXXXX[5] = letters[v % 62];
564 fd = open (tmpl, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0600);
568 else if (errno != EEXIST)
569 /* Any other error will apply also to other names we might
570 * try, and there are 2^32 or so of them, so give up now.
575 /* We got out of the loop because we ran out of combinations to try. */