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.
28 #include <sys/types.h>
37 * @filename: a filename to test
38 * @test: bitfield of #GFileTest flags
40 * Returns TRUE if any of the tests in the bitfield @test are
41 * TRUE. For example, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)
42 * will return TRUE if the file exists; the check whether it's
43 * a directory doesn't matter since the existence test is TRUE.
44 * With the current set of available tests, there's no point
45 * passing in more than one test at a time.
47 * Return value: whether a test was TRUE
50 g_file_test (const gchar *filename,
53 if (test & G_FILE_TEST_EXISTS)
54 return (access (filename, F_OK) == 0);
55 else if (test & G_FILE_TEST_IS_EXECUTABLE)
56 return (access (filename, X_OK) == 0);
61 if (stat (filename, &s) < 0)
64 if ((test & G_FILE_TEST_IS_REGULAR) &&
67 else if ((test & G_FILE_TEST_IS_DIR) &&
70 else if ((test & G_FILE_TEST_IS_SYMLINK) &&
79 g_file_error_quark (void)
83 q = g_quark_from_static_string ("g-file-error-quark");
89 g_file_error_from_errno (gint en)
95 return G_FILE_ERROR_EXIST;
101 return G_FILE_ERROR_ISDIR;
107 return G_FILE_ERROR_ACCES;
113 return G_FILE_ERROR_NAMETOOLONG;
119 return G_FILE_ERROR_NOENT;
125 return G_FILE_ERROR_NOTDIR;
131 return G_FILE_ERROR_NXIO;
137 return G_FILE_ERROR_NODEV;
143 return G_FILE_ERROR_ROFS;
149 return G_FILE_ERROR_TXTBSY;
155 return G_FILE_ERROR_FAULT;
161 return G_FILE_ERROR_LOOP;
167 return G_FILE_ERROR_NOSPC;
173 return G_FILE_ERROR_NOMEM;
179 return G_FILE_ERROR_MFILE;
185 return G_FILE_ERROR_NFILE;
191 return G_FILE_ERROR_BADF;
197 return G_FILE_ERROR_INVAL;
203 return G_FILE_ERROR_PIPE;
209 return G_FILE_ERROR_AGAIN;
215 return G_FILE_ERROR_INTR;
221 return G_FILE_ERROR_IO;
227 return G_FILE_ERROR_PERM;
232 return G_FILE_ERROR_FAILED;
238 get_contents_stdio (const gchar *filename,
248 g_assert (f != NULL);
250 str = g_string_new ("");
254 bytes = fread (buf, 1, 2048, f);
260 g_file_error_from_errno (errno),
261 _("Error reading file '%s': %s"),
262 filename, strerror (errno));
264 g_string_free (str, TRUE);
269 g_string_append_len (str, buf, bytes);
277 *contents = g_string_free (str, FALSE);
283 get_contents_regfile (const gchar *filename,
284 struct stat *stat_buf,
294 size = stat_buf->st_size;
296 buf = g_new (gchar, size + 1);
299 while (bytes_read < size)
303 rc = read (fd, buf + bytes_read, size - bytes_read);
315 g_file_error_from_errno (errno),
316 _("Failed to read from file '%s': %s"),
317 filename, strerror (errno));
328 buf[bytes_read] = '\0';
331 *length = bytes_read;
339 get_contents_posix (const gchar *filename,
344 struct stat stat_buf;
347 fd = open (filename, O_RDONLY);
353 g_file_error_from_errno (errno),
354 _("Failed to open file '%s': %s"),
355 filename, strerror (errno));
360 /* I don't think this will ever fail, aside from ENOMEM, but. */
361 if (fstat (fd, &stat_buf) < 0)
367 g_file_error_from_errno (errno),
368 _("Failed to get attributes of file '%s': fstat() failed: %s"),
369 filename, strerror (errno));
374 if (stat_buf.st_size > 0 && S_ISREG (stat_buf.st_mode))
376 return get_contents_regfile (filename,
387 f = fdopen (fd, "r");
393 g_file_error_from_errno (errno),
394 _("Failed to open file '%s': fdopen() failed: %s"),
395 filename, strerror (errno));
400 return get_contents_stdio (filename, f, contents, length, error);
406 get_contents_win32 (const gchar *filename,
413 /* I guess you want binary mode; maybe you want text sometimes? */
414 f = fopen (filename, "rb");
420 g_file_error_from_errno (errno),
421 _("Failed to open file '%s': %s"),
422 filename, strerror (errno));
427 return get_contents_stdio (filename, f, contents, length, error);
432 * g_file_get_contents:
433 * @filename: a file to read contents from
434 * @contents: location to store an allocated string
435 * @length: location to store length in bytes of the contents
436 * @error: return location for a #GError
438 * Reads an entire file into allocated memory, with good error
439 * checking. If @error is set, FALSE is returned, and @contents is set
440 * to NULL. If TRUE is returned, @error will not be set, and @contents
441 * will be set to the file contents. The string stored in @contents
442 * will be nul-terminated, so for text files you can pass NULL for the
443 * @length argument. The error domain is #G_FILE_ERROR. Possible
444 * error codes are those in the #GFileError enumeration.
446 * FIXME currently crashes if the file is too big to fit in memory;
447 * should probably use g_try_malloc() when we have that function.
449 * Return value: TRUE on success, FALSE if error is set
452 g_file_get_contents (const gchar *filename,
457 g_return_val_if_fail (filename != NULL, FALSE);
458 g_return_val_if_fail (contents != NULL, FALSE);
465 return get_contents_win32 (filename, contents, length, error);
467 return get_contents_posix (filename, contents, length, error);