RTEMS  5.1
fat_file.h
Go to the documentation of this file.
1 
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 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 typedef enum {
41  FAT_DIRECTORY = 0,
42  FAT_HARD_LINK = 2, /* pseudo type */
43  FAT_FILE = 4
44 } fat_file_type_t;
45 
61 typedef struct fat_file_map_s
62 {
63  uint32_t file_cln;
64  uint32_t disk_cln;
65  uint32_t last_cln;
67 
73 typedef struct fat_file_fd_s
74 {
75  rtems_chain_node link; /*
76  * fat-file descriptors organized into hash;
77  * collision lists are handled via link
78  * field
79  */
80  uint32_t links_num; /*
81  * the number of fat_file_open call on
82  * this fat-file
83  */
84  uint32_t ino; /* inode, file serial number :)))) */
85  fat_file_type_t fat_file_type;
86  uint32_t size_limit;
87  uint32_t fat_file_size; /* length */
88  uint32_t cln;
89  fat_dir_pos_t dir_pos;
90  uint8_t flags;
92  time_t ctime;
93  time_t mtime;
94 
96 
97 #define FAT_FILE_REMOVED 0x01
98 
99 #define FAT_FILE_META_DATA_CHANGED 0x02
100 
101 static inline bool FAT_FILE_IS_REMOVED(const fat_file_fd_t *fat_fd)
102 {
103  return (fat_fd->flags & FAT_FILE_REMOVED) != 0;
104 }
105 
106 static inline bool FAT_FILE_HAS_META_DATA_CHANGED(const fat_file_fd_t *fat_fd)
107 {
108  return (fat_fd->flags & FAT_FILE_META_DATA_CHANGED) != 0;
109 }
110 
111 /* ioctl macros */
112 #define F_CLU_NUM 0x01
113 
114 /*
115  * Each file and directory on a MSDOS volume is unique identified by it
116  * location, i.e. location of it 32 Bytes Directory Entry Structure. We can
117  * distinguish them by cluster number it locates on and offset inside this
118  * cluster. But root directory on any volumes (FAT12/16/32) has no 32 Bytes
119  * Directory Entry Structure corresponded to it. So we assume 32 Bytes
120  * Directory Entry Structure of root directory locates at cluster 1 (invalid
121  * cluaster number) and offset 0
122  */
123 #define FAT_ROOTDIR_CLUSTER_NUM 0x01
124 
125 #define FAT_FD_OF_ROOT_DIR(fat_fd) \
126  ((fat_fd->dir_pos.sname.cln == FAT_ROOTDIR_CLUSTER_NUM) && \
127  (fat_fd->dir_pos.sname.ofs == 0))
128 
129 #define FAT_EOF 0x00
130 
131 /* @brief Construct key for hash access.
132  *
133  * Construct key for hash access: convert (cluster num, offset) to
134  * (sector512 num, new offset) and than construct key as
135  * key = (sector512 num) << 4 | (new offset)
136  *
137  * @param[in] cl - cluster number
138  * @param[in] ofs - offset inside cluster 'cl'
139  * @param[in] fs_info - FS info
140  *
141  * @retval constructed key
142  */
143 static inline uint32_t
144 fat_construct_key(
145  const fat_fs_info_t *fs_info,
146  fat_pos_t *pos)
147 {
148  return ( ((fat_cluster_num_to_sector512_num(fs_info, pos->cln) +
149  (pos->ofs >> FAT_SECTOR512_BITS)) << 4) +
150  ((pos->ofs >> 5) & (FAT_DIRENTRIES_PER_SEC512 - 1)) );
151 }
152 
153 static inline void
154 fat_file_set_first_cluster_num(fat_file_fd_t *fat_fd, uint32_t cln)
155 {
156  fat_fd->cln = cln;
157  fat_fd->flags |= FAT_FILE_META_DATA_CHANGED;
158 }
159 
160 static inline void fat_file_set_file_size(fat_file_fd_t *fat_fd, uint32_t s)
161 {
162  fat_fd->fat_file_size = s;
163  fat_fd->flags |= FAT_FILE_META_DATA_CHANGED;
164 }
165 
166 static inline void fat_file_set_ctime(fat_file_fd_t *fat_fd, time_t t)
167 {
168  fat_fd->ctime = t;
169  fat_fd->flags |= FAT_FILE_META_DATA_CHANGED;
170 }
171 
172 static inline void fat_file_set_mtime(fat_file_fd_t *fat_fd, time_t t)
173 {
174  fat_fd->mtime = t;
175  fat_fd->flags |= FAT_FILE_META_DATA_CHANGED;
176 }
177 
178 static inline void fat_file_set_ctime_mtime(fat_file_fd_t *fat_fd, time_t t)
179 {
180  fat_fd->ctime = t;
181  fat_fd->mtime = t;
182  fat_fd->flags |= FAT_FILE_META_DATA_CHANGED;
183 }
184 
185 /* Prototypes for "fat-file" operations */
186 int
187 fat_file_open(fat_fs_info_t *fs_info,
188  fat_dir_pos_t *dir_pos,
189  fat_file_fd_t **fat_fd);
190 
191 int
192 fat_file_reopen(fat_file_fd_t *fat_fd);
193 
194 int
195 fat_file_close(fat_fs_info_t *fs_info,
196  fat_file_fd_t *fat_fd);
197 
198 ssize_t
199 fat_file_read(fat_fs_info_t *fs_info,
200  fat_file_fd_t *fat_fd,
201  uint32_t start,
202  uint32_t count,
203  uint8_t *buf);
204 
205 ssize_t
206 fat_file_write(fat_fs_info_t *fs_info,
207  fat_file_fd_t *fat_fd,
208  uint32_t start,
209  uint32_t count,
210  const uint8_t *buf);
211 
212 int
213 fat_file_extend(fat_fs_info_t *fs_info,
214  fat_file_fd_t *fat_fd,
215  bool zero_fill,
216  uint32_t new_length,
217  uint32_t *a_length);
218 
219 int
220 fat_file_truncate(fat_fs_info_t *fs_info,
221  fat_file_fd_t *fat_fd,
222  uint32_t new_length);
223 
224 int
225 fat_file_ioctl(fat_fs_info_t *fs_info,
226  fat_file_fd_t *fat_fd,
227  int cmd,
228  ...);
229 
230 int
231 fat_file_size(fat_fs_info_t *fs_info,
232  fat_file_fd_t *fat_fd);
233 
234 void
235 fat_file_mark_removed(fat_fs_info_t *fs_info,
236  fat_file_fd_t *fat_fd);
237 
238 int
239 fat_file_size(fat_fs_info_t *fs_info,
240  fat_file_fd_t *fat_fd);
241 
242 int
243 fat_file_write_first_cluster_num(fat_fs_info_t *fs_info,
244  fat_file_fd_t *fat_fd);
245 
246 int
247 fat_file_write_file_size(fat_fs_info_t *fs_info,
248  fat_file_fd_t *fat_fd);
249 
250 int
251 fat_file_write_time_and_date(fat_fs_info_t *fs_info,
252  fat_file_fd_t *fat_fd);
253 
254 int
255 fat_file_update(fat_fs_info_t *fs_info,
256  fat_file_fd_t *fat_fd);
257 
258 #ifdef __cplusplus
259 }
260 #endif
261 
262 #endif /* __DOSFS_FAT_FILE_H__ */
Definition: chain.h:68
Definition: fat.h:380
The "fat-file" representation.
Definition: fat_file.h:61
struct fat_file_map_s fat_file_map_t
The "fat-file" representation.
Definition: fat.h:351
Definition: fat.h:368
Descriptor of a fat-file.
Definition: fat_file.h:73
LibIO Internal Interface.
Definition: mm.c:60
struct fat_file_fd_s fat_file_fd_t
Descriptor of a fat-file.
Constants/Data Structures/Prototypes on a Volume with FAT Filesystem.