summaryrefslogtreecommitdiff
path: root/src/common/pathname.c
blob: 1f66ffbdb807760f1dfcfbe73573d817d83b80df (plain)
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
256

/* Chrysalide - Outil d'analyse de fichiers binaires
 * pathname.c - manipulation de chemins de fichiers
 *
 * Copyright (C) 2015 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  Chrysalide is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Chrysalide 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "pathname.h"


#include <assert.h>
#include <glib.h>
#include <malloc.h>
#include <string.h>
#include <sys/stat.h>


#include "extstr.h"



/******************************************************************************
*                                                                             *
*  Paramètres  : ref    = fichier servant de référence aux calculs.           *
*                target = fichier absolu ciblé par la procédure.              *
*                                                                             *
*  Description : Calcule le chemin relatif entre deux fichiers donnés.        *
*                                                                             *
*  Retour      : Chemin d'accès déterminé.                                    *
*                                                                             *
*  Remarques   : Les chemins de type 'a//b' ne sont pas supportés.            *
*                                                                             *
******************************************************************************/

char *build_relative_filename(const char *ref, const char *target)
{
    char *result;                           /* Chemin à retourner          */
    size_t common;                          /* Taille de la partie commune */
    const char *found;                      /* Séparateur suivant rencontré*/
    size_t ref_next;                        /* Prochain séparateur #1      */
    size_t target_next;                     /* Prochain séparateur #2      */
    int ret;                                /* Bilan d'un appel            */
    unsigned int levels;                    /* Niveaux de décalage         */
    unsigned int i;                         /* Boucle de parcours #1       */

    common = 0;

    /* Recherche d'une base commune */

    while (1)
    {
        found = strchr(ref + common, G_DIR_SEPARATOR);
        if (found == NULL) break;

        ref_next = found - ref;

        found = strchr(target + common, G_DIR_SEPARATOR);
        if (found == NULL) break;

        target_next = found - target;

        /* Comparaison rapide sur la longeur du nom */
        if (ref_next != target_next) break;

        /* Comparaison sur une portion de chemin */
        ret = strncmp(ref + common, target + common, ref_next - common);
        if (ret != 0) break;

        common = ref_next + 1;

    }

    /* Décompte du décalage entre la référence et la cible */

    found = ref + common;

    for (levels = 0; ; levels++)
    {
        found = strchr(found, G_DIR_SEPARATOR);
        if (found == NULL) break;

        found++;

    }

    /* Construction du résultat final */

    result = strdup(target + common);

    for (i = 0; i < levels; i++)
    {
        result = strprep(result, G_DIR_SEPARATOR_S);
        result = strprep(result, "..");
    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : ref    = fichier servant de référence aux calculs.           *
*                target = fichier relatif ciblé par la procédure.             *
*                                                                             *
*  Description : Calcule le chemin absolu d'un fichier par rapport à un autre.*
*                                                                             *
*  Retour      : Chemin d'accès déterminé ou NULL en cas d'erreur.            *
*                                                                             *
*  Remarques   : Les chemins de type 'a//b' ne sont pas supportés.            *
*                                                                             *
******************************************************************************/

char *build_absolute_filename(const char *ref, const char *target)
{
    char *result;                           /* Chemin à retourner          */
    char *last_sep;                         /* Dernier séparateur trouvé   */
    const char *target_base;                /* Base de la relativité       */

    static const char upper[4] = { '.', '.', G_DIR_SEPARATOR, '\0' };

    result = strdup(ref);

    last_sep = strrchr(result, G_DIR_SEPARATOR);
    assert(last_sep != NULL);

    target_base = target;

    /* Remontée des répertoires */
    while (1)
    {
        if (strncmp(target_base, upper, 3) != 0)
            break;

        target_base += 3;

        *last_sep = '\0';
        last_sep = strrchr(result, G_DIR_SEPARATOR);

        /* S'il devient impossible de remonter autant... */
        if (last_sep == NULL) break;

    }

    if (last_sep == NULL)
    {
        free(result);
        result = NULL;
    }
    else
    {
        *(last_sep + 1) = '\0';
        result = stradd(result, target_base);
    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : path = chemin d'accès avec répertoires.                      *
*                                                                             *
*  Description : S'assure que le chemin fourni est bien en place.             *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool mkpath(const char *path)
{
    char tmp[PATH_MAX];                     /* Recopie de travail          */
    size_t len;                             /* Taille du chemin fourni     */
    char *iter;                             /* Boucle de parcours          */
    struct stat info;                       /* Information sur l'existant  */
    int ret;                                /* Bilan d'un appel système    */

    snprintf(tmp, PATH_MAX, "%s", path);
    len = strlen(tmp);

    /* Le chemin fournit ne contient que des répertoires ? */
    if (tmp[len - 1] == G_DIR_SEPARATOR)
        tmp[len - 1] = '\0';

    /* Sinon, on supprime le dernier élément, qui est un fichier */
    else
    {
        iter = strrchr(tmp, G_DIR_SEPARATOR);
        if (iter == NULL) return true;

        *iter = '\0';

    }

    for(iter = tmp + 1; *iter; iter++)
        if(*iter == G_DIR_SEPARATOR)
        {
            *iter = '\0';

            /* Analyse de l'existant */
            if (stat(tmp, &info) == 0)
            {
                if (S_ISDIR(info.st_mode) == 0)
                    return false;
                else
                {
                    *iter = G_DIR_SEPARATOR;
                    continue;
                }
            }

            ret = mkdir(tmp, S_IRWXU);
            if (ret != 0)
            {
                perror("mkdir");
                return false;
            }

            *iter = G_DIR_SEPARATOR;

        }

    /* Analyse de l'existant */
    if (stat(tmp, &info) == 0)
    {
        if (S_ISDIR(info.st_mode) == 0)
            return false;
        else
            return true;
    }

    ret = mkdir(tmp, S_IRWXU);
    if (ret != 0) perror("mkdir");

    return (ret == 0);

}