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)
60 razor_archive_get_file_contents(const char *filename, int fd, const char *path,
61 size_t *length, struct razor_error **error)
69 struct archive_entry *entry;
71 a = archive_read_new();
72 archive_read_support_compression_all(a);
73 archive_read_support_format_all(a);
75 r = archive_read_open_fd(a, fd, 10240);
78 razor_set_error(error, RAZOR_POSIX_ERROR, archive_errno(a),
79 filename, archive_error_string(a));
80 archive_read_finish(a);
85 r = archive_read_next_header(a, &entry);
88 else if (r < ARCHIVE_WARN) {
89 razor_set_error(error, RAZOR_POSIX_ERROR,
90 archive_errno(a), filename,
91 archive_error_string(a));
92 archive_read_close(a);
93 archive_read_finish(a);
98 * TODO: Unicode support. Might need to wait for libarchive v4.
100 if (!strcmp(archive_entry_pathname(entry), path)) {
101 addr = malloc(archive_entry_size(entry));
103 archive_read_close(a);
104 archive_read_finish(a);
105 razor_set_error(error, RAZOR_POSIX_ERROR,
107 "Not enough memory");
112 r = archive_read_data_block(a, &buf, &size,
114 if (r == ARCHIVE_EOF)
116 if (r < ARCHIVE_OK) {
117 razor_set_error(error, RAZOR_POSIX_ERROR,
118 archive_errno(a), path,
119 archive_error_string(a));
124 memcpy((char *)addr + offset, buf, size);
127 archive_read_close(a);
128 archive_read_finish(a);
134 archive_read_close(a);
135 archive_read_finish(a);
137 razor_set_error(error, RAZOR_POSIX_ERROR, ENOENT, path,
138 "No such file or directory in archive");
144 razor_file_get_contents_archive(const char *filename, size_t *length,
145 struct razor_error **error)
148 char *path, *slash, *s;
151 path = strdup(filename);
152 slash = strrchr(path, '/');
156 fd = open(path, O_RDONLY | O_BINARY);
158 addr = razor_archive_get_file_contents(path, fd,
164 } else if (errno != ENOTDIR) {
166 razor_set_error_posix(error, filename);
169 s = strrchr(path, '/');
176 razor_set_error(error, RAZOR_POSIX_ERROR, ENOTDIR, filename,
182 #define OPEN_FILE_USED (1U<<0)
183 #define OPEN_FILE_MMAPPED (1U<<1)
191 struct array open_files = { 0, };
193 void *razor_file_get_contents(const char *filename, size_t *length, int private,
194 struct razor_error **error)
201 struct open_file *of, *ofend;
203 fd = open(filename, O_RDONLY | O_BINARY);
206 if (errno != ENOTDIR) {
207 razor_set_error_posix(error, filename);
210 addr = razor_file_get_contents_archive(filename, &size, error);
214 razor_set_error_posix(error, filename);
218 if (fstat(fd, &st) < 0) {
219 razor_set_error_posix(error, filename);
226 ofend = open_files.data + open_files.size;
227 for (of = open_files.data; of < ofend; of++)
228 if (!(of->flags & OPEN_FILE_USED))
231 of = array_add(&open_files, sizeof *of);
236 if (!addr && !private) {
237 addr = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
238 if (addr == MAP_FAILED)
241 of->flags = OPEN_FILE_USED | OPEN_FILE_MMAPPED;
243 #endif /* HAVE_SYS_MMAN_H */
250 res = read(fd, addr + nb, size - nb);
252 razor_set_error_posix(error, filename);
260 of->flags = OPEN_FILE_USED;
262 razor_set_error(error, RAZOR_POSIX_ERROR, ENOMEM, NULL,
263 "Not enough memory");
278 int razor_file_free_contents(void *addr, size_t length)
283 struct open_file *of, *ofend;
285 ofend = open_files.data + open_files.size;
286 for (of = open_files.data; of < ofend; of++)
287 if ((of->flags & OPEN_FILE_USED) && of->addr == addr)
295 mmapped = of->flags & OPEN_FILE_MMAPPED;
297 of->flags &= ~OPEN_FILE_USED;
299 for (of = open_files.data; of < ofend; of++)
300 if (of->flags & OPEN_FILE_USED)
304 array_release(&open_files);
305 array_init(&open_files);
310 return munmap(addr, length);
317 int razor_file_mkdir(const char *path, mode_t mode, struct razor_error **error)
322 retval = mkdir(path, mode);
326 * Ignore the case of a pre-existing directory and give
327 * an explicit error message if there is something other
328 * than a directory already at path.
331 if (code != EEXIST || stat(path, &buf))
332 razor_set_error(error, RAZOR_POSIX_ERROR, code, path,
334 else if (!S_ISDIR(buf.st_mode))
335 razor_set_error(error, RAZOR_POSIX_ERROR, code, path,
342 int razor_file_unlink(const char *path, struct razor_error **error)
346 retval = unlink(path);
349 razor_set_error_posix(error, path);
354 int razor_file_open(const char *path, int flags, mode_t mode,
355 struct razor_error **error)
359 retval = open(path, flags, mode);
362 razor_set_error_posix(error, path);
367 int razor_file_move(const char *path, const char *dest,
368 struct razor_error **error)
373 wchar_t *oldbuf, *newbuf;
374 const DWORD flags = MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING;
376 newbuf = razor_utf8_to_utf16(dest, -1);
377 oldbuf = razor_utf8_to_utf16(path, -1);
380 * Passing MOVEFILE_REPLACE_EXISTING to MoveFileEx() will
381 * cover every case we care about _except_ replacing an empty
382 * directory with a file. Calling RemoveDirectory() will deal
383 * with this case while having no effect in all other cases.
385 (void)RemoveDirectoryW(newbuf);
387 if (!MoveFileExW(oldbuf, newbuf, flags)) {
388 razor_set_error_mswin(error, newbuf, GetLastError());
398 if (rename(path, dest)) {
401 if (access(path, F_OK) < 0)
405 razor_set_error(error, RAZOR_POSIX_ERROR, code, object,
416 static char *absolute_path(const char *path)
419 char *result, *subpath, *p, *s, *t;
421 result = realpath(path, NULL);
423 if (!result && errno == ENOENT) {
429 result = strdup("/");
434 subpath = realpath(p, NULL);
438 len = strlen(subpath);
439 result = malloc(len + strlen(s) + 1);
440 memcpy(result, subpath, len);
441 strcpy(result + len, s);
444 } else if (errno != ENOENT)
453 result = realpath(".", NULL);
462 int razor_file_is_directory(const char *path, struct razor_error **error)
466 if (stat(path, &st) < 0) {
467 razor_set_error_posix(error, path);
471 return !!S_ISDIR(st.st_mode);
474 char *razor_file_mkdtemp_near(const char *path, const char *template,
475 struct razor_error **error)
480 if (path[0]=='\\' && path[1]=='\\' && path[2] && path[2]!='\\'
481 && strchr(path+3,'\\')) {
482 /* We have a UNC path: \\servername\sharename... */
483 const char *sharename, *root;
486 sharename = strchr(path+3,'\\')+1;
487 root = strchr(sharename,'\\');
489 disklen = root - path;
491 disklen = strlen(path);
493 retval = malloc(disklen + 1 + strlen(template) + 1);
494 memcpy(retval, path, disklen);
495 retval[disklen] = '\\';
496 strcpy(retval + disklen + 1, template);
497 } else if ((*path>='A' && *path<='Z' || *path>='a' && *path<='z') &&
499 retval = malloc(3 + strlen(template) + 1);
503 strcpy(retval + 3, template);
509 n = GetCurrentDirectoryW(0, NULL);
510 buf = malloc(n * sizeof(wchar_t));
512 if (GetCurrentDirectoryW(n, buf)) {
513 dir = razor_utf16_to_utf8(buf, n - 1);
515 retval = razor_file_mkdtemp_near(dir, template, error);
519 retval = malloc(3 + strlen(template) + 1);
523 strcpy(retval + 3, template);
530 * Find the mount point (assuming we can write to the
531 * whole filesystem). Otherwise stop at the first
532 * unwritable directory and take one step back.
534 char *s, *abspath, saved;
535 int len, can_step_back = 0;
539 abspath = absolute_path(path);
541 razor_set_error_posix(error, path);
545 if (stat(abspath, &buf) < 0) {
549 razor_set_error_posix(error, abspath);
554 filesystem = buf.st_dev;
556 len = strlen(abspath);
557 while(len > 1 && (s = strrchr(abspath, '/'))) {
561 len = s + 1 - abspath;
567 if (stat(abspath, &buf) < 0) {
571 razor_set_error_posix(error, abspath);
575 } else if (!filesystem)
576 filesystem = buf.st_dev;
578 if (buf.st_dev != filesystem || access(abspath, W_OK)) {
585 len = strlen(abspath);
592 len = 0; /* Avoid an unslightly double slash. */
593 retval = malloc(len + 1 + strlen(template) + 1);
594 memcpy(retval, abspath, len);
596 strcpy(retval + len + 1, template);
601 if (!mkdtemp(retval)) {
606 char *s = strdup(template);
609 if (stat(".", &buf) < 0) {
610 razor_set_error_posix(error, ".");
615 if (buf.st_dev != filesystem)
617 * Don't use a different filesystem. It will
618 * only fail later on (in rename) and cause
619 * an unhelpful error message (EXDEV).
633 razor_set_error(error, RAZOR_POSIX_ERROR, err, retval,
654 #define OPEN_DIR_USED (1U<<0)
656 struct array open_dirs = { 0, };
658 void *razor_file_opendir(const char *path, struct razor_error **error)
660 struct open_dir *od, *odend;
662 odend = open_dirs.data + open_dirs.size;
663 for (od = open_dirs.data; od < odend; od++)
664 if (!(od->flags & OPEN_DIR_USED))
667 od = array_add(&open_dirs, sizeof *od);
672 od->path2 = razor_utf8_to_utf16(path, -1);
673 od->dp = _wopendir(od->path2);
675 od->path = strdup(path);
676 od->dp = opendir(od->path);
681 razor_set_error_mswin(error, od->path2, GetLastError());
684 razor_set_error_posix(error, od->path);
690 od->flags = OPEN_DIR_USED;
694 char *razor_file_readdir(void *dp, struct razor_error **error)
696 struct open_dir *od = dp, *odend;
698 struct _wdirent *dirp;
704 odend = open_dirs.data + open_dirs.size;
705 if (dp < open_dirs.data || od >= odend || !(od->flags & OPEN_DIR_USED))
711 while((dirp = _wreaddir(od->dp))) {
712 path = razor_utf16_to_utf8(dirp->d_name, -1);
713 if (strcmp(path, ".") && strcmp(path, ".."))
719 while((dirp = readdir(od->dp)))
720 if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, ".."))
721 return strdup(dirp->d_name);
726 razor_set_error_mswin(error, od->path2, GetLastError());
728 razor_set_error_posix(error, od->path);
735 int razor_file_closedir(void *dp, struct razor_error **error)
737 struct open_dir *od = dp, *odend;
740 odend = open_dirs.data + open_dirs.size;
741 if (dp < open_dirs.data || od >= odend || !(od->flags & OPEN_DIR_USED))
746 * I can't find documentation to state that _wclosedir()
747 * returns -1 on failure, so be paranoid.
749 retval = _wclosedir(od->dp) ? -1 : 0;
751 retval = closedir(od->dp);
756 razor_set_error_mswin(error, od->path2, GetLastError());
758 razor_set_error_posix(error, od->path);
768 od->flags &= ~OPEN_DIR_USED;
770 for (od = open_dirs.data; od < odend; od++)
771 if (od->flags & OPEN_DIR_USED)
775 array_release(&open_dirs);
776 array_init(&open_dirs);
782 struct razor_uri_vtable_entry {
783 struct razor_uri_vtable vtable;
785 struct array open_files, open_directories;
788 static struct array razor_uri_vtable_entries;
790 static struct razor_uri_vtable_entry *
791 razor_uri_get_vtable_entry(const char *scheme)
793 struct razor_uri_vtable_entry *entry, *eend, *fallback = NULL;
795 eend = razor_uri_vtable_entries.data + razor_uri_vtable_entries.size;
796 for(entry = razor_uri_vtable_entries.data; entry < eend; entry++) {
797 if (!strcmp0(entry->scheme, scheme))
799 else if (!entry->scheme)
807 razor_uri_mkdir(const char *uri, mode_t mode, struct razor_error **error)
812 struct razor_uri_vtable_entry *entry;
813 struct razor_error *tmp_error = NULL;
815 if (razor_uri_parse(&ru, uri, error))
818 path = razor_path_from_parsed_uri(&ru, &tmp_error);
820 if (razor_error_matches(tmp_error, RAZOR_GENERAL_ERROR,
821 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI))
822 razor_error_free(tmp_error);
824 razor_propagate_error(error, tmp_error, NULL);
825 razor_uri_destroy(&ru);
830 razor_uri_destroy(&ru);
831 retval = razor_file_mkdir(path, mode, error);
834 entry = razor_uri_get_vtable_entry(ru.scheme);
835 razor_uri_destroy(&ru);
836 if (!entry || !entry->vtable.mkdir) {
838 razor_set_error(error, RAZOR_GENERAL_ERROR,
839 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI,
840 uri, "No URI handler installed");
842 retval = entry->vtable.mkdir(uri, mode, error);
849 razor_uri_unlink(const char *uri, struct razor_error **error)
854 struct razor_uri_vtable_entry *entry;
855 struct razor_error *tmp_error = NULL;
857 if (razor_uri_parse(&ru, uri, error))
860 path = razor_path_from_parsed_uri(&ru, &tmp_error);
862 if (razor_error_matches(tmp_error, RAZOR_GENERAL_ERROR,
863 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI))
864 razor_error_free(tmp_error);
866 razor_propagate_error(error, tmp_error, NULL);
867 razor_uri_destroy(&ru);
872 razor_uri_destroy(&ru);
873 retval = razor_file_unlink(path, error);
876 entry = razor_uri_get_vtable_entry(ru.scheme);
877 razor_uri_destroy(&ru);
878 if (!entry || !entry->vtable.unlink) {
880 razor_set_error(error, RAZOR_GENERAL_ERROR,
881 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI,
882 uri, "No URI handler installed");
884 retval = entry->vtable.unlink(uri, error);
891 razor_uri_open(const char *uri, int flags, mode_t mode,
892 struct razor_error **error)
897 struct razor_uri_vtable_entry *entry;
898 struct razor_error *tmp_error = NULL;
900 if (razor_uri_parse(&ru, uri, error))
903 path = razor_path_from_parsed_uri(&ru, &tmp_error);
905 if (razor_error_matches(tmp_error, RAZOR_GENERAL_ERROR,
906 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI))
907 razor_error_free(tmp_error);
909 razor_propagate_error(error, tmp_error, NULL);
910 razor_uri_destroy(&ru);
915 razor_uri_destroy(&ru);
916 retval = razor_file_open(path, flags, mode, error);
919 entry = razor_uri_get_vtable_entry(ru.scheme);
920 razor_uri_destroy(&ru);
921 if (!entry || !entry->vtable.open) {
923 razor_set_error(error, RAZOR_GENERAL_ERROR,
924 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI,
925 uri, "No URI handler installed");
927 retval = entry->vtable.open(uri, flags, mode, error);
934 razor_uri_move(const char *src_uri, const char *dst_uri,
935 struct razor_error **error)
938 char *src_path, *dst_path;
939 struct razor_uri src_ru, dst_ru;
940 struct razor_uri_vtable_entry *entry;
941 struct razor_error *tmp_error = NULL;
943 if (razor_uri_parse(&src_ru, src_uri, error) ||
944 razor_uri_parse(&dst_ru, dst_uri, error))
947 src_path = razor_path_from_parsed_uri(&src_ru, &tmp_error);
949 if (razor_error_matches(tmp_error, RAZOR_GENERAL_ERROR,
950 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI))
951 razor_error_free(tmp_error);
952 else if (!src_path) {
953 razor_propagate_error(error, tmp_error, NULL);
954 razor_uri_destroy(&src_ru);
958 dst_path = razor_path_from_parsed_uri(&dst_ru, &tmp_error);
960 if (razor_error_matches(tmp_error, RAZOR_GENERAL_ERROR,
961 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI))
962 razor_error_free(tmp_error);
963 else if (!dst_path) {
964 razor_propagate_error(error, tmp_error, NULL);
965 razor_uri_destroy(&dst_ru);
966 razor_uri_destroy(&src_ru);
971 if (src_path && dst_path)
972 retval = razor_file_move(src_path, dst_path, error);
974 if (!strcmp(src_ru.scheme, dst_ru.scheme))
975 entry = razor_uri_get_vtable_entry(src_ru.scheme);
978 if (entry && entry->vtable.move)
979 retval = entry->vtable.move(src_uri, dst_uri, error);
980 else if (strcmp(src_ru.scheme, dst_ru.scheme)) {
982 razor_set_error(error, RAZOR_GENERAL_ERROR,
983 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI,
984 dst_uri, "Cross-scheme URI move");
987 razor_set_error(error, RAZOR_GENERAL_ERROR,
988 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI,
989 src_path ? dst_uri : src_uri,
990 "No URI handler installed");
994 razor_uri_destroy(&src_ru);
995 razor_uri_destroy(&dst_ru);
1003 razor_uri_get_contents(const char *uri, size_t *length, int private,
1004 struct razor_error **error)
1008 struct razor_uri ru;
1009 struct razor_uri_vtable_entry *entry;
1010 struct razor_error *tmp_error = NULL;
1012 if (razor_uri_parse(&ru, uri, error))
1015 path = razor_path_from_parsed_uri(&ru, &tmp_error);
1017 if (razor_error_matches(tmp_error, RAZOR_GENERAL_ERROR,
1018 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI))
1019 razor_error_free(tmp_error);
1021 razor_propagate_error(error, tmp_error, NULL);
1022 razor_uri_destroy(&ru);
1027 razor_uri_destroy(&ru);
1028 retval = razor_file_get_contents(path, length, private, error);
1031 entry = razor_uri_get_vtable_entry(ru.scheme);
1032 razor_uri_destroy(&ru);
1033 if (!entry || !entry->vtable.get_contents) {
1035 razor_set_error(error, RAZOR_GENERAL_ERROR,
1036 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI,
1037 uri, "No URI handler installed");
1039 retval = entry->vtable.get_contents(uri, length,
1042 ptr_array_add(&entry->open_files, retval);
1049 RAZOR_EXPORT int razor_uri_free_contents(void *addr, size_t length)
1052 struct razor_uri_vtable_entry *entry, *eend;
1057 retval = razor_file_free_contents(addr, length);
1062 eend = razor_uri_vtable_entries.data + razor_uri_vtable_entries.size;
1063 for (entry = razor_uri_vtable_entries.data; entry < eend; entry++) {
1064 of = ptr_array_find(&entry->open_files, addr);
1066 if (entry->vtable.free_contents)
1067 retval = entry->vtable.free_contents(addr,
1069 ptr_array_remove_index(&entry->open_files, of);
1078 razor_uri_is_directory(const char *uri, struct razor_error **error)
1082 struct razor_uri ru;
1083 struct razor_uri_vtable_entry *entry;
1084 struct razor_error *tmp_error = NULL;
1086 if (razor_uri_parse(&ru, uri, error))
1089 path = razor_path_from_parsed_uri(&ru, &tmp_error);
1091 if (razor_error_matches(tmp_error, RAZOR_GENERAL_ERROR,
1092 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI))
1093 razor_error_free(tmp_error);
1095 razor_propagate_error(error, tmp_error, NULL);
1096 razor_uri_destroy(&ru);
1101 razor_uri_destroy(&ru);
1102 retval = razor_file_is_directory(path, error);
1105 entry = razor_uri_get_vtable_entry(ru.scheme);
1106 razor_uri_destroy(&ru);
1107 if (!entry || !entry->vtable.is_directory) {
1109 razor_set_error(error, RAZOR_GENERAL_ERROR,
1110 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI,
1111 uri, "No URI handler installed");
1113 retval = entry->vtable.is_directory(uri, error);
1120 razor_uri_mkdtemp_near(const char *uri, const char *template,
1121 struct razor_error **error)
1125 struct razor_uri ru;
1126 struct razor_uri_vtable_entry *entry;
1127 struct razor_error *tmp_error = NULL;
1129 if (razor_uri_parse(&ru, uri, error))
1132 path = razor_path_from_parsed_uri(&ru, &tmp_error);
1134 if (razor_error_matches(tmp_error, RAZOR_GENERAL_ERROR,
1135 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI))
1136 razor_error_free(tmp_error);
1138 razor_propagate_error(error, tmp_error, NULL);
1139 razor_uri_destroy(&ru);
1144 razor_uri_destroy(&ru);
1145 s = razor_file_mkdtemp_near(path, template, error);
1147 retval = razor_path_to_uri(s);
1153 entry = razor_uri_get_vtable_entry(ru.scheme);
1154 razor_uri_destroy(&ru);
1155 if (!entry || !entry->vtable.mkdtemp_near) {
1157 razor_set_error(error, RAZOR_GENERAL_ERROR,
1158 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI,
1159 uri, "No URI handler installed");
1161 retval = entry->vtable.mkdtemp_near(uri, template,
1169 razor_uri_opendir(const char *uri, struct razor_error **error)
1173 struct razor_uri ru;
1174 struct razor_uri_vtable_entry *entry;
1175 struct razor_error *tmp_error = NULL;
1177 if (razor_uri_parse(&ru, uri, error))
1180 path = razor_path_from_parsed_uri(&ru, &tmp_error);
1182 if (razor_error_matches(tmp_error, RAZOR_GENERAL_ERROR,
1183 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI))
1184 razor_error_free(tmp_error);
1186 razor_propagate_error(error, tmp_error, NULL);
1187 razor_uri_destroy(&ru);
1192 razor_uri_destroy(&ru);
1193 retval = razor_file_opendir(path, error);
1196 entry = razor_uri_get_vtable_entry(ru.scheme);
1197 razor_uri_destroy(&ru);
1198 if (!entry || !entry->vtable.opendir) {
1200 razor_set_error(error, RAZOR_GENERAL_ERROR,
1201 RAZOR_GENERAL_ERROR_UNSUPPORTED_URI,
1202 uri, "No URI handler installed");
1204 retval = entry->vtable.opendir(uri, error);
1206 ptr_array_add(&entry->open_directories, retval);
1213 RAZOR_EXPORT char *razor_uri_readdir(void *dir, struct razor_error **error)
1217 struct razor_uri_vtable_entry *entry, *eend;
1219 retval = razor_file_readdir(dir, error);
1221 if (retval != (char *)-1)
1224 eend = razor_uri_vtable_entries.data + razor_uri_vtable_entries.size;
1225 for (entry = razor_uri_vtable_entries.data; entry < eend; entry++) {
1226 od = ptr_array_find(&entry->open_directories, dir);
1228 if (entry->vtable.readdir)
1229 retval = entry->vtable.readdir(dir, error);
1234 if (retval == (char *)-1) {
1236 razor_set_error(error, RAZOR_GENERAL_ERROR,
1237 RAZOR_GENERAL_ERROR_FAILED, NULL,
1238 "Invalid directory handle");
1244 RAZOR_EXPORT int razor_uri_closedir(void *dir, struct razor_error **error)
1248 struct razor_uri_vtable_entry *entry, *eend;
1250 retval = razor_file_closedir(dir, error);
1255 eend = razor_uri_vtable_entries.data + razor_uri_vtable_entries.size;
1256 for (entry = razor_uri_vtable_entries.data; entry < eend; entry++) {
1257 od = ptr_array_find(&entry->open_directories, dir);
1259 if (entry->vtable.closedir)
1260 retval = entry->vtable.closedir(dir, error);
1266 razor_set_error(error, RAZOR_GENERAL_ERROR,
1267 RAZOR_GENERAL_ERROR_FAILED, NULL,
1268 "Invalid directory handle");
1273 RAZOR_EXPORT int razor_uri_set_vtable(const char *scheme,
1274 struct razor_uri_vtable *vtable, struct razor_error **error)
1276 struct razor_uri_vtable_entry *entry, *eend;
1278 if (!strcmp0(scheme, "file")) {
1280 * There's no fundamental reason why we couldn't support
1281 * this, but it's a lot of work without any obvious need.
1283 razor_set_error(error, RAZOR_GENERAL_ERROR,
1284 RAZOR_GENERAL_ERROR_FAILED, scheme,
1285 "Can't override file scheme handler");
1289 if (vtable->structure_size < sizeof(struct razor_uri_vtable)) {
1291 * A future version of the API might add vfuncs to the
1292 * table (which we ignore), but won't remove any.
1294 razor_set_error(error, RAZOR_GENERAL_ERROR,
1295 RAZOR_GENERAL_ERROR_FAILED, scheme,
1296 "Invalid vtable structure size");
1300 eend = razor_uri_vtable_entries.data + razor_uri_vtable_entries.size;
1301 for (entry = razor_uri_vtable_entries.data; entry < eend; entry++) {
1302 if (!strcmp0(entry->scheme, scheme))
1306 if (entry == eend) {
1309 entry = array_add(&razor_uri_vtable_entries, sizeof *entry);
1310 entry->scheme = scheme ? strdup(scheme) : NULL;
1311 array_init(&entry->open_files);
1312 array_init(&entry->open_directories);
1313 } else if (entry->open_files.size || entry->open_directories.size) {
1314 razor_set_error(error, RAZOR_GENERAL_ERROR,
1315 RAZOR_GENERAL_ERROR_FAILED, scheme,
1316 "URI handler busy");
1321 entry->vtable = *vtable;
1322 entry->vtable.structure_size = sizeof(struct razor_uri_vtable);
1324 free(entry->scheme);
1325 if (entry + 1 < eend)
1326 memmove(entry, entry + 1, eend - (entry + 1));
1327 array_set_size(&razor_uri_vtable_entries,
1328 razor_uri_vtable_entries.size - sizeof *entry);