1 /* gstdio.c - wrappers for C library functions
3 * Copyright 2004 Tor Lillqvist
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library 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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "glibconfig.h"
22 #define G_STDIO_NO_WRAP_ON_UNIX
24 #include <sys/types.h>
38 #include <sys/utime.h>
45 #include "gstdioprivate.h"
47 #if !defined (G_OS_UNIX) && !defined (G_OS_WIN32)
48 #error Please port this to your operating system
51 #if defined (_MSC_VER) && !defined(_WIN64)
53 #define _wstat _wstat32
56 #if defined (G_OS_WIN32)
58 /* We can't include Windows DDK and Windows SDK simultaneously,
59 * so let's copy this here from MinGW-w64 DDK.
60 * The structure is ultimately documented here:
61 * https://msdn.microsoft.com/en-us/library/ff552012(v=vs.85).aspx
63 typedef struct _REPARSE_DATA_BUFFER
66 USHORT ReparseDataLength
;
72 USHORT SubstituteNameOffset
;
73 USHORT SubstituteNameLength
;
74 USHORT PrintNameOffset
;
75 USHORT PrintNameLength
;
78 } SymbolicLinkReparseBuffer
;
81 USHORT SubstituteNameOffset
;
82 USHORT SubstituteNameLength
;
83 USHORT PrintNameOffset
;
84 USHORT PrintNameLength
;
86 } MountPointReparseBuffer
;
90 } GenericReparseBuffer
;
92 } REPARSE_DATA_BUFFER
, *PREPARSE_DATA_BUFFER
;
95 w32_error_to_errno (DWORD error_code
)
99 case ERROR_ACCESS_DENIED
:
102 case ERROR_INVALID_HANDLE
:
105 case ERROR_INVALID_FUNCTION
:
108 case ERROR_FILE_NOT_FOUND
:
111 case ERROR_PATH_NOT_FOUND
:
112 return ENOENT
; /* or ELOOP, or ENAMETOOLONG */
114 case ERROR_NOT_ENOUGH_MEMORY
:
115 case ERROR_OUTOFMEMORY
:
125 _g_win32_stat_utf16_no_trailing_slashes (const gunichar2
*filename
,
127 GWin32PrivateStat
*buf
,
128 gboolean for_symlink
)
131 gboolean succeeded_so_far
;
133 struct __stat64 statbuf
;
134 BY_HANDLE_FILE_INFORMATION handle_info
;
135 FILE_STANDARD_INFO std_info
;
136 WIN32_FIND_DATAW finddata
;
137 DWORD immediate_attributes
;
138 gboolean is_symlink
= FALSE
;
139 gboolean is_directory
;
141 wchar_t *filename_target
= NULL
;
146 immediate_attributes
= GetFileAttributesW (filename
);
148 if (immediate_attributes
== INVALID_FILE_ATTRIBUTES
)
150 error_code
= GetLastError ();
151 errno
= w32_error_to_errno (error_code
);
156 is_symlink
= (immediate_attributes
& FILE_ATTRIBUTE_REPARSE_POINT
) == FILE_ATTRIBUTE_REPARSE_POINT
;
157 is_directory
= (immediate_attributes
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
;
159 open_flags
= FILE_ATTRIBUTE_NORMAL
;
161 if (for_symlink
&& is_symlink
)
162 open_flags
|= FILE_FLAG_OPEN_REPARSE_POINT
;
165 open_flags
|= FILE_FLAG_BACKUP_SEMANTICS
;
167 file_handle
= CreateFileW (filename
, FILE_READ_ATTRIBUTES
,
168 FILE_SHARE_READ
, NULL
, OPEN_EXISTING
,
172 if (file_handle
== INVALID_HANDLE_VALUE
)
174 error_code
= GetLastError ();
175 errno
= w32_error_to_errno (error_code
);
181 file_handle
= (HANDLE
) _get_osfhandle (fd
);
183 if (file_handle
== INVALID_HANDLE_VALUE
)
187 succeeded_so_far
= GetFileInformationByHandle (file_handle
,
189 error_code
= GetLastError ();
191 if (succeeded_so_far
)
193 succeeded_so_far
= GetFileInformationByHandleEx (file_handle
,
197 error_code
= GetLastError ();
200 if (!succeeded_so_far
)
203 CloseHandle (file_handle
);
204 errno
= w32_error_to_errno (error_code
);
208 /* It's tempting to use GetFileInformationByHandleEx(FileAttributeTagInfo),
209 * but it always reports that the ReparseTag is 0.
213 memset (&finddata
, 0, sizeof (finddata
));
215 if (handle_info
.dwFileAttributes
& FILE_ATTRIBUTE_REPARSE_POINT
)
217 HANDLE tmp
= FindFirstFileW (filename
,
220 if (tmp
== INVALID_HANDLE_VALUE
)
222 error_code
= GetLastError ();
223 errno
= w32_error_to_errno (error_code
);
224 CloseHandle (file_handle
);
231 if (is_symlink
&& !for_symlink
)
233 /* If filename is a symlink, _wstat64 obtains information about
234 * the symlink (except that st_size will be 0).
235 * To get information about the target we need to resolve
236 * the symlink first. And we need _wstat64() to get st_dev,
237 * it's a bother to try finding it ourselves.
239 DWORD filename_target_len
;
242 /* Just in case, give it a real memory location instead of NULL */
243 new_len
= GetFinalPathNameByHandleW (file_handle
,
244 (wchar_t *) &filename_target_len
,
246 FILE_NAME_NORMALIZED
);
248 #define SANE_LIMIT 1024 * 10
249 if (new_len
>= SANE_LIMIT
)
253 error_code
= ERROR_BUFFER_OVERFLOW
;
255 else if (new_len
== 0)
257 error_code
= GetLastError ();
262 const wchar_t *extended_prefix
= L
"\\\\?\\";
263 const gsize extended_prefix_len
= wcslen (extended_prefix
);
264 const gsize extended_prefix_len_bytes
= sizeof (wchar_t) * extended_prefix_len
;
266 /* Pretend that new_len doesn't count the terminating NUL char,
267 * and ask for a bit more space than is needed.
269 filename_target_len
= new_len
+ 5;
270 filename_target
= g_malloc (filename_target_len
* sizeof (wchar_t));
272 new_len
= GetFinalPathNameByHandleW (file_handle
,
275 FILE_NAME_NORMALIZED
);
277 /* filename_target_len is already larger than needed,
278 * new_len should be smaller than that, even if the size
279 * is off by 1 for some reason.
281 if (new_len
>= filename_target_len
- 1)
284 error_code
= ERROR_BUFFER_OVERFLOW
;
285 g_clear_pointer (&filename_target
, g_free
);
287 /* GetFinalPathNameByHandle() is documented to return extended paths,
288 * strip the extended prefix.
290 else if (new_len
> extended_prefix_len
&&
291 memcmp (filename_target
, extended_prefix
, extended_prefix_len_bytes
) == 0)
293 new_len
-= extended_prefix_len
;
294 memmove (filename_target
,
295 filename_target
+ extended_prefix_len
,
296 (new_len
+ 1) * sizeof (wchar_t));
301 succeeded_so_far
= FALSE
;
304 CloseHandle (file_handle
);
306 /* else if fd >= 0 the file_handle was obtained via _get_osfhandle()
307 * and must not be closed, it is owned by fd.
310 if (!succeeded_so_far
)
312 errno
= w32_error_to_errno (error_code
);
317 result
= _wstat64 (filename_target
!= NULL
? filename_target
: filename
, &statbuf
);
319 result
= _fstat64 (fd
, &statbuf
);
325 g_free (filename_target
);
331 g_free (filename_target
);
333 buf
->st_dev
= statbuf
.st_dev
;
334 buf
->st_mode
= statbuf
.st_mode
;
335 buf
->volume_serial
= handle_info
.dwVolumeSerialNumber
;
336 buf
->file_index
= (((guint64
) handle_info
.nFileIndexHigh
) << 32) | handle_info
.nFileIndexLow
;
337 /* Note that immediate_attributes is for the symlink
338 * (if it's a symlink), while handle_info contains info
339 * about the symlink or the target, depending on the flags
342 buf
->attributes
= handle_info
.dwFileAttributes
;
343 buf
->st_nlink
= handle_info
.nNumberOfLinks
;
344 buf
->st_size
= (((guint64
) handle_info
.nFileSizeHigh
) << 32) | handle_info
.nFileSizeLow
;
345 buf
->allocated_size
= std_info
.AllocationSize
.QuadPart
;
347 if (fd
< 0 && buf
->attributes
& FILE_ATTRIBUTE_REPARSE_POINT
)
348 buf
->reparse_tag
= finddata
.dwReserved0
;
350 buf
->reparse_tag
= 0;
352 buf
->st_ctime
= statbuf
.st_ctime
;
353 buf
->st_atime
= statbuf
.st_atime
;
354 buf
->st_mtime
= statbuf
.st_mtime
;
360 _g_win32_stat_utf8 (const gchar
*filename
,
361 GWin32PrivateStat
*buf
,
362 gboolean for_symlink
)
368 if (filename
== NULL
)
374 len
= strlen (filename
);
376 while (len
> 0 && G_IS_DIR_SEPARATOR (filename
[len
- 1]))
380 (g_path_is_absolute (filename
) && len
<= g_path_skip_root (filename
) - filename
))
381 len
= strlen (filename
);
383 wfilename
= g_utf8_to_utf16 (filename
, len
, NULL
, NULL
, NULL
);
385 if (wfilename
== NULL
)
391 result
= _g_win32_stat_utf16_no_trailing_slashes (wfilename
, -1, buf
, for_symlink
);
399 g_win32_stat_utf8 (const gchar
*filename
,
400 GWin32PrivateStat
*buf
)
402 return _g_win32_stat_utf8 (filename
, buf
, FALSE
);
406 g_win32_lstat_utf8 (const gchar
*filename
,
407 GWin32PrivateStat
*buf
)
409 return _g_win32_stat_utf8 (filename
, buf
, TRUE
);
413 g_win32_fstat (int fd
,
414 GWin32PrivateStat
*buf
)
416 return _g_win32_stat_utf16_no_trailing_slashes (NULL
, fd
, buf
, FALSE
);
420 _g_win32_readlink_utf16_raw (const gunichar2
*filename
,
424 DWORD returned_bytes
;
425 BYTE returned_data
[MAXIMUM_REPARSE_DATA_BUFFER_SIZE
]; /* This is 16k, by the way */
428 REPARSE_DATA_BUFFER
*rep_buf
;
432 if (buf_size
> G_MAXSIZE
/ sizeof (wchar_t))
434 /* "buf_size * sizeof (wchar_t)" overflows */
439 if ((attributes
= GetFileAttributesW (filename
)) == 0)
441 error_code
= GetLastError ();
442 errno
= w32_error_to_errno (error_code
);
446 if ((attributes
& FILE_ATTRIBUTE_REPARSE_POINT
) == 0)
452 /* To read symlink target we need to open the file as a reparse
453 * point and use DeviceIoControl() on it.
455 h
= CreateFileW (filename
,
456 FILE_READ_ATTRIBUTES
| SYNCHRONIZE
| GENERIC_READ
,
457 FILE_SHARE_READ
, NULL
, OPEN_EXISTING
,
458 FILE_ATTRIBUTE_NORMAL
459 | FILE_FLAG_OPEN_REPARSE_POINT
460 | (attributes
& FILE_ATTRIBUTE_DIRECTORY
? FILE_FLAG_BACKUP_SEMANTICS
: 0),
463 if (h
== INVALID_HANDLE_VALUE
)
465 error_code
= GetLastError ();
466 errno
= w32_error_to_errno (error_code
);
470 if (!DeviceIoControl (h
, FSCTL_GET_REPARSE_POINT
, NULL
, 0,
471 returned_data
, MAXIMUM_REPARSE_DATA_BUFFER_SIZE
,
472 &returned_bytes
, NULL
))
474 error_code
= GetLastError ();
475 errno
= w32_error_to_errno (error_code
);
480 rep_buf
= (REPARSE_DATA_BUFFER
*) returned_data
;
483 if (rep_buf
->ReparseTag
== IO_REPARSE_TAG_SYMLINK
)
485 to_copy
= rep_buf
->SymbolicLinkReparseBuffer
.SubstituteNameLength
;
487 if (to_copy
> buf_size
* sizeof (wchar_t))
488 to_copy
= buf_size
* sizeof (wchar_t);
491 &((BYTE
*) rep_buf
->SymbolicLinkReparseBuffer
.PathBuffer
)[rep_buf
->SymbolicLinkReparseBuffer
.SubstituteNameOffset
],
494 else if (rep_buf
->ReparseTag
== IO_REPARSE_TAG_MOUNT_POINT
)
496 to_copy
= rep_buf
->MountPointReparseBuffer
.SubstituteNameLength
;
498 if (to_copy
> buf_size
* sizeof (wchar_t))
499 to_copy
= buf_size
* sizeof (wchar_t);
502 &((BYTE
*) rep_buf
->MountPointReparseBuffer
.PathBuffer
)[rep_buf
->MountPointReparseBuffer
.SubstituteNameOffset
],
512 _g_win32_readlink_utf16 (const gunichar2
*filename
,
516 const wchar_t *ntobjm_prefix
= L
"\\??\\";
517 const gsize ntobjm_prefix_len_unichar2
= wcslen (ntobjm_prefix
);
518 const gsize ntobjm_prefix_len_bytes
= sizeof (gunichar2
) * ntobjm_prefix_len_unichar2
;
519 int result
= _g_win32_readlink_utf16_raw (filename
, buf
, buf_size
);
524 /* Ensure that output is a multiple of sizeof (gunichar2),
525 * cutting any trailing partial gunichar2, if present.
527 result
-= result
% sizeof (gunichar2
);
532 /* DeviceIoControl () tends to return filenames as NT Object Manager
533 * names , i.e. "\\??\\C:\\foo\\bar".
534 * Remove the leading 4-byte \??\ prefix, as glib (as well as many W32 API
535 * functions) is unprepared to deal with it.
537 if (result
> ntobjm_prefix_len_bytes
&&
538 memcmp (buf
, ntobjm_prefix
, ntobjm_prefix_len_bytes
) == 0)
540 result
-= ntobjm_prefix_len_bytes
;
541 memmove (buf
, buf
+ ntobjm_prefix_len_unichar2
, result
);
548 _g_win32_get_mode_alias (const gchar
*mode
)
552 alias
= g_strdup (mode
);
553 if (strlen (mode
) > 2 && mode
[2] == '+')
555 /* Windows implementation of fopen() does not accept modes such as
556 * "wb+". The 'b' needs to be appended to "w+", i.e. "w+b". Note
557 * that otherwise these 2 modes are supposed to be aliases, hence
568 g_win32_readlink_utf8 (const gchar
*filename
,
575 wfilename
= g_utf8_to_utf16 (filename
, -1, NULL
, NULL
, NULL
);
577 if (wfilename
== NULL
)
583 result
= _g_win32_readlink_utf16 (wfilename
, (gunichar2
*) buf
, buf_size
);
590 gchar
*tmp
= g_utf16_to_utf8 ((const gunichar2
*) buf
,
591 result
/ sizeof (gunichar2
),
602 if (tmp_len
> buf_size
- 1)
603 tmp_len
= buf_size
- 1;
605 memcpy (buf
, tmp
, tmp_len
);
606 /* readlink() doesn't NUL-terminate, but we do.
607 * To be compliant, however, we return the
608 * number of bytes without the NUL-terminator.
622 * @filename: (type filename): a pathname in the GLib file name encoding
624 * @mode: as in access()
626 * A wrapper for the POSIX access() function. This function is used to
627 * test a pathname for one or several of read, write or execute
628 * permissions, or just existence.
630 * On Windows, the file protection mechanism is not at all POSIX-like,
631 * and the underlying function in the C library only checks the
632 * FAT-style READONLY attribute, and does not look at the ACL of a
633 * file at all. This function is this in practise almost useless on
634 * Windows. Software that needs to handle file permissions on Windows
635 * more exactly should use the Win32 API.
637 * See your C library manual for more details about access().
639 * Returns: zero if the pathname refers to an existing file system
640 * object that has all the tested permissions, or -1 otherwise
646 g_access (const gchar
*filename
,
650 wchar_t *wfilename
= g_utf8_to_utf16 (filename
, -1, NULL
, NULL
, NULL
);
654 if (wfilename
== NULL
)
664 retval
= _waccess (wfilename
, mode
& ~X_OK
);
672 return access (filename
, mode
);
678 * @filename: (type filename): a pathname in the GLib file name encoding
680 * @mode: as in chmod()
682 * A wrapper for the POSIX chmod() function. The chmod() function is
683 * used to set the permissions of a file system object.
685 * On Windows the file protection mechanism is not at all POSIX-like,
686 * and the underlying chmod() function in the C library just sets or
687 * clears the FAT-style READONLY attribute. It does not touch any
688 * ACL. Software that needs to manage file permissions on Windows
689 * exactly should use the Win32 API.
691 * See your C library manual for more details about chmod().
693 * Returns: 0 if the operation succeeded, -1 on error
698 g_chmod (const gchar
*filename
,
702 wchar_t *wfilename
= g_utf8_to_utf16 (filename
, -1, NULL
, NULL
, NULL
);
706 if (wfilename
== NULL
)
712 retval
= _wchmod (wfilename
, mode
);
720 return chmod (filename
, mode
);
725 * @filename: (type filename): a pathname in the GLib file name encoding
727 * @flags: as in open()
728 * @mode: as in open()
730 * A wrapper for the POSIX open() function. The open() function is
731 * used to convert a pathname into a file descriptor.
733 * On POSIX systems file descriptors are implemented by the operating
734 * system. On Windows, it's the C library that implements open() and
735 * file descriptors. The actual Win32 API for opening files is quite
736 * different, see MSDN documentation for CreateFile(). The Win32 API
737 * uses file handles, which are more randomish integers, not small
738 * integers like file descriptors.
740 * Because file descriptors are specific to the C library on Windows,
741 * the file descriptor returned by this function makes sense only to
742 * functions in the same C library. Thus if the GLib-using code uses a
743 * different C library than GLib does, the file descriptor returned by
744 * this function cannot be passed to C library functions like write()
747 * See your C library manual for more details about open().
749 * Returns: a new file descriptor, or -1 if an error occurred.
750 * The return value can be used exactly like the return value
756 g_open (const gchar
*filename
,
761 wchar_t *wfilename
= g_utf8_to_utf16 (filename
, -1, NULL
, NULL
, NULL
);
765 if (wfilename
== NULL
)
771 retval
= _wopen (wfilename
, flags
, mode
);
781 fd
= open (filename
, flags
, mode
);
782 while (G_UNLIKELY (fd
== -1 && errno
== EINTR
));
789 * @filename: (type filename): a pathname in the GLib file name encoding
791 * @mode: as in creat()
793 * A wrapper for the POSIX creat() function. The creat() function is
794 * used to convert a pathname into a file descriptor, creating a file
797 * On POSIX systems file descriptors are implemented by the operating
798 * system. On Windows, it's the C library that implements creat() and
799 * file descriptors. The actual Windows API for opening files is
800 * different, see MSDN documentation for CreateFile(). The Win32 API
801 * uses file handles, which are more randomish integers, not small
802 * integers like file descriptors.
804 * Because file descriptors are specific to the C library on Windows,
805 * the file descriptor returned by this function makes sense only to
806 * functions in the same C library. Thus if the GLib-using code uses a
807 * different C library than GLib does, the file descriptor returned by
808 * this function cannot be passed to C library functions like write()
811 * See your C library manual for more details about creat().
813 * Returns: a new file descriptor, or -1 if an error occurred.
814 * The return value can be used exactly like the return value
820 g_creat (const gchar
*filename
,
824 wchar_t *wfilename
= g_utf8_to_utf16 (filename
, -1, NULL
, NULL
, NULL
);
828 if (wfilename
== NULL
)
834 retval
= _wcreat (wfilename
, mode
);
842 return creat (filename
, mode
);
848 * @oldfilename: (type filename): a pathname in the GLib file name encoding
850 * @newfilename: (type filename): a pathname in the GLib file name encoding
852 * A wrapper for the POSIX rename() function. The rename() function
853 * renames a file, moving it between directories if required.
855 * See your C library manual for more details about how rename() works
856 * on your system. It is not possible in general on Windows to rename
857 * a file that is open to some process.
859 * Returns: 0 if the renaming succeeded, -1 if an error occurred
864 g_rename (const gchar
*oldfilename
,
865 const gchar
*newfilename
)
868 wchar_t *woldfilename
= g_utf8_to_utf16 (oldfilename
, -1, NULL
, NULL
, NULL
);
869 wchar_t *wnewfilename
;
873 if (woldfilename
== NULL
)
879 wnewfilename
= g_utf8_to_utf16 (newfilename
, -1, NULL
, NULL
, NULL
);
881 if (wnewfilename
== NULL
)
883 g_free (woldfilename
);
888 if (MoveFileExW (woldfilename
, wnewfilename
, MOVEFILE_REPLACE_EXISTING
))
893 switch (GetLastError ())
895 #define CASE(a,b) case ERROR_##a: save_errno = b; break
896 CASE (FILE_NOT_FOUND
, ENOENT
);
897 CASE (PATH_NOT_FOUND
, ENOENT
);
898 CASE (ACCESS_DENIED
, EACCES
);
899 CASE (NOT_SAME_DEVICE
, EXDEV
);
900 CASE (LOCK_VIOLATION
, EACCES
);
901 CASE (SHARING_VIOLATION
, EACCES
);
902 CASE (FILE_EXISTS
, EEXIST
);
903 CASE (ALREADY_EXISTS
, EEXIST
);
905 default: save_errno
= EIO
;
909 g_free (woldfilename
);
910 g_free (wnewfilename
);
915 return rename (oldfilename
, newfilename
);
921 * @filename: (type filename): a pathname in the GLib file name encoding
923 * @mode: permissions to use for the newly created directory
925 * A wrapper for the POSIX mkdir() function. The mkdir() function
926 * attempts to create a directory with the given name and permissions.
927 * The mode argument is ignored on Windows.
929 * See your C library manual for more details about mkdir().
931 * Returns: 0 if the directory was successfully created, -1 if an error
937 g_mkdir (const gchar
*filename
,
941 wchar_t *wfilename
= g_utf8_to_utf16 (filename
, -1, NULL
, NULL
, NULL
);
945 if (wfilename
== NULL
)
951 retval
= _wmkdir (wfilename
);
959 return mkdir (filename
, mode
);
965 * @path: (type filename): a pathname in the GLib file name encoding
968 * A wrapper for the POSIX chdir() function. The function changes the
969 * current directory of the process to @path.
971 * See your C library manual for more details about chdir().
973 * Returns: 0 on success, -1 if an error occurred.
978 g_chdir (const gchar
*path
)
981 wchar_t *wpath
= g_utf8_to_utf16 (path
, -1, NULL
, NULL
, NULL
);
991 retval
= _wchdir (wpath
);
1006 * A type corresponding to the appropriate struct type for the stat()
1007 * system call, depending on the platform and/or compiler being used.
1009 * See g_stat() for more information.
1013 * @filename: (type filename): a pathname in the GLib file name encoding
1014 * (UTF-8 on Windows)
1015 * @buf: a pointer to a stat struct, which will be filled with the file
1018 * A wrapper for the POSIX stat() function. The stat() function
1019 * returns information about a file. On Windows the stat() function in
1020 * the C library checks only the FAT-style READONLY attribute and does
1021 * not look at the ACL at all. Thus on Windows the protection bits in
1022 * the @st_mode field are a fabrication of little use.
1024 * On Windows the Microsoft C libraries have several variants of the
1025 * stat struct and stat() function with names like _stat(), _stat32(),
1026 * _stat32i64() and _stat64i32(). The one used here is for 32-bit code
1027 * the one with 32-bit size and time fields, specifically called _stat32().
1029 * In Microsoft's compiler, by default struct stat means one with
1030 * 64-bit time fields while in MinGW struct stat is the legacy one
1031 * with 32-bit fields. To hopefully clear up this messs, the gstdio.h
1032 * header defines a type #GStatBuf which is the appropriate struct type
1033 * depending on the platform and/or compiler being used. On POSIX it
1034 * is just struct stat, but note that even on POSIX platforms, stat()
1037 * See your C library manual for more details about stat().
1039 * Returns: 0 if the information was successfully retrieved,
1040 * -1 if an error occurred
1045 g_stat (const gchar
*filename
,
1049 GWin32PrivateStat w32_buf
;
1050 int retval
= g_win32_stat_utf8 (filename
, &w32_buf
);
1052 buf
->st_dev
= w32_buf
.st_dev
;
1053 buf
->st_ino
= w32_buf
.st_ino
;
1054 buf
->st_mode
= w32_buf
.st_mode
;
1055 buf
->st_nlink
= w32_buf
.st_nlink
;
1056 buf
->st_uid
= w32_buf
.st_uid
;
1057 buf
->st_gid
= w32_buf
.st_gid
;
1058 buf
->st_rdev
= w32_buf
.st_dev
;
1059 buf
->st_size
= w32_buf
.st_size
;
1060 buf
->st_atime
= w32_buf
.st_atime
;
1061 buf
->st_mtime
= w32_buf
.st_mtime
;
1062 buf
->st_ctime
= w32_buf
.st_ctime
;
1066 return stat (filename
, buf
);
1072 * @filename: (type filename): a pathname in the GLib file name encoding
1073 * (UTF-8 on Windows)
1074 * @buf: a pointer to a stat struct, which will be filled with the file
1077 * A wrapper for the POSIX lstat() function. The lstat() function is
1078 * like stat() except that in the case of symbolic links, it returns
1079 * information about the symbolic link itself and not the file that it
1080 * refers to. If the system does not support symbolic links g_lstat()
1081 * is identical to g_stat().
1083 * See your C library manual for more details about lstat().
1085 * Returns: 0 if the information was successfully retrieved,
1086 * -1 if an error occurred
1091 g_lstat (const gchar
*filename
,
1095 /* This can't be Win32, so don't do the widechar dance. */
1096 return lstat (filename
, buf
);
1097 #elif defined (G_OS_WIN32)
1098 GWin32PrivateStat w32_buf
;
1099 int retval
= g_win32_lstat_utf8 (filename
, &w32_buf
);
1101 buf
->st_dev
= w32_buf
.st_dev
;
1102 buf
->st_ino
= w32_buf
.st_ino
;
1103 buf
->st_mode
= w32_buf
.st_mode
;
1104 buf
->st_nlink
= w32_buf
.st_nlink
;
1105 buf
->st_uid
= w32_buf
.st_uid
;
1106 buf
->st_gid
= w32_buf
.st_gid
;
1107 buf
->st_rdev
= w32_buf
.st_dev
;
1108 buf
->st_size
= w32_buf
.st_size
;
1109 buf
->st_atime
= w32_buf
.st_atime
;
1110 buf
->st_mtime
= w32_buf
.st_mtime
;
1111 buf
->st_ctime
= w32_buf
.st_ctime
;
1115 return g_stat (filename
, buf
);
1121 * @filename: (type filename): a pathname in the GLib file name encoding
1122 * (UTF-8 on Windows)
1124 * A wrapper for the POSIX unlink() function. The unlink() function
1125 * deletes a name from the filesystem. If this was the last link to the
1126 * file and no processes have it opened, the diskspace occupied by the
1129 * See your C library manual for more details about unlink(). Note
1130 * that on Windows, it is in general not possible to delete files that
1131 * are open to some process, or mapped into memory.
1133 * Returns: 0 if the name was successfully deleted, -1 if an error
1139 g_unlink (const gchar
*filename
)
1142 wchar_t *wfilename
= g_utf8_to_utf16 (filename
, -1, NULL
, NULL
, NULL
);
1146 if (wfilename
== NULL
)
1152 retval
= _wunlink (wfilename
);
1160 return unlink (filename
);
1166 * @filename: (type filename): a pathname in the GLib file name encoding
1167 * (UTF-8 on Windows)
1169 * A wrapper for the POSIX remove() function. The remove() function
1170 * deletes a name from the filesystem.
1172 * See your C library manual for more details about how remove() works
1173 * on your system. On Unix, remove() removes also directories, as it
1174 * calls unlink() for files and rmdir() for directories. On Windows,
1175 * although remove() in the C library only works for files, this
1176 * function tries first remove() and then if that fails rmdir(), and
1177 * thus works for both files and directories. Note however, that on
1178 * Windows, it is in general not possible to remove a file that is
1179 * open to some process, or mapped into memory.
1181 * If this function fails on Windows you can't infer too much from the
1182 * errno value. rmdir() is tried regardless of what caused remove() to
1183 * fail. Any errno value set by remove() will be overwritten by that
1186 * Returns: 0 if the file was successfully removed, -1 if an error
1192 g_remove (const gchar
*filename
)
1195 wchar_t *wfilename
= g_utf8_to_utf16 (filename
, -1, NULL
, NULL
, NULL
);
1199 if (wfilename
== NULL
)
1205 retval
= _wremove (wfilename
);
1207 retval
= _wrmdir (wfilename
);
1215 return remove (filename
);
1221 * @filename: (type filename): a pathname in the GLib file name encoding
1222 * (UTF-8 on Windows)
1224 * A wrapper for the POSIX rmdir() function. The rmdir() function
1225 * deletes a directory from the filesystem.
1227 * See your C library manual for more details about how rmdir() works
1230 * Returns: 0 if the directory was successfully removed, -1 if an error
1236 g_rmdir (const gchar
*filename
)
1239 wchar_t *wfilename
= g_utf8_to_utf16 (filename
, -1, NULL
, NULL
, NULL
);
1243 if (wfilename
== NULL
)
1249 retval
= _wrmdir (wfilename
);
1257 return rmdir (filename
);
1263 * @filename: (type filename): a pathname in the GLib file name encoding
1264 * (UTF-8 on Windows)
1265 * @mode: a string describing the mode in which the file should be opened
1267 * A wrapper for the stdio fopen() function. The fopen() function
1268 * opens a file and associates a new stream with it.
1270 * Because file descriptors are specific to the C library on Windows,
1271 * and a file descriptor is part of the FILE struct, the FILE* returned
1272 * by this function makes sense only to functions in the same C library.
1273 * Thus if the GLib-using code uses a different C library than GLib does,
1274 * the FILE* returned by this function cannot be passed to C library
1275 * functions like fprintf() or fread().
1277 * See your C library manual for more details about fopen().
1279 * Returns: A FILE* if the file was successfully opened, or %NULL if
1285 g_fopen (const gchar
*filename
,
1289 wchar_t *wfilename
= g_utf8_to_utf16 (filename
, -1, NULL
, NULL
, NULL
);
1295 if (wfilename
== NULL
)
1301 mode2
= _g_win32_get_mode_alias (mode
);
1302 wmode
= g_utf8_to_utf16 (mode2
, -1, NULL
, NULL
, NULL
);
1312 retval
= _wfopen (wfilename
, wmode
);
1321 return fopen (filename
, mode
);
1327 * @filename: (type filename): a pathname in the GLib file name encoding
1328 * (UTF-8 on Windows)
1329 * @mode: a string describing the mode in which the file should be opened
1330 * @stream: (nullable): an existing stream which will be reused, or %NULL
1332 * A wrapper for the POSIX freopen() function. The freopen() function
1333 * opens a file and associates it with an existing stream.
1335 * See your C library manual for more details about freopen().
1337 * Returns: A FILE* if the file was successfully opened, or %NULL if
1338 * an error occurred.
1343 g_freopen (const gchar
*filename
,
1348 wchar_t *wfilename
= g_utf8_to_utf16 (filename
, -1, NULL
, NULL
, NULL
);
1354 if (wfilename
== NULL
)
1360 mode2
= _g_win32_get_mode_alias (mode
);
1361 wmode
= g_utf8_to_utf16 (mode2
, -1, NULL
, NULL
, NULL
);
1371 retval
= _wfreopen (wfilename
, wmode
, stream
);
1380 return freopen (filename
, mode
, stream
);
1386 * @filename: (type filename): a pathname in the GLib file name encoding
1387 * (UTF-8 on Windows)
1388 * @utb: a pointer to a struct utimbuf.
1390 * A wrapper for the POSIX utime() function. The utime() function
1391 * sets the access and modification timestamps of a file.
1393 * See your C library manual for more details about how utime() works
1396 * Returns: 0 if the operation was successful, -1 if an error occurred
1401 g_utime (const gchar
*filename
,
1402 struct utimbuf
*utb
)
1405 wchar_t *wfilename
= g_utf8_to_utf16 (filename
, -1, NULL
, NULL
, NULL
);
1409 if (wfilename
== NULL
)
1415 retval
= _wutime (wfilename
, (struct _utimbuf
*) utb
);
1423 return utime (filename
, utb
);
1429 * @fd: A file descriptor
1432 * This wraps the close() call; in case of error, %errno will be
1433 * preserved, but the error will also be stored as a #GError in @error.
1435 * Besides using #GError, there is another major reason to prefer this
1436 * function over the call provided by the system; on Unix, it will
1437 * attempt to correctly handle %EINTR, which has platform-specific
1440 * Returns: %TRUE on success, %FALSE if there was an error.
1450 /* Just ignore EINTR for now; a retry loop is the wrong thing to do
1451 * on Linux at least. Anyone who wants to add a conditional check
1452 * for e.g. HP-UX is welcome to do so later...
1454 * http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html
1455 * https://bugzilla.gnome.org/show_bug.cgi?id=682819
1456 * http://utcc.utoronto.ca/~cks/space/blog/unix/CloseEINTR
1457 * https://sites.google.com/site/michaelsafyan/software-engineering/checkforeintrwheninvokingclosethinkagain
1459 if (G_UNLIKELY (res
== -1 && errno
== EINTR
))
1464 g_set_error_literal (error
, G_FILE_ERROR
,
1465 g_file_error_from_errno (errsv
),
1466 g_strerror (errsv
));