aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-path.c
blob: 089ee03842dba8205a58e2deede396300b3767bc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-path.c
 *
 * Copyright (C) 2001 Ximian, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>

#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <glib.h>

#include "e-path.h"

#define SUBFOLDER_DIR_NAME     "subfolders"
#define SUBFOLDER_DIR_NAME_LEN 10

/**
 * e_path_to_physical:
 * @prefix: a prefix to prepend to the path, or %NULL
 * @path: the virtual path to convert to a filesystem path.
 *
 * This converts the "virtual" path @path into an expanded form that
 * allows a given name to refer to both a file and a directory. The
 * expanded path will have a "subfolders" directory inserted between
 * each path component. If the path ends with "/", the returned
 * physical path will end with "/subfolders"
 *
 * If @prefix is non-%NULL, it will be prepended to the returned path.
 *
 * Return value: the expanded path
 **/
char *
e_path_to_physical (const char *prefix, const char *vpath)
{
    const char *p, *newp;
    char *dp;
    char *ppath;
    int ppath_len;
    int prefix_len;

    while (*vpath == '/')
        vpath++;
    if (!prefix)
        prefix = "";

    /* Calculate the length of the real path. */
    ppath_len = strlen (vpath);
    ppath_len++;    /* For the ending zero.  */

    prefix_len = strlen (prefix);
    ppath_len += prefix_len;
    ppath_len++;    /* For the separating slash.  */

    /* Take account of the fact that we need to translate every
     * separator into `subfolders/'.
     */
    p = vpath;
    while (1) {
        newp = strchr (p, '/');
        if (newp == NULL)
            break;

        ppath_len += SUBFOLDER_DIR_NAME_LEN;
        ppath_len++; /* For the separating slash.  */

        /* Skip consecutive slashes.  */
        while (*newp == '/')
            newp++;

        p = newp;
    };

    ppath = g_malloc (ppath_len);
    dp = ppath;

    memcpy (dp, prefix, prefix_len);
    dp += prefix_len;
    *(dp++) = '/';

    /* Copy the mangled path.  */
    p = vpath;
    while (1) {
        newp = strchr (p, '/');
        if (newp == NULL) {
            strcpy (dp, p);
            break;
        }

        memcpy (dp, p, newp - p + 1); /* `+ 1' to copy the slash too.  */
        dp += newp - p + 1;

        memcpy (dp, SUBFOLDER_DIR_NAME, SUBFOLDER_DIR_NAME_LEN);
        dp += SUBFOLDER_DIR_NAME_LEN;

        *(dp++) = '/';

        /* Skip consecutive slashes.  */
        while (*newp == '/')
            newp++;

        p = newp;
    }

    return ppath;
}


static gboolean
find_folders_recursive (const char *physical_path, const char *path,
            EPathFindFoldersCallback callback, gpointer data)
{
    DIR *dir;
    char *subfolder_directory_path;
    gboolean ok;

    if (*path) {
        if (!callback (physical_path, path, data))
            return FALSE;

        subfolder_directory_path = g_strdup_printf ("%s/%s", physical_path, SUBFOLDER_DIR_NAME);
    } else {
        /* On the top level, we have no folders and,
         * consequently, no subfolder directory.
         */

        subfolder_directory_path = g_strdup (physical_path);
    }

    /* Now scan the subfolders and load them. */
    dir = opendir (subfolder_directory_path);
    if (dir == NULL) {
        g_free (subfolder_directory_path);
        return TRUE;
    }

    ok = TRUE;
    while (ok) {
        struct stat file_stat;
        struct dirent *dirent;
        char *file_path;
        char *new_path;

        dirent = readdir (dir);
        if (dirent == NULL)
            break;

        if (strcmp (dirent->d_name, ".") == 0 || strcmp (dirent->d_name, "..") == 0)
            continue;

        file_path = g_strdup_printf ("%s/%s", subfolder_directory_path,
                         dirent->d_name);

        if (stat (file_path, &file_stat) < 0 ||
            ! S_ISDIR (file_stat.st_mode)) {
            g_free (file_path);
            continue;
        }

        new_path = g_strdup_printf ("%s/%s", path, dirent->d_name);

        ok = find_folders_recursive (file_path, new_path, callback, data);

        g_free (file_path);
        g_free (new_path);
    }

    closedir (dir);
    g_free (subfolder_directory_path);

    return ok;
}

/**
 * e_path_find_folders:
 * @prefix: directory to start from
 * @callback: Callback to invoke on each folder
 * @data: Data for @callback
 *
 * Walks the folder tree starting at @prefix and calls @callback
 * on each folder.
 *
 * Return value: %TRUE on success, %FALSE if an error occurs at any point
 **/
gboolean
e_path_find_folders (const char *prefix,
             EPathFindFoldersCallback callback,
             gpointer data)
{
    return find_folders_recursive (prefix, "", callback, data);
}


/**
 * e_path_rmdir:
 * @prefix: a prefix to prepend to the path, or %NULL
 * @path: the virtual path to convert to a filesystem path.
 *
 * This removes the directory pointed to by @prefix and @path
 * and attempts to remove its parent "subfolders" directory too
 * if it's empty.
 *
 * Return value: -1 (with errno set) if it failed to rmdir the
 * specified directory. 0 otherwise, whether or not it removed
 * the parent directory.
 **/
int
e_path_rmdir (const char *prefix, const char *vpath)
{
    char *physical_path, *p;

    /* Remove the directory itself */
    physical_path = e_path_to_physical (prefix, vpath);
    if (rmdir (physical_path) == -1) {
        g_free (physical_path);
        return -1;
    }

    /* Attempt to remove its parent "subfolders" directory,
     * ignoring errors since it might not be empty.
     */

    p = strrchr (physical_path, '/');
    if (p[1] == '\0') {
        g_free (physical_path);
        return 0;
    }
    *p = '\0';
    p = strrchr (physical_path, '/');
    if (!p || strcmp (p + 1, SUBFOLDER_DIR_NAME) != 0) {
        g_free (physical_path);
        return 0;
    }

    rmdir (physical_path);
    g_free (physical_path);
    return 0;
}