2 * Copyright (C) 2008 Kristian Høgsberg <krh@redhat.com>
3 * Copyright (C) 2008 Red Hat, Inc
4 * Copyright (C) 2009, 2011, 2012, 2014, 2016 J. Ali Harlow <ali@juiblex.co.uk>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <sys/types.h>
43 #include <archive_entry.h>
48 #include "types/types.h"
49 #include "razor-internal.h"
55 #define strcmp0(s1, s2) ((s1) && (s2) ? strcmp(s1, s2) : \
56 (s1) ? 1 : (s2) ? -1 : 0)
59 razor_archive_get_file_contents(const char *filename, int fd, const char *path,
60 size_t *length, struct razor_error **error)
67 size_t size, total_size;
70 struct archive_entry *entry;
72 a = archive_read_new();
73 archive_read_support_compression_all(a);
74 archive_read_support_format_all(a);
76 r = archive_read_open_fd(a, fd, 10240);
79 errmsg = archive_error_string(a);
80 if (!strcmp(errmsg, "Unrecognized archive format"))
81 razor_set_error(error, RAZOR_GENERAL_ERROR,
82 RAZOR_GENERAL_ERROR_UNSUPPORTED_ARCHIVE,
85 razor_set_error(error, RAZOR_POSIX_ERROR,
86 archive_errno(a), filename, errmsg);
87 archive_read_finish(a);
92 r = archive_read_next_header(a, &entry);
95 else if (r < ARCHIVE_WARN) {
96 razor_set_error(error, RAZOR_POSIX_ERROR,
97 archive_errno(a), filename,
98 archive_error_string(a));
99 archive_read_close(a);
100 archive_read_finish(a);
105 * TODO: Unicode support. Might need to wait for libarchive v4.
107 if (!strcmp(archive_entry_pathname(entry), path)) {
108 total_size = archive_entry_size(entry);
109 addr = malloc(total_size);
111 archive_read_close(a);
112 archive_read_finish(a);
113 razor_set_error(error, RAZOR_POSIX_ERROR,
115 "Not enough memory");
120 r = archive_read_data_block(a, &buf, &size,
122 if (r == ARCHIVE_EOF)
124 if (r < ARCHIVE_OK) {
125 razor_set_error(error, RAZOR_POSIX_ERROR,
126 archive_errno(a), path,
127 archive_error_string(a));
132 memcpy((char *)addr + offset, buf, size);
135 archive_read_close(a);
136 archive_read_finish(a);
138 *length = total_size;
144 archive_read_close(a);
145 archive_read_finish(a);
147 razor_set_error(error, RAZOR_POSIX_ERROR, ENOENT, path,
148 "No such file or directory in archive");
152 razor_set_error(error, RAZOR_GENERAL_ERROR,
153 RAZOR_GENERAL_ERROR_UNSUPPORTED_ARCHIVE,
154 filename, "Archives are not supported in this build");
157 #endif /* HAVE_LIBARCHIVE */
161 razor_file_get_contents_archive(const char *filename, size_t *length,
162 struct razor_error **error)
165 char *path, *slash, *s;
167 struct razor_error *tmp_error = NULL;
169 path = strdup(filename);
170 slash = strrchr(path, '/');
174 fd = open(path, O_RDONLY | O_BINARY);
176 addr = razor_archive_get_file_contents(path, fd,
184 } else if (errno != ENOTDIR && errno != ENOENT) {
186 } else if (errno != ENOTDIR) {
189 razor_set_error_posix(error, filename);
192 s = strrchr(path, '/');
199 razor_set_error(error, RAZOR_POSIX_ERROR, ENOENT, filename,
202 razor_set_error(error, RAZOR_POSIX_ERROR, ENOTDIR, filename,
208 #define OPEN_FILE_USED (1U<<0)
209 #define OPEN_FILE_MMAPPED (1U<<1)
217 struct array open_files = { 0, };
219 void *razor_file_get_contents(const char *filename, size_t *length, int private,
220 struct razor_error **error)
227 struct open_file *of, *ofend;
229 fd = open(filename, O_RDONLY | O_BINARY);
232 if (errno != ENOTDIR && errno != ENOENT) {
234 if (errno != ENOTDIR) {
236 razor_set_error_posix(error, filename);
239 addr = razor_file_get_contents_archive(filename, &size, error);
243 if (fstat(fd, &st) < 0) {
244 razor_set_error_posix(error, filename);
251 ofend = open_files.data + open_files.size;
252 for (of = open_files.data; of < ofend; of++)
253 if (!(of->flags & OPEN_FILE_USED))
256 of = array_add(&open_files, sizeof *of);
261 if (!addr && !private) {
262 addr = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
263 if (addr == MAP_FAILED)
266 of->flags = OPEN_FILE_USED | OPEN_FILE_MMAPPED;
268 #endif /* HAVE_SYS_MMAN_H */
275 res = read(fd, addr + nb, size - nb);
277 razor_set_error_posix(error, filename);
285 of->flags = OPEN_FILE_USED;
287 razor_set_error(error, RAZOR_POSIX_ERROR, ENOMEM, NULL,
288 "Not enough memory");
303 int razor_file_free_contents(void *addr, size_t length)
308 struct open_file *of, *ofend;
310 ofend = open_files.data + open_files.size;
311 for (of = open_files.data; of < ofend; of++)
312 if ((of->flags & OPEN_FILE_USED) && of->addr == addr)
320 mmapped = of->flags & OPEN_FILE_MMAPPED;
322 of->flags &= ~OPEN_FILE_USED;
324 for (of = open_files.data; of < ofend; of++)
325 if (of->flags & OPEN_FILE_USED)
329 array_release(&open_files);
330 array_init(&open_files);
335 return munmap(addr, length);
342 int razor_file_mkdir(const char *path, mode_t mode, struct razor_error **error)
347 retval = mkdir(path, mode);
351 * Ignore the case of a pre-existing directory and give
352 * an explicit error message if there is something other
353 * than a directory already at path.
356 if (code != EEXIST || stat(path, &buf))
357 razor_set_error(error, RAZOR_POSIX_ERROR, code, path,
359 else if (!S_ISDIR(buf.st_mode))
360 razor_set_error(error, RAZOR_POSIX_ERROR, code, path,
367 int razor_file_unlink(const char *path, struct razor_error **error)
371 retval = unlink(path);
374 razor_set_error_posix(error, path);
379 int razor_file_open(const char *path, int flags, mode_t mode,
380 struct razor_error **error)
384 retval = open(path, flags, mode);
387 razor_set_error_posix(error, path);
392 int razor_file_move(const char *path, const char *dest,
393 struct razor_error **error)
398 wchar_t *oldbuf, *newbuf;
399 const DWORD flags = MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING;
401 newbuf = razor_utf8_to_utf16(dest, -1);
402 oldbuf = razor_utf8_to_utf16(path, -1);
405 * Passing MOVEFILE_REPLACE_EXISTING to MoveFileEx() will
406 * cover every case we care about _except_ replacing an empty
407 * directory with a file. Calling RemoveDirectory() will deal
408 * with this case while having no effect in all other cases.
410 (void)RemoveDirectoryW(newbuf);
412 if (!MoveFileExW(oldbuf, newbuf, flags)) {
413 razor_set_error_mswin(error, newbuf, GetLastError());
423 if (rename(path, dest)) {
426 if (access(path, F_OK) < 0)
430 razor_set_error(error, RAZOR_POSIX_ERROR, code, object,
441 static char *absolute_path(const char *path)
444 char *result, *subpath, *p, *s, *t;
446 result = realpath(path, NULL);
448 if (!result && errno == ENOENT) {
454 result = strdup("/");
459 subpath = realpath(p, NULL);
463 len = strlen(subpath);
464 result = malloc(len + strlen(s) + 1);
465 memcpy(result, subpath, len);
466 strcpy(result + len, s);
469 } else if (errno != ENOENT)
478 result = realpath(".", NULL);
487 int razor_file_is_directory(const char *path, struct razor_error **error)
491 if (stat(path, &st) < 0) {
492 razor_set_error_posix(error, path);
496 return !!S_ISDIR(st.st_mode);
499 char *razor_file_mkdtemp_near(const char *path, const char *template,
500 struct razor_error **error)
505 if (path[0]=='\\' && path[1]=='\\' && path[2] && path[2]!='\\'
506 && strchr(path+3,'\\')) {
507 /* We have a UNC path: \\servername\sharename... */
508 const char *sharename, *root;
511 sharename = strchr(path+3,'\\')+1;
512 root = strchr(sharename,'\\');
514 disklen = root - path;
516 disklen = strlen(path);
518 retval = malloc(disklen + 1 + strlen(template) + 1);
519 memcpy(retval, path, disklen);
520 retval[disklen] = '\\';
521 strcpy(retval + disklen + 1, template);
522 } else if ((*path>='A' && *path<='Z' || *path>='a' && *path<='z') &&
524 retval = malloc(3 + strlen(template) + 1);
528 strcpy(retval + 3, template);
534 n = GetCurrentDirectoryW(0, NULL);
535 buf = malloc(n * sizeof(wchar_t));
537 if (GetCurrentDirectoryW(n, buf)) {
538 dir = razor_utf16_to_utf8(buf, n - 1);
540 retval = razor_file_mkdtemp_near(dir, template, error);
544 retval = malloc(3 + strlen(template) + 1);
548 strcpy(retval + 3, template);
555 * Find the mount point (assuming we can write to the
556 * whole filesystem). Otherwise stop at the first
557 * unwritable directory and take one step back.
559 char *s, *abspath, saved;
560 int len, can_step_back = 0;
564 abspath = absolute_path(path);
566 razor_set_error_posix(error, path);
570 if (stat(abspath, &buf) < 0) {
574 razor_set_error_posix(error, abspath);
579 filesystem = buf.st_dev;
581 len = strlen(abspath);
582 while(len > 1 && (s = strrchr(abspath, '/'))) {
586 len = s + 1 - abspath;
592 if (stat(abspath, &buf) < 0) {
596 razor_set_error_posix(error, abspath);
600 } else if (!filesystem)
601 filesystem = buf.st_dev;
603 if (buf.st_dev != filesystem || access(abspath, W_OK)) {
610 len = strlen(abspath);
617 len = 0; /* Avoid an unslightly double slash. */
618 retval = malloc(len + 1 + strlen(template) + 1);
619 memcpy(retval, abspath, len);
621 strcpy(retval + len + 1, template);
626 if (!mkdtemp(retval)) {
631 char *s = strdup(template);
634 if (stat(".", &buf) < 0) {
635 razor_set_error_posix(error, ".");
640 if (buf.st_dev != filesystem)
642 * Don't use a different filesystem. It will
643 * only fail later on (in rename) and cause
644 * an unhelpful error message (EXDEV).
658 razor_set_error(error, RAZOR_POSIX_ERROR, err, retval,
679 #define OPEN_DIR_USED (1U<<0)
681 struct array open_dirs = { 0, };
683 void *razor_file_opendir(const char *path, struct razor_error **error)
685 struct open_dir *od, *odend;
687 odend = open_dirs.data + open_dirs.size;
688 for (od = open_dirs.data; od < odend; od++)
689 if (!(od->flags & OPEN_DIR_USED))
692 od = array_add(&open_dirs, sizeof *od);
697 od->path2 = razor_utf8_to_utf16(path, -1);
698 od->dp = _wopendir(od->path2);
700 od->path = strdup(path);
701 od->dp = opendir(od->path);
706 razor_set_error_mswin(error, od->path2, GetLastError());
709 razor_set_error_posix(error, od->path);
715 od->flags = OPEN_DIR_USED;
719 char *razor_file_readdir(void *dp, struct razor_error **error)
721 struct open_dir *od = dp, *odend;
723 struct _wdirent *dirp;
729 odend = open_dirs.data + open_dirs.size;
730 if (dp < open_dirs.data || od >= odend || !(od->flags & OPEN_DIR_USED))
736 while((dirp = _wreaddir(od->dp))) {
737 path = razor_utf16_to_utf8(dirp->d_name, -1);
738 if (strcmp(path, ".") && strcmp(path, ".."))
744 while((dirp = readdir(od->dp)))
745 if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, ".."))
746 return strdup(dirp->d_name);
751 razor_set_error_mswin(error, od->path2, GetLastError());
753 razor_set_error_posix(error, od->path);
760 int razor_file_closedir(void *dp, struct razor_error **error)
762 struct open_dir *od = dp, *odend;
765 odend = open_dirs.data + open_dirs.size;
766 if (dp < open_dirs.data || od >= odend || !(od->flags & OPEN_DIR_USED))
771 * I can't find documentation to state that _wclosedir()
772 * returns -1 on failure, so be paranoid.
774 retval = _wclosedir(od->dp) ? -1 : 0;
776 retval = closedir(od->dp);
781 razor_set_error_mswin(error, od->path2, GetLastError());
783 razor_set_error_posix(error, od->path);
793 od->flags &= ~OPEN_DIR_USED;
795 for (od = open_dirs.data; od < odend; od++)
796 if (od->flags & OPEN_DIR_USED)
800 array_release(&open_dirs);
801 array_init(&open_dirs);
807 struct razor_uri_vtable_entry {
808 struct razor_uri_vtable vtable;
810 struct array open_files, open_directories;
813 static struct array razor_uri_vtable_entries;
815 static struct razor_uri_vtable_entry *
816 razor_uri_get_vtable_entry(const char *scheme)
818 struct razor_uri_vtable_entry *entry, *eend, *fallback = NULL;
820 eend = razor_uri_vtable_entries.data + razor_uri_vtable_entries.size;
821 for(entry = razor_uri_vtable_entries.data; entry < eend; entry++) {
822 if (!strcmp0(entry->scheme, scheme))
824 else if (!entry->scheme)
832 razor_uri_mkdir(const char *uri, mode_t mode, struct razor_error **error)
837 struct razor_uri_vtable_entry *entry;
838 struct razor_error *tmp_error = NULL;
840 if (razor_uri_parse(&ru, uri, error))
843 path = razor_path_from_parsed_uri(&ru, &tmp_error);
845 if (razor_error_matches(tmp_error, RAZOR_GENERAL_ERROR,
846 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI))
847 razor_error_free(tmp_error);
849 razor_propagate_error(error, tmp_error, NULL);
850 razor_uri_destroy(&ru);
855 razor_uri_destroy(&ru);
856 retval = razor_file_mkdir(path, mode, error);
859 entry = razor_uri_get_vtable_entry(ru.scheme);
860 razor_uri_destroy(&ru);
861 if (!entry || !entry->vtable.mkdir) {
863 razor_set_error(error, RAZOR_GENERAL_ERROR,
864 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI,
865 uri, "No URI handler installed");
867 retval = entry->vtable.mkdir(uri, mode, error);
874 razor_uri_unlink(const char *uri, struct razor_error **error)
879 struct razor_uri_vtable_entry *entry;
880 struct razor_error *tmp_error = NULL;
882 if (razor_uri_parse(&ru, uri, error))
885 path = razor_path_from_parsed_uri(&ru, &tmp_error);
887 if (razor_error_matches(tmp_error, RAZOR_GENERAL_ERROR,
888 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI))
889 razor_error_free(tmp_error);
891 razor_propagate_error(error, tmp_error, NULL);
892 razor_uri_destroy(&ru);
897 razor_uri_destroy(&ru);
898 retval = razor_file_unlink(path, error);
901 entry = razor_uri_get_vtable_entry(ru.scheme);
902 razor_uri_destroy(&ru);
903 if (!entry || !entry->vtable.unlink) {
905 razor_set_error(error, RAZOR_GENERAL_ERROR,
906 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI,
907 uri, "No URI handler installed");
909 retval = entry->vtable.unlink(uri, error);
916 razor_uri_open(const char *uri, int flags, mode_t mode,
917 struct razor_error **error)
922 struct razor_uri_vtable_entry *entry;
923 struct razor_error *tmp_error = NULL;
925 if (razor_uri_parse(&ru, uri, error))
928 path = razor_path_from_parsed_uri(&ru, &tmp_error);
930 if (razor_error_matches(tmp_error, RAZOR_GENERAL_ERROR,
931 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI))
932 razor_error_free(tmp_error);
934 razor_propagate_error(error, tmp_error, NULL);
935 razor_uri_destroy(&ru);
940 razor_uri_destroy(&ru);
941 retval = razor_file_open(path, flags, mode, error);
944 entry = razor_uri_get_vtable_entry(ru.scheme);
945 razor_uri_destroy(&ru);
946 if (!entry || !entry->vtable.open) {
948 razor_set_error(error, RAZOR_GENERAL_ERROR,
949 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI,
950 uri, "No URI handler installed");
952 retval = entry->vtable.open(uri, flags, mode, error);
959 razor_uri_move(const char *src_uri, const char *dst_uri,
960 struct razor_error **error)
963 char *src_path, *dst_path;
964 struct razor_uri src_ru, dst_ru;
965 struct razor_uri_vtable_entry *entry;
966 struct razor_error *tmp_error = NULL;
968 if (razor_uri_parse(&src_ru, src_uri, error) ||
969 razor_uri_parse(&dst_ru, dst_uri, error))
972 src_path = razor_path_from_parsed_uri(&src_ru, &tmp_error);
974 if (razor_error_matches(tmp_error, RAZOR_GENERAL_ERROR,
975 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI))
976 razor_error_free(tmp_error);
977 else if (!src_path) {
978 razor_propagate_error(error, tmp_error, NULL);
979 razor_uri_destroy(&src_ru);
983 dst_path = razor_path_from_parsed_uri(&dst_ru, &tmp_error);
985 if (razor_error_matches(tmp_error, RAZOR_GENERAL_ERROR,
986 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI))
987 razor_error_free(tmp_error);
988 else if (!dst_path) {
989 razor_propagate_error(error, tmp_error, NULL);
990 razor_uri_destroy(&dst_ru);
991 razor_uri_destroy(&src_ru);
996 if (src_path && dst_path)
997 retval = razor_file_move(src_path, dst_path, error);
999 if (!strcmp(src_ru.scheme, dst_ru.scheme))
1000 entry = razor_uri_get_vtable_entry(src_ru.scheme);
1003 if (entry && entry->vtable.move)
1004 retval = entry->vtable.move(src_uri, dst_uri, error);
1005 else if (strcmp(src_ru.scheme, dst_ru.scheme)) {
1007 razor_set_error(error, RAZOR_GENERAL_ERROR,
1008 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI,
1009 dst_uri, "Cross-scheme URI move");
1012 razor_set_error(error, RAZOR_GENERAL_ERROR,
1013 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI,
1014 src_path ? dst_uri : src_uri,
1015 "No URI handler installed");
1019 razor_uri_destroy(&src_ru);
1020 razor_uri_destroy(&dst_ru);
1028 razor_uri_get_contents(const char *uri, size_t *length, int private,
1029 struct razor_error **error)
1033 struct razor_uri ru;
1034 struct razor_uri_vtable_entry *entry;
1035 struct razor_error *tmp_error = NULL;
1037 if (razor_uri_parse(&ru, uri, error))
1040 path = razor_path_from_parsed_uri(&ru, &tmp_error);
1042 if (razor_error_matches(tmp_error, RAZOR_GENERAL_ERROR,
1043 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI))
1044 razor_error_free(tmp_error);
1046 razor_propagate_error(error, tmp_error, NULL);
1047 razor_uri_destroy(&ru);
1052 razor_uri_destroy(&ru);
1053 retval = razor_file_get_contents(path, length, private, error);
1056 entry = razor_uri_get_vtable_entry(ru.scheme);
1057 razor_uri_destroy(&ru);
1058 if (!entry || !entry->vtable.get_contents) {
1060 razor_set_error(error, RAZOR_GENERAL_ERROR,
1061 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI,
1062 uri, "No URI handler installed");
1064 retval = entry->vtable.get_contents(uri, length,
1067 ptr_array_add(&entry->open_files, retval);
1074 RAZOR_EXPORT int razor_uri_free_contents(void *addr, size_t length)
1077 struct razor_uri_vtable_entry *entry, *eend;
1082 retval = razor_file_free_contents(addr, length);
1087 eend = razor_uri_vtable_entries.data + razor_uri_vtable_entries.size;
1088 for (entry = razor_uri_vtable_entries.data; entry < eend; entry++) {
1089 of = ptr_array_find(&entry->open_files, addr);
1091 if (entry->vtable.free_contents)
1092 retval = entry->vtable.free_contents(addr,
1094 ptr_array_remove_index(&entry->open_files, of);
1103 razor_uri_is_directory(const char *uri, struct razor_error **error)
1107 struct razor_uri ru;
1108 struct razor_uri_vtable_entry *entry;
1109 struct razor_error *tmp_error = NULL;
1111 if (razor_uri_parse(&ru, uri, error))
1114 path = razor_path_from_parsed_uri(&ru, &tmp_error);
1116 if (razor_error_matches(tmp_error, RAZOR_GENERAL_ERROR,
1117 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI))
1118 razor_error_free(tmp_error);
1120 razor_propagate_error(error, tmp_error, NULL);
1121 razor_uri_destroy(&ru);
1126 razor_uri_destroy(&ru);
1127 retval = razor_file_is_directory(path, error);
1130 entry = razor_uri_get_vtable_entry(ru.scheme);
1131 razor_uri_destroy(&ru);
1132 if (!entry || !entry->vtable.is_directory) {
1134 razor_set_error(error, RAZOR_GENERAL_ERROR,
1135 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI,
1136 uri, "No URI handler installed");
1138 retval = entry->vtable.is_directory(uri, error);
1145 razor_uri_mkdtemp_near(const char *uri, const char *template,
1146 struct razor_error **error)
1150 struct razor_uri ru;
1151 struct razor_uri_vtable_entry *entry;
1152 struct razor_error *tmp_error = NULL;
1154 if (razor_uri_parse(&ru, uri, error))
1157 path = razor_path_from_parsed_uri(&ru, &tmp_error);
1159 if (razor_error_matches(tmp_error, RAZOR_GENERAL_ERROR,
1160 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI))
1161 razor_error_free(tmp_error);
1163 razor_propagate_error(error, tmp_error, NULL);
1164 razor_uri_destroy(&ru);
1169 razor_uri_destroy(&ru);
1170 s = razor_file_mkdtemp_near(path, template, error);
1172 retval = razor_path_to_uri(s);
1178 entry = razor_uri_get_vtable_entry(ru.scheme);
1179 razor_uri_destroy(&ru);
1180 if (!entry || !entry->vtable.mkdtemp_near) {
1182 razor_set_error(error, RAZOR_GENERAL_ERROR,
1183 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI,
1184 uri, "No URI handler installed");
1186 retval = entry->vtable.mkdtemp_near(uri, template,
1194 razor_uri_opendir(const char *uri, struct razor_error **error)
1198 struct razor_uri ru;
1199 struct razor_uri_vtable_entry *entry;
1200 struct razor_error *tmp_error = NULL;
1202 if (razor_uri_parse(&ru, uri, error))
1205 path = razor_path_from_parsed_uri(&ru, &tmp_error);
1207 if (razor_error_matches(tmp_error, RAZOR_GENERAL_ERROR,
1208 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI))
1209 razor_error_free(tmp_error);
1211 razor_propagate_error(error, tmp_error, NULL);
1212 razor_uri_destroy(&ru);
1217 razor_uri_destroy(&ru);
1218 retval = razor_file_opendir(path, error);
1221 entry = razor_uri_get_vtable_entry(ru.scheme);
1222 razor_uri_destroy(&ru);
1223 if (!entry || !entry->vtable.opendir) {
1225 razor_set_error(error, RAZOR_GENERAL_ERROR,
1226 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI,
1227 uri, "No URI handler installed");
1229 retval = entry->vtable.opendir(uri, error);
1231 ptr_array_add(&entry->open_directories, retval);
1238 RAZOR_EXPORT char *razor_uri_readdir(void *dir, struct razor_error **error)
1242 struct razor_uri_vtable_entry *entry, *eend;
1244 retval = razor_file_readdir(dir, error);
1246 if (retval != (char *)-1)
1249 eend = razor_uri_vtable_entries.data + razor_uri_vtable_entries.size;
1250 for (entry = razor_uri_vtable_entries.data; entry < eend; entry++) {
1251 od = ptr_array_find(&entry->open_directories, dir);
1253 if (entry->vtable.readdir)
1254 retval = entry->vtable.readdir(dir, error);
1259 if (retval == (char *)-1) {
1261 razor_set_error(error, RAZOR_GENERAL_ERROR,
1262 RAZOR_GENERAL_ERROR_FAILED, NULL,
1263 "Invalid directory handle");
1269 RAZOR_EXPORT int razor_uri_closedir(void *dir, struct razor_error **error)
1273 struct razor_uri_vtable_entry *entry, *eend;
1275 retval = razor_file_closedir(dir, error);
1280 eend = razor_uri_vtable_entries.data + razor_uri_vtable_entries.size;
1281 for (entry = razor_uri_vtable_entries.data; entry < eend; entry++) {
1282 od = ptr_array_find(&entry->open_directories, dir);
1284 if (entry->vtable.closedir)
1285 retval = entry->vtable.closedir(dir, error);
1291 razor_set_error(error, RAZOR_GENERAL_ERROR,
1292 RAZOR_GENERAL_ERROR_FAILED, NULL,
1293 "Invalid directory handle");
1298 RAZOR_EXPORT int razor_uri_set_vtable(const char *scheme,
1299 struct razor_uri_vtable *vtable, struct razor_error **error)
1301 struct razor_uri_vtable_entry *entry, *eend;
1303 if (!strcmp0(scheme, "file")) {
1305 * There's no fundamental reason why we couldn't support
1306 * this, but it's a lot of work without any obvious need.
1308 razor_set_error(error, RAZOR_GENERAL_ERROR,
1309 RAZOR_GENERAL_ERROR_FAILED, scheme,
1310 "Can't override file scheme handler");
1314 if (vtable->structure_size < sizeof(struct razor_uri_vtable)) {
1316 * A future version of the API might add vfuncs to the
1317 * table (which we ignore), but won't remove any.
1319 razor_set_error(error, RAZOR_GENERAL_ERROR,
1320 RAZOR_GENERAL_ERROR_FAILED, scheme,
1321 "Invalid vtable structure size");
1325 eend = razor_uri_vtable_entries.data + razor_uri_vtable_entries.size;
1326 for (entry = razor_uri_vtable_entries.data; entry < eend; entry++) {
1327 if (!strcmp0(entry->scheme, scheme))
1331 if (entry == eend) {
1334 entry = array_add(&razor_uri_vtable_entries, sizeof *entry);
1335 entry->scheme = scheme ? strdup(scheme) : NULL;
1336 array_init(&entry->open_files);
1337 array_init(&entry->open_directories);
1338 } else if (entry->open_files.size || entry->open_directories.size) {
1339 razor_set_error(error, RAZOR_GENERAL_ERROR,
1340 RAZOR_GENERAL_ERROR_FAILED, scheme,
1341 "URI handler busy");
1346 entry->vtable = *vtable;
1347 entry->vtable.structure_size = sizeof(struct razor_uri_vtable);
1349 free(entry->scheme);
1350 if (entry + 1 < eend)
1351 memmove(entry, entry + 1, eend - (entry + 1));
1352 array_set_size(&razor_uri_vtable_entries,
1353 razor_uri_vtable_entries.size - sizeof *entry);