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 */
63 * @filename: a filename to test
64 * @test: bitfield of #GFileTest flags
66 * Returns TRUE if any of the tests in the bitfield @test are
67 * TRUE. For example, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)
68 * will return TRUE if the file exists; the check whether it's
69 * a directory doesn't matter since the existence test is TRUE.
70 * With the current set of available tests, there's no point
71 * passing in more than one test at a time.
73 * Return value: whether a test was TRUE
76 g_file_test (const gchar *filename,
79 if (test & G_FILE_TEST_EXISTS)
80 return (access (filename, F_OK) == 0);
81 else if (test & G_FILE_TEST_IS_EXECUTABLE)
82 return (access (filename, X_OK) == 0);
87 if (stat (filename, &s) < 0)
90 if ((test & G_FILE_TEST_IS_REGULAR) &&
93 else if ((test & G_FILE_TEST_IS_DIR) &&
96 else if ((test & G_FILE_TEST_IS_SYMLINK) &&
105 g_file_error_quark (void)
109 q = g_quark_from_static_string ("g-file-error-quark");
115 g_file_error_from_errno (gint en)
121 return G_FILE_ERROR_EXIST;
127 return G_FILE_ERROR_ISDIR;
133 return G_FILE_ERROR_ACCES;
139 return G_FILE_ERROR_NAMETOOLONG;
145 return G_FILE_ERROR_NOENT;
151 return G_FILE_ERROR_NOTDIR;
157 return G_FILE_ERROR_NXIO;
163 return G_FILE_ERROR_NODEV;
169 return G_FILE_ERROR_ROFS;
175 return G_FILE_ERROR_TXTBSY;
181 return G_FILE_ERROR_FAULT;
187 return G_FILE_ERROR_LOOP;
193 return G_FILE_ERROR_NOSPC;
199 return G_FILE_ERROR_NOMEM;
205 return G_FILE_ERROR_MFILE;
211 return G_FILE_ERROR_NFILE;
217 return G_FILE_ERROR_BADF;
223 return G_FILE_ERROR_INVAL;
229 return G_FILE_ERROR_PIPE;
235 return G_FILE_ERROR_AGAIN;
241 return G_FILE_ERROR_INTR;
247 return G_FILE_ERROR_IO;
253 return G_FILE_ERROR_PERM;
258 return G_FILE_ERROR_FAILED;
264 get_contents_stdio (const gchar *filename,
274 g_assert (f != NULL);
276 str = g_string_new ("");
280 bytes = fread (buf, 1, 2048, f);
286 g_file_error_from_errno (errno),
287 _("Error reading file '%s': %s"),
288 filename, strerror (errno));
290 g_string_free (str, TRUE);
295 g_string_append_len (str, buf, bytes);
303 *contents = g_string_free (str, FALSE);
311 get_contents_regfile (const gchar *filename,
312 struct stat *stat_buf,
322 size = stat_buf->st_size;
324 buf = g_new (gchar, size + 1);
327 while (bytes_read < size)
331 rc = read (fd, buf + bytes_read, size - bytes_read);
343 g_file_error_from_errno (errno),
344 _("Failed to read from file '%s': %s"),
345 filename, strerror (errno));
356 buf[bytes_read] = '\0';
359 *length = bytes_read;
367 get_contents_posix (const gchar *filename,
372 struct stat stat_buf;
375 fd = open (filename, O_RDONLY);
381 g_file_error_from_errno (errno),
382 _("Failed to open file '%s': %s"),
383 filename, strerror (errno));
388 /* I don't think this will ever fail, aside from ENOMEM, but. */
389 if (fstat (fd, &stat_buf) < 0)
395 g_file_error_from_errno (errno),
396 _("Failed to get attributes of file '%s': fstat() failed: %s"),
397 filename, strerror (errno));
402 if (stat_buf.st_size > 0 && S_ISREG (stat_buf.st_mode))
404 return get_contents_regfile (filename,
415 f = fdopen (fd, "r");
421 g_file_error_from_errno (errno),
422 _("Failed to open file '%s': fdopen() failed: %s"),
423 filename, strerror (errno));
428 return get_contents_stdio (filename, f, contents, length, error);
432 #else /* G_OS_WIN32 */
435 get_contents_win32 (const gchar *filename,
442 /* I guess you want binary mode; maybe you want text sometimes? */
443 f = fopen (filename, "rb");
449 g_file_error_from_errno (errno),
450 _("Failed to open file '%s': %s"),
451 filename, strerror (errno));
456 return get_contents_stdio (filename, f, contents, length, error);
462 * g_file_get_contents:
463 * @filename: a file to read contents from
464 * @contents: location to store an allocated string
465 * @length: location to store length in bytes of the contents
466 * @error: return location for a #GError
468 * Reads an entire file into allocated memory, with good error
469 * checking. If @error is set, FALSE is returned, and @contents is set
470 * to NULL. If TRUE is returned, @error will not be set, and @contents
471 * will be set to the file contents. The string stored in @contents
472 * will be nul-terminated, so for text files you can pass NULL for the
473 * @length argument. The error domain is #G_FILE_ERROR. Possible
474 * error codes are those in the #GFileError enumeration.
476 * FIXME currently crashes if the file is too big to fit in memory;
477 * should probably use g_try_malloc() when we have that function.
479 * Return value: TRUE on success, FALSE if error is set
482 g_file_get_contents (const gchar *filename,
487 g_return_val_if_fail (filename != NULL, FALSE);
488 g_return_val_if_fail (contents != NULL, FALSE);
495 return get_contents_win32 (filename, contents, length, error);
497 return get_contents_posix (filename, contents, length, error);