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>
44 * @filename: a filename to test
45 * @test: bitfield of #GFileTest flags
47 * Returns TRUE if any of the tests in the bitfield @test are
48 * TRUE. For example, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)
49 * will return TRUE if the file exists; the check whether it's
50 * a directory doesn't matter since the existence test is TRUE.
51 * With the current set of available tests, there's no point
52 * passing in more than one test at a time.
54 * Return value: whether a test was TRUE
57 g_file_test (const gchar *filename,
60 if (test & G_FILE_TEST_EXISTS)
61 return (access (filename, F_OK) == 0);
62 else if (test & G_FILE_TEST_IS_EXECUTABLE)
63 return (access (filename, X_OK) == 0);
68 if (stat (filename, &s) < 0)
71 if ((test & G_FILE_TEST_IS_REGULAR) &&
74 else if ((test & G_FILE_TEST_IS_DIR) &&
77 else if ((test & G_FILE_TEST_IS_SYMLINK) &&
86 g_file_error_quark (void)
90 q = g_quark_from_static_string ("g-file-error-quark");
96 g_file_error_from_errno (gint en)
102 return G_FILE_ERROR_EXIST;
108 return G_FILE_ERROR_ISDIR;
114 return G_FILE_ERROR_ACCES;
120 return G_FILE_ERROR_NAMETOOLONG;
126 return G_FILE_ERROR_NOENT;
132 return G_FILE_ERROR_NOTDIR;
138 return G_FILE_ERROR_NXIO;
144 return G_FILE_ERROR_NODEV;
150 return G_FILE_ERROR_ROFS;
156 return G_FILE_ERROR_TXTBSY;
162 return G_FILE_ERROR_FAULT;
168 return G_FILE_ERROR_LOOP;
174 return G_FILE_ERROR_NOSPC;
180 return G_FILE_ERROR_NOMEM;
186 return G_FILE_ERROR_MFILE;
192 return G_FILE_ERROR_NFILE;
198 return G_FILE_ERROR_BADF;
204 return G_FILE_ERROR_INVAL;
210 return G_FILE_ERROR_PIPE;
216 return G_FILE_ERROR_AGAIN;
222 return G_FILE_ERROR_INTR;
228 return G_FILE_ERROR_IO;
234 return G_FILE_ERROR_PERM;
239 return G_FILE_ERROR_FAILED;
245 get_contents_stdio (const gchar *filename,
255 g_assert (f != NULL);
257 str = g_string_new ("");
261 bytes = fread (buf, 1, 2048, f);
267 g_file_error_from_errno (errno),
268 _("Error reading file '%s': %s"),
269 filename, strerror (errno));
271 g_string_free (str, TRUE);
276 g_string_append_len (str, buf, bytes);
284 *contents = g_string_free (str, FALSE);
292 get_contents_regfile (const gchar *filename,
293 struct stat *stat_buf,
303 size = stat_buf->st_size;
305 buf = g_new (gchar, size + 1);
308 while (bytes_read < size)
312 rc = read (fd, buf + bytes_read, size - bytes_read);
324 g_file_error_from_errno (errno),
325 _("Failed to read from file '%s': %s"),
326 filename, strerror (errno));
337 buf[bytes_read] = '\0';
340 *length = bytes_read;
348 get_contents_posix (const gchar *filename,
353 struct stat stat_buf;
356 fd = open (filename, O_RDONLY);
362 g_file_error_from_errno (errno),
363 _("Failed to open file '%s': %s"),
364 filename, strerror (errno));
369 /* I don't think this will ever fail, aside from ENOMEM, but. */
370 if (fstat (fd, &stat_buf) < 0)
376 g_file_error_from_errno (errno),
377 _("Failed to get attributes of file '%s': fstat() failed: %s"),
378 filename, strerror (errno));
383 if (stat_buf.st_size > 0 && S_ISREG (stat_buf.st_mode))
385 return get_contents_regfile (filename,
396 f = fdopen (fd, "r");
402 g_file_error_from_errno (errno),
403 _("Failed to open file '%s': fdopen() failed: %s"),
404 filename, strerror (errno));
409 return get_contents_stdio (filename, f, contents, length, error);
413 #else /* G_OS_WIN32 */
416 get_contents_win32 (const gchar *filename,
423 /* I guess you want binary mode; maybe you want text sometimes? */
424 f = fopen (filename, "rb");
430 g_file_error_from_errno (errno),
431 _("Failed to open file '%s': %s"),
432 filename, strerror (errno));
437 return get_contents_stdio (filename, f, contents, length, error);
443 * g_file_get_contents:
444 * @filename: a file to read contents from
445 * @contents: location to store an allocated string
446 * @length: location to store length in bytes of the contents
447 * @error: return location for a #GError
449 * Reads an entire file into allocated memory, with good error
450 * checking. If @error is set, FALSE is returned, and @contents is set
451 * to NULL. If TRUE is returned, @error will not be set, and @contents
452 * will be set to the file contents. The string stored in @contents
453 * will be nul-terminated, so for text files you can pass NULL for the
454 * @length argument. The error domain is #G_FILE_ERROR. Possible
455 * error codes are those in the #GFileError enumeration.
457 * FIXME currently crashes if the file is too big to fit in memory;
458 * should probably use g_try_malloc() when we have that function.
460 * Return value: TRUE on success, FALSE if error is set
463 g_file_get_contents (const gchar *filename,
468 g_return_val_if_fail (filename != NULL, FALSE);
469 g_return_val_if_fail (contents != NULL, FALSE);
476 return get_contents_win32 (filename, contents, length, error);
478 return get_contents_posix (filename, contents, length, error);