Add some more cases to the app-id unit tests
[glib.git] / glib / gstdio.c
blob831ae98e04be80ad3074175e6d971e7f524014c1
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 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/>.
19 #include "config.h"
20 #include "glibconfig.h"
22 #define G_STDIO_NO_WRAP_ON_UNIX
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
28 #ifdef G_OS_UNIX
29 #include <unistd.h>
30 #endif
32 #ifdef G_OS_WIN32
33 #include <windows.h>
34 #include <errno.h>
35 #include <wchar.h>
36 #include <direct.h>
37 #include <io.h>
38 #include <sys/utime.h>
39 #else
40 #include <utime.h>
41 #include <errno.h>
42 #endif
44 #include "gstdio.h"
47 #if !defined (G_OS_UNIX) && !defined (G_OS_WIN32)
48 #error Please port this to your operating system
49 #endif
51 #if defined (_MSC_VER) && !defined(_WIN64)
52 #undef _wstat
53 #define _wstat _wstat32
54 #endif
56 /**
57 * g_access:
58 * @filename: (type filename): a pathname in the GLib file name encoding
59 * (UTF-8 on Windows)
60 * @mode: as in access()
62 * A wrapper for the POSIX access() function. This function is used to
63 * test a pathname for one or several of read, write or execute
64 * permissions, or just existence.
66 * On Windows, the file protection mechanism is not at all POSIX-like,
67 * and the underlying function in the C library only checks the
68 * FAT-style READONLY attribute, and does not look at the ACL of a
69 * file at all. This function is this in practise almost useless on
70 * Windows. Software that needs to handle file permissions on Windows
71 * more exactly should use the Win32 API.
73 * See your C library manual for more details about access().
75 * Returns: zero if the pathname refers to an existing file system
76 * object that has all the tested permissions, or -1 otherwise
77 * or on error.
79 * Since: 2.8
81 int
82 g_access (const gchar *filename,
83 int mode)
85 #ifdef G_OS_WIN32
86 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
87 int retval;
88 int save_errno;
90 if (wfilename == NULL)
92 errno = EINVAL;
93 return -1;
96 #ifndef X_OK
97 #define X_OK 1
98 #endif
100 retval = _waccess (wfilename, mode & ~X_OK);
101 save_errno = errno;
103 g_free (wfilename);
105 errno = save_errno;
106 return retval;
107 #else
108 return access (filename, mode);
109 #endif
113 * g_chmod:
114 * @filename: (type filename): a pathname in the GLib file name encoding
115 * (UTF-8 on Windows)
116 * @mode: as in chmod()
118 * A wrapper for the POSIX chmod() function. The chmod() function is
119 * used to set the permissions of a file system object.
121 * On Windows the file protection mechanism is not at all POSIX-like,
122 * and the underlying chmod() function in the C library just sets or
123 * clears the FAT-style READONLY attribute. It does not touch any
124 * ACL. Software that needs to manage file permissions on Windows
125 * exactly should use the Win32 API.
127 * See your C library manual for more details about chmod().
129 * Returns: 0 if the operation succeeded, -1 on error
131 * Since: 2.8
134 g_chmod (const gchar *filename,
135 int mode)
137 #ifdef G_OS_WIN32
138 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
139 int retval;
140 int save_errno;
142 if (wfilename == NULL)
144 errno = EINVAL;
145 return -1;
148 retval = _wchmod (wfilename, mode);
149 save_errno = errno;
151 g_free (wfilename);
153 errno = save_errno;
154 return retval;
155 #else
156 return chmod (filename, mode);
157 #endif
160 * g_open:
161 * @filename: (type filename): a pathname in the GLib file name encoding
162 * (UTF-8 on Windows)
163 * @flags: as in open()
164 * @mode: as in open()
166 * A wrapper for the POSIX open() function. The open() function is
167 * used to convert a pathname into a file descriptor.
169 * On POSIX systems file descriptors are implemented by the operating
170 * system. On Windows, it's the C library that implements open() and
171 * file descriptors. The actual Win32 API for opening files is quite
172 * different, see MSDN documentation for CreateFile(). The Win32 API
173 * uses file handles, which are more randomish integers, not small
174 * integers like file descriptors.
176 * Because file descriptors are specific to the C library on Windows,
177 * the file descriptor returned by this function makes sense only to
178 * functions in the same C library. Thus if the GLib-using code uses a
179 * different C library than GLib does, the file descriptor returned by
180 * this function cannot be passed to C library functions like write()
181 * or read().
183 * See your C library manual for more details about open().
185 * Returns: a new file descriptor, or -1 if an error occurred.
186 * The return value can be used exactly like the return value
187 * from open().
189 * Since: 2.6
192 g_open (const gchar *filename,
193 int flags,
194 int mode)
196 #ifdef G_OS_WIN32
197 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
198 int retval;
199 int save_errno;
201 if (wfilename == NULL)
203 errno = EINVAL;
204 return -1;
207 retval = _wopen (wfilename, flags, mode);
208 save_errno = errno;
210 g_free (wfilename);
212 errno = save_errno;
213 return retval;
214 #else
215 int fd;
217 fd = open (filename, flags, mode);
218 while (G_UNLIKELY (fd == -1 && errno == EINTR));
219 return fd;
220 #endif
224 * g_creat:
225 * @filename: (type filename): a pathname in the GLib file name encoding
226 * (UTF-8 on Windows)
227 * @mode: as in creat()
229 * A wrapper for the POSIX creat() function. The creat() function is
230 * used to convert a pathname into a file descriptor, creating a file
231 * if necessary.
233 * On POSIX systems file descriptors are implemented by the operating
234 * system. On Windows, it's the C library that implements creat() and
235 * file descriptors. The actual Windows API for opening files is
236 * different, see MSDN documentation for CreateFile(). The Win32 API
237 * uses file handles, which are more randomish integers, not small
238 * integers like file descriptors.
240 * Because file descriptors are specific to the C library on Windows,
241 * the file descriptor returned by this function makes sense only to
242 * functions in the same C library. Thus if the GLib-using code uses a
243 * different C library than GLib does, the file descriptor returned by
244 * this function cannot be passed to C library functions like write()
245 * or read().
247 * See your C library manual for more details about creat().
249 * Returns: a new file descriptor, or -1 if an error occurred.
250 * The return value can be used exactly like the return value
251 * from creat().
253 * Since: 2.8
256 g_creat (const gchar *filename,
257 int mode)
259 #ifdef G_OS_WIN32
260 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
261 int retval;
262 int save_errno;
264 if (wfilename == NULL)
266 errno = EINVAL;
267 return -1;
270 retval = _wcreat (wfilename, mode);
271 save_errno = errno;
273 g_free (wfilename);
275 errno = save_errno;
276 return retval;
277 #else
278 return creat (filename, mode);
279 #endif
283 * g_rename:
284 * @oldfilename: (type filename): a pathname in the GLib file name encoding
285 * (UTF-8 on Windows)
286 * @newfilename: (type filename): a pathname in the GLib file name encoding
288 * A wrapper for the POSIX rename() function. The rename() function
289 * renames a file, moving it between directories if required.
291 * See your C library manual for more details about how rename() works
292 * on your system. It is not possible in general on Windows to rename
293 * a file that is open to some process.
295 * Returns: 0 if the renaming succeeded, -1 if an error occurred
297 * Since: 2.6
300 g_rename (const gchar *oldfilename,
301 const gchar *newfilename)
303 #ifdef G_OS_WIN32
304 wchar_t *woldfilename = g_utf8_to_utf16 (oldfilename, -1, NULL, NULL, NULL);
305 wchar_t *wnewfilename;
306 int retval;
307 int save_errno = 0;
309 if (woldfilename == NULL)
311 errno = EINVAL;
312 return -1;
315 wnewfilename = g_utf8_to_utf16 (newfilename, -1, NULL, NULL, NULL);
317 if (wnewfilename == NULL)
319 g_free (woldfilename);
320 errno = EINVAL;
321 return -1;
324 if (MoveFileExW (woldfilename, wnewfilename, MOVEFILE_REPLACE_EXISTING))
325 retval = 0;
326 else
328 retval = -1;
329 switch (GetLastError ())
331 #define CASE(a,b) case ERROR_##a: save_errno = b; break
332 CASE (FILE_NOT_FOUND, ENOENT);
333 CASE (PATH_NOT_FOUND, ENOENT);
334 CASE (ACCESS_DENIED, EACCES);
335 CASE (NOT_SAME_DEVICE, EXDEV);
336 CASE (LOCK_VIOLATION, EACCES);
337 CASE (SHARING_VIOLATION, EACCES);
338 CASE (FILE_EXISTS, EEXIST);
339 CASE (ALREADY_EXISTS, EEXIST);
340 #undef CASE
341 default: save_errno = EIO;
345 g_free (woldfilename);
346 g_free (wnewfilename);
348 errno = save_errno;
349 return retval;
350 #else
351 return rename (oldfilename, newfilename);
352 #endif
356 * g_mkdir:
357 * @filename: (type filename): a pathname in the GLib file name encoding
358 * (UTF-8 on Windows)
359 * @mode: permissions to use for the newly created directory
361 * A wrapper for the POSIX mkdir() function. The mkdir() function
362 * attempts to create a directory with the given name and permissions.
363 * The mode argument is ignored on Windows.
365 * See your C library manual for more details about mkdir().
367 * Returns: 0 if the directory was successfully created, -1 if an error
368 * occurred
370 * Since: 2.6
373 g_mkdir (const gchar *filename,
374 int mode)
376 #ifdef G_OS_WIN32
377 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
378 int retval;
379 int save_errno;
381 if (wfilename == NULL)
383 errno = EINVAL;
384 return -1;
387 retval = _wmkdir (wfilename);
388 save_errno = errno;
390 g_free (wfilename);
392 errno = save_errno;
393 return retval;
394 #else
395 return mkdir (filename, mode);
396 #endif
400 * g_chdir:
401 * @path: (type filename): a pathname in the GLib file name encoding
402 * (UTF-8 on Windows)
404 * A wrapper for the POSIX chdir() function. The function changes the
405 * current directory of the process to @path.
407 * See your C library manual for more details about chdir().
409 * Returns: 0 on success, -1 if an error occurred.
411 * Since: 2.8
414 g_chdir (const gchar *path)
416 #ifdef G_OS_WIN32
417 wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
418 int retval;
419 int save_errno;
421 if (wpath == NULL)
423 errno = EINVAL;
424 return -1;
427 retval = _wchdir (wpath);
428 save_errno = errno;
430 g_free (wpath);
432 errno = save_errno;
433 return retval;
434 #else
435 return chdir (path);
436 #endif
440 * GStatBuf:
442 * A type corresponding to the appropriate struct type for the stat()
443 * system call, depending on the platform and/or compiler being used.
445 * See g_stat() for more information.
448 * g_stat:
449 * @filename: (type filename): a pathname in the GLib file name encoding
450 * (UTF-8 on Windows)
451 * @buf: a pointer to a stat struct, which will be filled with the file
452 * information
454 * A wrapper for the POSIX stat() function. The stat() function
455 * returns information about a file. On Windows the stat() function in
456 * the C library checks only the FAT-style READONLY attribute and does
457 * not look at the ACL at all. Thus on Windows the protection bits in
458 * the @st_mode field are a fabrication of little use.
460 * On Windows the Microsoft C libraries have several variants of the
461 * stat struct and stat() function with names like _stat(), _stat32(),
462 * _stat32i64() and _stat64i32(). The one used here is for 32-bit code
463 * the one with 32-bit size and time fields, specifically called _stat32().
465 * In Microsoft's compiler, by default struct stat means one with
466 * 64-bit time fields while in MinGW struct stat is the legacy one
467 * with 32-bit fields. To hopefully clear up this messs, the gstdio.h
468 * header defines a type #GStatBuf which is the appropriate struct type
469 * depending on the platform and/or compiler being used. On POSIX it
470 * is just struct stat, but note that even on POSIX platforms, stat()
471 * might be a macro.
473 * See your C library manual for more details about stat().
475 * Returns: 0 if the information was successfully retrieved,
476 * -1 if an error occurred
478 * Since: 2.6
481 g_stat (const gchar *filename,
482 GStatBuf *buf)
484 #ifdef G_OS_WIN32
485 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
486 int retval;
487 int save_errno;
488 int len;
490 if (wfilename == NULL)
492 errno = EINVAL;
493 return -1;
496 len = wcslen (wfilename);
497 while (len > 0 && G_IS_DIR_SEPARATOR (wfilename[len-1]))
498 len--;
499 if (len > 0 &&
500 (!g_path_is_absolute (filename) || len > g_path_skip_root (filename) - filename))
501 wfilename[len] = '\0';
503 retval = _wstat (wfilename, buf);
504 save_errno = errno;
506 g_free (wfilename);
508 errno = save_errno;
509 return retval;
510 #else
511 return stat (filename, buf);
512 #endif
516 * g_lstat:
517 * @filename: (type filename): a pathname in the GLib file name encoding
518 * (UTF-8 on Windows)
519 * @buf: a pointer to a stat struct, which will be filled with the file
520 * information
522 * A wrapper for the POSIX lstat() function. The lstat() function is
523 * like stat() except that in the case of symbolic links, it returns
524 * information about the symbolic link itself and not the file that it
525 * refers to. If the system does not support symbolic links g_lstat()
526 * is identical to g_stat().
528 * See your C library manual for more details about lstat().
530 * Returns: 0 if the information was successfully retrieved,
531 * -1 if an error occurred
533 * Since: 2.6
536 g_lstat (const gchar *filename,
537 GStatBuf *buf)
539 #ifdef HAVE_LSTAT
540 /* This can't be Win32, so don't do the widechar dance. */
541 return lstat (filename, buf);
542 #else
543 return g_stat (filename, buf);
544 #endif
548 * g_unlink:
549 * @filename: (type filename): a pathname in the GLib file name encoding
550 * (UTF-8 on Windows)
552 * A wrapper for the POSIX unlink() function. The unlink() function
553 * deletes a name from the filesystem. If this was the last link to the
554 * file and no processes have it opened, the diskspace occupied by the
555 * file is freed.
557 * See your C library manual for more details about unlink(). Note
558 * that on Windows, it is in general not possible to delete files that
559 * are open to some process, or mapped into memory.
561 * Returns: 0 if the name was successfully deleted, -1 if an error
562 * occurred
564 * Since: 2.6
567 g_unlink (const gchar *filename)
569 #ifdef G_OS_WIN32
570 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
571 int retval;
572 int save_errno;
574 if (wfilename == NULL)
576 errno = EINVAL;
577 return -1;
580 retval = _wunlink (wfilename);
581 save_errno = errno;
583 g_free (wfilename);
585 errno = save_errno;
586 return retval;
587 #else
588 return unlink (filename);
589 #endif
593 * g_remove:
594 * @filename: (type filename): a pathname in the GLib file name encoding
595 * (UTF-8 on Windows)
597 * A wrapper for the POSIX remove() function. The remove() function
598 * deletes a name from the filesystem.
600 * See your C library manual for more details about how remove() works
601 * on your system. On Unix, remove() removes also directories, as it
602 * calls unlink() for files and rmdir() for directories. On Windows,
603 * although remove() in the C library only works for files, this
604 * function tries first remove() and then if that fails rmdir(), and
605 * thus works for both files and directories. Note however, that on
606 * Windows, it is in general not possible to remove a file that is
607 * open to some process, or mapped into memory.
609 * If this function fails on Windows you can't infer too much from the
610 * errno value. rmdir() is tried regardless of what caused remove() to
611 * fail. Any errno value set by remove() will be overwritten by that
612 * set by rmdir().
614 * Returns: 0 if the file was successfully removed, -1 if an error
615 * occurred
617 * Since: 2.6
620 g_remove (const gchar *filename)
622 #ifdef G_OS_WIN32
623 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
624 int retval;
625 int save_errno;
627 if (wfilename == NULL)
629 errno = EINVAL;
630 return -1;
633 retval = _wremove (wfilename);
634 if (retval == -1)
635 retval = _wrmdir (wfilename);
636 save_errno = errno;
638 g_free (wfilename);
640 errno = save_errno;
641 return retval;
642 #else
643 return remove (filename);
644 #endif
648 * g_rmdir:
649 * @filename: (type filename): a pathname in the GLib file name encoding
650 * (UTF-8 on Windows)
652 * A wrapper for the POSIX rmdir() function. The rmdir() function
653 * deletes a directory from the filesystem.
655 * See your C library manual for more details about how rmdir() works
656 * on your system.
658 * Returns: 0 if the directory was successfully removed, -1 if an error
659 * occurred
661 * Since: 2.6
664 g_rmdir (const gchar *filename)
666 #ifdef G_OS_WIN32
667 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
668 int retval;
669 int save_errno;
671 if (wfilename == NULL)
673 errno = EINVAL;
674 return -1;
677 retval = _wrmdir (wfilename);
678 save_errno = errno;
680 g_free (wfilename);
682 errno = save_errno;
683 return retval;
684 #else
685 return rmdir (filename);
686 #endif
690 * g_fopen:
691 * @filename: (type filename): a pathname in the GLib file name encoding
692 * (UTF-8 on Windows)
693 * @mode: a string describing the mode in which the file should be opened
695 * A wrapper for the stdio fopen() function. The fopen() function
696 * opens a file and associates a new stream with it.
698 * Because file descriptors are specific to the C library on Windows,
699 * and a file descriptor is part of the FILE struct, the FILE* returned
700 * by this function makes sense only to functions in the same C library.
701 * Thus if the GLib-using code uses a different C library than GLib does,
702 * the FILE* returned by this function cannot be passed to C library
703 * functions like fprintf() or fread().
705 * See your C library manual for more details about fopen().
707 * Returns: A FILE* if the file was successfully opened, or %NULL if
708 * an error occurred
710 * Since: 2.6
712 FILE *
713 g_fopen (const gchar *filename,
714 const gchar *mode)
716 #ifdef G_OS_WIN32
717 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
718 wchar_t *wmode;
719 FILE *retval;
720 int save_errno;
722 if (wfilename == NULL)
724 errno = EINVAL;
725 return NULL;
728 wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
730 if (wmode == NULL)
732 g_free (wfilename);
733 errno = EINVAL;
734 return NULL;
737 retval = _wfopen (wfilename, wmode);
738 save_errno = errno;
740 g_free (wfilename);
741 g_free (wmode);
743 errno = save_errno;
744 return retval;
745 #else
746 return fopen (filename, mode);
747 #endif
751 * g_freopen:
752 * @filename: (type filename): a pathname in the GLib file name encoding
753 * (UTF-8 on Windows)
754 * @mode: a string describing the mode in which the file should be opened
755 * @stream: (nullable): an existing stream which will be reused, or %NULL
757 * A wrapper for the POSIX freopen() function. The freopen() function
758 * opens a file and associates it with an existing stream.
760 * See your C library manual for more details about freopen().
762 * Returns: A FILE* if the file was successfully opened, or %NULL if
763 * an error occurred.
765 * Since: 2.6
767 FILE *
768 g_freopen (const gchar *filename,
769 const gchar *mode,
770 FILE *stream)
772 #ifdef G_OS_WIN32
773 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
774 wchar_t *wmode;
775 FILE *retval;
776 int save_errno;
778 if (wfilename == NULL)
780 errno = EINVAL;
781 return NULL;
784 wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
786 if (wmode == NULL)
788 g_free (wfilename);
789 errno = EINVAL;
790 return NULL;
793 retval = _wfreopen (wfilename, wmode, stream);
794 save_errno = errno;
796 g_free (wfilename);
797 g_free (wmode);
799 errno = save_errno;
800 return retval;
801 #else
802 return freopen (filename, mode, stream);
803 #endif
807 * g_utime:
808 * @filename: (type filename): a pathname in the GLib file name encoding
809 * (UTF-8 on Windows)
810 * @utb: a pointer to a struct utimbuf.
812 * A wrapper for the POSIX utime() function. The utime() function
813 * sets the access and modification timestamps of a file.
815 * See your C library manual for more details about how utime() works
816 * on your system.
818 * Returns: 0 if the operation was successful, -1 if an error occurred
820 * Since: 2.18
823 g_utime (const gchar *filename,
824 struct utimbuf *utb)
826 #ifdef G_OS_WIN32
827 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
828 int retval;
829 int save_errno;
831 if (wfilename == NULL)
833 errno = EINVAL;
834 return -1;
837 retval = _wutime (wfilename, (struct _utimbuf*) utb);
838 save_errno = errno;
840 g_free (wfilename);
842 errno = save_errno;
843 return retval;
844 #else
845 return utime (filename, utb);
846 #endif
850 * g_close:
851 * @fd: A file descriptor
852 * @error: a #GError
854 * This wraps the close() call; in case of error, %errno will be
855 * preserved, but the error will also be stored as a #GError in @error.
857 * Besides using #GError, there is another major reason to prefer this
858 * function over the call provided by the system; on Unix, it will
859 * attempt to correctly handle %EINTR, which has platform-specific
860 * semantics.
862 * Returns: %TRUE on success, %FALSE if there was an error.
864 * Since: 2.36
866 gboolean
867 g_close (gint fd,
868 GError **error)
870 int res;
871 res = close (fd);
872 /* Just ignore EINTR for now; a retry loop is the wrong thing to do
873 * on Linux at least. Anyone who wants to add a conditional check
874 * for e.g. HP-UX is welcome to do so later...
876 * http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html
877 * https://bugzilla.gnome.org/show_bug.cgi?id=682819
878 * http://utcc.utoronto.ca/~cks/space/blog/unix/CloseEINTR
879 * https://sites.google.com/site/michaelsafyan/software-engineering/checkforeintrwheninvokingclosethinkagain
881 if (G_UNLIKELY (res == -1 && errno == EINTR))
882 return TRUE;
883 else if (res == -1)
885 int errsv = errno;
886 g_set_error_literal (error, G_FILE_ERROR,
887 g_file_error_from_errno (errsv),
888 g_strerror (errsv));
889 errno = errsv;
890 return FALSE;
892 return TRUE;