2 * File utility functions.
5 * Gonzalo Paniagua Javier (gonzalo@novell.com)
7 * (C) 2006 Novell, Inc.
9 * Permission is hereby granted, free of charge, to any person obtaining
10 * a copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 #include <sys/types.h>
40 #define S_ISREG(x) ((x & _S_IFMT) == _S_IFREG)
41 #define S_ISDIR(x) ((x & _S_IFMT) == _S_IFDIR)
49 g_file_error_from_errno (gint err_no)
53 return G_FILE_ERROR_EXIST;
55 return G_FILE_ERROR_ISDIR;
57 return G_FILE_ERROR_ACCES;
59 return G_FILE_ERROR_NAMETOOLONG;
61 return G_FILE_ERROR_NOENT;
63 return G_FILE_ERROR_NOTDIR;
65 return G_FILE_ERROR_NXIO;
67 return G_FILE_ERROR_NODEV;
69 return G_FILE_ERROR_ROFS;
72 return G_FILE_ERROR_TXTBSY;
75 return G_FILE_ERROR_FAULT;
78 return G_FILE_ERROR_LOOP;
81 return G_FILE_ERROR_NOSPC;
83 return G_FILE_ERROR_NOMEM;
85 return G_FILE_ERROR_MFILE;
87 return G_FILE_ERROR_NFILE;
89 return G_FILE_ERROR_BADF;
91 return G_FILE_ERROR_INVAL;
93 return G_FILE_ERROR_PIPE;
95 return G_FILE_ERROR_AGAIN;
97 return G_FILE_ERROR_INTR;
99 return G_FILE_ERROR_IO;
101 return G_FILE_ERROR_PERM;
103 return G_FILE_ERROR_NOSYS;
105 return G_FILE_ERROR_FAILED;
110 #define OPEN_FLAGS (O_RDONLY)
112 #define OPEN_FLAGS (O_RDONLY | O_LARGEFILE)
115 g_file_get_contents (const gchar *filename, gchar **contents, gsize *length, GError **error)
123 g_return_val_if_fail (filename != NULL, FALSE);
124 g_return_val_if_fail (contents != NULL, FALSE);
125 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
131 fd = open (filename, OPEN_FLAGS);
135 *error = g_error_new (G_LOG_DOMAIN, g_file_error_from_errno (err), "Error opening file");
140 if (fstat (fd, &st) != 0) {
143 *error = g_error_new (G_LOG_DOMAIN, g_file_error_from_errno (err), "Error in fstat()");
149 str = g_malloc (st.st_size + 1);
152 nread = read (fd, str + offset, st.st_size - offset);
156 } while ((nread > 0 && offset < st.st_size) || (nread == -1 && errno == EINTR));
159 str [st.st_size] = '\0';
161 *length = st.st_size;
168 int mkstemp (char *tmp_template)
171 gunichar2* utf16_template;
173 utf16_template = u8to16 (tmp_template);
176 utf16_template = _wmktemp( utf16_template);
177 if (utf16_template && *utf16_template) {
178 /* FIXME: _O_TEMPORARY causes file to disappear on close causing a test to fail */
179 fd = _wopen( utf16_template, _O_BINARY | _O_CREAT /*| _O_TEMPORARY*/ | _O_EXCL, _S_IREAD | _S_IWRITE);
182 sprintf (tmp_template + strlen (tmp_template) - 6, "%S", utf16_template + wcslen (utf16_template) - 6);
184 g_free (utf16_template);
190 g_file_open_tmp (const gchar *tmpl, gchar **name_used, GError **error)
192 const static gchar *default_tmpl = ".XXXXXX";
197 g_return_val_if_fail (error == NULL || *error == NULL, -1);
202 if (strchr (tmpl, G_DIR_SEPARATOR) != NULL) {
204 *error = g_error_new (G_LOG_DOMAIN, 24, "Template should not have any " G_DIR_SEPARATOR_S);
210 if (len < 6 || strcmp (tmpl + len - 6, "XXXXXX")) {
212 *error = g_error_new (G_LOG_DOMAIN, 24, "Template should end with XXXXXX");
217 t = g_build_filename (g_get_tmp_dir (), tmpl, NULL);
224 *error = g_error_new (G_LOG_DOMAIN, g_file_error_from_errno (err), "Error in mkstemp()");
239 #pragma warning(disable:4701)
243 g_file_test (const gchar *filename, GFileTest test)
248 gunichar2* utf16_filename = NULL;
250 if (filename == NULL || test == 0)
253 utf16_filename = u8to16 (filename);
254 ret = _wstati64 (utf16_filename, &stat);
255 g_free (utf16_filename);
257 if ((test & G_FILE_TEST_EXISTS) != 0) {
265 if ((test & G_FILE_TEST_IS_EXECUTABLE) != 0) {
266 if (stat.st_mode & _S_IEXEC)
270 if ((test & G_FILE_TEST_IS_REGULAR) != 0) {
271 if (stat.st_mode & _S_IFREG)
275 if ((test & G_FILE_TEST_IS_DIR) != 0) {
276 if (stat.st_mode & _S_IFDIR)
280 /* make this last in case it is OR'd with something else */
281 if ((test & G_FILE_TEST_IS_SYMLINK) != 0) {
290 if (filename == NULL || test == 0)
295 if ((test & G_FILE_TEST_EXISTS) != 0) {
296 if (access (filename, F_OK) == 0)
300 if ((test & G_FILE_TEST_IS_EXECUTABLE) != 0) {
301 if (access (filename, X_OK) == 0)
304 if ((test & G_FILE_TEST_IS_SYMLINK) != 0) {
305 have_stat = (lstat (filename, &st) == 0);
306 if (have_stat && S_ISLNK (st.st_mode))
310 if ((test & G_FILE_TEST_IS_REGULAR) != 0) {
312 have_stat = (stat (filename, &st) == 0);
313 if (have_stat && S_ISREG (st.st_mode))
316 if ((test & G_FILE_TEST_IS_DIR) != 0) {
318 have_stat = (stat (filename, &st) == 0);
319 if (have_stat && S_ISDIR (st.st_mode))