1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Constants/Data Structures/Prototypes for Operations on "fat-file" |
---|
5 | * |
---|
6 | * @ingroup libfs_ff |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * |
---|
11 | * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia |
---|
12 | * Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru> |
---|
13 | * |
---|
14 | * The license and distribution terms for this file may be |
---|
15 | * found in the file LICENSE in this distribution or at |
---|
16 | * http://www.rtems.org/license/LICENSE. |
---|
17 | */ |
---|
18 | |
---|
19 | #ifndef __DOSFS_FAT_FILE_H__ |
---|
20 | #define __DOSFS_FAT_FILE_H__ |
---|
21 | |
---|
22 | #include <rtems.h> |
---|
23 | #include <rtems/libio_.h> |
---|
24 | |
---|
25 | #include <time.h> |
---|
26 | |
---|
27 | #include "fat.h" |
---|
28 | |
---|
29 | /** |
---|
30 | * @defgroup libfs_ff Fat File |
---|
31 | * |
---|
32 | * @ingroup libfs |
---|
33 | */ |
---|
34 | /**@{*/ |
---|
35 | |
---|
36 | #ifdef __cplusplus |
---|
37 | extern "C" { |
---|
38 | #endif |
---|
39 | |
---|
40 | /** |
---|
41 | * @brief The "fat-file" representation. |
---|
42 | * |
---|
43 | * the idea is: fat-file is nothing but a cluster chain, any open fat-file is |
---|
44 | * represented in system by fat-file descriptor and has well-known |
---|
45 | * file interface: |
---|
46 | * |
---|
47 | * fat_file_open() |
---|
48 | * fat_file_close() |
---|
49 | * fat_file_read() |
---|
50 | * fat_file_write() |
---|
51 | * |
---|
52 | * Such interface hides the architecture of fat-file and represents it like |
---|
53 | * linear file |
---|
54 | */ |
---|
55 | typedef rtems_filesystem_node_types_t fat_file_type_t; |
---|
56 | |
---|
57 | #define FAT_DIRECTORY RTEMS_FILESYSTEM_DIRECTORY |
---|
58 | #define FAT_FILE RTEMS_FILESYSTEM_MEMORY_FILE |
---|
59 | |
---|
60 | typedef struct fat_file_map_s |
---|
61 | { |
---|
62 | uint32_t file_cln; |
---|
63 | uint32_t disk_cln; |
---|
64 | uint32_t last_cln; |
---|
65 | } fat_file_map_t; |
---|
66 | |
---|
67 | /** |
---|
68 | * @brief Descriptor of a fat-file. |
---|
69 | * |
---|
70 | * To each particular clusters chain |
---|
71 | */ |
---|
72 | typedef struct fat_file_fd_s |
---|
73 | { |
---|
74 | rtems_chain_node link; /* |
---|
75 | * fat-file descriptors organized into hash; |
---|
76 | * collision lists are handled via link |
---|
77 | * field |
---|
78 | */ |
---|
79 | uint32_t links_num; /* |
---|
80 | * the number of fat_file_open call on |
---|
81 | * this fat-file |
---|
82 | */ |
---|
83 | uint32_t ino; /* inode, file serial number :)))) */ |
---|
84 | fat_file_type_t fat_file_type; |
---|
85 | uint32_t size_limit; |
---|
86 | uint32_t fat_file_size; /* length */ |
---|
87 | uint32_t cln; |
---|
88 | fat_dir_pos_t dir_pos; |
---|
89 | uint8_t flags; |
---|
90 | fat_file_map_t map; |
---|
91 | time_t mtime; |
---|
92 | |
---|
93 | } fat_file_fd_t; |
---|
94 | |
---|
95 | #define FAT_FILE_REMOVED 0x01 |
---|
96 | |
---|
97 | #define FAT_FILE_IS_REMOVED(p)\ |
---|
98 | (((p)->flags & FAT_FILE_REMOVED) ? 1 : 0) |
---|
99 | |
---|
100 | /* ioctl macros */ |
---|
101 | #define F_CLU_NUM 0x01 |
---|
102 | |
---|
103 | /* |
---|
104 | * Each file and directory on a MSDOS volume is unique identified by it |
---|
105 | * location, i.e. location of it 32 Bytes Directory Entry Structure. We can |
---|
106 | * distinguish them by cluster number it locates on and offset inside this |
---|
107 | * cluster. But root directory on any volumes (FAT12/16/32) has no 32 Bytes |
---|
108 | * Directory Entry Structure corresponded to it. So we assume 32 Bytes |
---|
109 | * Directory Entry Structure of root directory locates at cluster 1 (invalid |
---|
110 | * cluaster number) and offset 0 |
---|
111 | */ |
---|
112 | #define FAT_ROOTDIR_CLUSTER_NUM 0x01 |
---|
113 | |
---|
114 | #define FAT_FD_OF_ROOT_DIR(fat_fd) \ |
---|
115 | ((fat_fd->dir_pos.sname.cln == FAT_ROOTDIR_CLUSTER_NUM) && \ |
---|
116 | (fat_fd->dir_pos.sname.ofs == 0)) |
---|
117 | |
---|
118 | #define FAT_EOF 0x00 |
---|
119 | |
---|
120 | /* @brief Construct key for hash access. |
---|
121 | * |
---|
122 | * Construct key for hash access: convert (cluster num, offset) to |
---|
123 | * (sector512 num, new offset) and than construct key as |
---|
124 | * key = (sector512 num) << 4 | (new offset) |
---|
125 | * |
---|
126 | * @param[in] cl - cluster number |
---|
127 | * @param[in] ofs - offset inside cluster 'cl' |
---|
128 | * @param[in] fs_info - FS info |
---|
129 | * |
---|
130 | * @retval constructed key |
---|
131 | */ |
---|
132 | static inline uint32_t |
---|
133 | fat_construct_key( |
---|
134 | const fat_fs_info_t *fs_info, |
---|
135 | fat_pos_t *pos) |
---|
136 | { |
---|
137 | return ( ((fat_cluster_num_to_sector512_num(fs_info, pos->cln) + |
---|
138 | (pos->ofs >> FAT_SECTOR512_BITS)) << 4) + |
---|
139 | ((pos->ofs >> 5) & (FAT_DIRENTRIES_PER_SEC512 - 1)) ); |
---|
140 | } |
---|
141 | |
---|
142 | /* Prototypes for "fat-file" operations */ |
---|
143 | int |
---|
144 | fat_file_open(fat_fs_info_t *fs_info, |
---|
145 | fat_dir_pos_t *dir_pos, |
---|
146 | fat_file_fd_t **fat_fd); |
---|
147 | |
---|
148 | int |
---|
149 | fat_file_reopen(fat_file_fd_t *fat_fd); |
---|
150 | |
---|
151 | int |
---|
152 | fat_file_close(fat_fs_info_t *fs_info, |
---|
153 | fat_file_fd_t *fat_fd); |
---|
154 | |
---|
155 | ssize_t |
---|
156 | fat_file_read(fat_fs_info_t *fs_info, |
---|
157 | fat_file_fd_t *fat_fd, |
---|
158 | uint32_t start, |
---|
159 | uint32_t count, |
---|
160 | uint8_t *buf); |
---|
161 | |
---|
162 | ssize_t |
---|
163 | fat_file_write(fat_fs_info_t *fs_info, |
---|
164 | fat_file_fd_t *fat_fd, |
---|
165 | uint32_t start, |
---|
166 | uint32_t count, |
---|
167 | const uint8_t *buf); |
---|
168 | |
---|
169 | int |
---|
170 | fat_file_extend(fat_fs_info_t *fs_info, |
---|
171 | fat_file_fd_t *fat_fd, |
---|
172 | bool zero_fill, |
---|
173 | uint32_t new_length, |
---|
174 | uint32_t *a_length); |
---|
175 | |
---|
176 | int |
---|
177 | fat_file_truncate(fat_fs_info_t *fs_info, |
---|
178 | fat_file_fd_t *fat_fd, |
---|
179 | uint32_t new_length); |
---|
180 | |
---|
181 | int |
---|
182 | fat_file_ioctl(fat_fs_info_t *fs_info, |
---|
183 | fat_file_fd_t *fat_fd, |
---|
184 | int cmd, |
---|
185 | ...); |
---|
186 | |
---|
187 | int |
---|
188 | fat_file_size(fat_fs_info_t *fs_info, |
---|
189 | fat_file_fd_t *fat_fd); |
---|
190 | |
---|
191 | void |
---|
192 | fat_file_mark_removed(fat_fs_info_t *fs_info, |
---|
193 | fat_file_fd_t *fat_fd); |
---|
194 | |
---|
195 | #ifdef __cplusplus |
---|
196 | } |
---|
197 | #endif |
---|
198 | /**@}*/ |
---|
199 | #endif /* __DOSFS_FAT_FILE_H__ */ |
---|