Branch data Line data Source code
1 : : /* gstdio.c - wrappers for C library functions
2 : : *
3 : : * Copyright 2004 Tor Lillqvist
4 : : *
5 : : * SPDX-License-Identifier: LGPL-2.1-or-later
6 : : *
7 : : * This library is free software; you can redistribute it and/or
8 : : * modify it under the terms of the GNU Lesser General Public
9 : : * License as published by the Free Software Foundation; either
10 : : * version 2.1 of the License, or (at your option) any later version.
11 : : *
12 : : * This library is distributed in the hope that it will be useful,
13 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : : * Lesser General Public License for more details.
16 : : *
17 : : * You should have received a copy of the GNU Lesser General Public License
18 : : * along with this library; if not, see <http://www.gnu.org/licenses/>.
19 : : */
20 : :
21 : : #include "config.h"
22 : : #include "glibconfig.h"
23 : :
24 : : /* Don’t redefine (for example) g_open() to open(), since we actually want to
25 : : * define g_open() in this file and export it as a symbol. See gstdio.h. */
26 : : #define G_STDIO_WRAP_ON_UNIX
27 : :
28 : : #include <sys/types.h>
29 : : #include <sys/stat.h>
30 : : #include <fcntl.h>
31 : :
32 : : #ifdef G_OS_UNIX
33 : : #include <unistd.h>
34 : : #endif
35 : :
36 : : #ifdef G_OS_WIN32
37 : : #include <windows.h>
38 : : #include <errno.h>
39 : : #include <wchar.h>
40 : : #include <direct.h>
41 : : #include <io.h>
42 : : #include <sys/utime.h>
43 : : #include <stdlib.h> /* for MB_CUR_MAX */
44 : : #else
45 : : #include <utime.h>
46 : : #include <errno.h>
47 : : #endif
48 : :
49 : : #include "gstdio.h"
50 : : #include "gstdioprivate.h"
51 : :
52 : : #if !defined (G_OS_UNIX) && !defined (G_OS_WIN32)
53 : : #error Please port this to your operating system
54 : : #endif
55 : :
56 : : #if defined (_MSC_VER) && !defined(_WIN64)
57 : : #undef _wstat
58 : : #define _wstat _wstat32
59 : : #endif
60 : :
61 : : #if defined (G_OS_WIN32)
62 : :
63 : : /* We can't include Windows DDK and Windows SDK simultaneously,
64 : : * so let's copy this here from MinGW-w64 DDK.
65 : : * The structure is ultimately documented here:
66 : : * https://msdn.microsoft.com/en-us/library/ff552012(v=vs.85).aspx
67 : : */
68 : : typedef struct _REPARSE_DATA_BUFFER
69 : : {
70 : : ULONG ReparseTag;
71 : : USHORT ReparseDataLength;
72 : : USHORT Reserved;
73 : : union
74 : : {
75 : : struct
76 : : {
77 : : USHORT SubstituteNameOffset;
78 : : USHORT SubstituteNameLength;
79 : : USHORT PrintNameOffset;
80 : : USHORT PrintNameLength;
81 : : ULONG Flags;
82 : : WCHAR PathBuffer[1];
83 : : } SymbolicLinkReparseBuffer;
84 : : struct
85 : : {
86 : : USHORT SubstituteNameOffset;
87 : : USHORT SubstituteNameLength;
88 : : USHORT PrintNameOffset;
89 : : USHORT PrintNameLength;
90 : : WCHAR PathBuffer[1];
91 : : } MountPointReparseBuffer;
92 : : struct
93 : : {
94 : : UCHAR DataBuffer[1];
95 : : } GenericReparseBuffer;
96 : : };
97 : : } REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
98 : :
99 : : static int
100 : : w32_error_to_errno (DWORD error_code)
101 : : {
102 : : switch (error_code)
103 : : {
104 : : case ERROR_ACCESS_DENIED:
105 : : return EACCES;
106 : : break;
107 : : case ERROR_ALREADY_EXISTS:
108 : : case ERROR_FILE_EXISTS:
109 : : return EEXIST;
110 : : case ERROR_FILE_NOT_FOUND:
111 : : return ENOENT;
112 : : break;
113 : : case ERROR_INVALID_FUNCTION:
114 : : return EFAULT;
115 : : break;
116 : : case ERROR_INVALID_HANDLE:
117 : : return EBADF;
118 : : break;
119 : : case ERROR_INVALID_PARAMETER:
120 : : return EINVAL;
121 : : break;
122 : : case ERROR_LOCK_VIOLATION:
123 : : case ERROR_SHARING_VIOLATION:
124 : : return EACCES;
125 : : break;
126 : : case ERROR_NOT_ENOUGH_MEMORY:
127 : : case ERROR_OUTOFMEMORY:
128 : : return ENOMEM;
129 : : break;
130 : : case ERROR_NOT_SAME_DEVICE:
131 : : return EXDEV;
132 : : break;
133 : : case ERROR_PATH_NOT_FOUND:
134 : : return ENOENT; /* or ELOOP, or ENAMETOOLONG */
135 : : break;
136 : : default:
137 : : return EIO;
138 : : break;
139 : : }
140 : : }
141 : :
142 : : #include "gstdio-private.c"
143 : :
144 : : /* Windows implementation of fopen() does not accept modes such as
145 : : * "wb+". The 'b' needs to be appended to "w+", i.e. "w+b". Note
146 : : * that otherwise these 2 modes are supposed to be aliases, hence
147 : : * swappable at will. TODO: Is this still true?
148 : : *
149 : : * It also doesn’t accept `e`, which Unix uses for O_CLOEXEC. This function
150 : : * rewrites that to `N` — see
151 : : * https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fopen-wfopen?view=msvc-170
152 : : */
153 : : static void
154 : : _g_win32_fix_mode (wchar_t *mode)
155 : : {
156 : : wchar_t *ptr, *e_ptr, *comma_ptr;
157 : : wchar_t temp;
158 : :
159 : : ptr = wcschr (mode, L'+');
160 : : if (ptr != NULL && (ptr - mode) > 1)
161 : : {
162 : : temp = mode[1];
163 : : mode[1] = *ptr;
164 : : *ptr = temp;
165 : : }
166 : :
167 : : /* Rewrite `e` (O_CLOEXEC) to `N`, if it occurs before any extended attributes
168 : : * (https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fopen-wfopen?view=msvc-170#unicode-support) */
169 : : e_ptr = wcschr (mode, L'e');
170 : : comma_ptr = wcschr (mode, L',');
171 : : if (e_ptr != NULL && (comma_ptr == NULL || e_ptr < comma_ptr))
172 : : *e_ptr = L'N';
173 : : }
174 : :
175 : : /* From
176 : : * https://support.microsoft.com/en-ca/help/167296/how-to-convert-a-unix-time-t-to-a-win32-filetime-or-systemtime
177 : : * FT = UT * 10000000 + 116444736000000000.
178 : : * Therefore:
179 : : * UT = (FT - 116444736000000000) / 10000000.
180 : : * Converts FILETIME to unix epoch time in form
181 : : * of a signed 64-bit integer (can be negative).
182 : : *
183 : : * The function that does the reverse can be found in
184 : : * gio/glocalfileinfo.c.
185 : : */
186 : : static gint64
187 : : _g_win32_filetime_to_unix_time (const FILETIME *ft,
188 : : gint32 *nsec)
189 : : {
190 : : gint64 result;
191 : : /* 1 unit of FILETIME is 100ns */
192 : : const gint64 hundreds_of_usec_per_sec = 10000000;
193 : : /* The difference between January 1, 1601 UTC (FILETIME epoch) and UNIX epoch
194 : : * in hundreds of nanoseconds.
195 : : */
196 : : const gint64 filetime_unix_epoch_offset = 116444736000000000;
197 : :
198 : : result = ((gint64) ft->dwLowDateTime) | (((gint64) ft->dwHighDateTime) << 32);
199 : : result -= filetime_unix_epoch_offset;
200 : :
201 : : if (nsec)
202 : : *nsec = (result % hundreds_of_usec_per_sec) * 100;
203 : :
204 : : return result / hundreds_of_usec_per_sec;
205 : : }
206 : :
207 : : # ifdef _MSC_VER
208 : : # ifndef S_IXUSR
209 : : # define _S_IRUSR _S_IREAD
210 : : # define _S_IWUSR _S_IWRITE
211 : : # define _S_IXUSR _S_IEXEC
212 : : # define S_IRUSR _S_IRUSR
213 : : # define S_IWUSR _S_IWUSR
214 : : # define S_IXUSR _S_IXUSR
215 : : # define S_IRGRP (S_IRUSR >> 3)
216 : : # define S_IWGRP (S_IWUSR >> 3)
217 : : # define S_IXGRP (S_IXUSR >> 3)
218 : : # define S_IROTH (S_IRGRP >> 3)
219 : : # define S_IWOTH (S_IWGRP >> 3)
220 : : # define S_IXOTH (S_IXGRP >> 3)
221 : : # endif
222 : : # ifndef S_ISDIR
223 : : # define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
224 : : # endif
225 : : # endif
226 : :
227 : : /* Uses filename and BHFI to fill a stat64 structure.
228 : : * Tries to reproduce the behaviour and quirks of MS C runtime stat().
229 : : */
230 : : static int
231 : : _g_win32_fill_statbuf_from_handle_info (const wchar_t *filename,
232 : : const wchar_t *filename_target,
233 : : const BY_HANDLE_FILE_INFORMATION *handle_info,
234 : : struct __stat64 *statbuf)
235 : : {
236 : : wchar_t drive_letter_w = 0;
237 : : size_t drive_letter_size = MB_CUR_MAX;
238 : : char *drive_letter = _alloca (drive_letter_size);
239 : :
240 : : /* If filename (target or link) is absolute,
241 : : * then use the drive letter from it as-is.
242 : : */
243 : : if (filename_target != NULL &&
244 : : filename_target[0] != L'\0' &&
245 : : filename_target[1] == L':')
246 : : drive_letter_w = filename_target[0];
247 : : else if (filename[0] != L'\0' &&
248 : : filename[1] == L':')
249 : : drive_letter_w = filename[0];
250 : :
251 : : if (drive_letter_w > 0 &&
252 : : iswalpha (drive_letter_w) &&
253 : : iswascii (drive_letter_w) &&
254 : : wctomb (drive_letter, drive_letter_w) == 1)
255 : : statbuf->st_dev = toupper (drive_letter[0]) - 'A'; /* 0 means A: drive */
256 : : else
257 : : /* Otherwise use the PWD drive.
258 : : * Return value of 0 gives us 0 - 1 = -1,
259 : : * which is the "no idea" value for st_dev.
260 : : */
261 : : statbuf->st_dev = _getdrive () - 1;
262 : :
263 : : statbuf->st_rdev = statbuf->st_dev;
264 : : /* Theoretically, it's possible to set it for ext-FS. No idea how.
265 : : * Meaningless for all filesystems that Windows normally uses.
266 : : */
267 : : statbuf->st_ino = 0;
268 : : statbuf->st_mode = 0;
269 : :
270 : : if ((handle_info->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
271 : : statbuf->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
272 : : else
273 : : statbuf->st_mode |= S_IFREG;
274 : : /* No idea what S_IFCHR means here. */
275 : : /* S_IFIFO is not even mentioned in MSDN */
276 : : /* S_IFBLK is also not mentioned */
277 : :
278 : : /* The aim here is to reproduce MS stat() behaviour,
279 : : * even if it's braindead.
280 : : */
281 : : statbuf->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
282 : : if ((handle_info->dwFileAttributes & FILE_ATTRIBUTE_READONLY) != FILE_ATTRIBUTE_READONLY)
283 : : statbuf->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
284 : :
285 : : if (!S_ISDIR (statbuf->st_mode))
286 : : {
287 : : const wchar_t *name;
288 : : const wchar_t *dot = NULL;
289 : :
290 : : if (filename_target != NULL)
291 : : name = filename_target;
292 : : else
293 : : name = filename;
294 : :
295 : : do
296 : : {
297 : : wchar_t *last_dot = wcschr (name, L'.');
298 : : if (last_dot == NULL)
299 : : break;
300 : : dot = last_dot;
301 : : name = &last_dot[1];
302 : : }
303 : : while (TRUE);
304 : :
305 : : if ((dot != NULL &&
306 : : (wcsicmp (dot, L".exe") == 0 ||
307 : : wcsicmp (dot, L".com") == 0 ||
308 : : wcsicmp (dot, L".bat") == 0 ||
309 : : wcsicmp (dot, L".cmd") == 0)))
310 : : statbuf->st_mode |= S_IXUSR | S_IXGRP | S_IXOTH;
311 : : }
312 : :
313 : : statbuf->st_nlink = handle_info->nNumberOfLinks;
314 : : statbuf->st_uid = statbuf->st_gid = 0;
315 : : statbuf->st_size = (((guint64) handle_info->nFileSizeHigh) << 32) | handle_info->nFileSizeLow;
316 : : statbuf->st_ctime = _g_win32_filetime_to_unix_time (&handle_info->ftCreationTime, NULL);
317 : : statbuf->st_mtime = _g_win32_filetime_to_unix_time (&handle_info->ftLastWriteTime, NULL);
318 : : statbuf->st_atime = _g_win32_filetime_to_unix_time (&handle_info->ftLastAccessTime, NULL);
319 : :
320 : : return 0;
321 : : }
322 : :
323 : : /* Fills our private stat-like structure using data from
324 : : * a normal stat64 struct, BHFI, FSI and a reparse tag.
325 : : */
326 : : static void
327 : : _g_win32_fill_privatestat (const struct __stat64 *statbuf,
328 : : const BY_HANDLE_FILE_INFORMATION *handle_info,
329 : : const FILE_STANDARD_INFO *std_info,
330 : : DWORD reparse_tag,
331 : : GWin32PrivateStat *buf)
332 : : {
333 : : gint32 nsec;
334 : :
335 : : buf->st_dev = statbuf->st_dev;
336 : : buf->st_ino = statbuf->st_ino;
337 : : buf->st_mode = statbuf->st_mode;
338 : : buf->volume_serial = handle_info->dwVolumeSerialNumber;
339 : : buf->file_index = (((guint64) handle_info->nFileIndexHigh) << 32) | handle_info->nFileIndexLow;
340 : : buf->attributes = handle_info->dwFileAttributes;
341 : : buf->st_nlink = handle_info->nNumberOfLinks;
342 : : buf->st_size = (((guint64) handle_info->nFileSizeHigh) << 32) | handle_info->nFileSizeLow;
343 : : buf->allocated_size = std_info->AllocationSize.QuadPart;
344 : :
345 : : buf->reparse_tag = reparse_tag;
346 : :
347 : : buf->st_ctim.tv_sec = _g_win32_filetime_to_unix_time (&handle_info->ftCreationTime, &nsec);
348 : : buf->st_ctim.tv_nsec = nsec;
349 : : buf->st_mtim.tv_sec = _g_win32_filetime_to_unix_time (&handle_info->ftLastWriteTime, &nsec);
350 : : buf->st_mtim.tv_nsec = nsec;
351 : : buf->st_atim.tv_sec = _g_win32_filetime_to_unix_time (&handle_info->ftLastAccessTime, &nsec);
352 : : buf->st_atim.tv_nsec = nsec;
353 : : }
354 : :
355 : : /* Read the link data from a symlink/mountpoint represented
356 : : * by the handle. Also reads reparse tag.
357 : : * @reparse_tag receives the tag. Can be %NULL if @buf or @alloc_buf
358 : : * is non-NULL.
359 : : * @buf receives the link data. Can be %NULL if reparse_tag is non-%NULL.
360 : : * Mutually-exclusive with @alloc_buf.
361 : : * @buf_size is the size of the @buf, in bytes.
362 : : * @alloc_buf points to a location where internally-allocated buffer
363 : : * pointer will be written. That buffer receives the
364 : : * link data. Mutually-exclusive with @buf.
365 : : * @terminate ensures that the buffer is NUL-terminated if
366 : : * it isn't already. Note that this can erase useful
367 : : * data if @buf is provided and @buf_size is too small.
368 : : * Specifically, with @buf_size <= 2 the buffer will
369 : : * receive an empty string, even if there is some
370 : : * data in the reparse point.
371 : : * The contents of @buf or @alloc_buf are presented as-is - could
372 : : * be non-NUL-terminated (unless @terminate is %TRUE) or even malformed.
373 : : * Returns the number of bytes (!) placed into @buf or @alloc_buf,
374 : : * including NUL-terminator (if any).
375 : : *
376 : : * Returned value of 0 means that there's no recognizable data in the
377 : : * reparse point. @alloc_buf will not be allocated in that case,
378 : : * and @buf will be left unmodified.
379 : : *
380 : : * If @buf and @alloc_buf are %NULL, returns 0 to indicate success.
381 : : * Returns -1 to indicate an error, sets errno.
382 : : */
383 : : static int
384 : : _g_win32_readlink_handle_raw (HANDLE h,
385 : : DWORD *reparse_tag,
386 : : gunichar2 *buf,
387 : : gsize buf_size,
388 : : gunichar2 **alloc_buf,
389 : : gboolean terminate)
390 : : {
391 : : DWORD error_code;
392 : : DWORD returned_bytes = 0;
393 : : BYTE *data = NULL;
394 : : gsize to_copy;
395 : : /* This is 16k. It's impossible to make DeviceIoControl() tell us
396 : : * the required size. NtFsControlFile() does have such a feature,
397 : : * but for some reason it doesn't work with CreateFile()-returned handles.
398 : : * The only alternative is to repeatedly call DeviceIoControl()
399 : : * with bigger and bigger buffers, until it succeeds.
400 : : * We choose to sacrifice stack space for speed.
401 : : */
402 : : BYTE max_buffer[sizeof (REPARSE_DATA_BUFFER) + MAXIMUM_REPARSE_DATA_BUFFER_SIZE] = {0,};
403 : : DWORD max_buffer_size = sizeof (REPARSE_DATA_BUFFER) + MAXIMUM_REPARSE_DATA_BUFFER_SIZE;
404 : : REPARSE_DATA_BUFFER *rep_buf;
405 : :
406 : : g_return_val_if_fail ((buf != NULL || alloc_buf != NULL || reparse_tag != NULL) &&
407 : : (buf == NULL || alloc_buf == NULL),
408 : : -1);
409 : :
410 : : if (!DeviceIoControl (h, FSCTL_GET_REPARSE_POINT, NULL, 0,
411 : : max_buffer,
412 : : max_buffer_size,
413 : : &returned_bytes, NULL))
414 : : {
415 : : error_code = GetLastError ();
416 : : errno = w32_error_to_errno (error_code);
417 : : return -1;
418 : : }
419 : :
420 : : rep_buf = (REPARSE_DATA_BUFFER *) max_buffer;
421 : :
422 : : if (reparse_tag != NULL)
423 : : *reparse_tag = rep_buf->ReparseTag;
424 : :
425 : : if (buf == NULL && alloc_buf == NULL)
426 : : return 0;
427 : :
428 : : if (rep_buf->ReparseTag == IO_REPARSE_TAG_SYMLINK)
429 : : {
430 : : data = &((BYTE *) rep_buf->SymbolicLinkReparseBuffer.PathBuffer)[rep_buf->SymbolicLinkReparseBuffer.SubstituteNameOffset];
431 : :
432 : : to_copy = rep_buf->SymbolicLinkReparseBuffer.SubstituteNameLength;
433 : : }
434 : : else if (rep_buf->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT)
435 : : {
436 : : data = &((BYTE *) rep_buf->MountPointReparseBuffer.PathBuffer)[rep_buf->MountPointReparseBuffer.SubstituteNameOffset];
437 : :
438 : : to_copy = rep_buf->MountPointReparseBuffer.SubstituteNameLength;
439 : : }
440 : : else
441 : : to_copy = 0;
442 : :
443 : : return _g_win32_copy_and_maybe_terminate (data, to_copy, buf, buf_size, alloc_buf, terminate);
444 : : }
445 : :
446 : : /* Read the link data from a symlink/mountpoint represented
447 : : * by the @filename.
448 : : * @filename is the name of the file.
449 : : * @reparse_tag receives the tag. Can be %NULL if @buf or @alloc_buf
450 : : * is non-%NULL.
451 : : * @buf receives the link data. Mutually-exclusive with @alloc_buf.
452 : : * @buf_size is the size of the @buf, in bytes.
453 : : * @alloc_buf points to a location where internally-allocated buffer
454 : : * pointer will be written. That buffer receives the
455 : : * link data. Mutually-exclusive with @buf.
456 : : * @terminate ensures that the buffer is NUL-terminated if
457 : : * it isn't already
458 : : * The contents of @buf or @alloc_buf are presented as-is - could
459 : : * be non-NUL-terminated (unless @terminate is TRUE) or even malformed.
460 : : * Returns the number of bytes (!) placed into @buf or @alloc_buf.
461 : : * Returned value of 0 means that there's no recognizable data in the
462 : : * reparse point. @alloc_buf will not be allocated in that case,
463 : : * and @buf will be left unmodified.
464 : : * If @buf and @alloc_buf are %NULL, returns 0 to indicate success.
465 : : * Returns -1 to indicate an error, sets errno.
466 : : */
467 : : static int
468 : : _g_win32_readlink_utf16_raw (const gunichar2 *filename,
469 : : DWORD *reparse_tag,
470 : : gunichar2 *buf,
471 : : gsize buf_size,
472 : : gunichar2 **alloc_buf,
473 : : gboolean terminate)
474 : : {
475 : : HANDLE h;
476 : : DWORD attributes;
477 : : DWORD to_copy;
478 : : DWORD error_code;
479 : :
480 : : if ((attributes = GetFileAttributesW (filename)) == 0)
481 : : {
482 : : error_code = GetLastError ();
483 : : errno = w32_error_to_errno (error_code);
484 : : return -1;
485 : : }
486 : :
487 : : if ((attributes & FILE_ATTRIBUTE_REPARSE_POINT) == 0)
488 : : {
489 : : errno = EINVAL;
490 : : return -1;
491 : : }
492 : :
493 : : /* To read symlink target we need to open the file as a reparse
494 : : * point and use DeviceIoControl() on it.
495 : : */
496 : : h = CreateFileW (filename,
497 : : FILE_READ_EA,
498 : : FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
499 : : NULL, OPEN_EXISTING,
500 : : FILE_ATTRIBUTE_NORMAL
501 : : | FILE_FLAG_OPEN_REPARSE_POINT
502 : : | (attributes & FILE_ATTRIBUTE_DIRECTORY ? FILE_FLAG_BACKUP_SEMANTICS : 0),
503 : : NULL);
504 : :
505 : : if (h == INVALID_HANDLE_VALUE)
506 : : {
507 : : error_code = GetLastError ();
508 : : errno = w32_error_to_errno (error_code);
509 : : return -1;
510 : : }
511 : :
512 : : to_copy = _g_win32_readlink_handle_raw (h, reparse_tag, buf, buf_size, alloc_buf, terminate);
513 : :
514 : : CloseHandle (h);
515 : :
516 : : return to_copy;
517 : : }
518 : :
519 : : /* Read the link data from a symlink/mountpoint represented
520 : : * by a UTF-16 filename or a file handle.
521 : : * @filename is the name of the file. Mutually-exclusive with @file_handle.
522 : : * @file_handle is the handle of the file. Mutually-exclusive with @filename.
523 : : * @reparse_tag receives the tag. Can be %NULL if @buf or @alloc_buf
524 : : * is non-%NULL.
525 : : * @buf receives the link data. Mutually-exclusive with @alloc_buf.
526 : : * @buf_size is the size of the @buf, in bytes.
527 : : * @alloc_buf points to a location where internally-allocated buffer
528 : : * pointer will be written. That buffer receives the
529 : : * link data. Mutually-exclusive with @buf.
530 : : * @terminate ensures that the buffer is NUL-terminated if
531 : : * it isn't already
532 : : * The contents of @buf or @alloc_buf are adjusted
533 : : * (extended or nt object manager prefix is stripped),
534 : : * but otherwise they are presented as-is - could be non-NUL-terminated
535 : : * (unless @terminate is TRUE) or even malformed.
536 : : * Returns the number of bytes (!) placed into @buf or @alloc_buf.
537 : : * Returned value of 0 means that there's no recognizable data in the
538 : : * reparse point. @alloc_buf will not be allocated in that case,
539 : : * and @buf will be left unmodified.
540 : : * Returns -1 to indicate an error, sets errno.
541 : : */
542 : : static int
543 : : _g_win32_readlink_utf16_handle (const gunichar2 *filename,
544 : : HANDLE file_handle,
545 : : DWORD *reparse_tag,
546 : : gunichar2 *buf,
547 : : gsize buf_size,
548 : : gunichar2 **alloc_buf,
549 : : gboolean terminate)
550 : : {
551 : : int result;
552 : : gsize string_size;
553 : :
554 : : g_return_val_if_fail ((buf != NULL || alloc_buf != NULL || reparse_tag != NULL) &&
555 : : (filename != NULL || file_handle != NULL) &&
556 : : (buf == NULL || alloc_buf == NULL) &&
557 : : (filename == NULL || file_handle == NULL),
558 : : -1);
559 : :
560 : : if (filename)
561 : : result = _g_win32_readlink_utf16_raw (filename, reparse_tag, buf, buf_size, alloc_buf, terminate);
562 : : else
563 : : result = _g_win32_readlink_handle_raw (file_handle, reparse_tag, buf, buf_size, alloc_buf, terminate);
564 : :
565 : : if (result <= 0)
566 : : return result;
567 : :
568 : : /* Ensure that output is a multiple of sizeof (gunichar2),
569 : : * cutting any trailing partial gunichar2, if present.
570 : : */
571 : : result -= result % sizeof (gunichar2);
572 : :
573 : : if (result <= 0)
574 : : return result;
575 : :
576 : : /* DeviceIoControl () tends to return filenames as NT Object Manager
577 : : * names , i.e. "\\??\\C:\\foo\\bar".
578 : : * Remove the leading 4-byte "\\??\\" prefix, as glib (as well as many W32 API
579 : : * functions) is unprepared to deal with it. Unless it has no 'x:' drive
580 : : * letter part after the prefix, in which case we leave everything
581 : : * as-is, because the path could be "\\??\\Volume{GUID}" - stripping
582 : : * the prefix will allow it to be confused with relative links
583 : : * targeting "Volume{GUID}".
584 : : */
585 : : string_size = result / sizeof (gunichar2);
586 : : _g_win32_strip_extended_ntobjm_prefix (buf ? buf : *alloc_buf, &string_size);
587 : :
588 : : return string_size * sizeof (gunichar2);
589 : : }
590 : :
591 : : /* Works like stat() or lstat(), depending on the value of @for_symlink,
592 : : * but accepts filename in UTF-16 and fills our custom stat structure.
593 : : * The @filename must not have trailing slashes.
594 : : */
595 : : static int
596 : : _g_win32_stat_utf16_no_trailing_slashes (const gunichar2 *filename,
597 : : GWin32PrivateStat *buf,
598 : : gboolean for_symlink)
599 : : {
600 : : struct __stat64 statbuf;
601 : : BY_HANDLE_FILE_INFORMATION handle_info;
602 : : FILE_STANDARD_INFO std_info;
603 : : gboolean is_symlink = FALSE;
604 : : wchar_t *filename_target = NULL;
605 : : DWORD immediate_attributes;
606 : : DWORD open_flags;
607 : : gboolean is_directory;
608 : : DWORD reparse_tag = 0;
609 : : DWORD error_code;
610 : : BOOL succeeded_so_far;
611 : : HANDLE file_handle;
612 : :
613 : : immediate_attributes = GetFileAttributesW (filename);
614 : :
615 : : if (immediate_attributes == INVALID_FILE_ATTRIBUTES)
616 : : {
617 : : error_code = GetLastError ();
618 : : errno = w32_error_to_errno (error_code);
619 : :
620 : : return -1;
621 : : }
622 : :
623 : : is_symlink = (immediate_attributes & FILE_ATTRIBUTE_REPARSE_POINT) == FILE_ATTRIBUTE_REPARSE_POINT;
624 : : is_directory = (immediate_attributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY;
625 : :
626 : : open_flags = FILE_ATTRIBUTE_NORMAL;
627 : :
628 : : if (for_symlink && is_symlink)
629 : : open_flags |= FILE_FLAG_OPEN_REPARSE_POINT;
630 : :
631 : : if (is_directory)
632 : : open_flags |= FILE_FLAG_BACKUP_SEMANTICS;
633 : :
634 : : file_handle = CreateFileW (filename, FILE_READ_ATTRIBUTES | FILE_READ_EA,
635 : : FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
636 : : NULL, OPEN_EXISTING,
637 : : open_flags,
638 : : NULL);
639 : :
640 : : if (file_handle == INVALID_HANDLE_VALUE)
641 : : {
642 : : error_code = GetLastError ();
643 : : errno = w32_error_to_errno (error_code);
644 : : return -1;
645 : : }
646 : :
647 : : succeeded_so_far = GetFileInformationByHandle (file_handle,
648 : : &handle_info);
649 : : error_code = GetLastError ();
650 : :
651 : : if (succeeded_so_far)
652 : : {
653 : : succeeded_so_far = GetFileInformationByHandleEx (file_handle,
654 : : FileStandardInfo,
655 : : &std_info,
656 : : sizeof (std_info));
657 : : error_code = GetLastError ();
658 : : }
659 : :
660 : : if (!succeeded_so_far)
661 : : {
662 : : CloseHandle (file_handle);
663 : : errno = w32_error_to_errno (error_code);
664 : : return -1;
665 : : }
666 : :
667 : : /* It's tempting to use GetFileInformationByHandleEx(FileAttributeTagInfo),
668 : : * but it always reports that the ReparseTag is 0.
669 : : * We already have a handle open for symlink, use that.
670 : : * For the target we have to specify a filename, and the function
671 : : * will open another handle internally.
672 : : */
673 : : if (is_symlink &&
674 : : _g_win32_readlink_utf16_handle (for_symlink ? NULL : filename,
675 : : for_symlink ? file_handle : NULL,
676 : : &reparse_tag,
677 : : NULL, 0,
678 : : for_symlink ? NULL : &filename_target,
679 : : TRUE) < 0)
680 : : {
681 : : CloseHandle (file_handle);
682 : : return -1;
683 : : }
684 : :
685 : : CloseHandle (file_handle);
686 : :
687 : : _g_win32_fill_statbuf_from_handle_info (filename,
688 : : filename_target,
689 : : &handle_info,
690 : : &statbuf);
691 : : g_free (filename_target);
692 : : _g_win32_fill_privatestat (&statbuf,
693 : : &handle_info,
694 : : &std_info,
695 : : reparse_tag,
696 : : buf);
697 : :
698 : : return 0;
699 : : }
700 : :
701 : : /* Works like fstat(), but fills our custom stat structure. */
702 : : static int
703 : : _g_win32_stat_fd (int fd,
704 : : GWin32PrivateStat *buf)
705 : : {
706 : : HANDLE file_handle;
707 : : gboolean succeeded_so_far;
708 : : DWORD error_code;
709 : : struct __stat64 statbuf;
710 : : BY_HANDLE_FILE_INFORMATION handle_info;
711 : : FILE_STANDARD_INFO std_info;
712 : : DWORD reparse_tag = 0;
713 : : gboolean is_symlink = FALSE;
714 : :
715 : : file_handle = (HANDLE) _get_osfhandle (fd);
716 : :
717 : : if (file_handle == INVALID_HANDLE_VALUE)
718 : : return -1;
719 : :
720 : : succeeded_so_far = GetFileInformationByHandle (file_handle,
721 : : &handle_info);
722 : : error_code = GetLastError ();
723 : :
724 : : if (succeeded_so_far)
725 : : {
726 : : succeeded_so_far = GetFileInformationByHandleEx (file_handle,
727 : : FileStandardInfo,
728 : : &std_info,
729 : : sizeof (std_info));
730 : : error_code = GetLastError ();
731 : : }
732 : :
733 : : if (!succeeded_so_far)
734 : : {
735 : : errno = w32_error_to_errno (error_code);
736 : : return -1;
737 : : }
738 : :
739 : : is_symlink = (handle_info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) == FILE_ATTRIBUTE_REPARSE_POINT;
740 : :
741 : : if (is_symlink &&
742 : : _g_win32_readlink_handle_raw (file_handle, &reparse_tag, NULL, 0, NULL, FALSE) < 0)
743 : : return -1;
744 : :
745 : : if (_fstat64 (fd, &statbuf) != 0)
746 : : return -1;
747 : :
748 : : _g_win32_fill_privatestat (&statbuf,
749 : : &handle_info,
750 : : &std_info,
751 : : reparse_tag,
752 : : buf);
753 : :
754 : : return 0;
755 : : }
756 : :
757 : : /* Works like stat() or lstat(), depending on the value of @for_symlink,
758 : : * but accepts filename in UTF-8 and fills our custom stat structure.
759 : : */
760 : : static int
761 : : _g_win32_stat_utf8 (const gchar *filename,
762 : : GWin32PrivateStat *buf,
763 : : gboolean for_symlink)
764 : : {
765 : : wchar_t *wfilename;
766 : : int result;
767 : : gsize len;
768 : :
769 : : if (filename == NULL)
770 : : {
771 : : errno = EINVAL;
772 : : return -1;
773 : : }
774 : :
775 : : len = strlen (filename);
776 : :
777 : : while (len > 0 && G_IS_DIR_SEPARATOR (filename[len - 1]))
778 : : len--;
779 : :
780 : : if (len <= 0 ||
781 : : (g_path_is_absolute (filename) && len <= (gsize) (g_path_skip_root (filename) - filename)))
782 : : len = strlen (filename);
783 : :
784 : : wfilename = g_utf8_to_utf16 (filename, len, NULL, NULL, NULL);
785 : :
786 : : if (wfilename == NULL)
787 : : {
788 : : errno = EINVAL;
789 : : return -1;
790 : : }
791 : :
792 : : result = _g_win32_stat_utf16_no_trailing_slashes (wfilename, buf, for_symlink);
793 : :
794 : : g_free (wfilename);
795 : :
796 : : return result;
797 : : }
798 : :
799 : : /* Works like stat(), but accepts filename in UTF-8
800 : : * and fills our custom stat structure.
801 : : */
802 : : int
803 : : g_win32_stat_utf8 (const gchar *filename,
804 : : GWin32PrivateStat *buf)
805 : : {
806 : : return _g_win32_stat_utf8 (filename, buf, FALSE);
807 : : }
808 : :
809 : : /* Works like lstat(), but accepts filename in UTF-8
810 : : * and fills our custom stat structure.
811 : : */
812 : : int
813 : : g_win32_lstat_utf8 (const gchar *filename,
814 : : GWin32PrivateStat *buf)
815 : : {
816 : : return _g_win32_stat_utf8 (filename, buf, TRUE);
817 : : }
818 : :
819 : : /* Works like fstat(), but accepts filename in UTF-8
820 : : * and fills our custom stat structure.
821 : : */
822 : : int
823 : : g_win32_fstat (int fd,
824 : : GWin32PrivateStat *buf)
825 : : {
826 : : return _g_win32_stat_fd (fd, buf);
827 : : }
828 : :
829 : : /**
830 : : * g_win32_readlink_utf8:
831 : : * @filename: (type filename): a pathname in UTF-8
832 : : * @buf: (array length=buf_size) : a buffer to receive the reparse point
833 : : * target path. Mutually-exclusive
834 : : * with @alloc_buf.
835 : : * @buf_size: size of the @buf, in bytes
836 : : * @alloc_buf: points to a location where internally-allocated buffer
837 : : * pointer will be written. That buffer receives the
838 : : * link data. Mutually-exclusive with @buf.
839 : : * @terminate: ensures that the buffer is NUL-terminated if
840 : : * it isn't already. If %FALSE, the returned string
841 : : * might not be NUL-terminated (depends entirely on
842 : : * what the contents of the filesystem are).
843 : : *
844 : : * Tries to read the reparse point indicated by @filename, filling
845 : : * @buf or @alloc_buf with the path that the reparse point redirects to.
846 : : * The path will be UTF-8-encoded, and an extended path prefix
847 : : * or a NT object manager prefix will be removed from it, if
848 : : * possible, but otherwise the path is returned as-is. Specifically,
849 : : * it could be a "\\\\Volume{GUID}\\" path. It also might use
850 : : * backslashes as path separators.
851 : : *
852 : : * Returns: -1 on error (sets errno), 0 if there's no (recognizable)
853 : : * path in the reparse point (@alloc_buf will not be allocated in that case,
854 : : * and @buf will be left unmodified),
855 : : * or the number of bytes placed into @buf otherwise,
856 : : * including NUL-terminator (if present or if @terminate is TRUE).
857 : : * The buffer returned via @alloc_buf should be freed with g_free().
858 : : *
859 : : * Since: 2.60
860 : : */
861 : : int
862 : : g_win32_readlink_utf8 (const gchar *filename,
863 : : gchar *buf,
864 : : gsize buf_size,
865 : : gchar **alloc_buf,
866 : : gboolean terminate)
867 : : {
868 : : wchar_t *wfilename;
869 : : int result;
870 : : wchar_t *buf_utf16;
871 : : glong tmp_len;
872 : : gchar *tmp;
873 : :
874 : : g_return_val_if_fail ((buf != NULL || alloc_buf != NULL) &&
875 : : (buf == NULL || alloc_buf == NULL),
876 : : -1);
877 : :
878 : : wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
879 : :
880 : : if (wfilename == NULL)
881 : : {
882 : : errno = EINVAL;
883 : : return -1;
884 : : }
885 : :
886 : : result = _g_win32_readlink_utf16_handle (wfilename, NULL, NULL,
887 : : NULL, 0, &buf_utf16, terminate);
888 : :
889 : : g_free (wfilename);
890 : :
891 : : if (result <= 0)
892 : : return result;
893 : :
894 : : tmp = g_utf16_to_utf8 (buf_utf16,
895 : : result / sizeof (gunichar2),
896 : : NULL,
897 : : &tmp_len,
898 : : NULL);
899 : :
900 : : g_free (buf_utf16);
901 : :
902 : : if (tmp == NULL)
903 : : {
904 : : errno = EINVAL;
905 : : return -1;
906 : : }
907 : :
908 : : if (alloc_buf)
909 : : {
910 : : *alloc_buf = tmp;
911 : : return tmp_len;
912 : : }
913 : :
914 : : if ((gsize) tmp_len > buf_size)
915 : : tmp_len = buf_size;
916 : :
917 : : memcpy (buf, tmp, tmp_len);
918 : : g_free (tmp);
919 : :
920 : : return tmp_len;
921 : : }
922 : :
923 : : #endif
924 : :
925 : : /**
926 : : * g_access:
927 : : * @filename: (type filename): a pathname in the GLib file name encoding
928 : : * (UTF-8 on Windows)
929 : : * @mode: as in access()
930 : : *
931 : : * A wrapper for the POSIX access() function. This function is used to
932 : : * test a pathname for one or several of read, write or execute
933 : : * permissions, or just existence.
934 : : *
935 : : * On Windows, the file protection mechanism is not at all POSIX-like,
936 : : * and the underlying function in the C library only checks the
937 : : * FAT-style READONLY attribute, and does not look at the ACL of a
938 : : * file at all. This function is this in practise almost useless on
939 : : * Windows. Software that needs to handle file permissions on Windows
940 : : * more exactly should use the Win32 API.
941 : : *
942 : : * See your C library manual for more details about access().
943 : : *
944 : : * Returns: zero if the pathname refers to an existing file system
945 : : * object that has all the tested permissions, or -1 otherwise
946 : : * or on error.
947 : : *
948 : : * Since: 2.8
949 : : */
950 : : int
951 : 3334 : g_access (const gchar *filename,
952 : : int mode)
953 : : {
954 : : #ifdef G_OS_WIN32
955 : : wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
956 : : int retval;
957 : : int save_errno;
958 : :
959 : : if (wfilename == NULL)
960 : : {
961 : : errno = EINVAL;
962 : : return -1;
963 : : }
964 : :
965 : : #ifndef X_OK
966 : : #define X_OK 1
967 : : #endif
968 : :
969 : : retval = _waccess (wfilename, mode & ~X_OK);
970 : : save_errno = errno;
971 : :
972 : : g_free (wfilename);
973 : :
974 : : errno = save_errno;
975 : : return retval;
976 : : #else
977 : 3334 : return access (filename, mode);
978 : : #endif
979 : : }
980 : :
981 : : /**
982 : : * g_chmod:
983 : : * @filename: (type filename): a pathname in the GLib file name encoding
984 : : * (UTF-8 on Windows)
985 : : * @mode: as in chmod()
986 : : *
987 : : * A wrapper for the POSIX chmod() function. The chmod() function is
988 : : * used to set the permissions of a file system object.
989 : : *
990 : : * On Windows the file protection mechanism is not at all POSIX-like,
991 : : * and the underlying chmod() function in the C library just sets or
992 : : * clears the FAT-style READONLY attribute. It does not touch any
993 : : * ACL. Software that needs to manage file permissions on Windows
994 : : * exactly should use the Win32 API.
995 : : *
996 : : * See your C library manual for more details about chmod().
997 : : *
998 : : * Returns: 0 if the operation succeeded, -1 on error
999 : : *
1000 : : * Since: 2.8
1001 : : */
1002 : : int
1003 : 33 : g_chmod (const gchar *filename,
1004 : : int mode)
1005 : : {
1006 : : #ifdef G_OS_WIN32
1007 : : wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1008 : : int retval;
1009 : : int save_errno;
1010 : :
1011 : : if (wfilename == NULL)
1012 : : {
1013 : : errno = EINVAL;
1014 : : return -1;
1015 : : }
1016 : :
1017 : : retval = _wchmod (wfilename, mode);
1018 : : save_errno = errno;
1019 : :
1020 : : g_free (wfilename);
1021 : :
1022 : : errno = save_errno;
1023 : : return retval;
1024 : : #else
1025 : 33 : return chmod (filename, mode);
1026 : : #endif
1027 : : }
1028 : : /**
1029 : : * g_open:
1030 : : * @filename: (type filename): a pathname in the GLib file name encoding
1031 : : * (UTF-8 on Windows)
1032 : : * @flags: as in open()
1033 : : * @mode: as in open()
1034 : : *
1035 : : * A wrapper for the POSIX open() function. The open() function is
1036 : : * used to convert a pathname into a file descriptor.
1037 : : *
1038 : : * On POSIX systems file descriptors are implemented by the operating
1039 : : * system. On Windows, it's the C library that implements open() and
1040 : : * file descriptors. The actual Win32 API for opening files is quite
1041 : : * different, see MSDN documentation for CreateFile(). The Win32 API
1042 : : * uses file handles, which are more randomish integers, not small
1043 : : * integers like file descriptors.
1044 : : *
1045 : : * Because file descriptors are specific to the C library on Windows,
1046 : : * the file descriptor returned by this function makes sense only to
1047 : : * functions in the same C library. Thus if the GLib-using code uses a
1048 : : * different C library than GLib does, the file descriptor returned by
1049 : : * this function cannot be passed to C library functions like write()
1050 : : * or read().
1051 : : *
1052 : : * See your C library manual for more details about open().
1053 : : *
1054 : : * Returns: a new file descriptor, or -1 if an error occurred.
1055 : : * The return value can be used exactly like the return value
1056 : : * from open().
1057 : : *
1058 : : * Since: 2.6
1059 : : */
1060 : : int
1061 : 9 : g_open (const gchar *filename,
1062 : : int flags,
1063 : : int mode)
1064 : : {
1065 : : #ifdef G_OS_WIN32
1066 : : wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1067 : : int retval;
1068 : : int save_errno;
1069 : :
1070 : : if (wfilename == NULL)
1071 : : {
1072 : : errno = EINVAL;
1073 : : return -1;
1074 : : }
1075 : :
1076 : : retval = _wopen (wfilename, flags, mode);
1077 : : save_errno = errno;
1078 : :
1079 : : g_free (wfilename);
1080 : :
1081 : : errno = save_errno;
1082 : : return retval;
1083 : : #else
1084 : : int fd;
1085 : : do
1086 : 9 : fd = open (filename, flags, mode);
1087 : 9 : while (G_UNLIKELY (fd == -1 && errno == EINTR));
1088 : 9 : return fd;
1089 : : #endif
1090 : : }
1091 : :
1092 : : /**
1093 : : * g_creat:
1094 : : * @filename: (type filename): a pathname in the GLib file name encoding
1095 : : * (UTF-8 on Windows)
1096 : : * @mode: as in creat()
1097 : : *
1098 : : * A wrapper for the POSIX creat() function. The creat() function is
1099 : : * used to convert a pathname into a file descriptor, creating a file
1100 : : * if necessary.
1101 : : *
1102 : : * On POSIX systems file descriptors are implemented by the operating
1103 : : * system. On Windows, it's the C library that implements creat() and
1104 : : * file descriptors. The actual Windows API for opening files is
1105 : : * different, see MSDN documentation for CreateFile(). The Win32 API
1106 : : * uses file handles, which are more randomish integers, not small
1107 : : * integers like file descriptors.
1108 : : *
1109 : : * Because file descriptors are specific to the C library on Windows,
1110 : : * the file descriptor returned by this function makes sense only to
1111 : : * functions in the same C library. Thus if the GLib-using code uses a
1112 : : * different C library than GLib does, the file descriptor returned by
1113 : : * this function cannot be passed to C library functions like write()
1114 : : * or read().
1115 : : *
1116 : : * See your C library manual for more details about creat().
1117 : : *
1118 : : * Returns: a new file descriptor, or -1 if an error occurred.
1119 : : * The return value can be used exactly like the return value
1120 : : * from creat().
1121 : : *
1122 : : * Since: 2.8
1123 : : */
1124 : : int
1125 : 1 : g_creat (const gchar *filename,
1126 : : int mode)
1127 : : {
1128 : : #ifdef G_OS_WIN32
1129 : : wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1130 : : int retval;
1131 : : int save_errno;
1132 : :
1133 : : if (wfilename == NULL)
1134 : : {
1135 : : errno = EINVAL;
1136 : : return -1;
1137 : : }
1138 : :
1139 : : retval = _wcreat (wfilename, mode);
1140 : : save_errno = errno;
1141 : :
1142 : : g_free (wfilename);
1143 : :
1144 : : errno = save_errno;
1145 : : return retval;
1146 : : #else
1147 : 1 : return creat (filename, mode);
1148 : : #endif
1149 : : }
1150 : :
1151 : : /**
1152 : : * g_rename:
1153 : : * @oldfilename: (type filename): a pathname in the GLib file name encoding
1154 : : * (UTF-8 on Windows)
1155 : : * @newfilename: (type filename): a pathname in the GLib file name encoding
1156 : : *
1157 : : * A wrapper for the POSIX rename() function. The rename() function
1158 : : * renames a file, moving it between directories if required.
1159 : : *
1160 : : * See your C library manual for more details about how rename() works
1161 : : * on your system. It is not possible in general on Windows to rename
1162 : : * a file that is open to some process.
1163 : : *
1164 : : * Returns: 0 if the renaming succeeded, -1 if an error occurred
1165 : : *
1166 : : * Since: 2.6
1167 : : */
1168 : : int
1169 : 1 : g_rename (const gchar *oldfilename,
1170 : : const gchar *newfilename)
1171 : : {
1172 : : #ifdef G_OS_WIN32
1173 : : wchar_t *woldfilename = g_utf8_to_utf16 (oldfilename, -1, NULL, NULL, NULL);
1174 : : wchar_t *wnewfilename;
1175 : : int retval;
1176 : : int save_errno = 0;
1177 : :
1178 : : if (woldfilename == NULL)
1179 : : {
1180 : : errno = EINVAL;
1181 : : return -1;
1182 : : }
1183 : :
1184 : : wnewfilename = g_utf8_to_utf16 (newfilename, -1, NULL, NULL, NULL);
1185 : :
1186 : : if (wnewfilename == NULL)
1187 : : {
1188 : : g_free (woldfilename);
1189 : : errno = EINVAL;
1190 : : return -1;
1191 : : }
1192 : :
1193 : : if (MoveFileExW (woldfilename, wnewfilename, MOVEFILE_REPLACE_EXISTING))
1194 : : retval = 0;
1195 : : else
1196 : : {
1197 : : retval = -1;
1198 : : save_errno = w32_error_to_errno (GetLastError ());
1199 : : }
1200 : :
1201 : : g_free (woldfilename);
1202 : : g_free (wnewfilename);
1203 : :
1204 : : errno = save_errno;
1205 : : return retval;
1206 : : #else
1207 : 1 : return rename (oldfilename, newfilename);
1208 : : #endif
1209 : : }
1210 : :
1211 : : /**
1212 : : * g_mkdir:
1213 : : * @filename: (type filename): a pathname in the GLib file name encoding
1214 : : * (UTF-8 on Windows)
1215 : : * @mode: permissions to use for the newly created directory
1216 : : *
1217 : : * A wrapper for the POSIX mkdir() function. The mkdir() function
1218 : : * attempts to create a directory with the given name and permissions.
1219 : : * The mode argument is ignored on Windows.
1220 : : *
1221 : : * See your C library manual for more details about mkdir().
1222 : : *
1223 : : * Returns: 0 if the directory was successfully created, -1 if an error
1224 : : * occurred
1225 : : *
1226 : : * Since: 2.6
1227 : : */
1228 : : int
1229 : 2 : g_mkdir (const gchar *filename,
1230 : : int mode)
1231 : : {
1232 : : #ifdef G_OS_WIN32
1233 : : wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1234 : : int retval;
1235 : : int save_errno;
1236 : :
1237 : : if (wfilename == NULL)
1238 : : {
1239 : : errno = EINVAL;
1240 : : return -1;
1241 : : }
1242 : :
1243 : : retval = _wmkdir (wfilename);
1244 : : save_errno = errno;
1245 : :
1246 : : g_free (wfilename);
1247 : :
1248 : : errno = save_errno;
1249 : : return retval;
1250 : : #else
1251 : 2 : return mkdir (filename, mode);
1252 : : #endif
1253 : : }
1254 : :
1255 : : /**
1256 : : * g_chdir:
1257 : : * @path: (type filename): a pathname in the GLib file name encoding
1258 : : * (UTF-8 on Windows)
1259 : : *
1260 : : * A wrapper for the POSIX chdir() function. The function changes the
1261 : : * current directory of the process to @path.
1262 : : *
1263 : : * See your C library manual for more details about chdir().
1264 : : *
1265 : : * Returns: 0 on success, -1 if an error occurred.
1266 : : *
1267 : : * Since: 2.8
1268 : : */
1269 : : int
1270 : 39 : g_chdir (const gchar *path)
1271 : : {
1272 : : #ifdef G_OS_WIN32
1273 : : wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
1274 : : int retval;
1275 : : int save_errno;
1276 : :
1277 : : if (wpath == NULL)
1278 : : {
1279 : : errno = EINVAL;
1280 : : return -1;
1281 : : }
1282 : :
1283 : : retval = _wchdir (wpath);
1284 : : save_errno = errno;
1285 : :
1286 : : g_free (wpath);
1287 : :
1288 : : errno = save_errno;
1289 : : return retval;
1290 : : #else
1291 : 39 : return chdir (path);
1292 : : #endif
1293 : : }
1294 : :
1295 : : /**
1296 : : * GStatBuf:
1297 : : *
1298 : : * A type corresponding to the appropriate struct type for the stat()
1299 : : * system call, depending on the platform and/or compiler being used.
1300 : : *
1301 : : * See g_stat() for more information.
1302 : : */
1303 : : /**
1304 : : * g_stat:
1305 : : * @filename: (type filename): a pathname in the GLib file name encoding
1306 : : * (UTF-8 on Windows)
1307 : : * @buf: a pointer to a stat struct, which will be filled with the file
1308 : : * information
1309 : : *
1310 : : * A wrapper for the POSIX stat() function. The stat() function
1311 : : * returns information about a file. On Windows the stat() function in
1312 : : * the C library checks only the FAT-style READONLY attribute and does
1313 : : * not look at the ACL at all. Thus on Windows the protection bits in
1314 : : * the @st_mode field are a fabrication of little use.
1315 : : *
1316 : : * On Windows the Microsoft C libraries have several variants of the
1317 : : * stat struct and stat() function with names like _stat(), _stat32(),
1318 : : * _stat32i64() and _stat64i32(). The one used here is for 32-bit code
1319 : : * the one with 32-bit size and time fields, specifically called _stat32().
1320 : : *
1321 : : * In Microsoft's compiler, by default struct stat means one with
1322 : : * 64-bit time fields while in MinGW struct stat is the legacy one
1323 : : * with 32-bit fields. To hopefully clear up this messs, the gstdio.h
1324 : : * header defines a type #GStatBuf which is the appropriate struct type
1325 : : * depending on the platform and/or compiler being used. On POSIX it
1326 : : * is just struct stat, but note that even on POSIX platforms, stat()
1327 : : * might be a macro.
1328 : : *
1329 : : * See your C library manual for more details about stat().
1330 : : *
1331 : : * Returns: 0 if the information was successfully retrieved,
1332 : : * -1 if an error occurred
1333 : : *
1334 : : * Since: 2.6
1335 : : */
1336 : : int
1337 : 4 : g_stat (const gchar *filename,
1338 : : GStatBuf *buf)
1339 : : {
1340 : : #ifdef G_OS_WIN32
1341 : : GWin32PrivateStat w32_buf;
1342 : : int retval = g_win32_stat_utf8 (filename, &w32_buf);
1343 : :
1344 : : buf->st_dev = w32_buf.st_dev;
1345 : : buf->st_ino = w32_buf.st_ino;
1346 : : buf->st_mode = w32_buf.st_mode;
1347 : : buf->st_nlink = w32_buf.st_nlink;
1348 : : buf->st_uid = w32_buf.st_uid;
1349 : : buf->st_gid = w32_buf.st_gid;
1350 : : buf->st_rdev = w32_buf.st_dev;
1351 : : buf->st_size = w32_buf.st_size;
1352 : : buf->st_atime = w32_buf.st_atim.tv_sec;
1353 : : buf->st_mtime = w32_buf.st_mtim.tv_sec;
1354 : : buf->st_ctime = w32_buf.st_ctim.tv_sec;
1355 : :
1356 : : return retval;
1357 : : #else
1358 : 4 : return stat (filename, buf);
1359 : : #endif
1360 : : }
1361 : :
1362 : : /**
1363 : : * g_lstat:
1364 : : * @filename: (type filename): a pathname in the GLib file name encoding
1365 : : * (UTF-8 on Windows)
1366 : : * @buf: a pointer to a stat struct, which will be filled with the file
1367 : : * information
1368 : : *
1369 : : * A wrapper for the POSIX lstat() function. The lstat() function is
1370 : : * like stat() except that in the case of symbolic links, it returns
1371 : : * information about the symbolic link itself and not the file that it
1372 : : * refers to. If the system does not support symbolic links g_lstat()
1373 : : * is identical to g_stat().
1374 : : *
1375 : : * See your C library manual for more details about lstat().
1376 : : *
1377 : : * Returns: 0 if the information was successfully retrieved,
1378 : : * -1 if an error occurred
1379 : : *
1380 : : * Since: 2.6
1381 : : */
1382 : : int
1383 : 43 : g_lstat (const gchar *filename,
1384 : : GStatBuf *buf)
1385 : : {
1386 : : #ifdef HAVE_LSTAT
1387 : : /* This can't be Win32, so don't do the widechar dance. */
1388 : 43 : return lstat (filename, buf);
1389 : : #elif defined (G_OS_WIN32)
1390 : : GWin32PrivateStat w32_buf;
1391 : : int retval = g_win32_lstat_utf8 (filename, &w32_buf);
1392 : :
1393 : : buf->st_dev = w32_buf.st_dev;
1394 : : buf->st_ino = w32_buf.st_ino;
1395 : : buf->st_mode = w32_buf.st_mode;
1396 : : buf->st_nlink = w32_buf.st_nlink;
1397 : : buf->st_uid = w32_buf.st_uid;
1398 : : buf->st_gid = w32_buf.st_gid;
1399 : : buf->st_rdev = w32_buf.st_dev;
1400 : : buf->st_size = w32_buf.st_size;
1401 : : buf->st_atime = w32_buf.st_atim.tv_sec;
1402 : : buf->st_mtime = w32_buf.st_mtim.tv_sec;
1403 : : buf->st_ctime = w32_buf.st_ctim.tv_sec;
1404 : :
1405 : : return retval;
1406 : : #else
1407 : : return g_stat (filename, buf);
1408 : : #endif
1409 : : }
1410 : :
1411 : : /**
1412 : : * g_unlink:
1413 : : * @filename: (type filename): a pathname in the GLib file name encoding
1414 : : * (UTF-8 on Windows)
1415 : : *
1416 : : * A wrapper for the POSIX unlink() function. The unlink() function
1417 : : * deletes a name from the filesystem. If this was the last link to the
1418 : : * file and no processes have it opened, the diskspace occupied by the
1419 : : * file is freed.
1420 : : *
1421 : : * See your C library manual for more details about unlink(). Note
1422 : : * that on Windows, it is in general not possible to delete files that
1423 : : * are open to some process, or mapped into memory.
1424 : : *
1425 : : * Returns: 0 if the name was successfully deleted, -1 if an error
1426 : : * occurred
1427 : : *
1428 : : * Since: 2.6
1429 : : */
1430 : : int
1431 : 285 : g_unlink (const gchar *filename)
1432 : : {
1433 : : #ifdef G_OS_WIN32
1434 : : wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1435 : : int retval;
1436 : : int save_errno;
1437 : :
1438 : : if (wfilename == NULL)
1439 : : {
1440 : : errno = EINVAL;
1441 : : return -1;
1442 : : }
1443 : :
1444 : : retval = _wunlink (wfilename);
1445 : : save_errno = errno;
1446 : :
1447 : : g_free (wfilename);
1448 : :
1449 : : errno = save_errno;
1450 : : return retval;
1451 : : #else
1452 : 285 : return unlink (filename);
1453 : : #endif
1454 : : }
1455 : :
1456 : : /**
1457 : : * g_remove:
1458 : : * @filename: (type filename): a pathname in the GLib file name encoding
1459 : : * (UTF-8 on Windows)
1460 : : *
1461 : : * A wrapper for the POSIX remove() function. The remove() function
1462 : : * deletes a name from the filesystem.
1463 : : *
1464 : : * See your C library manual for more details about how remove() works
1465 : : * on your system. On Unix, remove() removes also directories, as it
1466 : : * calls unlink() for files and rmdir() for directories. On Windows,
1467 : : * although remove() in the C library only works for files, this
1468 : : * function tries first remove() and then if that fails rmdir(), and
1469 : : * thus works for both files and directories. Note however, that on
1470 : : * Windows, it is in general not possible to remove a file that is
1471 : : * open to some process, or mapped into memory.
1472 : : *
1473 : : * If this function fails on Windows you can't infer too much from the
1474 : : * errno value. rmdir() is tried regardless of what caused remove() to
1475 : : * fail. Any errno value set by remove() will be overwritten by that
1476 : : * set by rmdir().
1477 : : *
1478 : : * Returns: 0 if the file was successfully removed, -1 if an error
1479 : : * occurred
1480 : : *
1481 : : * Since: 2.6
1482 : : */
1483 : : int
1484 : 103 : g_remove (const gchar *filename)
1485 : : {
1486 : : #ifdef G_OS_WIN32
1487 : : wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1488 : : int retval;
1489 : : int save_errno;
1490 : :
1491 : : if (wfilename == NULL)
1492 : : {
1493 : : errno = EINVAL;
1494 : : return -1;
1495 : : }
1496 : :
1497 : : retval = _wremove (wfilename);
1498 : : if (retval == -1)
1499 : : retval = _wrmdir (wfilename);
1500 : : save_errno = errno;
1501 : :
1502 : : g_free (wfilename);
1503 : :
1504 : : errno = save_errno;
1505 : : return retval;
1506 : : #else
1507 : 103 : return remove (filename);
1508 : : #endif
1509 : : }
1510 : :
1511 : : /**
1512 : : * g_rmdir:
1513 : : * @filename: (type filename): a pathname in the GLib file name encoding
1514 : : * (UTF-8 on Windows)
1515 : : *
1516 : : * A wrapper for the POSIX rmdir() function. The rmdir() function
1517 : : * deletes a directory from the filesystem.
1518 : : *
1519 : : * See your C library manual for more details about how rmdir() works
1520 : : * on your system.
1521 : : *
1522 : : * Returns: 0 if the directory was successfully removed, -1 if an error
1523 : : * occurred
1524 : : *
1525 : : * Since: 2.6
1526 : : */
1527 : : int
1528 : 3583 : g_rmdir (const gchar *filename)
1529 : : {
1530 : : #ifdef G_OS_WIN32
1531 : : wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1532 : : int retval;
1533 : : int save_errno;
1534 : :
1535 : : if (wfilename == NULL)
1536 : : {
1537 : : errno = EINVAL;
1538 : : return -1;
1539 : : }
1540 : :
1541 : : retval = _wrmdir (wfilename);
1542 : : save_errno = errno;
1543 : :
1544 : : g_free (wfilename);
1545 : :
1546 : : errno = save_errno;
1547 : : return retval;
1548 : : #else
1549 : 3583 : return rmdir (filename);
1550 : : #endif
1551 : : }
1552 : :
1553 : : /**
1554 : : * g_fopen:
1555 : : * @filename: (type filename): a pathname in the GLib file name encoding
1556 : : * (UTF-8 on Windows)
1557 : : * @mode: a string describing the mode in which the file should be opened
1558 : : *
1559 : : * A wrapper for the stdio `fopen()` function. The `fopen()` function
1560 : : * opens a file and associates a new stream with it.
1561 : : *
1562 : : * Because file descriptors are specific to the C library on Windows,
1563 : : * and a file descriptor is part of the `FILE` struct, the `FILE*` returned
1564 : : * by this function makes sense only to functions in the same C library.
1565 : : * Thus if the GLib-using code uses a different C library than GLib does,
1566 : : * the FILE* returned by this function cannot be passed to C library
1567 : : * functions like `fprintf()` or `fread()`.
1568 : : *
1569 : : * See your C library manual for more details about `fopen()`.
1570 : : *
1571 : : * As `close()` and `fclose()` are part of the C library, this implies that it is
1572 : : * currently impossible to close a file if the application C library and the C library
1573 : : * used by GLib are different. Convenience functions like g_file_set_contents_full()
1574 : : * avoid this problem.
1575 : : *
1576 : : * Since GLib 2.86, the `e` option is supported in @mode on all platforms. On
1577 : : * Unix platforms it will set `O_CLOEXEC` on the opened file descriptor. On
1578 : : * Windows platforms it will be converted to the
1579 : : * [`N` modifier](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fopen-wfopen?view=msvc-170).
1580 : : * It is recommended to set `e` unconditionally, unless you know the returned
1581 : : * file should be shared between this process and a new fork.
1582 : : *
1583 : : * Returns: A `FILE*` if the file was successfully opened, or %NULL if
1584 : : * an error occurred
1585 : : *
1586 : : * Since: 2.6
1587 : : */
1588 : : FILE *
1589 : 43 : g_fopen (const gchar *filename,
1590 : : const gchar *mode)
1591 : : {
1592 : : #ifdef G_OS_WIN32
1593 : : wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1594 : : wchar_t *wmode;
1595 : : FILE *retval;
1596 : : int save_errno;
1597 : :
1598 : : if (wfilename == NULL)
1599 : : {
1600 : : errno = EINVAL;
1601 : : return NULL;
1602 : : }
1603 : :
1604 : : wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
1605 : :
1606 : : if (wmode == NULL)
1607 : : {
1608 : : g_free (wfilename);
1609 : : errno = EINVAL;
1610 : : return NULL;
1611 : : }
1612 : :
1613 : : _g_win32_fix_mode (wmode);
1614 : : retval = _wfopen (wfilename, wmode);
1615 : : save_errno = errno;
1616 : :
1617 : : g_free (wfilename);
1618 : : g_free (wmode);
1619 : :
1620 : : errno = save_errno;
1621 : : return retval;
1622 : : #else
1623 : 43 : return fopen (filename, mode);
1624 : : #endif
1625 : : }
1626 : :
1627 : : /**
1628 : : * g_freopen:
1629 : : * @filename: (type filename): a pathname in the GLib file name encoding
1630 : : * (UTF-8 on Windows)
1631 : : * @mode: a string describing the mode in which the file should be opened
1632 : : * @stream: (nullable): an existing stream which will be reused, or %NULL
1633 : : *
1634 : : * A wrapper for the POSIX freopen() function. The freopen() function
1635 : : * opens a file and associates it with an existing stream.
1636 : : *
1637 : : * See your C library manual for more details about freopen().
1638 : : *
1639 : : * Since GLib 2.86, the `e` option is supported in @mode on all platforms. See
1640 : : * the documentation for [func@GLib.fopen] for more details.
1641 : : *
1642 : : * Returns: A FILE* if the file was successfully opened, or %NULL if
1643 : : * an error occurred.
1644 : : *
1645 : : * Since: 2.6
1646 : : */
1647 : : FILE *
1648 : 0 : g_freopen (const gchar *filename,
1649 : : const gchar *mode,
1650 : : FILE *stream)
1651 : : {
1652 : : #ifdef G_OS_WIN32
1653 : : wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1654 : : wchar_t *wmode;
1655 : : FILE *retval;
1656 : : int save_errno;
1657 : :
1658 : : if (wfilename == NULL)
1659 : : {
1660 : : errno = EINVAL;
1661 : : return NULL;
1662 : : }
1663 : :
1664 : : wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
1665 : :
1666 : : if (wmode == NULL)
1667 : : {
1668 : : g_free (wfilename);
1669 : : errno = EINVAL;
1670 : : return NULL;
1671 : : }
1672 : :
1673 : : _g_win32_fix_mode (wmode);
1674 : : retval = _wfreopen (wfilename, wmode, stream);
1675 : : save_errno = errno;
1676 : :
1677 : : g_free (wfilename);
1678 : : g_free (wmode);
1679 : :
1680 : : errno = save_errno;
1681 : : return retval;
1682 : : #else
1683 : 0 : return freopen (filename, mode, stream);
1684 : : #endif
1685 : : }
1686 : :
1687 : : /**
1688 : : * g_fsync:
1689 : : * @fd: a file descriptor
1690 : : *
1691 : : * A wrapper for the POSIX `fsync()` function. On Windows, `_commit()` will be
1692 : : * used. On macOS, `fcntl(F_FULLFSYNC)` will be used.
1693 : : * The `fsync()` function is used to synchronize a file's in-core
1694 : : * state with that of the disk.
1695 : : *
1696 : : * This wrapper will handle retrying on `EINTR`.
1697 : : *
1698 : : * See the C library manual for more details about fsync().
1699 : : *
1700 : : * Returns: 0 on success, or -1 if an error occurred.
1701 : : * The return value can be used exactly like the return value from fsync().
1702 : : *
1703 : : * Since: 2.64
1704 : : */
1705 : : gint
1706 : 36 : g_fsync (gint fd)
1707 : : {
1708 : : #ifdef G_OS_WIN32
1709 : : return _commit (fd);
1710 : : #elif defined(HAVE_FSYNC) || defined(HAVE_FCNTL_F_FULLFSYNC)
1711 : : int retval;
1712 : : do
1713 : : #ifdef HAVE_FCNTL_F_FULLFSYNC
1714 : : retval = fcntl (fd, F_FULLFSYNC, 0);
1715 : : #else
1716 : 36 : retval = fsync (fd);
1717 : : #endif
1718 : 36 : while (G_UNLIKELY (retval < 0 && errno == EINTR));
1719 : 36 : return retval;
1720 : : #else
1721 : : return 0;
1722 : : #endif
1723 : : }
1724 : :
1725 : : /**
1726 : : * g_utime:
1727 : : * @filename: (type filename): a pathname in the GLib file name encoding
1728 : : * (UTF-8 on Windows)
1729 : : * @utb: a pointer to a struct utimbuf.
1730 : : *
1731 : : * A wrapper for the POSIX utime() function. The utime() function
1732 : : * sets the access and modification timestamps of a file.
1733 : : *
1734 : : * See your C library manual for more details about how utime() works
1735 : : * on your system.
1736 : : *
1737 : : * Returns: 0 if the operation was successful, -1 if an error occurred
1738 : : *
1739 : : * Since: 2.18
1740 : : */
1741 : : int
1742 : 1 : g_utime (const gchar *filename,
1743 : : struct utimbuf *utb)
1744 : : {
1745 : : #ifdef G_OS_WIN32
1746 : : wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
1747 : : int retval;
1748 : : int save_errno;
1749 : :
1750 : : if (wfilename == NULL)
1751 : : {
1752 : : errno = EINVAL;
1753 : : return -1;
1754 : : }
1755 : :
1756 : : retval = _wutime (wfilename, (struct _utimbuf*) utb);
1757 : : save_errno = errno;
1758 : :
1759 : : g_free (wfilename);
1760 : :
1761 : : errno = save_errno;
1762 : : return retval;
1763 : : #else
1764 : 1 : return utime (filename, utb);
1765 : : #endif
1766 : : }
1767 : :
1768 : : /**
1769 : : * g_close:
1770 : : * @fd: A file descriptor
1771 : : * @error: a #GError
1772 : : *
1773 : : * This wraps the close() call. In case of error, %errno will be
1774 : : * preserved, but the error will also be stored as a #GError in @error.
1775 : : * In case of success, %errno is undefined.
1776 : : *
1777 : : * Besides using #GError, there is another major reason to prefer this
1778 : : * function over the call provided by the system; on Unix, it will
1779 : : * attempt to correctly handle %EINTR, which has platform-specific
1780 : : * semantics.
1781 : : *
1782 : : * It is a bug to call this function with an invalid file descriptor.
1783 : : *
1784 : : * On POSIX platforms since GLib 2.76, this function is async-signal safe
1785 : : * if (and only if) @error is %NULL and @fd is a valid open file descriptor.
1786 : : * This makes it safe to call from a signal handler or a #GSpawnChildSetupFunc
1787 : : * under those conditions.
1788 : : * See [`signal(7)`](man:signal(7)) and
1789 : : * [`signal-safety(7)`](man:signal-safety(7)) for more details.
1790 : : *
1791 : : * Returns: %TRUE on success, %FALSE if there was an error.
1792 : : *
1793 : : * Since: 2.36
1794 : : */
1795 : : gboolean
1796 : 9260 : g_close (gint fd,
1797 : : GError **error)
1798 : : {
1799 : : int res;
1800 : :
1801 : : /* Important: if @error is NULL, we must not do anything that is
1802 : : * not async-signal-safe.
1803 : : */
1804 : 9260 : res = close (fd);
1805 : :
1806 : 9260 : if (res == -1)
1807 : : {
1808 : 3 : int errsv = errno;
1809 : :
1810 : 3 : if (errsv == EINTR)
1811 : : {
1812 : : /* Just ignore EINTR for now; a retry loop is the wrong thing to do
1813 : : * on Linux at least. Anyone who wants to add a conditional check
1814 : : * for e.g. HP-UX is welcome to do so later...
1815 : : *
1816 : : * close_func_with_invalid_fds() in gspawn.c has similar logic.
1817 : : *
1818 : : * https://lwn.net/Articles/576478/
1819 : : * http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html
1820 : : * https://bugzilla.gnome.org/show_bug.cgi?id=682819
1821 : : * http://utcc.utoronto.ca/~cks/space/blog/unix/CloseEINTR
1822 : : * https://sites.google.com/site/michaelsafyan/software-engineering/checkforeintrwheninvokingclosethinkagain
1823 : : *
1824 : : * `close$NOCANCEL()` in gstdioprivate.h, on macOS, ensures that the fd is
1825 : : * closed even if it did return EINTR.
1826 : : */
1827 : 0 : return TRUE;
1828 : : }
1829 : :
1830 : 3 : if (error)
1831 : : {
1832 : 0 : g_set_error_literal (error, G_FILE_ERROR,
1833 : 0 : g_file_error_from_errno (errsv),
1834 : : g_strerror (errsv));
1835 : : }
1836 : :
1837 : 3 : if (errsv == EBADF)
1838 : : {
1839 : : /* There is a bug. Fail an assertion. Note that this function is supposed to be
1840 : : * async-signal-safe, but in case an assertion fails, all bets are already off. */
1841 : 3 : if (fd >= 0)
1842 : : {
1843 : : /* Closing an non-negative, invalid file descriptor is a bug. The bug is
1844 : : * not necessarily in the caller of g_close(), but somebody else
1845 : : * might have wrongly closed fd. In any case, there is a serious bug
1846 : : * somewhere. */
1847 : 3 : g_critical ("g_close(fd:%d) failed with EBADF. The tracking of file descriptors got messed up", fd);
1848 : : }
1849 : : else
1850 : : {
1851 : : /* Closing a negative "file descriptor" is less problematic. It's still a nonsensical action
1852 : : * from the caller. Assert against that too. */
1853 : 0 : g_critical ("g_close(fd:%d) failed with EBADF. This is not a valid file descriptor", fd);
1854 : : }
1855 : : }
1856 : :
1857 : 3 : errno = errsv;
1858 : :
1859 : 3 : return FALSE;
1860 : : }
1861 : :
1862 : 9257 : return TRUE;
1863 : : }
1864 : :
1865 : : /**
1866 : : * g_autofd: (skip)
1867 : : *
1868 : : * Macro to add an attribute to a file descriptor variable to ensure
1869 : : * automatic cleanup using g_clear_fd().
1870 : : *
1871 : : * This macro behaves like #g_autofree rather than g_autoptr(): it is
1872 : : * an attribute supplied before the type name, rather than wrapping the
1873 : : * type definition.
1874 : : *
1875 : : * Otherwise, this macro has similar constraints as g_autoptr(): it is
1876 : : * only supported on GCC and clang, and the variable must be initialized
1877 : : * (to either a valid file descriptor or a negative number).
1878 : : *
1879 : : * Using this macro is async-signal-safe if the constraints described above
1880 : : * are met, so it can be used in a signal handler or after `fork()`.
1881 : : *
1882 : : * Any error from closing the file descriptor when it goes out of scope
1883 : : * is ignored. Use g_clear_fd() if error-checking is required.
1884 : : *
1885 : : * |[
1886 : : * gboolean
1887 : : * operate_on_fds (GError **error)
1888 : : * {
1889 : : * g_autofd int fd1 = open_a_fd (..., error);
1890 : : * g_autofd int fd2 = -1;
1891 : : *
1892 : : * // it is safe to return early here, nothing will be closed
1893 : : * if (fd1 < 0)
1894 : : * return FALSE;
1895 : : *
1896 : : * fd2 = open_a_fd (..., error);
1897 : : *
1898 : : * // fd1 will be closed automatically if we return here
1899 : : * if (fd2 < 0)
1900 : : * return FALSE;
1901 : : *
1902 : : * // fd1 and fd2 will be closed automatically if we return here
1903 : : * if (!do_something_useful (fd1, fd2, error))
1904 : : * return FALSE;
1905 : : *
1906 : : * // fd2 will be closed automatically if we return here
1907 : : * if (!g_clear_fd (&fd1, error))
1908 : : * return FALSE;
1909 : : *
1910 : : * // fd2 will be automatically closed here if still open
1911 : : * return TRUE;
1912 : : * }
1913 : : * ]|
1914 : : *
1915 : : * Since: 2.76
1916 : : */
|