3 exFAT file system implementation library.
5 Copyright (C) 2010-2013 Andrew Nayenko
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU 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.
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 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 int exfat_opendir(struct exfat* ef, struct exfat_node* dir,
27 struct exfat_iterator* it)
34 rc = exfat_cache_directory(ef, dir);
36 exfat_put_node(ef, dir);
40 void exfat_closedir(struct exfat* ef, struct exfat_iterator* it)
42 exfat_put_node(ef, it->parent);
47 struct exfat_node* exfat_readdir(struct exfat* ef, struct exfat_iterator* it)
49 if (it->current == NULL)
50 it->current = it->parent->child;
52 it->current = it->current->next;
54 if (it->current != NULL)
55 return exfat_get_node(it->current);
60 static int compare_char(struct exfat* ef, uint16_t a, uint16_t b)
62 if (a >= ef->upcase_chars || b >= ef->upcase_chars)
63 return (int) a - (int) b;
65 return (int) le16_to_cpu(ef->upcase[a]) - (int) le16_to_cpu(ef->upcase[b]);
68 static int compare_name(struct exfat* ef, const le16_t* a, const le16_t* b)
70 while (le16_to_cpu(*a) && le16_to_cpu(*b))
72 int rc = compare_char(ef, le16_to_cpu(*a), le16_to_cpu(*b));
78 return compare_char(ef, le16_to_cpu(*a), le16_to_cpu(*b));
81 static int lookup_name(struct exfat* ef, struct exfat_node* parent,
82 struct exfat_node** node, const char* name, size_t n)
84 struct exfat_iterator it;
85 le16_t buffer[EXFAT_NAME_MAX + 1];
90 rc = utf8_to_utf16(buffer, name, EXFAT_NAME_MAX, n);
94 rc = exfat_opendir(ef, parent, &it);
97 while ((*node = exfat_readdir(ef, &it)))
99 if (compare_name(ef, buffer, (*node)->name) == 0)
101 exfat_closedir(ef, &it);
104 exfat_put_node(ef, *node);
106 exfat_closedir(ef, &it);
110 static size_t get_comp(const char* path, const char** comp)
114 *comp = path + strspn(path, "/"); /* skip leading slashes */
115 end = strchr(*comp, '/');
117 return strlen(*comp);
122 int exfat_lookup(struct exfat* ef, struct exfat_node** node,
125 struct exfat_node* parent;
130 /* start from the root directory */
131 parent = *node = exfat_get_node(ef->root);
132 for (p = path; (n = get_comp(p, &p)); p += n)
134 if (n == 1 && *p == '.') /* skip "." component */
136 rc = lookup_name(ef, parent, node, p, n);
139 exfat_put_node(ef, parent);
142 exfat_put_node(ef, parent);
148 static bool is_last_comp(const char* comp, size_t length)
150 const char* p = comp + length;
152 return get_comp(p, &p) == 0;
155 static bool is_allowed(const char* comp, size_t length)
159 for (i = 0; i < length; i++)
177 int exfat_split(struct exfat* ef, struct exfat_node** parent,
178 struct exfat_node** node, le16_t* name, const char* path)
184 memset(name, 0, (EXFAT_NAME_MAX + 1) * sizeof(le16_t));
185 *parent = *node = exfat_get_node(ef->root);
186 for (p = path; (n = get_comp(p, &p)); p += n)
188 if (n == 1 && *p == '.')
190 if (is_last_comp(p, n))
192 if (!is_allowed(p, n))
194 /* contains characters that are not allowed */
195 exfat_put_node(ef, *parent);
198 rc = utf8_to_utf16(name, p, EXFAT_NAME_MAX, n);
201 exfat_put_node(ef, *parent);
205 rc = lookup_name(ef, *parent, node, p, n);
206 if (rc != 0 && rc != -ENOENT)
208 exfat_put_node(ef, *parent);
213 rc = lookup_name(ef, *parent, node, p, n);
216 exfat_put_node(ef, *parent);
219 exfat_put_node(ef, *parent);
222 exfat_bug("impossible");