1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD 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
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module glib.FileUtils;
26 
27 private import glib.ErrorG;
28 private import glib.GException;
29 private import glib.Str;
30 private import glib.c.functions;
31 public  import glib.c.types;
32 
33 
34 /** */
35 public struct FileUtils
36 {
37 
38 	/**
39 	 * A wrapper for the POSIX access() function. This function is used to
40 	 * test a pathname for one or several of read, write or execute
41 	 * permissions, or just existence.
42 	 *
43 	 * On Windows, the file protection mechanism is not at all POSIX-like,
44 	 * and the underlying function in the C library only checks the
45 	 * FAT-style READONLY attribute, and does not look at the ACL of a
46 	 * file at all. This function is this in practise almost useless on
47 	 * Windows. Software that needs to handle file permissions on Windows
48 	 * more exactly should use the Win32 API.
49 	 *
50 	 * See your C library manual for more details about access().
51 	 *
52 	 * Params:
53 	 *     filename = a pathname in the GLib file name encoding
54 	 *         (UTF-8 on Windows)
55 	 *     mode = as in access()
56 	 *
57 	 * Returns: zero if the pathname refers to an existing file system
58 	 *     object that has all the tested permissions, or -1 otherwise
59 	 *     or on error.
60 	 *
61 	 * Since: 2.8
62 	 */
63 	public static int access(string filename, int mode)
64 	{
65 		return g_access(Str.toStringz(filename), mode);
66 	}
67 
68 	/**
69 	 * A wrapper for the POSIX chdir() function. The function changes the
70 	 * current directory of the process to @path.
71 	 *
72 	 * See your C library manual for more details about chdir().
73 	 *
74 	 * Params:
75 	 *     path = a pathname in the GLib file name encoding
76 	 *         (UTF-8 on Windows)
77 	 *
78 	 * Returns: 0 on success, -1 if an error occurred.
79 	 *
80 	 * Since: 2.8
81 	 */
82 	public static int chdir(string path)
83 	{
84 		return g_chdir(Str.toStringz(path));
85 	}
86 
87 	/**
88 	 * This wraps the close() call; in case of error, %errno will be
89 	 * preserved, but the error will also be stored as a #GError in @error.
90 	 *
91 	 * Besides using #GError, there is another major reason to prefer this
92 	 * function over the call provided by the system; on Unix, it will
93 	 * attempt to correctly handle %EINTR, which has platform-specific
94 	 * semantics.
95 	 *
96 	 * Params:
97 	 *     fd = A file descriptor
98 	 *
99 	 * Returns: %TRUE on success, %FALSE if there was an error.
100 	 *
101 	 * Since: 2.36
102 	 *
103 	 * Throws: GException on failure.
104 	 */
105 	public static bool close(int fd)
106 	{
107 		GError* err = null;
108 
109 		auto __p = g_close(fd, &err) != 0;
110 
111 		if (err !is null)
112 		{
113 			throw new GException( new ErrorG(err) );
114 		}
115 
116 		return __p;
117 	}
118 
119 	/**
120 	 * Gets a #GFileError constant based on the passed-in @err_no.
121 	 * For example, if you pass in `EEXIST` this function returns
122 	 * #G_FILE_ERROR_EXIST. Unlike `errno` values, you can portably
123 	 * assume that all #GFileError values will exist.
124 	 *
125 	 * Normally a #GFileError value goes into a #GError returned
126 	 * from a function that manipulates files. So you would use
127 	 * g_file_error_from_errno() when constructing a #GError.
128 	 *
129 	 * Params:
130 	 *     errNo = an "errno" value
131 	 *
132 	 * Returns: #GFileError corresponding to the given @errno
133 	 */
134 	public static GFileError fileErrorFromErrno(int errNo)
135 	{
136 		return g_file_error_from_errno(errNo);
137 	}
138 
139 	/** */
140 	public static GQuark fileErrorQuark()
141 	{
142 		return g_file_error_quark();
143 	}
144 
145 	/**
146 	 * Reads an entire file into allocated memory, with good error
147 	 * checking.
148 	 *
149 	 * If the call was successful, it returns %TRUE and sets @contents to the file
150 	 * contents and @length to the length of the file contents in bytes. The string
151 	 * stored in @contents will be nul-terminated, so for text files you can pass
152 	 * %NULL for the @length argument. If the call was not successful, it returns
153 	 * %FALSE and sets @error. The error domain is #G_FILE_ERROR. Possible error
154 	 * codes are those in the #GFileError enumeration. In the error case,
155 	 * @contents is set to %NULL and @length is set to zero.
156 	 *
157 	 * Params:
158 	 *     filename = name of a file to read contents from, in the GLib file name encoding
159 	 *     contents = location to store an allocated string, use g_free() to free
160 	 *         the returned string
161 	 *
162 	 * Returns: %TRUE on success, %FALSE if an error occurred
163 	 *
164 	 * Throws: GException on failure.
165 	 */
166 	public static bool fileGetContents(string filename, out string contents)
167 	{
168 		char* outcontents = null;
169 		size_t length;
170 		GError* err = null;
171 
172 		auto __p = g_file_get_contents(Str.toStringz(filename), &outcontents, &length, &err) != 0;
173 
174 		if (err !is null)
175 		{
176 			throw new GException( new ErrorG(err) );
177 		}
178 
179 		contents = Str.toString(outcontents, length);
180 
181 		return __p;
182 	}
183 
184 	/**
185 	 * Opens a file for writing in the preferred directory for temporary
186 	 * files (as returned by g_get_tmp_dir()).
187 	 *
188 	 * @tmpl should be a string in the GLib file name encoding containing
189 	 * a sequence of six 'X' characters, as the parameter to g_mkstemp().
190 	 * However, unlike these functions, the template should only be a
191 	 * basename, no directory components are allowed. If template is
192 	 * %NULL, a default template is used.
193 	 *
194 	 * Note that in contrast to g_mkstemp() (and mkstemp()) @tmpl is not
195 	 * modified, and might thus be a read-only literal string.
196 	 *
197 	 * Upon success, and if @name_used is non-%NULL, the actual name used
198 	 * is returned in @name_used. This string should be freed with g_free()
199 	 * when not needed any longer. The returned name is in the GLib file
200 	 * name encoding.
201 	 *
202 	 * Params:
203 	 *     tmpl = Template for file name, as in
204 	 *         g_mkstemp(), basename only, or %NULL for a default template
205 	 *     nameUsed = location to store actual name used,
206 	 *         or %NULL
207 	 *
208 	 * Returns: A file handle (as from open()) to the file opened for
209 	 *     reading and writing. The file is opened in binary mode on platforms
210 	 *     where there is a difference. The file handle should be closed with
211 	 *     close(). In case of errors, -1 is returned and @error will be set.
212 	 *
213 	 * Throws: GException on failure.
214 	 */
215 	public static int fileOpenTmp(string tmpl, out string nameUsed)
216 	{
217 		char* outnameUsed = null;
218 		GError* err = null;
219 
220 		auto __p = g_file_open_tmp(Str.toStringz(tmpl), &outnameUsed, &err);
221 
222 		if (err !is null)
223 		{
224 			throw new GException( new ErrorG(err) );
225 		}
226 
227 		nameUsed = Str.toString(outnameUsed);
228 
229 		return __p;
230 	}
231 
232 	/**
233 	 * Reads the contents of the symbolic link @filename like the POSIX
234 	 * readlink() function.  The returned string is in the encoding used
235 	 * for filenames. Use g_filename_to_utf8() to convert it to UTF-8.
236 	 *
237 	 * Params:
238 	 *     filename = the symbolic link
239 	 *
240 	 * Returns: A newly-allocated string with the contents of
241 	 *     the symbolic link, or %NULL if an error occurred.
242 	 *
243 	 * Since: 2.4
244 	 *
245 	 * Throws: GException on failure.
246 	 */
247 	public static string fileReadLink(string filename)
248 	{
249 		GError* err = null;
250 
251 		auto retStr = g_file_read_link(Str.toStringz(filename), &err);
252 
253 		if (err !is null)
254 		{
255 			throw new GException( new ErrorG(err) );
256 		}
257 
258 		scope(exit) Str.freeString(retStr);
259 		return Str.toString(retStr);
260 	}
261 
262 	/**
263 	 * Writes all of @contents to a file named @filename. This is a convenience
264 	 * wrapper around calling g_file_set_contents_full() with `flags` set to
265 	 * `G_FILE_SET_CONTENTS_CONSISTENT | G_FILE_SET_CONTENTS_ONLY_EXISTING` and
266 	 * `mode` set to `0666`.
267 	 *
268 	 * Params:
269 	 *     filename = name of a file to write @contents to, in the GLib file name
270 	 *         encoding
271 	 *     contents = string to write to the file
272 	 *
273 	 * Returns: %TRUE on success, %FALSE if an error occurred
274 	 *
275 	 * Since: 2.8
276 	 *
277 	 * Throws: GException on failure.
278 	 */
279 	public static bool fileSetContents(string filename, string contents)
280 	{
281 		GError* err = null;
282 
283 		auto __p = g_file_set_contents(Str.toStringz(filename), Str.toStringz(contents), cast(ptrdiff_t)contents.length, &err) != 0;
284 
285 		if (err !is null)
286 		{
287 			throw new GException( new ErrorG(err) );
288 		}
289 
290 		return __p;
291 	}
292 
293 	/**
294 	 * Returns %TRUE if any of the tests in the bitfield @test are
295 	 * %TRUE. For example, `(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)`
296 	 * will return %TRUE if the file exists; the check whether it's a
297 	 * directory doesn't matter since the existence test is %TRUE. With
298 	 * the current set of available tests, there's no point passing in
299 	 * more than one test at a time.
300 	 *
301 	 * Apart from %G_FILE_TEST_IS_SYMLINK all tests follow symbolic links,
302 	 * so for a symbolic link to a regular file g_file_test() will return
303 	 * %TRUE for both %G_FILE_TEST_IS_SYMLINK and %G_FILE_TEST_IS_REGULAR.
304 	 *
305 	 * Note, that for a dangling symbolic link g_file_test() will return
306 	 * %TRUE for %G_FILE_TEST_IS_SYMLINK and %FALSE for all other flags.
307 	 *
308 	 * You should never use g_file_test() to test whether it is safe
309 	 * to perform an operation, because there is always the possibility
310 	 * of the condition changing before you actually perform the operation.
311 	 * For example, you might think you could use %G_FILE_TEST_IS_SYMLINK
312 	 * to know whether it is safe to write to a file without being
313 	 * tricked into writing into a different location. It doesn't work!
314 	 * |[<!-- language="C" -->
315 	 * // DON'T DO THIS
316 	 * if (!g_file_test (filename, G_FILE_TEST_IS_SYMLINK))
317 	 * {
318 	 * fd = g_open (filename, O_WRONLY);
319 	 * // write to fd
320 	 * }
321 	 * ]|
322 	 *
323 	 * Another thing to note is that %G_FILE_TEST_EXISTS and
324 	 * %G_FILE_TEST_IS_EXECUTABLE are implemented using the access()
325 	 * system call. This usually doesn't matter, but if your program
326 	 * is setuid or setgid it means that these tests will give you
327 	 * the answer for the real user ID and group ID, rather than the
328 	 * effective user ID and group ID.
329 	 *
330 	 * On Windows, there are no symlinks, so testing for
331 	 * %G_FILE_TEST_IS_SYMLINK will always return %FALSE. Testing for
332 	 * %G_FILE_TEST_IS_EXECUTABLE will just check that the file exists and
333 	 * its name indicates that it is executable, checking for well-known
334 	 * extensions and those listed in the `PATHEXT` environment variable.
335 	 *
336 	 * Params:
337 	 *     filename = a filename to test in the
338 	 *         GLib file name encoding
339 	 *     test = bitfield of #GFileTest flags
340 	 *
341 	 * Returns: whether a test was %TRUE
342 	 */
343 	public static bool fileTest(string filename, GFileTest test)
344 	{
345 		return g_file_test(Str.toStringz(filename), test) != 0;
346 	}
347 
348 	/**
349 	 * Create a directory if it doesn't already exist. Create intermediate
350 	 * parent directories as needed, too.
351 	 *
352 	 * Params:
353 	 *     pathname = a pathname in the GLib file name encoding
354 	 *     mode = permissions to use for newly created directories
355 	 *
356 	 * Returns: 0 if the directory already exists, or was successfully
357 	 *     created. Returns -1 if an error occurred, with errno set.
358 	 *
359 	 * Since: 2.8
360 	 */
361 	public static int mkdirWithParents(string pathname, int mode)
362 	{
363 		return g_mkdir_with_parents(Str.toStringz(pathname), mode);
364 	}
365 
366 	/**
367 	 * Creates a temporary directory. See the mkdtemp() documentation
368 	 * on most UNIX-like systems.
369 	 *
370 	 * The parameter is a string that should follow the rules for
371 	 * mkdtemp() templates, i.e. contain the string "XXXXXX".
372 	 * g_mkdtemp() is slightly more flexible than mkdtemp() in that the
373 	 * sequence does not have to occur at the very end of the template.
374 	 * The X string will be modified to form the name of a directory that
375 	 * didn't exist.
376 	 * The string should be in the GLib file name encoding. Most importantly,
377 	 * on Windows it should be in UTF-8.
378 	 *
379 	 * If you are going to be creating a temporary directory inside the
380 	 * directory returned by g_get_tmp_dir(), you might want to use
381 	 * g_dir_make_tmp() instead.
382 	 *
383 	 * Params:
384 	 *     tmpl = template directory name
385 	 *
386 	 * Returns: A pointer to @tmpl, which has been
387 	 *     modified to hold the directory name.  In case of errors, %NULL is
388 	 *     returned and %errno will be set.
389 	 *
390 	 * Since: 2.30
391 	 */
392 	public static string mkdtemp(string tmpl)
393 	{
394 		auto retStr = g_mkdtemp(Str.toStringz(tmpl));
395 
396 		scope(exit) Str.freeString(retStr);
397 		return Str.toString(retStr);
398 	}
399 
400 	/**
401 	 * Creates a temporary directory. See the mkdtemp() documentation
402 	 * on most UNIX-like systems.
403 	 *
404 	 * The parameter is a string that should follow the rules for
405 	 * mkdtemp() templates, i.e. contain the string "XXXXXX".
406 	 * g_mkdtemp_full() is slightly more flexible than mkdtemp() in that the
407 	 * sequence does not have to occur at the very end of the template
408 	 * and you can pass a @mode. The X string will be modified to form
409 	 * the name of a directory that didn't exist. The string should be
410 	 * in the GLib file name encoding. Most importantly, on Windows it
411 	 * should be in UTF-8.
412 	 *
413 	 * If you are going to be creating a temporary directory inside the
414 	 * directory returned by g_get_tmp_dir(), you might want to use
415 	 * g_dir_make_tmp() instead.
416 	 *
417 	 * Params:
418 	 *     tmpl = template directory name
419 	 *     mode = permissions to create the temporary directory with
420 	 *
421 	 * Returns: A pointer to @tmpl, which has been
422 	 *     modified to hold the directory name. In case of errors, %NULL is
423 	 *     returned, and %errno will be set.
424 	 *
425 	 * Since: 2.30
426 	 */
427 	public static string mkdtempFull(string tmpl, int mode)
428 	{
429 		auto retStr = g_mkdtemp_full(Str.toStringz(tmpl), mode);
430 
431 		scope(exit) Str.freeString(retStr);
432 		return Str.toString(retStr);
433 	}
434 
435 	/**
436 	 * Opens a temporary file. See the mkstemp() documentation
437 	 * on most UNIX-like systems.
438 	 *
439 	 * The parameter is a string that should follow the rules for
440 	 * mkstemp() templates, i.e. contain the string "XXXXXX".
441 	 * g_mkstemp() is slightly more flexible than mkstemp() in that the
442 	 * sequence does not have to occur at the very end of the template.
443 	 * The X string will be modified to form the name of a file that
444 	 * didn't exist. The string should be in the GLib file name encoding.
445 	 * Most importantly, on Windows it should be in UTF-8.
446 	 *
447 	 * Params:
448 	 *     tmpl = template filename
449 	 *
450 	 * Returns: A file handle (as from open()) to the file
451 	 *     opened for reading and writing. The file is opened in binary
452 	 *     mode on platforms where there is a difference. The file handle
453 	 *     should be closed with close(). In case of errors, -1 is
454 	 *     returned and %errno will be set.
455 	 */
456 	public static int mkstemp(string tmpl)
457 	{
458 		return g_mkstemp(Str.toStringz(tmpl));
459 	}
460 
461 	/**
462 	 * Opens a temporary file. See the mkstemp() documentation
463 	 * on most UNIX-like systems.
464 	 *
465 	 * The parameter is a string that should follow the rules for
466 	 * mkstemp() templates, i.e. contain the string "XXXXXX".
467 	 * g_mkstemp_full() is slightly more flexible than mkstemp()
468 	 * in that the sequence does not have to occur at the very end of the
469 	 * template and you can pass a @mode and additional @flags. The X
470 	 * string will be modified to form the name of a file that didn't exist.
471 	 * The string should be in the GLib file name encoding. Most importantly,
472 	 * on Windows it should be in UTF-8.
473 	 *
474 	 * Params:
475 	 *     tmpl = template filename
476 	 *     flags = flags to pass to an open() call in addition to O_EXCL
477 	 *         and O_CREAT, which are passed automatically
478 	 *     mode = permissions to create the temporary file with
479 	 *
480 	 * Returns: A file handle (as from open()) to the file
481 	 *     opened for reading and writing. The file handle should be
482 	 *     closed with close(). In case of errors, -1 is returned
483 	 *     and %errno will be set.
484 	 *
485 	 * Since: 2.22
486 	 */
487 	public static int mkstempFull(string tmpl, int flags, int mode)
488 	{
489 		return g_mkstemp_full(Str.toStringz(tmpl), flags, mode);
490 	}
491 
492 	/**
493 	 * A wrapper for the POSIX rmdir() function. The rmdir() function
494 	 * deletes a directory from the filesystem.
495 	 *
496 	 * See your C library manual for more details about how rmdir() works
497 	 * on your system.
498 	 *
499 	 * Params:
500 	 *     filename = a pathname in the GLib file name encoding
501 	 *         (UTF-8 on Windows)
502 	 *
503 	 * Returns: 0 if the directory was successfully removed, -1 if an error
504 	 *     occurred
505 	 *
506 	 * Since: 2.6
507 	 */
508 	public static int rmdir(string filename)
509 	{
510 		return g_rmdir(Str.toStringz(filename));
511 	}
512 
513 	/**
514 	 * A wrapper for the POSIX unlink() function. The unlink() function
515 	 * deletes a name from the filesystem. If this was the last link to the
516 	 * file and no processes have it opened, the diskspace occupied by the
517 	 * file is freed.
518 	 *
519 	 * See your C library manual for more details about unlink(). Note
520 	 * that on Windows, it is in general not possible to delete files that
521 	 * are open to some process, or mapped into memory.
522 	 *
523 	 * Params:
524 	 *     filename = a pathname in the GLib file name encoding
525 	 *         (UTF-8 on Windows)
526 	 *
527 	 * Returns: 0 if the name was successfully deleted, -1 if an error
528 	 *     occurred
529 	 *
530 	 * Since: 2.6
531 	 */
532 	public static int unlink(string filename)
533 	{
534 		return g_unlink(Str.toStringz(filename));
535 	}
536 }