ftp://ftp.redhat.com/pub/redhat/linux/rawhide/SRPMS/SRPMS/gnome-vfs2-2.3.8-1.src.rpm
[gnome-vfs-httpcaptive.git] / programs / gnomevfs-mkdir.c
1 /* gnomevfs-mkdir.c - Test for mkdir() for gnome-vfs
2
3    Copyright (C) 2003, Red Hat
4
5    The Gnome Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    The Gnome 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    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with the Gnome Library; see the file COPYING.LIB.  If not,
17    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA.
19
20    Author: Bastien Nocera <hadess@hadess.net>
21 */
22
23 #include <libgnomevfs/gnome-vfs.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 static GnomeVFSResult
28 make_directory_with_parents_for_uri (GnomeVFSURI * uri,
29                 guint perm)
30 {
31         GnomeVFSResult result;
32         GnomeVFSURI *parent, *work_uri;
33         GList *list = NULL;
34
35         result = gnome_vfs_make_directory_for_uri (uri, perm);
36         if (result == GNOME_VFS_OK || result != GNOME_VFS_ERROR_NOT_FOUND)
37                 return result;
38
39         work_uri = uri;
40
41         while (result == GNOME_VFS_ERROR_NOT_FOUND) {
42                 parent = gnome_vfs_uri_get_parent (work_uri);
43                 result = gnome_vfs_make_directory_for_uri (parent, perm);
44
45                 if (result == GNOME_VFS_ERROR_NOT_FOUND)
46                         list = g_list_prepend (list, parent);
47                 work_uri = parent;
48         }
49
50         if (result != GNOME_VFS_OK) {
51                 /* Clean up */
52                 while (list != NULL) {
53                         gnome_vfs_uri_unref ((GnomeVFSURI *) list->data);
54                         list = g_list_remove (list, list->data);
55                 }
56         }
57
58         while (result == GNOME_VFS_OK && list != NULL) {
59                 result = gnome_vfs_make_directory_for_uri
60                     ((GnomeVFSURI *) list->data, perm);
61
62                 gnome_vfs_uri_unref ((GnomeVFSURI *) list->data);
63                 list = g_list_remove (list, list->data);
64         }
65
66         result = gnome_vfs_make_directory_for_uri (uri, perm);
67         return result;
68 }
69
70 static GnomeVFSResult
71 make_directory_with_parents (const gchar * text_uri, guint perm)
72 {
73         GnomeVFSURI *uri;
74         GnomeVFSResult result;
75
76         uri = gnome_vfs_uri_new (text_uri);
77         result = make_directory_with_parents_for_uri (uri, perm);
78         gnome_vfs_uri_unref (uri);
79
80         return result;
81 }
82
83 int
84 main (int argc, char *argv[])
85 {
86         gchar *directory;
87         GnomeVFSResult result;
88         gboolean with_parents;
89
90         gnome_vfs_init ();
91
92         if (argc > 1) {
93                 if (strcmp (argv[1], "-p") == 0) {
94                         directory = argv[2];
95                         with_parents = TRUE;
96                 } else {
97                         directory = argv[1];
98                         with_parents = FALSE;
99                 }
100         } else {
101                 fprintf (stderr, "Usage: %s [-p] <dir>\n", argv[0]);
102                 fprintf (stderr, "   -p: Create parents of the directory if needed\n");
103                 return 0;
104         }
105
106         if (with_parents) {
107                 result = make_directory_with_parents (argv[1],
108                                 GNOME_VFS_PERM_USER_ALL
109                                 | GNOME_VFS_PERM_GROUP_ALL
110                                 | GNOME_VFS_PERM_OTHER_READ);
111         } else {
112                 result = gnome_vfs_make_directory (argv[1],
113                                 GNOME_VFS_PERM_USER_ALL
114                                 | GNOME_VFS_PERM_GROUP_ALL
115                                 | GNOME_VFS_PERM_OTHER_READ);
116         }
117
118         if (result != GNOME_VFS_OK) {
119                 g_print ("Error making directory %s\nReason: %s\n",
120                                 directory,
121                                 gnome_vfs_result_to_string (result));
122                 return 0;
123         }
124
125         gnome_vfs_shutdown ();
126         return 0;
127 }