Debian Codegraph @

path_utils/path_utils.c

raw symbols SCIP
1 /*
2 Authors:
3 John Dennis <jdennis.redhat.com>
4
5 Copyright (C) 2009 Red Hat
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /*****************************************************************************/
22 /******************************** Documentation ******************************/
23 /*****************************************************************************/
24
25 /*****************************************************************************/
26 /******************************* Include Files *******************************/
27 /*****************************************************************************/
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <dirent.h>
34 #include <sys/errno.h>
35 #include <sys/stat.h>
36
37 #include <libgen.h>
38
39 #include "path_utils.h"
40
41 /*****************************************************************************/
42 /****************************** Internal Defines *****************************/
43 /*****************************************************************************/
44
45 /*****************************************************************************/
46 /************************** Internal Type Definitions ************************/
47 /*****************************************************************************/
48
49 /*****************************************************************************/
50 /********************** External Function Declarations *********************/
51 /*****************************************************************************/
52
53 /*****************************************************************************/
54 /********************** Internal Function Declarations *********************/
55 /*****************************************************************************/
56
57 /*****************************************************************************/
58 /************************* External Global Variables ***********************/
59 /*****************************************************************************/
60
61 /*****************************************************************************/
62 /************************* Internal Global Variables ***********************/
63 /*****************************************************************************/
64
65 /*****************************************************************************/
66 /**************************** Inline Functions *****************************/
67 /*****************************************************************************/
68
69 /*****************************************************************************/
70 /*************************** Internal Functions ****************************/
71 /*****************************************************************************/
72
73 /*****************************************************************************/
74 /**************************** Exported Functions ***************************/
75 /*****************************************************************************/
76
77 const char *path_utils_error_string(int error)
78 {
79 switch(error) {
80 case SUCCESS: return _("Success");
81 case PATH_UTILS_ERROR_NOT_FULLY_NORMALIZED: return _("Path could not be fully normalized");
82 }
83 return NULL;
84 }
85
86 static int dot_to_absolute(char *rel_path, int rel_path_size)
87 {
88 char tmp_path[PATH_MAX];
89
90 if (strcmp(rel_path, ".") == 0) {
91 if (getcwd(rel_path, rel_path_size) == NULL) {
92 if (errno == ERANGE)
93 return ENOBUFS;
94 else
95 return errno;
96 }
97 } else if (strcmp(rel_path, "..") == 0) {
98 if (getcwd(tmp_path, sizeof(tmp_path)) == NULL) {
99 if (errno == ERANGE)
100 return ENOBUFS;
101 else
102 return errno;
103 }
104 strncpy(rel_path, dirname(tmp_path), rel_path_size);
105 if (rel_path[rel_path_size-1] != '\0') return ENOBUFS;
106 }
107
108 return SUCCESS;
109 }
110
111 int get_basename(char *base_name, size_t base_name_size, const char *path)
112 {
113 char tmp_path[PATH_MAX];
114 int ret;
115
116 if (!path) return EINVAL;
117 if (!base_name || base_name_size < 1) return ENOBUFS;
118
119 /* coverity[buffer_size_warning : SUPPRESS] */ /* false positive warning */
120 strncpy(tmp_path, path, sizeof(tmp_path));
121 if (tmp_path[sizeof(tmp_path)-1] != '\0') return ENOBUFS;
122 strncpy(base_name, basename(tmp_path), base_name_size);
123 if (base_name[base_name_size-1] != '\0') return ENOBUFS;
124
125 ret = dot_to_absolute(base_name, base_name_size);
126 if (ret != SUCCESS) {
127 return ret;
128 }
129
130 return SUCCESS;
131 }
132
133 int get_dirname(char *dir_path, size_t dir_path_size, const char *path)
134 {
135 char tmp_path[PATH_MAX];
136 int ret;
137
138 if (!path) return EINVAL;
139 if (!dir_path || dir_path_size < 1) return ENOBUFS;
140
141 /* coverity[buffer_size_warning : SUPPRESS] */ /* false positive warning */
142 strncpy(tmp_path, path, sizeof(tmp_path));
143 if (tmp_path[sizeof(tmp_path)-1] != '\0') return ENOBUFS;
144 strncpy(dir_path, dirname(tmp_path), dir_path_size);
145 if (dir_path[dir_path_size-1] != '\0') return ENOBUFS;
146
147 ret = dot_to_absolute(dir_path, dir_path_size);
148 if (ret != SUCCESS) {
149 return ret;
150 }
151
152 return SUCCESS;
153 }
154
155 int get_directory_and_base_name(char *dir_path, size_t dir_path_size,
156 char *base_name, size_t base_name_size,
157 const char *path)
158 {
159 char tmp_path[PATH_MAX];
160 int ret;
161
162 if (!path) return EINVAL;
163 if (!dir_path || dir_path_size < 1) return ENOBUFS;
164 if (!base_name || base_name_size < 1) return ENOBUFS;
165
166 /* coverity[buffer_size_warning : SUPPRESS] */ /* false positive warning */
167 strncpy(tmp_path, path, sizeof(tmp_path));
168 if (tmp_path[sizeof(tmp_path)-1] != '\0') return ENOBUFS;
169 strncpy(base_name, basename(tmp_path), base_name_size);
170 if (base_name[base_name_size-1] != '\0') return ENOBUFS;
171
172 /* coverity[buffer_size_warning : SUPPRESS] */ /* false positive warning */
173 strncpy(tmp_path, path, sizeof(tmp_path));
174 if (tmp_path[sizeof(tmp_path)-1] != '\0') return ENOBUFS;
175 strncpy(dir_path, dirname(tmp_path), dir_path_size);
176 if (dir_path[dir_path_size-1] != '\0') return ENOBUFS;
177
178 ret = dot_to_absolute(dir_path, dir_path_size);
179 if (ret != SUCCESS) {
180 return ret;
181 }
182
183 if (strcmp(base_name, ".") == 0) {
184 strncpy(base_name, "", base_name_size);
185 if (base_name[base_name_size-1] != '\0') return ENOBUFS;
186 }
187
188 return SUCCESS;
189 }
190
191 bool is_absolute_path(const char *path)
192 {
193 if (!path) return false;
194 return path[0] == '/';
195 }
196
197 int path_concat(char *path, size_t path_size, const char *head, const char *tail)
198 {
199 int ret;
200 const char *p, *src;
201 char *dst, *dst_end;
202
203 if (!path || path_size < 1) return ENOBUFS;
204
205 dst = path;
206 dst_end = path + path_size - 1; /* -1 allows for NULL terminator */
207
208 if (head && *head) {
209 /* walk to end of head */
210 for (p = head; *p; p++);
211
212 /* skip any trailing slashes in head */
213 for (p--; p > head && *p == '/'; p--);
214
215 /* If the length of head exceeds the buffer size, fail */
216 if ((p - head) > path_size-1) {
217 ret = ENOBUFS;
218 goto fail;
219 }
220
221 /* Copy head into path */
222 for (src = head; src <= p && dst < dst_end;) {
223 *dst++ = *src++;
224 }
225 }
226 if (tail && *tail) {
227 /* skip any leading slashes in tail */
228 for (p = tail; *p && *p == '/'; p++);
229
230 if (dst > path)
231 /* insert single slash between head & tail
232 * Making sure not to add an extra if the
233 * preceding character is also a slash
234 * (such as the case where head was the
235 * special-case "/".
236 */
237 if (dst < dst_end && *(dst-1) != '/') {
238 *dst++ = '/';
239 }
240
241 /* Copy the tail into the path */
242 for (src = p; *src && dst < dst_end;) {
243 *dst++ = *src++;
244 }
245
246 /* If we got past dst_end and there is more data
247 * in the src buffer, we should return ENOBUFS
248 */
249 if (*src) {
250 ret = ENOBUFS; /* failed to copy everything */
251 goto fail;
252 }
253 }
254 *dst = 0;
255
256 return SUCCESS;
257
258 fail:
259 /* On failure, set the buffer to the empty string for safety */
260 *path = '\0';
261 return ret;
262 }
263
264 int make_path_absolute(char *absolute_path, size_t absolute_path_size, const char *path)
265 {
266 int result = SUCCESS;
267 const char *src;
268 char *dst, *dst_end;
269
270 if (!absolute_path || absolute_path_size < 1) return ENOBUFS;
271
272 dst = absolute_path;
273 dst_end = absolute_path + absolute_path_size - 1; /* -1 allows for NULL terminator */
274
275 if (is_absolute_path(path)) {
276 for (src = path; *src && dst < dst_end;) *dst++ = *src++;
277 *dst = 0;
278 if (dst > dst_end || *src) result = ENOBUFS;
279 return result;
280 }
281
282 if ((getcwd(absolute_path, absolute_path_size) == NULL)) {
283 if (errno == ERANGE)
284 return ENOBUFS;
285 else
286 return errno;
287 }
288
289 for (dst = absolute_path; *dst && dst < dst_end; dst++);
290 if (!(path && *path)) return result;
291 if (dst > dst_end) {
292 *absolute_path = 0;
293 return ENOBUFS;
294 }
295
296 *dst++ = '/';
297 if (dst > dst_end) {
298 *absolute_path = 0;
299 return ENOBUFS;
300 }
301
302 for (src = path; *src && dst < dst_end;) *dst++ = *src++;
303 if (*src) return ENOBUFS; /* failed to copy everything */
304 *dst = 0;
305
306 return result;
307 }
308
309 char **split_path(const char *path, int *count)
310 {
311 int n_components, component_len, total_component_len, alloc_len;
312 const char *start, *end;
313 char *mem_block, **array_ptr, *component_ptr;
314
315 if (count) *count = 0;
316 if (!path) return NULL;
317
318 /* If path is absolute add in special "/" root component */
319 if (*path == '/') {
320 n_components = 1;
321 total_component_len = 2;
322 } else {
323 n_components = 0;
324 total_component_len = 0;
325 }
326
327 /* Scan for components, keep several counts */
328 for (start = end = path; *start; start = end) {
329 for (start = end; *start && *start == '/'; start++);
330 for (end = start; *end && *end != '/'; end++);
331 if ((component_len = end - start) == 0) break;
332 n_components++;
333 total_component_len += component_len + 1;
334 }
335
336 /*
337 * Allocate a block big enough for component array (with trailing NULL
338 * entry, hence n_components+1) and enough room for a copy of each NULL
339 * terminated component. We'll copy the components into the same allocation
340 * block after the end of the pointer array.
341 */
342 alloc_len = ((n_components+1) * sizeof(char *)) + total_component_len;
343
344 if ((mem_block = malloc(alloc_len)) == NULL) {
345 if (count) *count = -1;
346 return NULL;
347 }
348
349 /* component array */
350 array_ptr = (char **)mem_block;
351 /* components copied after end of array */
352 component_ptr = mem_block + ((n_components+1)*sizeof(char *));
353
354 /* If path is absolute add in special "/" root component */
355 if (*path == '/') {
356 *array_ptr++ = component_ptr;
357 *component_ptr++ = '/';
358 *component_ptr++ = 0;
359 }
360
361 for (start = end = path; *start; start = end) {
362 for (start = end; *start && *start == '/'; start++);
363 for (end = start; *end && *end != '/'; end++);
364 if ((end - start) == 0) break;
365
366 *array_ptr++ = component_ptr;
367 while (start < end) *component_ptr++ = *start++;
368 *component_ptr++ = 0;
369 }
370 *array_ptr = NULL;
371 if (count) *count = n_components;
372 return (char **)mem_block;
373 }
374
375 int normalize_path(char *normalized_path, size_t normalized_path_size, const char *path)
376 {
377 int result = SUCCESS;
378 int component_len;
379 bool is_absolute, can_backup;
380 const char *start, *end;
381 char *dst, *dst_end, *p, *limit;
382
383 if (!normalized_path || normalized_path_size < 1) return ENOBUFS;
384
385 dst = normalized_path;
386 dst_end = normalized_path + normalized_path_size - 1; /* -1 allows for NULL terminator */
387 can_backup = true;
388
389 if (!path || !*path) {
390 if (dst > dst_end) {
391 *dst = 0;
392 return ENOBUFS;
393 }
394 *dst++ = '.';
395 *dst = 0;
396 return result;
397 }
398
399 if ((is_absolute = *path == '/')) {
400 if (dst < dst_end) {
401 *dst++ = '/';
402 } else {
403 *dst = 0;
404 return ENOBUFS;
405 }
406 }
407
408 for (start = end = path; *start; start = end) {
409 for (start = end; *start && *start == '/'; start++);
410 for (end = start; *end && *end != '/'; end++);
411 if ((component_len = end - start) == 0) break;
412 if (component_len == 1 && start[0] == '.') continue;
413 if (component_len == 2 && start[0] == '.' && start[1] == '.' && can_backup) {
414 /* back up one level */
415 if ((is_absolute && dst == normalized_path+1) || (!is_absolute && dst == normalized_path)) {
416 if (is_absolute) continue;
417 can_backup = false;
418 result = PATH_UTILS_ERROR_NOT_FULLY_NORMALIZED;
419 } else {
420 if (is_absolute)
421 limit = normalized_path+1;
422 else
423 limit = normalized_path;
424 for (p = dst-1; p >= limit && *p != '/'; p--);
425 if (p < limit)
426 dst = limit;
427 else
428 dst = p;
429 continue;
430 }
431 }
432
433 if ((end-start) > (dst_end-dst)) {
434 return ENOBUFS;
435 }
436
437 if ((dst > normalized_path) && (dst < dst_end) && (dst[-1] != '/')) *dst++ = '/';
438 while ((start < end) && (dst < dst_end)) *dst++ = *start++;
439 }
440
441 if (dst == normalized_path) {
442 if (is_absolute)
443 *dst++ = '/';
444 else
445 *dst++ = '.';
446 }
447 *dst = 0;
448 return result;
449 }
450
451 int common_path_prefix(char *common_path,
452 size_t common_path_size,
453 int *common_count,
454 const char *path1, const char *path2)
455 {
456 int count1, count2, min_count, i, n_common, result;
457 char **split1, **split2;
458 char *dst, *dst_end, *src;
459
460 if (!common_path || common_path_size < 1) return ENOBUFS;
461
462 result = SUCCESS;
463 n_common = 0;
464 split1 = split_path(path1, &count1);
465 split2 = split_path(path2, &count2);
466
467 if (count1 <= count2)
468 min_count = count1;
469 else
470 min_count = count2;
471
472 if (min_count <= 0 || !split1 || !split2 ) {
473 result = SUCCESS;
474 *common_path = 0;
475 goto done;
476 }
477
478 for (n_common = 0; n_common < min_count; n_common++) {
479 if (strcmp(split1[n_common], split2[n_common]) != 0) break;
480 }
481
482 if (n_common == 0) {
483 result = SUCCESS;
484 *common_path = 0;
485 goto done;
486 }
487
488 dst = common_path;
489 dst_end = common_path + common_path_size - 1; /* -1 allows for NULL terminator */
490 for (i = 0; i < n_common; i++) {
491 for (src = split1[i]; *src && dst < dst_end;) *dst++ = *src++;
492 if (dst == dst_end && *src) {
493 *dst = 0;
494 result = ENOBUFS;
495 goto done;
496 }
497 if (dst[-1] != '/' && i < n_common-1) { /* insert path separator */
498 if (dst == dst_end) {
499 *dst = 0;
500 result = ENOBUFS;
501 goto done;
502 }
503 *dst++ = '/';
504 }
505 }
506 *dst = 0;
507
508 done:
509 free(split1);
510 free(split2);
511 if (common_count) *common_count = n_common;
512 return result;
513 }
514
515 int make_normalized_absolute_path(char *result_path, size_t result_path_size, const char *path)
516 {
517 int error;
518 char absolute_path[PATH_MAX];
519
520 if (!result_path || result_path_size < 1) return ENOBUFS;
521 *result_path = 0;
522 if ((error = make_path_absolute(absolute_path, sizeof(absolute_path), path)) != SUCCESS) return error;
523 if ((error = normalize_path(result_path, result_path_size, absolute_path)) != SUCCESS) return error;
524 return SUCCESS;
525 }
526
527 int find_existing_directory_ancestor(char *ancestor, size_t ancestor_size, const char *path)
528 {
529 int error;
530 char dir_path[PATH_MAX];
531 struct stat info;
532
533 if (!ancestor || ancestor_size < 1) return ENOBUFS;
534 *ancestor = 0;
535 /* coverity[buffer_size_warning : SUPPRESS] */ /* false positive warning */
536 strncpy(dir_path, path, sizeof(dir_path));
537 if (dir_path[sizeof(dir_path)-1] != '\0') return ENOBUFS;
538
539 while (strcmp(dir_path, "/") != 0) {
540 if (lstat(dir_path, &info) < 0) {
541 error = errno;
542 if (error != ENOENT) return error;
543 } else {
544 if (S_ISDIR(info.st_mode)) break;
545 }
546 error = get_dirname(dir_path, sizeof(dir_path), dir_path);
547 if (error != SUCCESS) {
548 return error;
549 }
550 }
551
552 strncpy(ancestor, dir_path, ancestor_size);
553 if (ancestor[ancestor_size-1] != '\0') return ENOBUFS;
554 return SUCCESS;
555 }
556
557 int directory_list(const char *path, bool recursive,
558 directory_list_callback_t callback, void *user_data)
559 {
560 DIR *dir;
561 struct dirent *entry;
562 struct stat info;
563 int error = 0;
564 char entry_path[PATH_MAX];
565 bool prune = false;
566
567 if (!(dir = opendir(path))) {
568 error = errno;
569 return error;
570 }
571
572 for (entry = readdir(dir); entry; entry = readdir(dir)) {
573
574 if (strcmp(entry->d_name, ".") == 0 ||
575 strcmp(entry->d_name, "..") == 0) {
576 continue;
577 }
578
579 error = path_concat(entry_path, sizeof(entry_path),
580 path, entry->d_name);
581 if (error != SUCCESS) {
582 closedir(dir);
583 /* Don't bother checking the return here.
584 * The path_concat error is more important
585 */
586 return error;
587 }
588
589 if (lstat(entry_path, &info) < 0) {
590 continue;
591 }
592
593 prune = !callback(path, entry->d_name, entry_path, &info, user_data);
594 if (S_ISDIR(info.st_mode)) {
595 if (recursive && !prune) {
596 error = directory_list(entry_path, recursive,
597 callback, user_data);
598 if (error != SUCCESS) {
599 closedir(dir);
600 /* Don't bother checking the return here.
601 * The directory_list error is more important
602 */
603 return error;
604 }
605 }
606 }
607 }
608 error = closedir(dir);
609 if (error) {
610 return error;
611 }
612 return SUCCESS;
613 }
614
615 bool is_ancestor_path(const char *ancestor, const char *path)
616 {
617 char **path_components, **ancestor_components;
618 int i, path_count, ancestor_count;
619 bool result = false;
620
621 path_components = split_path(path, &path_count);
622 ancestor_components = split_path(ancestor, &ancestor_count);
623
624 if (!path_components || !ancestor_components) {
625 goto exit;
626 }
627
628 if (ancestor_count >= path_count) {
629 goto exit;
630 }
631
632 for (i = 0; i < ancestor_count; i++) {
633 if (strcmp(path_components[i], ancestor_components[i]) != 0) {
634 goto exit;
635 }
636 }
637
638 result = true;
639
640 exit:
641 free(path_components);
642 free(ancestor_components);
643 return result;
644 }
645