Meson: Remove hack that got fixed a while ago
[glib.git] / gio / tests / g-file-info-filesystem-readonly.c
blob123dcd8a0e8e85ffc5de03bf3738e14bec4ce4a0
1 /* Testcase for bug in GIO function g_file_query_filesystem_info()
2 * Author: Nelson Benítez León
4 * This work is provided "as is"; redistribution and modification
5 * in whole or in part, in any medium, physical or electronic is
6 * permitted without restriction.
8 * This work is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * In no event shall the authors or contributors be liable for any
13 * direct, indirect, incidental, special, exemplary, or consequential
14 * damages (including, but not limited to, procurement of substitute
15 * goods or services; loss of use, data, or profits; or business
16 * interruption) however caused and on any theory of liability, whether
17 * in contract, strict liability, or tort (including negligence or
18 * otherwise) arising in any way out of the use of this software, even
19 * if advised of the possibility of such damage.
22 #include <glib.h>
23 #include <glib/gstdio.h>
24 #include <gio/gio.h>
25 #include <gio/gunixmounts.h>
27 static void
28 test_filesystem_readonly (gconstpointer with_mount_monitor)
30 GFileInfo *file_info;
31 GFile *mounted_file;
32 GUnixMountMonitor *mount_monitor = NULL;
33 gchar *bindfs, *fusermount;
34 gchar *command_mount, *command_mount_ro, *command_umount;
35 gchar *curdir, *dir_to_mount, *dir_mountpoint;
36 gchar *file_in_mount, *file_in_mountpoint;
38 /* installed by package 'bindfs' in Fedora */
39 bindfs = g_find_program_in_path ("bindfs");
41 /* installed by package 'fuse' in Fedora */
42 fusermount = g_find_program_in_path ("fusermount");
44 if (bindfs == NULL || fusermount == NULL)
46 /* We need these because "mount --bind" requires root privileges */
47 g_test_skip ("'bindfs' and 'fusermount' commands are needed to run this test");
48 g_free (fusermount);
49 g_free (bindfs);
50 return;
53 curdir = g_get_current_dir ();
54 dir_to_mount = g_strdup_printf ("%s/dir_bindfs_to_mount", curdir);
55 file_in_mount = g_strdup_printf ("%s/example.txt", dir_to_mount);
56 dir_mountpoint = g_strdup_printf ("%s/dir_bindfs_mountpoint", curdir);
58 g_mkdir (dir_to_mount, 0777);
59 g_mkdir (dir_mountpoint, 0777);
60 if (! g_file_set_contents (file_in_mount, "Example", -1, NULL))
62 g_test_skip ("Failed to create file needed to proceed further with the test");
63 return;
66 if (with_mount_monitor)
67 mount_monitor = g_unix_mount_monitor_get ();
69 /* Use bindfs, which does not need root privileges, to mount the contents of one dir
70 * into another dir (and do the mount as readonly as per passed '-o ro' option) */
71 command_mount_ro = g_strdup_printf ("%s -n -o ro '%s' '%s'", bindfs, dir_to_mount, dir_mountpoint);
72 g_spawn_command_line_sync (command_mount_ro, NULL, NULL, NULL, NULL);
74 /* Let's check now, that the file is in indeed in a readonly filesystem */
75 file_in_mountpoint = g_strdup_printf ("%s/example.txt", dir_mountpoint);
76 mounted_file = g_file_new_for_path (file_in_mountpoint);
78 if (with_mount_monitor)
80 /* Let UnixMountMonitor process its 'mounts-changed'
81 * signal triggered by mount operation above */
82 while (g_main_context_iteration (NULL, FALSE));
85 file_info = g_file_query_filesystem_info (mounted_file,
86 G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, NULL, NULL);
87 if (! g_file_info_get_attribute_boolean (file_info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY))
89 g_test_skip ("Failed to create readonly file needed to proceed further with the test");
90 return;
93 /* Now we unmount, and mount again but this time rw (not readonly) */
94 command_umount = g_strdup_printf ("%s -u '%s'", fusermount, dir_mountpoint);
95 g_spawn_command_line_sync (command_umount, NULL, NULL, NULL, NULL);
96 command_mount = g_strdup_printf ("%s -n '%s' '%s'", bindfs, dir_to_mount, dir_mountpoint);
97 g_spawn_command_line_sync (command_mount, NULL, NULL, NULL, NULL);
99 if (with_mount_monitor)
101 /* Let UnixMountMonitor process its 'mounts-changed' signal
102 * triggered by mount/umount operations above */
103 while (g_main_context_iteration (NULL, FALSE));
106 /* Now let's test if GIO will report the new filesystem state */
107 g_clear_object (&file_info);
108 g_clear_object (&mounted_file);
109 mounted_file = g_file_new_for_path (file_in_mountpoint);
110 file_info = g_file_query_filesystem_info (mounted_file,
111 G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, NULL, NULL);
113 if (g_file_info_get_attribute_boolean (file_info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY))
115 /* ¡¡ GIO still reports filesystem as being Readonly !!
116 * Let's check if that's true by trying to write to file */
117 GFileOutputStream *write_stream;
118 write_stream = g_file_append_to (mounted_file, G_FILE_CREATE_NONE, NULL, NULL);
119 if (write_stream != NULL)
121 /* The file has been opened for writing without error, so ¡¡ GIO IS WRONG !! */
122 g_object_unref (write_stream);
123 g_test_fail (); /* Marking test as FAILED */
127 /* Clean up */
128 g_clear_object (&mount_monitor);
129 g_clear_object (&file_info);
130 g_clear_object (&mounted_file);
131 g_spawn_command_line_sync (command_umount, NULL, NULL, NULL, NULL); /* unmount */
133 g_remove (file_in_mount);
134 g_remove (dir_to_mount);
135 g_remove (dir_mountpoint);
137 g_free (bindfs);
138 g_free (fusermount);
139 g_free (curdir);
140 g_free (dir_to_mount);
141 g_free (dir_mountpoint);
142 g_free (command_mount);
143 g_free (command_mount_ro);
144 g_free (command_umount);
145 g_free (file_in_mount);
146 g_free (file_in_mountpoint);
150 main (int argc, char *argv[])
152 /* To avoid unnecessary D-Bus calls, see http://goo.gl/ir56j2 */
153 g_setenv ("GIO_USE_VFS", "local", FALSE);
155 g_test_init (&argc, &argv, NULL);
157 g_test_bug_base ("http://bugzilla.gnome.org/");
158 g_test_bug ("787731");
160 g_test_add_data_func ("/g-file-info-filesystem-readonly/test-fs-ro",
161 GINT_TO_POINTER (FALSE), test_filesystem_readonly);
163 /* This second test is using a running GUnixMountMonitor, so the calls to:
164 * g_unix_mount_get(&time_read) - To fill the time_read parameter
165 * g_unix_mounts_changed_since()
167 * made from inside g_file_query_filesystem_info() will use the mount_poller_time
168 * from the monitoring of /proc/self/mountinfo , while in the previous test new
169 * created timestamps are returned from those g_unix_mount* functions. */
170 g_test_add_data_func ("/g-file-info-filesystem-readonly/test-fs-ro-with-mount-monitor",
171 GINT_TO_POINTER (TRUE), test_filesystem_readonly);
173 return g_test_run ();