1 | /*
|
---|
2 | * Copyright (c) 2011 Oleg Romanenko
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup fs
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file exfat_directory.c
|
---|
35 | * @brief Functions that work with FAT directory.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include "exfat_directory.h"
|
---|
39 | #include "exfat_fat.h"
|
---|
40 | #include <libblock.h>
|
---|
41 | #include <errno.h>
|
---|
42 | #include <byteorder.h>
|
---|
43 | #include <mem.h>
|
---|
44 | #include <str.h>
|
---|
45 |
|
---|
46 |
|
---|
47 | void exfat_directory_init(exfat_directory_t *di)
|
---|
48 | {
|
---|
49 | di->b = NULL;
|
---|
50 | di->nodep = NULL;
|
---|
51 | di->bs = NULL;
|
---|
52 | di->blocks = 0;
|
---|
53 | di->pos = 0;
|
---|
54 | di->bnum = 0;
|
---|
55 | di->last = false;
|
---|
56 | }
|
---|
57 |
|
---|
58 | int exfat_directory_open(exfat_node_t *nodep, exfat_directory_t *di)
|
---|
59 | {
|
---|
60 | exfat_directory_init(di);
|
---|
61 | di->nodep = nodep;
|
---|
62 | if (di->nodep->type != EXFAT_DIRECTORY)
|
---|
63 | return EINVAL;
|
---|
64 |
|
---|
65 | di->bs = block_bb_get(di->nodep->idx->devmap_handle);
|
---|
66 | di->blocks = di->nodep->size / BPS(di->bs);
|
---|
67 | return EOK;
|
---|
68 | }
|
---|
69 |
|
---|
70 | int exfat_directory_close(exfat_directory_t *di)
|
---|
71 | {
|
---|
72 | int rc=EOK;
|
---|
73 |
|
---|
74 | if (di->b)
|
---|
75 | rc = block_put(di->b);
|
---|
76 |
|
---|
77 | return rc;
|
---|
78 | }
|
---|
79 |
|
---|
80 | static int exfat_directory_block_load(exfat_directory_t *di)
|
---|
81 | {
|
---|
82 | uint32_t i;
|
---|
83 | int rc;
|
---|
84 |
|
---|
85 | i = (di->pos * sizeof(exfat_dentry_t)) / BPS(di->bs);
|
---|
86 | if (i < di->blocks) {
|
---|
87 | if (di->b && di->bnum != i) {
|
---|
88 | block_put(di->b);
|
---|
89 | di->b = NULL;
|
---|
90 | }
|
---|
91 | if (!di->b) {
|
---|
92 | rc = exfat_block_get(&di->b, di->bs, di->nodep, i, BLOCK_FLAGS_NONE);
|
---|
93 | if (rc != EOK) {
|
---|
94 | di->b = NULL;
|
---|
95 | return rc;
|
---|
96 | }
|
---|
97 | di->bnum = i;
|
---|
98 | }
|
---|
99 | return EOK;
|
---|
100 | }
|
---|
101 | return ENOENT;
|
---|
102 | }
|
---|
103 |
|
---|
104 | int exfat_directory_next(exfat_directory_t *di)
|
---|
105 | {
|
---|
106 | int rc;
|
---|
107 |
|
---|
108 | di->pos += 1;
|
---|
109 | rc = exfat_directory_block_load(di);
|
---|
110 | if (rc!=EOK)
|
---|
111 | di->pos -= 1;
|
---|
112 |
|
---|
113 | return rc;
|
---|
114 | }
|
---|
115 |
|
---|
116 | int exfat_directory_prev(exfat_directory_t *di)
|
---|
117 | {
|
---|
118 | int rc=EOK;
|
---|
119 |
|
---|
120 | if (di->pos > 0) {
|
---|
121 | di->pos -= 1;
|
---|
122 | rc=exfat_directory_block_load(di);
|
---|
123 | }
|
---|
124 | else
|
---|
125 | return ENOENT;
|
---|
126 |
|
---|
127 | if (rc!=EOK)
|
---|
128 | di->pos += 1;
|
---|
129 |
|
---|
130 | return rc;
|
---|
131 | }
|
---|
132 |
|
---|
133 | int exfat_directory_seek(exfat_directory_t *di, aoff64_t pos)
|
---|
134 | {
|
---|
135 | aoff64_t _pos = di->pos;
|
---|
136 | int rc;
|
---|
137 |
|
---|
138 | di->pos = pos;
|
---|
139 | rc = exfat_directory_block_load(di);
|
---|
140 | if (rc!=EOK)
|
---|
141 | di->pos = _pos;
|
---|
142 |
|
---|
143 | return rc;
|
---|
144 | }
|
---|
145 |
|
---|
146 | int exfat_directory_get(exfat_directory_t *di, exfat_dentry_t **d)
|
---|
147 | {
|
---|
148 | int rc;
|
---|
149 |
|
---|
150 | rc = exfat_directory_block_load(di);
|
---|
151 | if (rc == EOK) {
|
---|
152 | aoff64_t o = di->pos % (BPS(di->bs) / sizeof(exfat_dentry_t));
|
---|
153 | *d = ((exfat_dentry_t *)di->b->data) + o;
|
---|
154 | }
|
---|
155 |
|
---|
156 | return rc;
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * @}
|
---|
162 | */
|
---|