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 * Conversion parameters: 26 * inFile = glib-File-Utilities.html 27 * outPack = glib 28 * outFile = FileUtils 29 * strct = 30 * realStrct= 31 * ctorStrct= 32 * clss = FileUtils 33 * interf = 34 * class Code: Yes 35 * interface Code: No 36 * template for: 37 * extend = 38 * implements: 39 * prefixes: 40 * - g_ 41 * omit structs: 42 * omit prefixes: 43 * - g_dir_ 44 * - g_mapped_file_ 45 * omit code: 46 * - g_file_get_contents 47 * omit signals: 48 * imports: 49 * - std.c.stdio 50 * - glib.Str 51 * - glib.ErrorG 52 * - glib.GException 53 * structWrap: 54 * module aliases: 55 * local aliases: 56 * overrides: 57 */ 58 59 module glib.FileUtils; 60 61 public import gtkc.glibtypes; 62 63 private import gtkc.glib; 64 private import glib.ConstructionException; 65 66 67 private import glib.Str; 68 private import glib.ErrorG; 69 private import glib.GException; 70 71 72 version(Tango) { 73 private import tango.stdc.stdio; 74 } else { 75 private import std.c.stdio; 76 } 77 78 79 80 /** 81 * There is a group of functions which wrap the common POSIX functions 82 * dealing with filenames (g_open(), g_rename(), g_mkdir(), g_stat(), 83 * g_unlink(), g_remove(), g_fopen(), g_freopen()). The point of these 84 * wrappers is to make it possible to handle file names with any Unicode 85 * characters in them on Windows without having to use ifdefs and the 86 * wide character API in the application code. 87 * 88 * The pathname argument should be in the GLib file name encoding. 89 * On POSIX this is the actual on-disk encoding which might correspond 90 * to the locale settings of the process (or the 91 * G_FILENAME_ENCODING environment variable), or not. 92 * 93 * On Windows the GLib file name encoding is UTF-8. Note that the 94 * Microsoft C library does not use UTF-8, but has separate APIs for 95 * current system code page and wide characters (UTF-16). The GLib 96 * wrappers call the wide character API if present (on modern Windows 97 * systems), otherwise convert to/from the system code page. 98 * 99 * Another group of functions allows to open and read directories 100 * in the GLib file name encoding. These are g_dir_open(), 101 * g_dir_read_name(), g_dir_rewind(), g_dir_close(). 102 */ 103 public class FileUtils 104 { 105 106 /** 107 * Reads an entire file into allocated memory, with good error 108 * checking. 109 * If the call was successful, it returns TRUE and sets contents to the file 110 * contents and length to the length of the file contents in bytes. The string 111 * stored in contents will be nul-terminated, so for text files you can pass 112 * NULL for the length argument. If the call was not successful, it returns 113 * FALSE and sets error. The error domain is G_FILE_ERROR. Possible error 114 * codes are those in the GFileError enumeration. In the error case, 115 * contents is set to NULL and length is set to zero. 116 * Params: 117 * filename = name of a file to read contents from, in the GLib file name encoding. [type filename] 118 * contents = location to store an allocated string, use g_free() to free 119 * the returned string. [out][array length=length][element-type guint8] 120 * Returns: TRUE on success, FALSE if an error occurred 121 * Throws: GException on failure. 122 */ 123 public static int fileGetContents(string filename, out char[] contents) 124 { 125 // gboolean g_file_get_contents (const gchar *filename, gchar **contents, gsize *length, GError **error); 126 gchar* outcontents = null; 127 size_t length; 128 GError* err = null; 129 130 auto p = g_file_get_contents(Str.toStringz(filename), &outcontents, &length, &err); 131 132 if (err !is null) 133 { 134 throw new GException( new ErrorG(err) ); 135 } 136 137 contents = outcontents[0 .. length]; 138 return p; 139 } 140 141 /** 142 */ 143 144 /** 145 * Gets a GFileError constant based on the passed-in err_no. 146 * For example, if you pass in EEXIST this function returns 147 * G_FILE_ERROR_EXIST. Unlike errno values, you can portably 148 * assume that all GFileError values will exist. 149 * Normally a GFileError value goes into a GError returned 150 * from a function that manipulates files. So you would use 151 * g_file_error_from_errno() when constructing a GError. 152 * Params: 153 * errNo = an "errno" value 154 * Returns: GFileError corresponding to the given errno 155 */ 156 public static GFileError fileErrorFromErrno(int errNo) 157 { 158 // GFileError g_file_error_from_errno (gint err_no); 159 return g_file_error_from_errno(errNo); 160 } 161 162 /** 163 * Writes all of contents to a file named filename, with good error checking. 164 * If a file called filename already exists it will be overwritten. 165 * This write is atomic in the sense that it is first written to a temporary 166 * Since 2.8 167 * Params: 168 * filename = name of a file to write contents to, in the GLib file name 169 * encoding. [type filename] 170 * contents = string to write to the file. [array length=length][element-type guint8] 171 * Returns: TRUE on success, FALSE if an error occurred 172 * Throws: GException on failure. 173 */ 174 public static int fileSetContents(string filename, string contents) 175 { 176 // gboolean g_file_set_contents (const gchar *filename, const gchar *contents, gssize length, GError **error); 177 GError* err = null; 178 179 auto p = g_file_set_contents(Str.toStringz(filename), cast(char*)contents.ptr, cast(int) contents.length, &err); 180 181 if (err !is null) 182 { 183 throw new GException( new ErrorG(err) ); 184 } 185 186 return p; 187 } 188 189 /** 190 * Returns TRUE if any of the tests in the bitfield test are 191 * TRUE. For example, (G_FILE_TEST_EXISTS | 192 * G_FILE_TEST_IS_DIR) will return TRUE if the file exists; 193 * the check whether it's a directory doesn't matter since the existence 194 * test is TRUE. With the current set of available tests, there's no point 195 * passing in more than one test at a time. 196 * Apart from G_FILE_TEST_IS_SYMLINK all tests follow symbolic links, 197 * so for a symbolic link to a regular file g_file_test() will return 198 * TRUE for both G_FILE_TEST_IS_SYMLINK and G_FILE_TEST_IS_REGULAR. 199 * Note, that for a dangling symbolic link g_file_test() will return 200 * TRUE for G_FILE_TEST_IS_SYMLINK and FALSE for all other flags. 201 * You should never use g_file_test() to test whether it is safe 202 * to perform an operation, because there is always the possibility 203 * of the condition changing before you actually perform the operation. 204 * For example, you might think you could use G_FILE_TEST_IS_SYMLINK 205 * to know whether it is safe to write to a file without being 206 * tricked into writing into a different location. It doesn't work! 207 * $(DDOC_COMMENT example) 208 * Another thing to note is that G_FILE_TEST_EXISTS and 209 * G_FILE_TEST_IS_EXECUTABLE are implemented using the access() 210 * system call. This usually doesn't matter, but if your program 211 * is setuid or setgid it means that these tests will give you 212 * the answer for the real user ID and group ID, rather than the 213 * effective user ID and group ID. 214 * On Windows, there are no symlinks, so testing for 215 * G_FILE_TEST_IS_SYMLINK will always return FALSE. Testing for 216 * G_FILE_TEST_IS_EXECUTABLE will just check that the file exists and 217 * its name indicates that it is executable, checking for well-known 218 * extensions and those listed in the PATHEXT environment variable. 219 * Params: 220 * filename = a filename to test in the GLib file name encoding 221 * test = bitfield of GFileTest flags 222 * Returns: whether a test was TRUE 223 */ 224 public static int fileTest(string filename, GFileTest test) 225 { 226 // gboolean g_file_test (const gchar *filename, GFileTest test); 227 return g_file_test(Str.toStringz(filename), test); 228 } 229 230 /** 231 * Opens a temporary file. See the mkstemp() documentation 232 * on most UNIX-like systems. 233 * The parameter is a string that should follow the rules for 234 * mkstemp() templates, i.e. contain the string "XXXXXX". 235 * g_mkstemp() is slightly more flexible than mkstemp() in that the 236 * sequence does not have to occur at the very end of the template. 237 * The X string will be modified to form the name of a file that 238 * didn't exist. The string should be in the GLib file name encoding. 239 * Most importantly, on Windows it should be in UTF-8. 240 * Params: 241 * tmpl = template filename. [type filename] 242 * Returns: A file handle (as from open()) to the file opened for reading and writing. The file is opened in binary mode on platforms where there is a difference. The file handle should be closed with close(). In case of errors, -1 is returned and errno will be set. 243 */ 244 public static int mkstemp(string tmpl) 245 { 246 // gint g_mkstemp (gchar *tmpl); 247 return g_mkstemp(Str.toStringz(tmpl)); 248 } 249 250 /** 251 * Opens a temporary file. See the mkstemp() documentation 252 * on most UNIX-like systems. 253 * The parameter is a string that should follow the rules for 254 * mkstemp() templates, i.e. contain the string "XXXXXX". 255 * g_mkstemp_full() is slightly more flexible than mkstemp() 256 * in that the sequence does not have to occur at the very end of the 257 * template and you can pass a mode and additional flags. The X 258 * string will be modified to form the name of a file that didn't exist. 259 * The string should be in the GLib file name encoding. Most importantly, 260 * on Windows it should be in UTF-8. 261 * Since 2.22 262 * Params: 263 * tmpl = template filename. [type filename] 264 * flags = flags to pass to an open() call in addition to O_EXCL 265 * and O_CREAT, which are passed automatically 266 * mode = permissions to create the temporary file with 267 * Returns: A file handle (as from open()) to the file opened for reading and writing. The file handle should be closed with close(). In case of errors, -1 is returned and errno will be set. 268 */ 269 public static int mkstempFull(string tmpl, int flags, int mode) 270 { 271 // gint g_mkstemp_full (gchar *tmpl, gint flags, gint mode); 272 return g_mkstemp_full(Str.toStringz(tmpl), flags, mode); 273 } 274 275 /** 276 * Opens a file for writing in the preferred directory for temporary 277 * files (as returned by g_get_tmp_dir()). 278 * tmpl should be a string in the GLib file name encoding containing 279 * a sequence of six 'X' characters, as the parameter to g_mkstemp(). 280 * However, unlike these functions, the template should only be a 281 * basename, no directory components are allowed. If template is 282 * NULL, a default template is used. 283 * Note that in contrast to g_mkstemp() (and mkstemp()) tmpl is not 284 * modified, and might thus be a read-only literal string. 285 * Upon success, and if name_used is non-NULL, the actual name used 286 * is returned in name_used. This string should be freed with g_free() 287 * when not needed any longer. The returned name is in the GLib file 288 * name encoding. 289 * Params: 290 * tmpl = Template for file name, as in 291 * g_mkstemp(), basename only, or NULL for a default template. [type filename][allow-none] 292 * nameUsed = location to store actual name used, 293 * or NULL. [out][type filename] 294 * Returns: A file handle (as from open()) to the file opened for reading and writing. The file is opened in binary mode on platforms where there is a difference. The file handle should be closed with close(). In case of errors, -1 is returned and error will be set. 295 * Throws: GException on failure. 296 */ 297 public static int fileOpenTmp(string tmpl, out string nameUsed) 298 { 299 // gint g_file_open_tmp (const gchar *tmpl, gchar **name_used, GError **error); 300 char* outnameUsed = null; 301 GError* err = null; 302 303 auto p = g_file_open_tmp(Str.toStringz(tmpl), &outnameUsed, &err); 304 305 if (err !is null) 306 { 307 throw new GException( new ErrorG(err) ); 308 } 309 310 nameUsed = Str.toString(outnameUsed); 311 return p; 312 } 313 314 /** 315 * Reads the contents of the symbolic link filename like the POSIX 316 * readlink() function. The returned string is in the encoding used 317 * for filenames. Use g_filename_to_utf8() to convert it to UTF-8. 318 * Since 2.4 319 * Params: 320 * filename = the symbolic link 321 * Returns: A newly-allocated string with the contents of the symbolic link, or NULL if an error occurred. 322 * Throws: GException on failure. 323 */ 324 public static string fileReadLink(string filename) 325 { 326 // gchar * g_file_read_link (const gchar *filename, GError **error); 327 GError* err = null; 328 329 auto p = g_file_read_link(Str.toStringz(filename), &err); 330 331 if (err !is null) 332 { 333 throw new GException( new ErrorG(err) ); 334 } 335 336 return Str.toString(p); 337 } 338 339 /** 340 * Create a directory if it doesn't already exist. Create intermediate 341 * parent directories as needed, too. 342 * Since 2.8 343 * Params: 344 * pathname = a pathname in the GLib file name encoding 345 * mode = permissions to use for newly created directories 346 * Returns: 0 if the directory already exists, or was successfully created. Returns -1 if an error occurred, with errno set. 347 */ 348 public static int mkdirWithParents(string pathname, int mode) 349 { 350 // gint g_mkdir_with_parents (const gchar *pathname, gint mode); 351 return g_mkdir_with_parents(Str.toStringz(pathname), mode); 352 } 353 354 /** 355 * Creates a temporary directory. See the mkdtemp() documentation 356 * on most UNIX-like systems. 357 * The parameter is a string that should follow the rules for 358 * mkdtemp() templates, i.e. contain the string "XXXXXX". 359 * g_mkdtemp() is slightly more flexible than mkdtemp() in that the 360 * sequence does not have to occur at the very end of the template 361 * and you can pass a mode and additional flags. The X string will 362 * be modified to form the name of a directory that didn't exist. 363 * The string should be in the GLib file name encoding. Most importantly, 364 * on Windows it should be in UTF-8. 365 * Since 2.30 366 * Params: 367 * tmpl = template directory name. [type filename] 368 * Returns: A pointer to tmpl, which has been modified to hold the directory name. In case of errors, NULL is returned and errno will be set. 369 */ 370 public static string mkdtemp(string tmpl) 371 { 372 // gchar * g_mkdtemp (gchar *tmpl); 373 return Str.toString(g_mkdtemp(Str.toStringz(tmpl))); 374 } 375 376 /** 377 * Creates a temporary directory. See the mkdtemp() documentation 378 * on most UNIX-like systems. 379 * The parameter is a string that should follow the rules for 380 * mkdtemp() templates, i.e. contain the string "XXXXXX". 381 * g_mkdtemp() is slightly more flexible than mkdtemp() in that the 382 * sequence does not have to occur at the very end of the template 383 * and you can pass a mode. The X string will be modified to form 384 * the name of a directory that didn't exist. The string should be 385 * in the GLib file name encoding. Most importantly, on Windows it 386 * should be in UTF-8. 387 * Since 2.30 388 * Params: 389 * tmpl = template directory name. [type filename] 390 * mode = permissions to create the temporary directory with 391 * Returns: A pointer to tmpl, which has been modified to hold the directory name. In case of errors, NULL is returned, and errno will be set. 392 */ 393 public static string mkdtempFull(string tmpl, int mode) 394 { 395 // gchar * g_mkdtemp_full (gchar *tmpl, gint mode); 396 return Str.toString(g_mkdtemp_full(Str.toStringz(tmpl), mode)); 397 } 398 399 /** 400 * A wrapper for the POSIX open() function. The open() function is 401 * used to convert a pathname into a file descriptor. 402 * On POSIX systems file descriptors are implemented by the operating 403 * system. On Windows, it's the C library that implements open() and 404 * file descriptors. The actual Win32 API for opening files is quite 405 * different, see MSDN documentation for CreateFile(). The Win32 API 406 * uses file handles, which are more randomish integers, not small 407 * integers like file descriptors. 408 * Because file descriptors are specific to the C library on Windows, 409 * the file descriptor returned by this function makes sense only to 410 * functions in the same C library. Thus if the GLib-using code uses a 411 * different C library than GLib does, the file descriptor returned by 412 * this function cannot be passed to C library functions like write() 413 * or read(). 414 * See your C library manual for more details about open(). 415 * Since 2.6 416 * Params: 417 * filename = a pathname in the GLib file name encoding (UTF-8 on Windows) 418 * flags = as in open() 419 * mode = as in open() 420 * Returns: a new file descriptor, or -1 if an error occurred. The return value can be used exactly like the return value from open(). 421 */ 422 public static int open(string filename, int flags, int mode) 423 { 424 // int g_open (const gchar *filename, int flags, int mode); 425 return g_open(Str.toStringz(filename), flags, mode); 426 } 427 428 /** 429 * A wrapper for the POSIX rename() function. The rename() function 430 * renames a file, moving it between directories if required. 431 * See your C library manual for more details about how rename() works 432 * on your system. It is not possible in general on Windows to rename 433 * a file that is open to some process. 434 * Since 2.6 435 * Params: 436 * oldfilename = a pathname in the GLib file name encoding (UTF-8 on Windows) 437 * newfilename = a pathname in the GLib file name encoding 438 * Returns: 0 if the renaming succeeded, -1 if an error occurred 439 */ 440 public static int rename(string oldfilename, string newfilename) 441 { 442 // int g_rename (const gchar *oldfilename, const gchar *newfilename); 443 return g_rename(Str.toStringz(oldfilename), Str.toStringz(newfilename)); 444 } 445 446 /** 447 * A wrapper for the POSIX mkdir() function. The mkdir() function 448 * attempts to create a directory with the given name and permissions. 449 * The mode argument is ignored on Windows. 450 * See your C library manual for more details about mkdir(). 451 * Since 2.6 452 * Params: 453 * filename = a pathname in the GLib file name encoding (UTF-8 on Windows) 454 * mode = permissions to use for the newly created directory 455 * Returns: 0 if the directory was successfully created, -1 if an error occurred 456 */ 457 public static int mkdir(string filename, int mode) 458 { 459 // int g_mkdir (const gchar *filename, int mode); 460 return g_mkdir(Str.toStringz(filename), mode); 461 } 462 463 /** 464 * A wrapper for the POSIX stat() function. The stat() function 465 * returns information about a file. On Windows the stat() function in 466 * the C library checks only the FAT-style READONLY attribute and does 467 * not look at the ACL at all. Thus on Windows the protection bits in 468 * the st_mode field are a fabrication of little use. 469 * On Windows the Microsoft C libraries have several variants of the 470 * stat struct and stat() function with names 471 * like "_stat", "_stat32", "_stat32i64" and "_stat64i32". The one 472 * used here is for 32-bit code the one with 32-bit size and time 473 * fields, specifically called "_stat32". 474 * In Microsoft's compiler, by default "struct stat" means one with 475 * 64-bit time fields while in MinGW "struct stat" is the legacy one 476 * with 32-bit fields. To hopefully clear up this messs, the gstdio.h 477 * header defines a type GStatBuf which is the appropriate struct type 478 * depending on the platform and/or compiler being used. On POSIX it 479 * is just "struct stat", but note that even on POSIX platforms, 480 * "stat" might be a macro. 481 * See your C library manual for more details about stat(). 482 * Since 2.6 483 * Params: 484 * filename = a pathname in the GLib file name encoding (UTF-8 on Windows) 485 * buf = a pointer to a stat struct, which 486 * will be filled with the file information 487 * Returns: 0 if the information was successfully retrieved, -1 if an error occurred 488 */ 489 public static int stat(string filename, GStatBuf* buf) 490 { 491 // int g_stat (const gchar *filename, GStatBuf *buf); 492 return g_stat(Str.toStringz(filename), buf); 493 } 494 495 /** 496 * A wrapper for the POSIX lstat() function. The lstat() function is 497 * like stat() except that in the case of symbolic links, it returns 498 * information about the symbolic link itself and not the file that it 499 * refers to. If the system does not support symbolic links g_lstat() 500 * is identical to g_stat(). 501 * See your C library manual for more details about lstat(). 502 * Since 2.6 503 * Params: 504 * filename = a pathname in the GLib file name encoding (UTF-8 on Windows) 505 * buf = a pointer to a stat struct, which 506 * will be filled with the file information 507 * Returns: 0 if the information was successfully retrieved, -1 if an error occurred 508 */ 509 public static int lstat(string filename, GStatBuf* buf) 510 { 511 // int g_lstat (const gchar *filename, GStatBuf *buf); 512 return g_lstat(Str.toStringz(filename), buf); 513 } 514 515 /** 516 * A wrapper for the POSIX unlink() function. The unlink() function 517 * deletes a name from the filesystem. If this was the last link to the 518 * file and no processes have it opened, the diskspace occupied by the 519 * file is freed. 520 * See your C library manual for more details about unlink(). Note 521 * that on Windows, it is in general not possible to delete files that 522 * are open to some process, or mapped into memory. 523 * Since 2.6 524 * Params: 525 * filename = a pathname in the GLib file name encoding (UTF-8 on Windows) 526 * Returns: 0 if the name was successfully deleted, -1 if an error occurred 527 */ 528 public static int unlink(string filename) 529 { 530 // int g_unlink (const gchar *filename); 531 return g_unlink(Str.toStringz(filename)); 532 } 533 534 /** 535 * A wrapper for the POSIX remove() function. The remove() function 536 * deletes a name from the filesystem. 537 * See your C library manual for more details about how remove() works 538 * on your system. On Unix, remove() removes also directories, as it 539 * calls unlink() for files and rmdir() for directories. On Windows, 540 * although remove() in the C library only works for files, this 541 * function tries first remove() and then if that fails rmdir(), and 542 * thus works for both files and directories. Note however, that on 543 * Windows, it is in general not possible to remove a file that is 544 * open to some process, or mapped into memory. 545 * If this function fails on Windows you can't infer too much from the 546 * errno value. rmdir() is tried regardless of what caused remove() to 547 * fail. Any errno value set by remove() will be overwritten by that 548 * set by rmdir(). 549 * Since 2.6 550 * Params: 551 * filename = a pathname in the GLib file name encoding (UTF-8 on Windows) 552 * Returns: 0 if the file was successfully removed, -1 if an error occurred 553 */ 554 public static int remove(string filename) 555 { 556 // int g_remove (const gchar *filename); 557 return g_remove(Str.toStringz(filename)); 558 } 559 560 /** 561 * A wrapper for the POSIX rmdir() function. The rmdir() function 562 * deletes a directory from the filesystem. 563 * See your C library manual for more details about how rmdir() works 564 * on your system. 565 * Since 2.6 566 * Params: 567 * filename = a pathname in the GLib file name encoding (UTF-8 on Windows) 568 * Returns: 0 if the directory was successfully removed, -1 if an error occurred 569 */ 570 public static int rmdir(string filename) 571 { 572 // int g_rmdir (const gchar *filename); 573 return g_rmdir(Str.toStringz(filename)); 574 } 575 576 /** 577 * A wrapper for the stdio fopen() function. The fopen() function 578 * opens a file and associates a new stream with it. 579 * Because file descriptors are specific to the C library on Windows, 580 * and a file descriptor is partof the FILE struct, the 581 * FILE pointer returned by this function makes sense 582 * only to functions in the same C library. Thus if the GLib-using 583 * code uses a different C library than GLib does, the 584 * FILE pointer returned by this function cannot be 585 * passed to C library functions like fprintf() or fread(). 586 * See your C library manual for more details about fopen(). 587 * Since 2.6 588 * Params: 589 * filename = a pathname in the GLib file name encoding (UTF-8 on Windows) 590 * mode = a string describing the mode in which the file should be 591 * opened 592 * Returns: A FILE pointer if the file was successfully opened, or NULL if an error occurred 593 */ 594 public static FILE* fopen(string filename, string mode) 595 { 596 // FILE * g_fopen (const gchar *filename, const gchar *mode); 597 return cast(FILE*)g_fopen(Str.toStringz(filename), Str.toStringz(mode)); 598 } 599 600 /** 601 * A wrapper for the POSIX freopen() function. The freopen() function 602 * opens a file and associates it with an existing stream. 603 * See your C library manual for more details about freopen(). 604 * Since 2.6 605 * Params: 606 * filename = a pathname in the GLib file name encoding (UTF-8 on Windows) 607 * mode = a string describing the mode in which the file should be 608 * opened 609 * stream = an existing stream which will be reused, or NULL. [allow-none] 610 * Returns: A FILE pointer if the file was successfully opened, or NULL if an error occurred. 611 */ 612 public static FILE* freopen(string filename, string mode, FILE* stream) 613 { 614 // FILE * g_freopen (const gchar *filename, const gchar *mode, FILE *stream); 615 return cast(FILE*)g_freopen(Str.toStringz(filename), Str.toStringz(mode), cast(void*)stream); 616 } 617 618 /** 619 * A wrapper for the POSIX chmod() function. The chmod() function is 620 * used to set the permissions of a file system object. 621 * On Windows the file protection mechanism is not at all POSIX-like, 622 * and the underlying chmod() function in the C library just sets or 623 * clears the FAT-style READONLY attribute. It does not touch any 624 * ACL. Software that needs to manage file permissions on Windows 625 * exactly should use the Win32 API. 626 * See your C library manual for more details about chmod(). 627 * Since 2.8 628 * Params: 629 * filename = a pathname in the GLib file name encoding (UTF-8 on Windows) 630 * mode = as in chmod() 631 * Returns: zero if the operation succeeded, -1 on error. 632 */ 633 public static int chmod(string filename, int mode) 634 { 635 // int g_chmod (const gchar *filename, int mode); 636 return g_chmod(Str.toStringz(filename), mode); 637 } 638 639 /** 640 * A wrapper for the POSIX access() function. This function is used to 641 * test a pathname for one or several of read, write or execute 642 * permissions, or just existence. 643 * On Windows, the file protection mechanism is not at all POSIX-like, 644 * and the underlying function in the C library only checks the 645 * FAT-style READONLY attribute, and does not look at the ACL of a 646 * file at all. This function is this in practise almost useless on 647 * Windows. Software that needs to handle file permissions on Windows 648 * more exactly should use the Win32 API. 649 * See your C library manual for more details about access(). 650 * Since 2.8 651 * Params: 652 * filename = a pathname in the GLib file name encoding (UTF-8 on Windows) 653 * mode = as in access() 654 * Returns: zero if the pathname refers to an existing file system object that has all the tested permissions, or -1 otherwise or on error. 655 */ 656 public static int access(string filename, int mode) 657 { 658 // int g_access (const gchar *filename, int mode); 659 return g_access(Str.toStringz(filename), mode); 660 } 661 662 /** 663 * A wrapper for the POSIX creat() function. The creat() function is 664 * used to convert a pathname into a file descriptor, creating a file 665 * if necessary. 666 * On POSIX systems file descriptors are implemented by the operating 667 * system. On Windows, it's the C library that implements creat() and 668 * file descriptors. The actual Windows API for opening files is 669 * different, see MSDN documentation for CreateFile(). The Win32 API 670 * uses file handles, which are more randomish integers, not small 671 * integers like file descriptors. 672 * Because file descriptors are specific to the C library on Windows, 673 * the file descriptor returned by this function makes sense only to 674 * functions in the same C library. Thus if the GLib-using code uses a 675 * different C library than GLib does, the file descriptor returned by 676 * this function cannot be passed to C library functions like write() 677 * or read(). 678 * See your C library manual for more details about creat(). 679 * Since 2.8 680 * Params: 681 * filename = a pathname in the GLib file name encoding (UTF-8 on Windows) 682 * mode = as in creat() 683 * Returns: a new file descriptor, or -1 if an error occurred. The return value can be used exactly like the return value from creat(). 684 */ 685 public static int creat(string filename, int mode) 686 { 687 // int g_creat (const gchar *filename, int mode); 688 return g_creat(Str.toStringz(filename), mode); 689 } 690 691 /** 692 * A wrapper for the POSIX chdir() function. The function changes the 693 * current directory of the process to path. 694 * See your C library manual for more details about chdir(). 695 * Since 2.8 696 * Params: 697 * path = a pathname in the GLib file name encoding (UTF-8 on Windows) 698 * Returns: 0 on success, -1 if an error occurred. 699 */ 700 public static int chdir(string path) 701 { 702 // int g_chdir (const gchar *path); 703 return g_chdir(Str.toStringz(path)); 704 } 705 706 /** 707 * A wrapper for the POSIX utime() function. The utime() function 708 * sets the access and modification timestamps of a file. 709 * See your C library manual for more details about how utime() works 710 * on your system. 711 * Since 2.18 712 * Params: 713 * filename = a pathname in the GLib file name encoding (UTF-8 on Windows) 714 * utb = a pointer to a struct utimbuf. 715 * Returns: 0 if the operation was successful, -1 if an error occurred 716 */ 717 public static int utime(string filename, void* utb) 718 { 719 // int g_utime (const gchar *filename, struct utimbuf *utb); 720 return g_utime(Str.toStringz(filename), utb); 721 } 722 723 /** 724 * This wraps the close() call; in case of error, errno will be 725 * preserved, but the error will also be stored as a GError in error. 726 * Besides using GError, there is another major reason to prefer this 727 * function over the call provided by the system; on Unix, it will 728 * attempt to correctly handle EINTR, which has platform-specific 729 * semantics. 730 * Since 2.36 731 * Params: 732 * fd = A file descriptor 733 * Throws: GException on failure. 734 */ 735 public static int close(int fd) 736 { 737 // gboolean g_close (gint fd, GError **error); 738 GError* err = null; 739 740 auto p = g_close(fd, &err); 741 742 if (err !is null) 743 { 744 throw new GException( new ErrorG(err) ); 745 } 746 747 return p; 748 } 749 }