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