|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include <syscfg/syscfg.h> |
| 21 | +#include <disk/disk.h> |
| 22 | +#include <stdlib.h> |
| 23 | +#include <string.h> |
| 24 | + |
| 25 | +struct disk_info { |
| 26 | + const char *disk_name; |
| 27 | + const char *fs_name; |
| 28 | + struct disk_ops *dops; |
| 29 | + |
| 30 | + SLIST_ENTRY(disk_info) sc_next; |
| 31 | +}; |
| 32 | + |
| 33 | +static SLIST_HEAD(, disk_info) disks = SLIST_HEAD_INITIALIZER(); |
| 34 | + |
| 35 | +/** |
| 36 | + * |
| 37 | + */ |
| 38 | +int disk_register(const char *disk_name, const char *fs_name, struct disk_ops *dops) |
| 39 | +{ |
| 40 | + struct disk_info *info = NULL; |
| 41 | + struct disk_info *sc; |
| 42 | + |
| 43 | + SLIST_FOREACH(sc, &disks, sc_next) { |
| 44 | + if (strcmp(sc->disk_name, disk_name) == 0) { |
| 45 | + return DISK_ENOENT; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + info = malloc(sizeof(struct disk_info)); |
| 50 | + if (!info) { |
| 51 | + return DISK_ENOMEM; |
| 52 | + } |
| 53 | + |
| 54 | + info->disk_name = disk_name; |
| 55 | + info->fs_name = fs_name; |
| 56 | + info->dops = dops; |
| 57 | + |
| 58 | + SLIST_INSERT_HEAD(&disks, info, sc_next); |
| 59 | + |
| 60 | + return 0; |
| 61 | +} |
| 62 | + |
| 63 | +struct disk_ops * |
| 64 | +disk_ops_for(const char *disk_name) |
| 65 | +{ |
| 66 | + struct disk_info *sc; |
| 67 | + |
| 68 | + if (disk_name) { |
| 69 | + SLIST_FOREACH(sc, &disks, sc_next) { |
| 70 | + if (strcmp(sc->disk_name, disk_name) == 0) { |
| 71 | + return sc->dops; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return NULL; |
| 77 | +} |
| 78 | + |
| 79 | +char * |
| 80 | +disk_fs_for(const char *disk_name) |
| 81 | +{ |
| 82 | + struct disk_info *sc; |
| 83 | + |
| 84 | + if (disk_name) { |
| 85 | + SLIST_FOREACH(sc, &disks, sc_next) { |
| 86 | + if (strcmp(sc->disk_name, disk_name) == 0) { |
| 87 | + return ((char *) sc->fs_name); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return NULL; |
| 93 | +} |
| 94 | + |
| 95 | +char * |
| 96 | +disk_name_from_path(const char *path) |
| 97 | +{ |
| 98 | + char *colon; |
| 99 | + uint8_t len; |
| 100 | + char *disk; |
| 101 | + |
| 102 | + colon = (char *) path; |
| 103 | + while (*colon && *colon != ':') { |
| 104 | + colon++; |
| 105 | + } |
| 106 | + |
| 107 | + if (*colon != ':') { |
| 108 | + return NULL; |
| 109 | + } |
| 110 | + |
| 111 | + len = colon - path; |
| 112 | + disk = malloc(len + 1); |
| 113 | + if (!disk) { |
| 114 | + return NULL; |
| 115 | + } |
| 116 | + memcpy(disk, path, len); |
| 117 | + disk[len] = '\0'; |
| 118 | + |
| 119 | + return disk; |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * @brief Returns the path with the disk prefix removed (if found) |
| 124 | + * |
| 125 | + * Paths should be given in the form disk<number>:/path. This routine |
| 126 | + * will parse and return only the path, removing the disk information. |
| 127 | + */ |
| 128 | +char * |
| 129 | +disk_filepath_from_path(const char *path) |
| 130 | +{ |
| 131 | + char *colon; |
| 132 | + char *filepath; |
| 133 | + size_t len; |
| 134 | + |
| 135 | + colon = (char *) path; |
| 136 | + while (*colon && *colon != ':') { |
| 137 | + colon++; |
| 138 | + } |
| 139 | + |
| 140 | + if (*colon != ':') { |
| 141 | + filepath = strdup(path); |
| 142 | + } else { |
| 143 | + colon++; |
| 144 | + len = strlen(colon); |
| 145 | + filepath = malloc(len); |
| 146 | + memcpy(filepath, colon, len); |
| 147 | + filepath[len] = '\0'; |
| 148 | + } |
| 149 | + |
| 150 | + return filepath; |
| 151 | +} |
0 commit comments