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