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