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 Windows renaming a file will not remove an existing file with the
276 	 * new name, so on Windows there is a race condition between the existing
277 	 * file being removed and the temporary file being renamed.
278 	 *
279 	 * - On Windows there is no way to remove a file that is open to some
280 	 * process, or mapped into memory. Thus, this function will fail if
281 	 * @filename already exists and is open.
282 	 *
283 	 * If the call was successful, it returns %TRUE. If the call was not successful,
284 	 * it returns %FALSE and sets @error. The error domain is #G_FILE_ERROR.
285 	 * Possible error codes are those in the #GFileError enumeration.
286 	 *
287 	 * Note that the name for the temporary file is constructed by appending up
288 	 * to 7 characters to @filename.
289 	 *
290 	 * Params:
291 	 *     filename = name of a file to write @contents to, in the GLib file name
292 	 *         encoding
293 	 *     contents = string to write to the file
294 	 *
295 	 * Returns: %TRUE on success, %FALSE if an error occurred
296 	 *
297 	 * Since: 2.8
298 	 *
299 	 * Throws: GException on failure.
300 	 */
301 	public static bool fileSetContents(string filename, string contents)
302 	{
303 		GError* err = null;
304 
305 		auto p = g_file_set_contents(Str.toStringz(filename), Str.toStringz(contents), cast(ptrdiff_t)contents.length, &err) != 0;
306 
307 		if (err !is null)
308 		{
309 			throw new GException( new ErrorG(err) );
310 		}
311 
312 		return p;
313 	}
314 
315 	/**
316 	 * Returns %TRUE if any of the tests in the bitfield @test are
317 	 * %TRUE. For example, `(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)`
318 	 * will return %TRUE if the file exists; the check whether it's a
319 	 * directory doesn't matter since the existence test is %TRUE. With
320 	 * the current set of available tests, there's no point passing in
321 	 * more than one test at a time.
322 	 *
323 	 * Apart from %G_FILE_TEST_IS_SYMLINK all tests follow symbolic links,
324 	 * so for a symbolic link to a regular file g_file_test() will return
325 	 * %TRUE for both %G_FILE_TEST_IS_SYMLINK and %G_FILE_TEST_IS_REGULAR.
326 	 *
327 	 * Note, that for a dangling symbolic link g_file_test() will return
328 	 * %TRUE for %G_FILE_TEST_IS_SYMLINK and %FALSE for all other flags.
329 	 *
330 	 * You should never use g_file_test() to test whether it is safe
331 	 * to perform an operation, because there is always the possibility
332 	 * of the condition changing before you actually perform the operation.
333 	 * For example, you might think you could use %G_FILE_TEST_IS_SYMLINK
334 	 * to know whether it is safe to write to a file without being
335 	 * tricked into writing into a different location. It doesn't work!
336 	 * |[<!-- language="C" -->
337 	 * // DON'T DO THIS
338 	 * if (!g_file_test (filename, G_FILE_TEST_IS_SYMLINK))
339 	 * {
340 	 * fd = g_open (filename, O_WRONLY);
341 	 * // write to fd
342 	 * }
343 	 * ]|
344 	 *
345 	 * Another thing to note is that %G_FILE_TEST_EXISTS and
346 	 * %G_FILE_TEST_IS_EXECUTABLE are implemented using the access()
347 	 * system call. This usually doesn't matter, but if your program
348 	 * is setuid or setgid it means that these tests will give you
349 	 * the answer for the real user ID and group ID, rather than the
350 	 * effective user ID and group ID.
351 	 *
352 	 * On Windows, there are no symlinks, so testing for
353 	 * %G_FILE_TEST_IS_SYMLINK will always return %FALSE. Testing for
354 	 * %G_FILE_TEST_IS_EXECUTABLE will just check that the file exists and
355 	 * its name indicates that it is executable, checking for well-known
356 	 * extensions and those listed in the `PATHEXT` environment variable.
357 	 *
358 	 * Params:
359 	 *     filename = a filename to test in the
360 	 *         GLib file name encoding
361 	 *     test = bitfield of #GFileTest flags
362 	 *
363 	 * Returns: whether a test was %TRUE
364 	 */
365 	public static bool fileTest(string filename, GFileTest test)
366 	{
367 		return g_file_test(Str.toStringz(filename), test) != 0;
368 	}
369 
370 	/**
371 	 * Create a directory if it doesn't already exist. Create intermediate
372 	 * parent directories as needed, too.
373 	 *
374 	 * Params:
375 	 *     pathname = a pathname in the GLib file name encoding
376 	 *     mode = permissions to use for newly created directories
377 	 *
378 	 * Returns: 0 if the directory already exists, or was successfully
379 	 *     created. Returns -1 if an error occurred, with errno set.
380 	 *
381 	 * Since: 2.8
382 	 */
383 	public static int mkdirWithParents(string pathname, int mode)
384 	{
385 		return g_mkdir_with_parents(Str.toStringz(pathname), mode);
386 	}
387 
388 	/**
389 	 * Creates a temporary directory. See the mkdtemp() documentation
390 	 * on most UNIX-like systems.
391 	 *
392 	 * The parameter is a string that should follow the rules for
393 	 * mkdtemp() templates, i.e. contain the string "XXXXXX".
394 	 * g_mkdtemp() is slightly more flexible than mkdtemp() in that the
395 	 * sequence does not have to occur at the very end of the template.
396 	 * The X string will be modified to form the name of a directory that
397 	 * didn't exist.
398 	 * The string should be in the GLib file name encoding. Most importantly,
399 	 * on Windows it should be in UTF-8.
400 	 *
401 	 * If you are going to be creating a temporary directory inside the
402 	 * directory returned by g_get_tmp_dir(), you might want to use
403 	 * g_dir_make_tmp() instead.
404 	 *
405 	 * Params:
406 	 *     tmpl = template directory name
407 	 *
408 	 * Returns: A pointer to @tmpl, which has been
409 	 *     modified to hold the directory name.  In case of errors, %NULL is
410 	 *     returned and %errno will be set.
411 	 *
412 	 * Since: 2.30
413 	 */
414 	public static string mkdtemp(string tmpl)
415 	{
416 		auto retStr = g_mkdtemp(Str.toStringz(tmpl));
417 
418 		scope(exit) Str.freeString(retStr);
419 		return Str.toString(retStr);
420 	}
421 
422 	/**
423 	 * Creates a temporary directory. See the mkdtemp() documentation
424 	 * on most UNIX-like systems.
425 	 *
426 	 * The parameter is a string that should follow the rules for
427 	 * mkdtemp() templates, i.e. contain the string "XXXXXX".
428 	 * g_mkdtemp_full() is slightly more flexible than mkdtemp() in that the
429 	 * sequence does not have to occur at the very end of the template
430 	 * and you can pass a @mode. The X string will be modified to form
431 	 * the name of a directory that didn't exist. The string should be
432 	 * in the GLib file name encoding. Most importantly, on Windows it
433 	 * should be in UTF-8.
434 	 *
435 	 * If you are going to be creating a temporary directory inside the
436 	 * directory returned by g_get_tmp_dir(), you might want to use
437 	 * g_dir_make_tmp() instead.
438 	 *
439 	 * Params:
440 	 *     tmpl = template directory name
441 	 *     mode = permissions to create the temporary directory with
442 	 *
443 	 * Returns: A pointer to @tmpl, which has been
444 	 *     modified to hold the directory name. In case of errors, %NULL is
445 	 *     returned, and %errno will be set.
446 	 *
447 	 * Since: 2.30
448 	 */
449 	public static string mkdtempFull(string tmpl, int mode)
450 	{
451 		auto retStr = g_mkdtemp_full(Str.toStringz(tmpl), mode);
452 
453 		scope(exit) Str.freeString(retStr);
454 		return Str.toString(retStr);
455 	}
456 
457 	/**
458 	 * Opens a temporary file. See the mkstemp() documentation
459 	 * on most UNIX-like systems.
460 	 *
461 	 * The parameter is a string that should follow the rules for
462 	 * mkstemp() templates, i.e. contain the string "XXXXXX".
463 	 * g_mkstemp() is slightly more flexible than mkstemp() in that the
464 	 * sequence does not have to occur at the very end of the template.
465 	 * The X string will be modified to form the name of a file that
466 	 * didn't exist. The string should be in the GLib file name encoding.
467 	 * Most importantly, on Windows it should be in UTF-8.
468 	 *
469 	 * Params:
470 	 *     tmpl = template filename
471 	 *
472 	 * Returns: A file handle (as from open()) to the file
473 	 *     opened for reading and writing. The file is opened in binary
474 	 *     mode on platforms where there is a difference. The file handle
475 	 *     should be closed with close(). In case of errors, -1 is
476 	 *     returned and %errno will be set.
477 	 */
478 	public static int mkstemp(string tmpl)
479 	{
480 		return g_mkstemp(Str.toStringz(tmpl));
481 	}
482 
483 	/**
484 	 * Opens a temporary file. See the mkstemp() documentation
485 	 * on most UNIX-like systems.
486 	 *
487 	 * The parameter is a string that should follow the rules for
488 	 * mkstemp() templates, i.e. contain the string "XXXXXX".
489 	 * g_mkstemp_full() is slightly more flexible than mkstemp()
490 	 * in that the sequence does not have to occur at the very end of the
491 	 * template and you can pass a @mode and additional @flags. The X
492 	 * string will be modified to form the name of a file that didn't exist.
493 	 * The string should be in the GLib file name encoding. Most importantly,
494 	 * on Windows it should be in UTF-8.
495 	 *
496 	 * Params:
497 	 *     tmpl = template filename
498 	 *     flags = flags to pass to an open() call in addition to O_EXCL
499 	 *         and O_CREAT, which are passed automatically
500 	 *     mode = permissions to create the temporary file with
501 	 *
502 	 * Returns: A file handle (as from open()) to the file
503 	 *     opened for reading and writing. The file handle should be
504 	 *     closed with close(). In case of errors, -1 is returned
505 	 *     and %errno will be set.
506 	 *
507 	 * Since: 2.22
508 	 */
509 	public static int mkstempFull(string tmpl, int flags, int mode)
510 	{
511 		return g_mkstemp_full(Str.toStringz(tmpl), flags, mode);
512 	}
513 
514 	/**
515 	 * A wrapper for the POSIX rmdir() function. The rmdir() function
516 	 * deletes a directory from the filesystem.
517 	 *
518 	 * See your C library manual for more details about how rmdir() works
519 	 * on your system.
520 	 *
521 	 * Params:
522 	 *     filename = a pathname in the GLib file name encoding
523 	 *         (UTF-8 on Windows)
524 	 *
525 	 * Returns: 0 if the directory was successfully removed, -1 if an error
526 	 *     occurred
527 	 *
528 	 * Since: 2.6
529 	 */
530 	public static int rmdir(string filename)
531 	{
532 		return g_rmdir(Str.toStringz(filename));
533 	}
534 
535 	/**
536 	 * A wrapper for the POSIX unlink() function. The unlink() function
537 	 * deletes a name from the filesystem. If this was the last link to the
538 	 * file and no processes have it opened, the diskspace occupied by the
539 	 * file is freed.
540 	 *
541 	 * See your C library manual for more details about unlink(). Note
542 	 * that on Windows, it is in general not possible to delete files that
543 	 * are open to some process, or mapped into memory.
544 	 *
545 	 * Params:
546 	 *     filename = a pathname in the GLib file name encoding
547 	 *         (UTF-8 on Windows)
548 	 *
549 	 * Returns: 0 if the name was successfully deleted, -1 if an error
550 	 *     occurred
551 	 *
552 	 * Since: 2.6
553 	 */
554 	public static int unlink(string filename)
555 	{
556 		return g_unlink(Str.toStringz(filename));
557 	}
558 }