[d883ce2] | 1 | /** |
---|
| 2 | * @file |
---|
[f36a7bfc] | 3 | * |
---|
[d883ce2] | 4 | * @brief Miscellaneous Routines Implementation for MSDOS FileSystem |
---|
| 5 | * @ingroup libfs |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | /* |
---|
[f36a7bfc] | 9 | * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia |
---|
| 10 | * Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru> |
---|
| 11 | * |
---|
[d2e0bb3] | 12 | * Modifications to support UTF-8 in the file system are |
---|
| 13 | * Copyright (c) 2013 embedded brains GmbH. |
---|
| 14 | * |
---|
[f36a7bfc] | 15 | * The license and distribution terms for this file may be |
---|
| 16 | * found in the file LICENSE in this distribution or at |
---|
[c499856] | 17 | * http://www.rtems.org/license/LICENSE. |
---|
[f36a7bfc] | 18 | */ |
---|
| 19 | |
---|
[3899a537] | 20 | #define MSDOS_TRACE 1 |
---|
| 21 | |
---|
[f36a7bfc] | 22 | #if HAVE_CONFIG_H |
---|
| 23 | #include "config.h" |
---|
| 24 | #endif |
---|
| 25 | |
---|
| 26 | #include <stdlib.h> |
---|
[07d6fd5] | 27 | #include <ctype.h> |
---|
[f36a7bfc] | 28 | #include <sys/time.h> |
---|
| 29 | #include <unistd.h> |
---|
| 30 | #include <string.h> |
---|
| 31 | #include <assert.h> |
---|
[d2e0bb3] | 32 | #include <errno.h> |
---|
| 33 | #include <inttypes.h> |
---|
[f36a7bfc] | 34 | #include <rtems/libio_.h> |
---|
| 35 | |
---|
| 36 | #include "fat.h" |
---|
| 37 | #include "fat_fat_operations.h" |
---|
| 38 | #include "fat_file.h" |
---|
| 39 | |
---|
| 40 | #include "msdos.h" |
---|
| 41 | |
---|
[07d6fd5] | 42 | |
---|
| 43 | #include <stdio.h> |
---|
| 44 | |
---|
[83a4cbb] | 45 | #define MSDOS_LFN_ENTRY_SIZE \ |
---|
| 46 | (MSDOS_LFN_LEN_PER_ENTRY * MSDOS_NAME_LFN_BYTES_PER_CHAR) |
---|
| 47 | |
---|
| 48 | #define MSDOS_LFN_ENTRY_SIZE_UTF8 \ |
---|
| 49 | ((MSDOS_LFN_LEN_PER_ENTRY + 1 ) * MSDOS_NAME_LFN_BYTES_PER_CHAR \ |
---|
| 50 | * MSDOS_NAME_MAX_UTF8_BYTES_PER_CHAR) |
---|
| 51 | |
---|
[07d6fd5] | 52 | /* |
---|
[a0f3ce2] | 53 | * External strings. Saves space this way. |
---|
[07d6fd5] | 54 | */ |
---|
[85b9e7f] | 55 | const char *const MSDOS_DOT_NAME = ". "; |
---|
| 56 | const char *const MSDOS_DOTDOT_NAME = ".. "; |
---|
[07d6fd5] | 57 | |
---|
| 58 | /* msdos_is_valid_name_char -- |
---|
| 59 | * Routine to check the character in a file or directory name. |
---|
| 60 | * The characters support in the short file name are letters, |
---|
| 61 | * digits, or characters with code points values greater than |
---|
| 62 | * 127 (not sure what this last is) plus the following special |
---|
| 63 | * characters "$%'-_@~`!(){}^#&". The must be uppercase. |
---|
| 64 | * |
---|
| 65 | * The following 6 characters are allowed in a long names, |
---|
| 66 | * " +,;=[]" including a space and lower case letters. |
---|
| 67 | * |
---|
| 68 | * PARAMETERS: |
---|
| 69 | * ch - character to check. |
---|
| 70 | * |
---|
| 71 | * RETURNS: |
---|
| 72 | * MSDOS_NAME_INVALID - Not valid in a long or short name. |
---|
| 73 | * MSDOS_NAME_SHORT - Valid in a short name or long name. |
---|
| 74 | * MSDOS_NAME_LONG - Valid in a long name only. |
---|
| 75 | * |
---|
| 76 | */ |
---|
| 77 | static msdos_name_type_t |
---|
| 78 | msdos_is_valid_name_char(const char ch) |
---|
| 79 | { |
---|
| 80 | if (strchr(" +,;=[]", ch) != NULL) |
---|
| 81 | return MSDOS_NAME_LONG; |
---|
| 82 | |
---|
[bab5c5fa] | 83 | if ((ch == '.') || isalnum((unsigned char)ch) || |
---|
[d2e0bb3] | 84 | (strchr("$%'-_@~`!(){}^#&", ch) != NULL) || (unsigned char) ch > 127) |
---|
[07d6fd5] | 85 | return MSDOS_NAME_SHORT; |
---|
[0a7278e] | 86 | |
---|
[07d6fd5] | 87 | return MSDOS_NAME_INVALID; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | /* msdos_short_hex_number -- |
---|
| 91 | * Routine to set the hex number in the SFN. |
---|
| 92 | * |
---|
| 93 | * PARAMETERS: |
---|
| 94 | * name - name to change |
---|
| 95 | * num - number to set |
---|
| 96 | * |
---|
| 97 | * RETURNS: |
---|
| 98 | * nothing |
---|
| 99 | * |
---|
| 100 | */ |
---|
| 101 | static void |
---|
| 102 | msdos_short_name_hex(char* sfn, int num) |
---|
| 103 | { |
---|
| 104 | static const char* hex = "0123456789ABCDEF"; |
---|
| 105 | char* c = MSDOS_DIR_NAME(sfn); |
---|
| 106 | int i; |
---|
[3ae5950] | 107 | for (i = 0; i < 2; i++, c++) |
---|
[07d6fd5] | 108 | if ((*c == ' ') || (*c == '.')) |
---|
[3ae5950] | 109 | *c = '_'; |
---|
[07d6fd5] | 110 | for (i = 0; i < 4; i++, c++) |
---|
| 111 | *c = hex[(num >> ((3 - i) * 4)) & 0xf]; |
---|
[3ae5950] | 112 | *c++ = '~'; |
---|
[43d6a28] | 113 | *c = '1'; |
---|
[07d6fd5] | 114 | } |
---|
| 115 | |
---|
| 116 | /* msdos_name_type -- |
---|
| 117 | * Routine the type of file name. |
---|
| 118 | * |
---|
| 119 | * PARAMETERS: |
---|
| 120 | * name - name to check |
---|
| 121 | * |
---|
| 122 | * RETURNS: |
---|
| 123 | * true the name is long, else the name is short. |
---|
| 124 | * |
---|
| 125 | */ |
---|
[8f77f8f] | 126 | #define MSDOS_NAME_TYPE_PRINT 0 |
---|
[07d6fd5] | 127 | static msdos_name_type_t |
---|
| 128 | msdos_name_type(const char *name, int name_len) |
---|
| 129 | { |
---|
| 130 | bool lowercase = false; |
---|
| 131 | bool uppercase = false; |
---|
[1b3f7e92] | 132 | int dot_at = -1; |
---|
[07d6fd5] | 133 | int count = 0; |
---|
| 134 | |
---|
| 135 | while (*name && (count < name_len)) |
---|
| 136 | { |
---|
[1b3f7e92] | 137 | bool is_dot = *name == '.'; |
---|
[07d6fd5] | 138 | msdos_name_type_t type = msdos_is_valid_name_char(*name); |
---|
| 139 | |
---|
[8f77f8f] | 140 | #if MSDOS_NAME_TYPE_PRINT |
---|
| 141 | printf ("MSDOS_NAME_TYPE: c:%02x type:%d\n", *name, type); |
---|
| 142 | #endif |
---|
[66b8047] | 143 | |
---|
[07d6fd5] | 144 | if ((type == MSDOS_NAME_INVALID) || (type == MSDOS_NAME_LONG)) |
---|
| 145 | return type; |
---|
| 146 | |
---|
[1b3f7e92] | 147 | if (dot_at >= 0) |
---|
[07d6fd5] | 148 | { |
---|
[1b3f7e92] | 149 | if (is_dot || ((count - dot_at) > 3)) |
---|
[8f77f8f] | 150 | { |
---|
| 151 | #if MSDOS_NAME_TYPE_PRINT |
---|
| 152 | printf ("MSDOS_NAME_TYPE: LONG[1]: is_dot:%d, at:%d cnt\n", |
---|
| 153 | is_dot, dot_at, count); |
---|
| 154 | #endif |
---|
[07d6fd5] | 155 | return MSDOS_NAME_LONG; |
---|
[8f77f8f] | 156 | } |
---|
[07d6fd5] | 157 | } |
---|
| 158 | else |
---|
| 159 | { |
---|
[1b3f7e92] | 160 | if (count == 8 && !is_dot) |
---|
[8f77f8f] | 161 | { |
---|
| 162 | #if MSDOS_NAME_TYPE_PRINT |
---|
| 163 | printf ("MSDOS_NAME_TYPE: LONG[2]: is_dot:%d, at:%d cnt\n", |
---|
| 164 | is_dot, dot_at, count); |
---|
| 165 | #endif |
---|
[07d6fd5] | 166 | return MSDOS_NAME_LONG; |
---|
[8f77f8f] | 167 | } |
---|
[07d6fd5] | 168 | } |
---|
| 169 | |
---|
[1b3f7e92] | 170 | if (is_dot) |
---|
[07d6fd5] | 171 | dot_at = count; |
---|
| 172 | else if ((*name >= 'A') && (*name <= 'Z')) |
---|
| 173 | uppercase = true; |
---|
| 174 | else if ((*name >= 'a') && (*name <= 'z')) |
---|
| 175 | lowercase = true; |
---|
[0a7278e] | 176 | |
---|
[07d6fd5] | 177 | count++; |
---|
| 178 | name++; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | if (lowercase && uppercase) |
---|
[8f77f8f] | 182 | { |
---|
| 183 | #if MSDOS_NAME_TYPE_PRINT |
---|
| 184 | printf ("MSDOS_NAME_TYPE: LONG[3]\n"); |
---|
| 185 | #endif |
---|
[07d6fd5] | 186 | return MSDOS_NAME_LONG; |
---|
[8f77f8f] | 187 | } |
---|
[66b8047] | 188 | |
---|
[8f77f8f] | 189 | #if MSDOS_NAME_TYPE_PRINT |
---|
| 190 | printf ("MSDOS_NAME_TYPE: SHORT[1]\n"); |
---|
| 191 | #endif |
---|
[07d6fd5] | 192 | return MSDOS_NAME_SHORT; |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | /* msdos_long_to_short -- |
---|
[0a7278e] | 196 | * Routine to creates a short name from a long. Start the end of the |
---|
[07d6fd5] | 197 | * |
---|
| 198 | * PARAMETERS: |
---|
| 199 | * name - name to check |
---|
| 200 | * |
---|
| 201 | * RETURNS: |
---|
| 202 | * true the name is long, else the name is short. |
---|
| 203 | * |
---|
| 204 | */ |
---|
[8f77f8f] | 205 | #define MSDOS_L2S_PRINT 0 |
---|
[07d6fd5] | 206 | msdos_name_type_t |
---|
[d2e0bb3] | 207 | msdos_long_to_short(rtems_dosfs_convert_control *converter, |
---|
| 208 | const char *lfn, |
---|
| 209 | int lfn_len, |
---|
| 210 | char *sfn, |
---|
| 211 | int sfn_len) |
---|
[07d6fd5] | 212 | { |
---|
| 213 | msdos_name_type_t type; |
---|
[d2e0bb3] | 214 | int eno = 0; |
---|
[07d6fd5] | 215 | int i; |
---|
[d2e0bb3] | 216 | ssize_t short_filename_length = sfn_len; |
---|
| 217 | void *buffer = converter->buffer.data; |
---|
| 218 | size_t codepage_name_len = converter->buffer.size; |
---|
[07d6fd5] | 219 | |
---|
| 220 | /* |
---|
| 221 | * Fill with spaces. This is how a short directory entry is padded. |
---|
| 222 | */ |
---|
| 223 | memset (sfn, ' ', sfn_len); |
---|
| 224 | |
---|
| 225 | /* |
---|
| 226 | * Handle '.' and '..' specially. |
---|
| 227 | */ |
---|
| 228 | if ((lfn[0] == '.') && (lfn_len == 1)) |
---|
| 229 | { |
---|
| 230 | sfn[0] = '.'; |
---|
[8f77f8f] | 231 | #if MSDOS_L2S_PRINT |
---|
| 232 | printf ("MSDOS_L2S: SHORT[1]: lfn:'%s' SFN:'%s'\n", lfn, sfn); |
---|
| 233 | #endif |
---|
[07d6fd5] | 234 | return MSDOS_NAME_SHORT; |
---|
| 235 | } |
---|
[0a7278e] | 236 | |
---|
[07d6fd5] | 237 | if ((lfn[0] == '.') && (lfn[1] == '.') && (lfn_len == 2)) |
---|
| 238 | { |
---|
| 239 | sfn[0] = sfn[1] = '.'; |
---|
[8f77f8f] | 240 | #if MSDOS_L2S_PRINT |
---|
| 241 | printf ("MSDOS_L2S: SHORT[2]: lfn:'%s' SFN:'%s'\n", lfn, sfn); |
---|
| 242 | #endif |
---|
[07d6fd5] | 243 | return MSDOS_NAME_SHORT; |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | /* |
---|
| 247 | * Filenames with only blanks and dots are not allowed! |
---|
| 248 | */ |
---|
| 249 | for (i = 0; i < lfn_len; i++) |
---|
[1b3f7e92] | 250 | if ((lfn[i] != ' ') && (lfn[i] != '.')) |
---|
[07d6fd5] | 251 | break; |
---|
| 252 | |
---|
[1b3f7e92] | 253 | if (i == lfn_len) |
---|
[8f77f8f] | 254 | { |
---|
| 255 | #if MSDOS_L2S_PRINT |
---|
| 256 | printf ("MSDOS_L2S: INVALID[1]: lfn:'%s' SFN:'%s'\n", lfn, sfn); |
---|
| 257 | #endif |
---|
[07d6fd5] | 258 | return MSDOS_NAME_INVALID; |
---|
[8f77f8f] | 259 | } |
---|
[07d6fd5] | 260 | |
---|
| 261 | /* |
---|
| 262 | * Is this a short name ? |
---|
| 263 | */ |
---|
[0a7278e] | 264 | |
---|
[d2e0bb3] | 265 | eno = (*converter->handler->utf8_to_codepage) ( |
---|
| 266 | converter, |
---|
| 267 | (const uint8_t*)&lfn[0], |
---|
| 268 | lfn_len, |
---|
| 269 | buffer, |
---|
| 270 | &codepage_name_len); |
---|
| 271 | if (eno == EINVAL) |
---|
| 272 | { |
---|
| 273 | eno = 0; |
---|
| 274 | type = MSDOS_NAME_LONG; |
---|
| 275 | } |
---|
| 276 | else |
---|
| 277 | { |
---|
| 278 | type = msdos_name_type ( |
---|
| 279 | buffer, |
---|
| 280 | codepage_name_len); |
---|
| 281 | } |
---|
[07d6fd5] | 282 | |
---|
[d2e0bb3] | 283 | if (type != MSDOS_NAME_INVALID) |
---|
[8f77f8f] | 284 | { |
---|
[d2e0bb3] | 285 | short_filename_length = msdos_filename_utf8_to_short_name_for_save ( |
---|
| 286 | converter, |
---|
| 287 | (const uint8_t*)lfn, |
---|
| 288 | lfn_len, |
---|
| 289 | sfn, |
---|
| 290 | short_filename_length); |
---|
| 291 | if (short_filename_length < 0 ) { |
---|
| 292 | type = MSDOS_NAME_INVALID; |
---|
| 293 | } |
---|
[8f77f8f] | 294 | #if MSDOS_L2S_PRINT |
---|
[d2e0bb3] | 295 | printf ("MSDOS_L2S: TYPE:%d lfn:'%s' SFN:'%s'\n", type, lfn, sfn); |
---|
[8f77f8f] | 296 | #endif |
---|
| 297 | } |
---|
[d2e0bb3] | 298 | else |
---|
| 299 | { |
---|
[8f77f8f] | 300 | #if MSDOS_L2S_PRINT |
---|
[d2e0bb3] | 301 | printf ("MSDOS_L2S: INVALID[2]: lfn:'%s' SFN:'%s'\n", lfn, sfn); |
---|
[8f77f8f] | 302 | #endif |
---|
[d2e0bb3] | 303 | } |
---|
| 304 | |
---|
[07d6fd5] | 305 | return type; |
---|
| 306 | } |
---|
| 307 | |
---|
[f36a7bfc] | 308 | /* msdos_find_name -- |
---|
[a5305f6b] | 309 | * Find the node which correspondes to the name, open fat-file which |
---|
| 310 | * correspondes to the found node and close fat-file which correspondes |
---|
[f36a7bfc] | 311 | * to the node we searched in. |
---|
| 312 | * |
---|
| 313 | * PARAMETERS: |
---|
| 314 | * parent_loc - parent node description |
---|
| 315 | * name - name to find |
---|
| 316 | * |
---|
| 317 | * RETURNS: |
---|
[a5305f6b] | 318 | * RC_OK and updated 'parent_loc' on success, or -1 if error |
---|
[f36a7bfc] | 319 | * occured (errno set apropriately) |
---|
| 320 | * |
---|
| 321 | */ |
---|
[a5305f6b] | 322 | int |
---|
[f36a7bfc] | 323 | msdos_find_name( |
---|
| 324 | rtems_filesystem_location_info_t *parent_loc, |
---|
[07d6fd5] | 325 | const char *name, |
---|
| 326 | int name_len |
---|
[f36a7bfc] | 327 | ) |
---|
| 328 | { |
---|
[07d6fd5] | 329 | int rc = RC_OK; |
---|
| 330 | msdos_fs_info_t *fs_info = parent_loc->mt_entry->fs_info; |
---|
| 331 | fat_file_fd_t *fat_fd = NULL; |
---|
| 332 | msdos_name_type_t name_type; |
---|
| 333 | fat_dir_pos_t dir_pos; |
---|
| 334 | unsigned short time_val = 0; |
---|
| 335 | unsigned short date = 0; |
---|
| 336 | char node_entry[MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE]; |
---|
[a5305f6b] | 337 | |
---|
[f36a7bfc] | 338 | memset(node_entry, 0, MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE); |
---|
[a5305f6b] | 339 | |
---|
[d2e0bb3] | 340 | name_type = msdos_long_to_short ( |
---|
| 341 | fs_info->converter, |
---|
| 342 | name, |
---|
| 343 | name_len, |
---|
| 344 | MSDOS_DIR_NAME(node_entry), |
---|
| 345 | MSDOS_NAME_MAX); |
---|
[07d6fd5] | 346 | |
---|
[a5305f6b] | 347 | /* |
---|
[d2e0bb3] | 348 | * find the node which corresponds to the name in the directory pointed by |
---|
[f36a7bfc] | 349 | * 'parent_loc' |
---|
| 350 | */ |
---|
[07d6fd5] | 351 | rc = msdos_get_name_node(parent_loc, false, name, name_len, name_type, |
---|
| 352 | &dir_pos, node_entry); |
---|
[f36a7bfc] | 353 | if (rc != RC_OK) |
---|
| 354 | return rc; |
---|
| 355 | |
---|
[07d6fd5] | 356 | if (((*MSDOS_DIR_ATTR(node_entry)) & MSDOS_ATTR_VOLUME_ID) || |
---|
| 357 | ((*MSDOS_DIR_ATTR(node_entry) & MSDOS_ATTR_LFN_MASK) == MSDOS_ATTR_LFN)) |
---|
| 358 | return MSDOS_NAME_NOT_FOUND_ERR; |
---|
| 359 | |
---|
[f36a7bfc] | 360 | /* open fat-file corresponded to the found node */ |
---|
[c65afce4] | 361 | rc = fat_file_open(&fs_info->fat, &dir_pos, &fat_fd); |
---|
[f36a7bfc] | 362 | if (rc != RC_OK) |
---|
| 363 | return rc; |
---|
[a5305f6b] | 364 | |
---|
[07d6fd5] | 365 | fat_fd->dir_pos = dir_pos; |
---|
[0a7278e] | 366 | |
---|
[f36a7bfc] | 367 | /* |
---|
[07d6fd5] | 368 | * I don't like this if, but: we should do it, or should write new file |
---|
[a5305f6b] | 369 | * size and first cluster num to the disk after each write operation |
---|
[07d6fd5] | 370 | * (even if one byte is written - that is TOO slow) because |
---|
[a5305f6b] | 371 | * otherwise real values of these fields stored in fat-file descriptor |
---|
[07d6fd5] | 372 | * may be accidentally rewritten with wrong values stored on the disk |
---|
[f36a7bfc] | 373 | */ |
---|
| 374 | if (fat_fd->links_num == 1) |
---|
| 375 | { |
---|
| 376 | fat_fd->cln = MSDOS_EXTRACT_CLUSTER_NUM(node_entry); |
---|
[a5305f6b] | 377 | |
---|
[f36a7bfc] | 378 | time_val = *MSDOS_DIR_WRITE_TIME(node_entry); |
---|
| 379 | date = *MSDOS_DIR_WRITE_DATE(node_entry); |
---|
[a5305f6b] | 380 | |
---|
[07d6fd5] | 381 | fat_fd->mtime = msdos_date_dos2unix(CF_LE_W(date), CF_LE_W(time_val)); |
---|
[a5305f6b] | 382 | |
---|
[a7eaaae8] | 383 | time_val = *MSDOS_DIR_CRT_TIME(node_entry); |
---|
| 384 | date = *MSDOS_DIR_CRT_DATE(node_entry); |
---|
| 385 | |
---|
| 386 | fat_fd->ctime = msdos_date_dos2unix(CF_LE_W(date), CF_LE_W(time_val)); |
---|
| 387 | |
---|
[f36a7bfc] | 388 | if ((*MSDOS_DIR_ATTR(node_entry)) & MSDOS_ATTR_DIRECTORY) |
---|
| 389 | { |
---|
| 390 | fat_fd->fat_file_type = FAT_DIRECTORY; |
---|
[665f03a] | 391 | fat_fd->size_limit = MSDOS_MAX_DIR_LENGTH; |
---|
[a5305f6b] | 392 | |
---|
[c65afce4] | 393 | rc = fat_file_size(&fs_info->fat, fat_fd); |
---|
[f36a7bfc] | 394 | if (rc != RC_OK) |
---|
| 395 | { |
---|
[c65afce4] | 396 | fat_file_close(&fs_info->fat, fat_fd); |
---|
[f36a7bfc] | 397 | return rc; |
---|
| 398 | } |
---|
| 399 | } |
---|
| 400 | else |
---|
| 401 | { |
---|
[a5305f6b] | 402 | fat_fd->fat_file_size = CF_LE_L(*MSDOS_DIR_FILE_SIZE(node_entry)); |
---|
| 403 | fat_fd->fat_file_type = FAT_FILE; |
---|
[f36a7bfc] | 404 | fat_fd->size_limit = MSDOS_MAX_FILE_SIZE; |
---|
| 405 | } |
---|
[a5305f6b] | 406 | |
---|
[f36a7bfc] | 407 | /* these data is not actual for zero-length fat-file */ |
---|
| 408 | fat_fd->map.file_cln = 0; |
---|
| 409 | fat_fd->map.disk_cln = fat_fd->cln; |
---|
[a5305f6b] | 410 | |
---|
| 411 | if ((fat_fd->fat_file_size != 0) && |
---|
[f36a7bfc] | 412 | (fat_fd->fat_file_size <= fs_info->fat.vol.bpc)) |
---|
| 413 | { |
---|
| 414 | fat_fd->map.last_cln = fat_fd->cln; |
---|
| 415 | } |
---|
| 416 | else |
---|
| 417 | { |
---|
| 418 | fat_fd->map.last_cln = FAT_UNDEFINED_VALUE; |
---|
| 419 | } |
---|
[a5305f6b] | 420 | } |
---|
[f36a7bfc] | 421 | |
---|
| 422 | /* close fat-file corresponded to the node we searched in */ |
---|
[c65afce4] | 423 | rc = fat_file_close(&fs_info->fat, parent_loc->node_access); |
---|
[f36a7bfc] | 424 | if (rc != RC_OK) |
---|
| 425 | { |
---|
[c65afce4] | 426 | fat_file_close(&fs_info->fat, fat_fd); |
---|
[f36a7bfc] | 427 | return rc; |
---|
| 428 | } |
---|
| 429 | |
---|
| 430 | /* update node_info_ptr field */ |
---|
| 431 | parent_loc->node_access = fat_fd; |
---|
[a5305f6b] | 432 | |
---|
[f36a7bfc] | 433 | return rc; |
---|
[a5305f6b] | 434 | } |
---|
[f36a7bfc] | 435 | |
---|
| 436 | /* msdos_get_name_node -- |
---|
[07d6fd5] | 437 | * This routine is used in two ways: for a new node creation (a) or for |
---|
[f36a7bfc] | 438 | * search the node which correspondes to the name parameter (b). |
---|
[a5305f6b] | 439 | * In case (a) 'name' should be set up to NULL and 'name_dir_entry' should |
---|
| 440 | * point to initialized 32 bytes structure described a new node. |
---|
[f36a7bfc] | 441 | * In case (b) 'name' should contain a valid string. |
---|
| 442 | * |
---|
[a5305f6b] | 443 | * (a): reading fat-file which correspondes to directory we are going to |
---|
| 444 | * create node in. If free slot is found write contents of |
---|
| 445 | * 'name_dir_entry' into it. If reach end of fat-file and no free |
---|
[f36a7bfc] | 446 | * slot found, write 32 bytes to the end of fat-file. |
---|
| 447 | * |
---|
[a5305f6b] | 448 | * (b): reading fat-file which correspondes to directory and trying to |
---|
[f36a7bfc] | 449 | * find slot with the name field == 'name' parameter |
---|
| 450 | * |
---|
| 451 | * |
---|
| 452 | * PARAMETERS: |
---|
| 453 | * parent_loc - node description to create node in or to find name in |
---|
| 454 | * name - NULL or name to find |
---|
| 455 | * paux - identify a node location on the disk - |
---|
[a5305f6b] | 456 | * cluster num and offset inside the cluster |
---|
[07d6fd5] | 457 | * short_dir_entry - node to create/placeholder for found node (IN/OUT) |
---|
[f36a7bfc] | 458 | * |
---|
| 459 | * RETURNS: |
---|
[a5305f6b] | 460 | * RC_OK, filled aux_struct_ptr and name_dir_entry on success, or -1 if |
---|
[f36a7bfc] | 461 | * error occured (errno set apropriately) |
---|
| 462 | * |
---|
| 463 | */ |
---|
[e197621] | 464 | int |
---|
[f36a7bfc] | 465 | msdos_get_name_node( |
---|
[3b7c123] | 466 | const rtems_filesystem_location_info_t *parent_loc, |
---|
| 467 | bool create_node, |
---|
| 468 | const char *name, |
---|
| 469 | int name_len, |
---|
| 470 | msdos_name_type_t name_type, |
---|
| 471 | fat_dir_pos_t *dir_pos, |
---|
| 472 | char *name_dir_entry |
---|
[f36a7bfc] | 473 | ) |
---|
| 474 | { |
---|
[e197621] | 475 | int rc = RC_OK; |
---|
[f36a7bfc] | 476 | fat_file_fd_t *fat_fd = parent_loc->node_access; |
---|
[f91bbe64] | 477 | uint32_t dotdot_cln = 0; |
---|
[f36a7bfc] | 478 | |
---|
[07d6fd5] | 479 | /* find name in fat-file which corresponds to the directory */ |
---|
| 480 | rc = msdos_find_name_in_fat_file(parent_loc->mt_entry, fat_fd, |
---|
[d2e0bb3] | 481 | create_node, (const uint8_t*)name, name_len, name_type, |
---|
[07d6fd5] | 482 | dir_pos, name_dir_entry); |
---|
[f36a7bfc] | 483 | if ((rc != RC_OK) && (rc != MSDOS_NAME_NOT_FOUND_ERR)) |
---|
| 484 | return rc; |
---|
[a5305f6b] | 485 | |
---|
[07d6fd5] | 486 | if (!create_node) |
---|
[f36a7bfc] | 487 | { |
---|
[07d6fd5] | 488 | /* if we search for valid name and name not found -> return */ |
---|
| 489 | if (rc == MSDOS_NAME_NOT_FOUND_ERR) |
---|
[f36a7bfc] | 490 | return rc; |
---|
| 491 | |
---|
[a5305f6b] | 492 | /* |
---|
[07d6fd5] | 493 | * if we have deal with ".." - it is a special case :((( |
---|
| 494 | * |
---|
| 495 | * Really, we should return cluster num and offset not of ".." slot, but |
---|
| 496 | * slot which correspondes to real directory name. |
---|
[f36a7bfc] | 497 | */ |
---|
[07d6fd5] | 498 | if (rc == RC_OK) |
---|
[f36a7bfc] | 499 | { |
---|
[07d6fd5] | 500 | if (strncmp(name, "..", 2) == 0) |
---|
[f36a7bfc] | 501 | { |
---|
[07d6fd5] | 502 | dotdot_cln = MSDOS_EXTRACT_CLUSTER_NUM((name_dir_entry)); |
---|
| 503 | |
---|
| 504 | /* are we right under root dir ? */ |
---|
| 505 | if (dotdot_cln == 0) |
---|
| 506 | { |
---|
| 507 | /* |
---|
| 508 | * we can relax about first_char field - it never should be |
---|
| 509 | * used for root dir |
---|
| 510 | */ |
---|
| 511 | fat_dir_pos_init(dir_pos); |
---|
| 512 | dir_pos->sname.cln = FAT_ROOTDIR_CLUSTER_NUM; |
---|
| 513 | } |
---|
| 514 | else |
---|
| 515 | { |
---|
| 516 | rc = |
---|
| 517 | msdos_get_dotdot_dir_info_cluster_num_and_offset(parent_loc->mt_entry, |
---|
| 518 | dotdot_cln, |
---|
| 519 | dir_pos, |
---|
| 520 | name_dir_entry); |
---|
| 521 | if (rc != RC_OK) |
---|
| 522 | return rc; |
---|
| 523 | } |
---|
[f36a7bfc] | 524 | } |
---|
[a5305f6b] | 525 | } |
---|
[f36a7bfc] | 526 | } |
---|
| 527 | return rc; |
---|
| 528 | } |
---|
| 529 | |
---|
| 530 | /* |
---|
| 531 | * msdos_get_dotdot_dir_info_cluster_num_and_offset |
---|
| 532 | * |
---|
[a5305f6b] | 533 | * Unfortunately, in general, we cann't work here in fat-file ideologic |
---|
| 534 | * (open fat_file "..", get ".." and ".", open "..", find an entry ...) |
---|
[f36a7bfc] | 535 | * because if we open |
---|
| 536 | * fat-file ".." it may happend that we have two different fat-file |
---|
[a5305f6b] | 537 | * descriptors ( for real name of directory and ".." name ) for a single |
---|
[f36a7bfc] | 538 | * file ( cluster num of both pointers to the same cluster ) |
---|
| 539 | * But...we do it because we protected by semaphore |
---|
[a5305f6b] | 540 | * |
---|
[f36a7bfc] | 541 | */ |
---|
| 542 | |
---|
| 543 | /* msdos_get_dotdot_dir_info_cluster_num_and_offset -- |
---|
[a5305f6b] | 544 | * Get cluster num and offset not of ".." slot, but slot which correspondes |
---|
| 545 | * to real directory name. |
---|
[f36a7bfc] | 546 | * |
---|
| 547 | * PARAMETERS: |
---|
| 548 | * mt_entry - mount table entry |
---|
| 549 | * cln - data cluster num extracted drom ".." slot |
---|
| 550 | * paux - identify a node location on the disk - |
---|
| 551 | * number of cluster and offset inside the cluster |
---|
[a5305f6b] | 552 | * dir_entry - placeholder for found node |
---|
[f36a7bfc] | 553 | * |
---|
| 554 | * RETURNS: |
---|
| 555 | * RC_OK, filled 'paux' and 'dir_entry' on success, or -1 if error occured |
---|
| 556 | * (errno set apropriately) |
---|
| 557 | * |
---|
| 558 | */ |
---|
| 559 | int |
---|
| 560 | msdos_get_dotdot_dir_info_cluster_num_and_offset( |
---|
[a5305f6b] | 561 | rtems_filesystem_mount_table_entry_t *mt_entry, |
---|
[f91bbe64] | 562 | uint32_t cln, |
---|
[07d6fd5] | 563 | fat_dir_pos_t *dir_pos, |
---|
[f36a7bfc] | 564 | char *dir_entry |
---|
| 565 | ) |
---|
| 566 | { |
---|
| 567 | int rc = RC_OK; |
---|
| 568 | msdos_fs_info_t *fs_info = mt_entry->fs_info; |
---|
| 569 | fat_file_fd_t *fat_fd = NULL; |
---|
[adc829ed] | 570 | char dot_node[MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE]; |
---|
| 571 | char dotdot_node[MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE]; |
---|
[f91bbe64] | 572 | uint32_t cl4find = 0; |
---|
[d2e0bb3] | 573 | rtems_dosfs_convert_control *converter = fs_info->converter; |
---|
[a5305f6b] | 574 | |
---|
[f36a7bfc] | 575 | /* |
---|
| 576 | * open fat-file corresponded to ".." |
---|
| 577 | */ |
---|
[c65afce4] | 578 | rc = fat_file_open(&fs_info->fat, dir_pos, &fat_fd); |
---|
[f36a7bfc] | 579 | if (rc != RC_OK) |
---|
| 580 | return rc; |
---|
[a5305f6b] | 581 | |
---|
[f36a7bfc] | 582 | fat_fd->cln = cln; |
---|
| 583 | fat_fd->fat_file_type = FAT_DIRECTORY; |
---|
[665f03a] | 584 | fat_fd->size_limit = MSDOS_MAX_DIR_LENGTH; |
---|
[a5305f6b] | 585 | |
---|
[f36a7bfc] | 586 | fat_fd->map.file_cln = 0; |
---|
| 587 | fat_fd->map.disk_cln = fat_fd->cln; |
---|
| 588 | |
---|
[c65afce4] | 589 | rc = fat_file_size(&fs_info->fat, fat_fd); |
---|
[f36a7bfc] | 590 | if (rc != RC_OK) |
---|
| 591 | { |
---|
[c65afce4] | 592 | fat_file_close(&fs_info->fat, fat_fd); |
---|
[f36a7bfc] | 593 | return rc; |
---|
[a5305f6b] | 594 | } |
---|
| 595 | |
---|
[f36a7bfc] | 596 | /* find "." node in opened directory */ |
---|
[07d6fd5] | 597 | memset(dot_node, 0, MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE); |
---|
[d2e0bb3] | 598 | msdos_long_to_short( |
---|
| 599 | converter, |
---|
| 600 | ".", 1, dot_node, MSDOS_SHORT_NAME_LEN); |
---|
| 601 | rc = msdos_find_name_in_fat_file(mt_entry, fat_fd, false, (const uint8_t*)".", 1, |
---|
[07d6fd5] | 602 | MSDOS_NAME_SHORT, dir_pos, dot_node); |
---|
[a5305f6b] | 603 | |
---|
[f36a7bfc] | 604 | if (rc != RC_OK) |
---|
| 605 | { |
---|
[c65afce4] | 606 | fat_file_close(&fs_info->fat, fat_fd); |
---|
[f36a7bfc] | 607 | return rc; |
---|
| 608 | } |
---|
[a5305f6b] | 609 | |
---|
[f36a7bfc] | 610 | /* find ".." node in opened directory */ |
---|
[07d6fd5] | 611 | memset(dotdot_node, 0, MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE); |
---|
[d2e0bb3] | 612 | msdos_long_to_short( |
---|
| 613 | converter, |
---|
| 614 | "..", 2, dotdot_node, MSDOS_SHORT_NAME_LEN); |
---|
| 615 | rc = msdos_find_name_in_fat_file(mt_entry, fat_fd, false, (const uint8_t*)"..", 2, |
---|
[07d6fd5] | 616 | MSDOS_NAME_SHORT, dir_pos, |
---|
[f36a7bfc] | 617 | dotdot_node); |
---|
[a5305f6b] | 618 | |
---|
| 619 | if (rc != RC_OK) |
---|
[f36a7bfc] | 620 | { |
---|
[c65afce4] | 621 | fat_file_close(&fs_info->fat, fat_fd); |
---|
[f36a7bfc] | 622 | return rc; |
---|
| 623 | } |
---|
| 624 | |
---|
| 625 | cl4find = MSDOS_EXTRACT_CLUSTER_NUM(dot_node); |
---|
[a5305f6b] | 626 | |
---|
[f36a7bfc] | 627 | /* close fat-file corresponded to ".." directory */ |
---|
[c65afce4] | 628 | rc = fat_file_close(&fs_info->fat, fat_fd); |
---|
[f36a7bfc] | 629 | if ( rc != RC_OK ) |
---|
| 630 | return rc; |
---|
| 631 | |
---|
| 632 | if ( (MSDOS_EXTRACT_CLUSTER_NUM(dotdot_node)) == 0) |
---|
| 633 | { |
---|
[a5305f6b] | 634 | /* |
---|
| 635 | * we handle root dir for all FAT types in the same way with the |
---|
[f36a7bfc] | 636 | * ordinary directories ( through fat_file_* calls ) |
---|
| 637 | */ |
---|
[07d6fd5] | 638 | fat_dir_pos_init(dir_pos); |
---|
| 639 | dir_pos->sname.cln = FAT_ROOTDIR_CLUSTER_NUM; |
---|
[f36a7bfc] | 640 | } |
---|
| 641 | |
---|
| 642 | /* open fat-file corresponded to second ".." */ |
---|
[c65afce4] | 643 | rc = fat_file_open(&fs_info->fat, dir_pos, &fat_fd); |
---|
[f36a7bfc] | 644 | if (rc != RC_OK) |
---|
| 645 | return rc; |
---|
| 646 | |
---|
| 647 | if ((MSDOS_EXTRACT_CLUSTER_NUM(dotdot_node)) == 0) |
---|
| 648 | fat_fd->cln = fs_info->fat.vol.rdir_cl; |
---|
| 649 | else |
---|
| 650 | fat_fd->cln = MSDOS_EXTRACT_CLUSTER_NUM(dotdot_node); |
---|
| 651 | |
---|
| 652 | fat_fd->fat_file_type = FAT_DIRECTORY; |
---|
[665f03a] | 653 | fat_fd->size_limit = MSDOS_MAX_DIR_LENGTH; |
---|
[a5305f6b] | 654 | |
---|
[f36a7bfc] | 655 | fat_fd->map.file_cln = 0; |
---|
| 656 | fat_fd->map.disk_cln = fat_fd->cln; |
---|
| 657 | |
---|
[c65afce4] | 658 | rc = fat_file_size(&fs_info->fat, fat_fd); |
---|
[f36a7bfc] | 659 | if (rc != RC_OK) |
---|
| 660 | { |
---|
[c65afce4] | 661 | fat_file_close(&fs_info->fat, fat_fd); |
---|
[f36a7bfc] | 662 | return rc; |
---|
[a5305f6b] | 663 | } |
---|
| 664 | |
---|
[f36a7bfc] | 665 | /* in this directory find slot with specified cluster num */ |
---|
[a5305f6b] | 666 | rc = msdos_find_node_by_cluster_num_in_fat_file(mt_entry, fat_fd, cl4find, |
---|
[07d6fd5] | 667 | dir_pos, dir_entry); |
---|
[f36a7bfc] | 668 | if (rc != RC_OK) |
---|
| 669 | { |
---|
[c65afce4] | 670 | fat_file_close(&fs_info->fat, fat_fd); |
---|
[f36a7bfc] | 671 | return rc; |
---|
| 672 | } |
---|
[c65afce4] | 673 | rc = fat_file_close(&fs_info->fat, fat_fd); |
---|
[f36a7bfc] | 674 | return rc; |
---|
| 675 | } |
---|
| 676 | |
---|
| 677 | |
---|
[a7eaaae8] | 678 | /* fat_file_write_time_and_date -- |
---|
[a5305f6b] | 679 | * Write last write date and time for a file to the disk (to corresponded |
---|
| 680 | * 32bytes node) |
---|
[f36a7bfc] | 681 | * |
---|
| 682 | * PARAMETERS: |
---|
[a7eaaae8] | 683 | * fs_info - fat fs info |
---|
[f36a7bfc] | 684 | * fat_fd - fat-file descriptor |
---|
| 685 | * |
---|
| 686 | * RETURNS: |
---|
| 687 | * RC_OK on success, or -1 if error occured (errno set apropriately). |
---|
| 688 | * |
---|
| 689 | */ |
---|
| 690 | int |
---|
[a7eaaae8] | 691 | fat_file_write_time_and_date( |
---|
| 692 | fat_fs_info_t *fs_info, |
---|
[f36a7bfc] | 693 | fat_file_fd_t *fat_fd |
---|
| 694 | ) |
---|
| 695 | { |
---|
[a7eaaae8] | 696 | int rc = RC_OK; |
---|
| 697 | ssize_t ret; |
---|
[5ac15a5] | 698 | uint16_t time_val; |
---|
| 699 | uint16_t date; |
---|
[f91bbe64] | 700 | uint32_t sec = 0; |
---|
| 701 | uint32_t byte = 0; |
---|
[a5305f6b] | 702 | |
---|
| 703 | /* |
---|
[42a22f08] | 704 | * calculate input for fat_sector_write: convert (cluster num, offset) to |
---|
[f36a7bfc] | 705 | * (sector num, new offset) |
---|
| 706 | */ |
---|
[a7eaaae8] | 707 | sec = fat_cluster_num_to_sector_num(fs_info, fat_fd->dir_pos.sname.cln); |
---|
| 708 | sec += (fat_fd->dir_pos.sname.ofs >> fs_info->vol.sec_log2); |
---|
[f36a7bfc] | 709 | /* byte points to start of 32bytes structure */ |
---|
[a7eaaae8] | 710 | byte = fat_fd->dir_pos.sname.ofs & (fs_info->vol.bps - 1); |
---|
| 711 | |
---|
| 712 | msdos_date_unix2dos(fat_fd->mtime, &date, &time_val); |
---|
[a5305f6b] | 713 | |
---|
[f36a7bfc] | 714 | time_val = CT_LE_W(time_val); |
---|
[a7eaaae8] | 715 | ret = fat_sector_write(fs_info, sec, byte + MSDOS_FILE_WTIME_OFFSET, |
---|
| 716 | 2, (char *)(&time_val)); |
---|
| 717 | if ( ret < 0 ) |
---|
| 718 | rc = -1; |
---|
| 719 | |
---|
[f36a7bfc] | 720 | date = CT_LE_W(date); |
---|
[a7eaaae8] | 721 | ret = fat_sector_write(fs_info, sec, byte + MSDOS_FILE_WDATE_OFFSET, |
---|
| 722 | 2, (char *)(&date)); |
---|
| 723 | if ( ret < 0 ) |
---|
| 724 | rc = -1; |
---|
[f36a7bfc] | 725 | |
---|
[a7eaaae8] | 726 | ret = fat_sector_write(fs_info, sec, byte + MSDOS_FILE_ADATE_OFFSET, |
---|
| 727 | 2, (char *)(&date)); |
---|
| 728 | if ( ret < 0 ) |
---|
| 729 | rc = -1; |
---|
[f36a7bfc] | 730 | |
---|
[a7eaaae8] | 731 | msdos_date_unix2dos(fat_fd->ctime, &date, &time_val); |
---|
| 732 | |
---|
| 733 | time_val = CT_LE_W(time_val); |
---|
| 734 | ret = fat_sector_write(fs_info, sec, byte + MSDOS_FILE_CTIME_OFFSET, |
---|
| 735 | 2, (char *)(&time_val)); |
---|
| 736 | if ( ret < 0 ) |
---|
| 737 | rc = -1; |
---|
| 738 | |
---|
| 739 | date = CT_LE_W(date); |
---|
| 740 | ret = fat_sector_write(fs_info, sec, byte + MSDOS_FILE_CDATE_OFFSET, |
---|
| 741 | 2, (char *)(&date)); |
---|
| 742 | if ( ret < 0 ) |
---|
| 743 | rc = -1; |
---|
| 744 | |
---|
| 745 | return rc; |
---|
[f36a7bfc] | 746 | } |
---|
| 747 | |
---|
[a7eaaae8] | 748 | /* fat_set_first_cluster_num -- |
---|
[a5305f6b] | 749 | * Write number of first cluster of the file to the disk (to corresponded |
---|
| 750 | * 32bytes slot) |
---|
[f36a7bfc] | 751 | * |
---|
| 752 | * PARAMETERS: |
---|
[a7eaaae8] | 753 | * fs_info - fat fs info |
---|
[f36a7bfc] | 754 | * fat_fd - fat-file descriptor |
---|
| 755 | * |
---|
| 756 | * RETURNS: |
---|
| 757 | * RC_OK on success, or -1 if error occured |
---|
| 758 | * |
---|
| 759 | */ |
---|
| 760 | int |
---|
[a7eaaae8] | 761 | fat_file_write_first_cluster_num( |
---|
| 762 | fat_fs_info_t *fs_info, |
---|
[f36a7bfc] | 763 | fat_file_fd_t *fat_fd |
---|
| 764 | ) |
---|
| 765 | { |
---|
| 766 | ssize_t ret1 = 0, ret2 = 0; |
---|
[f91bbe64] | 767 | uint32_t new_cln = fat_fd->cln; |
---|
| 768 | uint16_t le_cl_low = 0; |
---|
[a5305f6b] | 769 | uint16_t le_cl_hi = 0; |
---|
[f91bbe64] | 770 | uint32_t sec = 0; |
---|
| 771 | uint32_t byte = 0; |
---|
[f36a7bfc] | 772 | |
---|
[a5305f6b] | 773 | /* |
---|
[42a22f08] | 774 | * calculate input for fat_sector_write: convert (cluster num, offset) to |
---|
[f36a7bfc] | 775 | * (sector num, new offset) |
---|
| 776 | */ |
---|
[a7eaaae8] | 777 | sec = fat_cluster_num_to_sector_num(fs_info, fat_fd->dir_pos.sname.cln); |
---|
| 778 | sec += (fat_fd->dir_pos.sname.ofs >> fs_info->vol.sec_log2); |
---|
[f36a7bfc] | 779 | /* byte from points to start of 32bytes structure */ |
---|
[a7eaaae8] | 780 | byte = fat_fd->dir_pos.sname.ofs & (fs_info->vol.bps - 1); |
---|
[a5305f6b] | 781 | |
---|
[f91bbe64] | 782 | le_cl_low = CT_LE_W((uint16_t )(new_cln & 0x0000FFFF)); |
---|
[a7eaaae8] | 783 | ret1 = fat_sector_write(fs_info, sec, |
---|
[f36a7bfc] | 784 | byte + MSDOS_FIRST_CLUSTER_LOW_OFFSET, 2, |
---|
| 785 | (char *)(&le_cl_low)); |
---|
[f91bbe64] | 786 | le_cl_hi = CT_LE_W((uint16_t )((new_cln & 0xFFFF0000) >> 16)); |
---|
[a7eaaae8] | 787 | ret2 = fat_sector_write(fs_info, sec, |
---|
[f36a7bfc] | 788 | byte + MSDOS_FIRST_CLUSTER_HI_OFFSET, 2, |
---|
| 789 | (char *)(&le_cl_hi)); |
---|
| 790 | if ( (ret1 < 0) || (ret2 < 0) ) |
---|
| 791 | return -1; |
---|
| 792 | |
---|
| 793 | return RC_OK; |
---|
| 794 | } |
---|
| 795 | |
---|
| 796 | |
---|
[a7eaaae8] | 797 | /* fat_set_file size -- |
---|
[a5305f6b] | 798 | * Write file size of the file to the disk (to corresponded 32bytes slot) |
---|
[f36a7bfc] | 799 | * |
---|
| 800 | * PARAMETERS: |
---|
[a7eaaae8] | 801 | * fs_info - fat fs info |
---|
[f36a7bfc] | 802 | * fat_fd - fat-file descriptor |
---|
| 803 | * |
---|
| 804 | * RETURNS: |
---|
| 805 | * RC_OK on success, or -1 if error occured (errno set apropriately). |
---|
| 806 | * |
---|
| 807 | */ |
---|
| 808 | int |
---|
[a7eaaae8] | 809 | fat_file_write_file_size( |
---|
| 810 | fat_fs_info_t *fs_info, |
---|
[f36a7bfc] | 811 | fat_file_fd_t *fat_fd |
---|
| 812 | ) |
---|
| 813 | { |
---|
| 814 | ssize_t ret = 0; |
---|
[f91bbe64] | 815 | uint32_t le_new_length = 0; |
---|
| 816 | uint32_t sec = 0; |
---|
| 817 | uint32_t byte = 0; |
---|
[f36a7bfc] | 818 | |
---|
[a7eaaae8] | 819 | sec = fat_cluster_num_to_sector_num(fs_info, fat_fd->dir_pos.sname.cln); |
---|
| 820 | sec += (fat_fd->dir_pos.sname.ofs >> fs_info->vol.sec_log2); |
---|
| 821 | byte = (fat_fd->dir_pos.sname.ofs & (fs_info->vol.bps - 1)); |
---|
[f36a7bfc] | 822 | |
---|
| 823 | le_new_length = CT_LE_L((fat_fd->fat_file_size)); |
---|
[a7eaaae8] | 824 | ret = fat_sector_write(fs_info, sec, byte + MSDOS_FILE_SIZE_OFFSET, 4, |
---|
[f36a7bfc] | 825 | (char *)(&le_new_length)); |
---|
| 826 | if ( ret < 0 ) |
---|
| 827 | return -1; |
---|
| 828 | |
---|
| 829 | return RC_OK; |
---|
| 830 | } |
---|
| 831 | |
---|
| 832 | /* |
---|
| 833 | * We should not check whether this routine is called for root dir - it |
---|
| 834 | * never can happend |
---|
| 835 | */ |
---|
| 836 | |
---|
| 837 | /* msdos_set_first_char4file_name -- |
---|
[a5305f6b] | 838 | * Write first character of the name of the file to the disk (to |
---|
| 839 | * corresponded 32bytes slot) |
---|
[f36a7bfc] | 840 | * |
---|
| 841 | * PARAMETERS: |
---|
[a5305f6b] | 842 | * mt_entry - mount table entry |
---|
[f36a7bfc] | 843 | * cl - number of cluster |
---|
| 844 | * ofs - offset inside cluster |
---|
| 845 | * fchar - character to set up |
---|
| 846 | * |
---|
| 847 | * RETURNS: |
---|
| 848 | * RC_OK on success, or -1 if error occured (errno set apropriately) |
---|
| 849 | * |
---|
| 850 | */ |
---|
[a5305f6b] | 851 | int |
---|
[f36a7bfc] | 852 | msdos_set_first_char4file_name( |
---|
[a5305f6b] | 853 | rtems_filesystem_mount_table_entry_t *mt_entry, |
---|
[07d6fd5] | 854 | fat_dir_pos_t *dir_pos, |
---|
[f36a7bfc] | 855 | unsigned char fchar |
---|
| 856 | ) |
---|
| 857 | { |
---|
[07d6fd5] | 858 | ssize_t ret; |
---|
[f36a7bfc] | 859 | msdos_fs_info_t *fs_info = mt_entry->fs_info; |
---|
[07d6fd5] | 860 | uint32_t dir_block_size; |
---|
| 861 | fat_pos_t start = dir_pos->lname; |
---|
| 862 | fat_pos_t end = dir_pos->sname; |
---|
[f36a7bfc] | 863 | |
---|
[07d6fd5] | 864 | if ((end.cln == fs_info->fat.vol.rdir_cl) && |
---|
| 865 | (fs_info->fat.vol.type & (FAT_FAT12 | FAT_FAT16))) |
---|
| 866 | dir_block_size = fs_info->fat.vol.rdir_size; |
---|
| 867 | else |
---|
| 868 | dir_block_size = fs_info->fat.vol.bpc; |
---|
[0a7278e] | 869 | |
---|
[07d6fd5] | 870 | if (dir_pos->lname.cln == FAT_FILE_SHORT_NAME) |
---|
| 871 | start = dir_pos->sname; |
---|
[f36a7bfc] | 872 | |
---|
[07d6fd5] | 873 | /* |
---|
| 874 | * We handle the changes directly due the way the short file |
---|
| 875 | * name code was written rather than use the fat_file_write |
---|
| 876 | * interface. |
---|
| 877 | */ |
---|
| 878 | while (true) |
---|
| 879 | { |
---|
[c65afce4] | 880 | uint32_t sec = (fat_cluster_num_to_sector_num(&fs_info->fat, start.cln) + |
---|
[07d6fd5] | 881 | (start.ofs >> fs_info->fat.vol.sec_log2)); |
---|
[721fe34] | 882 | uint32_t byte = (start.ofs & (fs_info->fat.vol.bps - 1)); |
---|
[07d6fd5] | 883 | |
---|
[42a22f08] | 884 | ret = fat_sector_write(&fs_info->fat, sec, byte + MSDOS_FILE_NAME_OFFSET, |
---|
[c65afce4] | 885 | 1, &fchar); |
---|
[07d6fd5] | 886 | if (ret < 0) |
---|
[f36a7bfc] | 887 | return -1; |
---|
| 888 | |
---|
[07d6fd5] | 889 | if ((start.cln == end.cln) && (start.ofs == end.ofs)) |
---|
| 890 | break; |
---|
| 891 | |
---|
| 892 | start.ofs += MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE; |
---|
| 893 | if (start.ofs >= dir_block_size) |
---|
| 894 | { |
---|
| 895 | int rc; |
---|
| 896 | if ((end.cln == fs_info->fat.vol.rdir_cl) && |
---|
| 897 | (fs_info->fat.vol.type & (FAT_FAT12 | FAT_FAT16))) |
---|
| 898 | break; |
---|
[c65afce4] | 899 | rc = fat_get_fat_cluster(&fs_info->fat, start.cln, &start.cln); |
---|
[07d6fd5] | 900 | if ( rc != RC_OK ) |
---|
| 901 | return rc; |
---|
| 902 | start.ofs = 0; |
---|
| 903 | } |
---|
| 904 | } |
---|
| 905 | |
---|
[f36a7bfc] | 906 | return RC_OK; |
---|
[a5305f6b] | 907 | } |
---|
[f36a7bfc] | 908 | |
---|
| 909 | /* msdos_dir_is_empty -- |
---|
[a5305f6b] | 910 | * Check whether directory which correspondes to the fat-file descriptor is |
---|
| 911 | * empty. |
---|
[f36a7bfc] | 912 | * |
---|
| 913 | * PARAMETERS: |
---|
[a5305f6b] | 914 | * mt_entry - mount table entry |
---|
| 915 | * fat_fd - fat-file descriptor |
---|
| 916 | * ret_val - placeholder for result |
---|
[f36a7bfc] | 917 | * |
---|
| 918 | * RETURNS: |
---|
| 919 | * RC_OK on success, or -1 if error occured |
---|
| 920 | * |
---|
| 921 | */ |
---|
| 922 | int |
---|
| 923 | msdos_dir_is_empty( |
---|
| 924 | rtems_filesystem_mount_table_entry_t *mt_entry, |
---|
[a5305f6b] | 925 | fat_file_fd_t *fat_fd, |
---|
[0a896eb] | 926 | bool *ret_val |
---|
[f36a7bfc] | 927 | ) |
---|
| 928 | { |
---|
| 929 | ssize_t ret = 0; |
---|
| 930 | msdos_fs_info_t *fs_info = mt_entry->fs_info; |
---|
[f91bbe64] | 931 | uint32_t j = 0, i = 0; |
---|
[a5305f6b] | 932 | |
---|
[f36a7bfc] | 933 | /* dir is not empty */ |
---|
[0a896eb] | 934 | *ret_val = false; |
---|
[f36a7bfc] | 935 | |
---|
[c65afce4] | 936 | while ((ret = fat_file_read(&fs_info->fat, fat_fd, j * fs_info->fat.vol.bps, |
---|
[a5305f6b] | 937 | fs_info->fat.vol.bps, |
---|
[f36a7bfc] | 938 | fs_info->cl_buf)) != FAT_EOF) |
---|
| 939 | { |
---|
| 940 | if (ret < MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE) |
---|
| 941 | return -1; |
---|
| 942 | |
---|
| 943 | assert(ret == fs_info->fat.vol.bps); |
---|
[a5305f6b] | 944 | |
---|
[b17ff491] | 945 | /* have to look at the DIR_NAME as "raw" 8-bit data */ |
---|
[a5305f6b] | 946 | for (i = 0; |
---|
| 947 | i < fs_info->fat.vol.bps; |
---|
[f36a7bfc] | 948 | i += MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE) |
---|
| 949 | { |
---|
[07d6fd5] | 950 | char* entry = (char*) fs_info->cl_buf + i; |
---|
| 951 | |
---|
| 952 | /* |
---|
| 953 | * If the entry is empty, a long file name entry, or '.' or '..' |
---|
| 954 | * then consider it as empty. |
---|
| 955 | * |
---|
| 956 | * Just ignore long file name entries. They must have a short entry to |
---|
| 957 | * be valid. |
---|
| 958 | */ |
---|
| 959 | if (((*MSDOS_DIR_ENTRY_TYPE(entry)) == |
---|
[a5305f6b] | 960 | MSDOS_THIS_DIR_ENTRY_EMPTY) || |
---|
[07d6fd5] | 961 | ((*MSDOS_DIR_ATTR(entry) & MSDOS_ATTR_LFN_MASK) == |
---|
| 962 | MSDOS_ATTR_LFN) || |
---|
| 963 | (strncmp(MSDOS_DIR_NAME((entry)), MSDOS_DOT_NAME, |
---|
[f36a7bfc] | 964 | MSDOS_SHORT_NAME_LEN) == 0) || |
---|
[07d6fd5] | 965 | (strncmp(MSDOS_DIR_NAME((entry)), |
---|
[a5305f6b] | 966 | MSDOS_DOTDOT_NAME, |
---|
[f36a7bfc] | 967 | MSDOS_SHORT_NAME_LEN) == 0)) |
---|
| 968 | continue; |
---|
| 969 | |
---|
[07d6fd5] | 970 | /* |
---|
| 971 | * Nothing more to look at. |
---|
| 972 | */ |
---|
| 973 | if ((*MSDOS_DIR_NAME(entry)) == |
---|
[f36a7bfc] | 974 | MSDOS_THIS_DIR_ENTRY_AND_REST_EMPTY) |
---|
| 975 | { |
---|
[0a896eb] | 976 | *ret_val = true; |
---|
[f36a7bfc] | 977 | return RC_OK; |
---|
| 978 | } |
---|
[07d6fd5] | 979 | |
---|
| 980 | /* |
---|
| 981 | * Short file name entries mean not empty. |
---|
| 982 | */ |
---|
[f36a7bfc] | 983 | return RC_OK; |
---|
[a5305f6b] | 984 | } |
---|
| 985 | j++; |
---|
[f36a7bfc] | 986 | } |
---|
[0a896eb] | 987 | *ret_val = true; |
---|
[f36a7bfc] | 988 | return RC_OK; |
---|
| 989 | } |
---|
| 990 | |
---|
[d2e0bb3] | 991 | #define MSDOS_FIND_PRINT 0 |
---|
| 992 | static int |
---|
| 993 | msdos_on_entry_found ( |
---|
| 994 | msdos_fs_info_t *fs_info, |
---|
| 995 | fat_file_fd_t *fat_fd, |
---|
| 996 | const uint32_t bts2rd, |
---|
| 997 | char *name_dir_entry, |
---|
| 998 | char *entry, |
---|
| 999 | fat_dir_pos_t *dir_pos, |
---|
| 1000 | uint32_t *dir_offset, |
---|
| 1001 | const uint32_t dir_entry, |
---|
| 1002 | const fat_pos_t *lfn_start |
---|
| 1003 | ) |
---|
| 1004 | { |
---|
| 1005 | int rc = RC_OK; |
---|
| 1006 | #if MSDOS_FIND_PRINT |
---|
| 1007 | printf ("MSFS:[9.3] SNF found\n"); |
---|
| 1008 | #endif |
---|
| 1009 | /* |
---|
| 1010 | * We get the entry we looked for - fill the position |
---|
| 1011 | * structure and the 32 bytes of the short entry |
---|
| 1012 | */ |
---|
| 1013 | rc = fat_file_ioctl(&fs_info->fat, |
---|
| 1014 | fat_fd, |
---|
| 1015 | F_CLU_NUM, |
---|
| 1016 | *dir_offset * bts2rd, |
---|
| 1017 | &dir_pos->sname.cln); |
---|
| 1018 | if (rc == RC_OK) { |
---|
| 1019 | dir_pos->sname.ofs = dir_entry; |
---|
| 1020 | |
---|
| 1021 | if (lfn_start->cln != FAT_FILE_SHORT_NAME) |
---|
| 1022 | { |
---|
| 1023 | rc = fat_file_ioctl (&fs_info->fat, |
---|
| 1024 | fat_fd, |
---|
| 1025 | F_CLU_NUM, |
---|
| 1026 | lfn_start->cln * bts2rd, |
---|
| 1027 | &lfn_start->cln); |
---|
| 1028 | } |
---|
| 1029 | if ( rc == RC_OK ) { |
---|
| 1030 | dir_pos->lname.cln = lfn_start->cln; |
---|
| 1031 | dir_pos->lname.ofs = lfn_start->ofs; |
---|
| 1032 | |
---|
| 1033 | memcpy(name_dir_entry, entry, |
---|
| 1034 | MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE); |
---|
| 1035 | } |
---|
| 1036 | } |
---|
| 1037 | |
---|
| 1038 | return rc; |
---|
| 1039 | } |
---|
| 1040 | |
---|
| 1041 | ssize_t |
---|
| 1042 | msdos_get_utf16_string_from_long_entry ( |
---|
| 1043 | const char *entry, |
---|
| 1044 | uint16_t *entry_string_buf, |
---|
| 1045 | const size_t buf_size, |
---|
| 1046 | bool is_first_entry |
---|
| 1047 | ) |
---|
| 1048 | { |
---|
| 1049 | ssize_t chars_in_entry; |
---|
| 1050 | |
---|
[83a4cbb] | 1051 | if (buf_size >= MSDOS_LFN_ENTRY_SIZE) { |
---|
[d2e0bb3] | 1052 | memcpy (&entry_string_buf[0], &entry[1], 10 ); |
---|
| 1053 | memcpy (&entry_string_buf[5], &entry[14], 12 ); |
---|
| 1054 | memcpy (&entry_string_buf[11], &entry[28], 4 ); |
---|
| 1055 | |
---|
| 1056 | if (is_first_entry) { |
---|
| 1057 | for (chars_in_entry = 0; |
---|
[92422be] | 1058 | ( chars_in_entry < MSDOS_LFN_LEN_PER_ENTRY |
---|
| 1059 | && entry_string_buf[chars_in_entry] != 0x0000); |
---|
[d2e0bb3] | 1060 | ++chars_in_entry) { |
---|
| 1061 | ; |
---|
| 1062 | } |
---|
| 1063 | } |
---|
| 1064 | else |
---|
| 1065 | chars_in_entry = MSDOS_LFN_LEN_PER_ENTRY; |
---|
| 1066 | |
---|
| 1067 | return chars_in_entry * MSDOS_NAME_LFN_BYTES_PER_CHAR; |
---|
| 1068 | } |
---|
| 1069 | else |
---|
| 1070 | return ENOMEM; |
---|
| 1071 | } |
---|
| 1072 | |
---|
| 1073 | /* msdos_format_dirent_with_dot -- |
---|
| 1074 | * This routine convert a (short) MSDOS filename as present on disk |
---|
| 1075 | * (fixed 8+3 characters, filled with blanks, without separator dot) |
---|
| 1076 | * to a "normal" format, with between 0 and 8 name chars, |
---|
| 1077 | * a separating dot and up to 3 extension characters |
---|
| 1078 | * Rules to work: |
---|
| 1079 | * - copy any (0-8) "name" part characters that are non-blank |
---|
| 1080 | * - if an extension exists, append a dot |
---|
| 1081 | * - copy any (0-3) non-blank extension characters |
---|
| 1082 | * - append a '\0' (dont count it for the rturn code |
---|
[f36a7bfc] | 1083 | * |
---|
| 1084 | * PARAMETERS: |
---|
[d2e0bb3] | 1085 | * dst: pointer to destination char array (must be big enough) |
---|
| 1086 | * src: pointer to source characters |
---|
[f36a7bfc] | 1087 | * |
---|
| 1088 | * |
---|
[d2e0bb3] | 1089 | * RETURNS: |
---|
| 1090 | * the number of bytes (without trailing '\0'(written to destination |
---|
[f36a7bfc] | 1091 | */ |
---|
[d2e0bb3] | 1092 | ssize_t |
---|
| 1093 | msdos_format_dirent_with_dot(char *dst,const char *src) |
---|
[f36a7bfc] | 1094 | { |
---|
[d2e0bb3] | 1095 | ssize_t len; |
---|
| 1096 | int i; |
---|
| 1097 | const char *src_tmp; |
---|
| 1098 | |
---|
| 1099 | /* |
---|
| 1100 | * find last non-blank character of base name |
---|
| 1101 | */ |
---|
| 1102 | for (i = MSDOS_SHORT_BASE_LEN, src_tmp = src + MSDOS_SHORT_BASE_LEN - 1; |
---|
| 1103 | i > 0 && *src_tmp == ' '; |
---|
| 1104 | --i,--src_tmp) |
---|
| 1105 | {}; |
---|
| 1106 | /* |
---|
| 1107 | * copy base name to destination |
---|
| 1108 | */ |
---|
| 1109 | src_tmp = src; |
---|
| 1110 | len = i; |
---|
| 1111 | while (i-- > 0) { |
---|
| 1112 | *dst++ = tolower((unsigned char)(*src_tmp++)); |
---|
| 1113 | } |
---|
| 1114 | /* |
---|
| 1115 | * find last non-blank character of extension |
---|
| 1116 | */ |
---|
| 1117 | for (i = MSDOS_SHORT_EXT_LEN, |
---|
| 1118 | src_tmp = src + MSDOS_SHORT_BASE_LEN+MSDOS_SHORT_EXT_LEN-1; |
---|
| 1119 | i > 0 && *src_tmp == ' '; |
---|
| 1120 | --i, --src_tmp) |
---|
| 1121 | {}; |
---|
| 1122 | /* |
---|
| 1123 | * extension is not empty |
---|
| 1124 | */ |
---|
| 1125 | if (i > 0) { |
---|
| 1126 | *dst++ = '.'; /* append dot */ |
---|
| 1127 | ++len; /* dot */ |
---|
| 1128 | src_tmp = src + MSDOS_SHORT_BASE_LEN; |
---|
| 1129 | while (i-- > 0) { |
---|
| 1130 | *dst++ = tolower((unsigned char)(*src_tmp++)); |
---|
| 1131 | ++len; |
---|
| 1132 | } |
---|
| 1133 | } |
---|
| 1134 | *dst = '\0'; /* terminate string */ |
---|
[07d6fd5] | 1135 | |
---|
[d2e0bb3] | 1136 | return len; |
---|
| 1137 | } |
---|
[0a7278e] | 1138 | |
---|
[d2e0bb3] | 1139 | static ssize_t |
---|
| 1140 | msdos_long_entry_to_utf8_name ( |
---|
| 1141 | rtems_dosfs_convert_control *converter, |
---|
| 1142 | const char *entry, |
---|
| 1143 | const bool is_first_entry, |
---|
| 1144 | uint8_t *entry_utf8_buf, |
---|
| 1145 | const size_t buf_size) |
---|
| 1146 | { |
---|
| 1147 | ssize_t retval = 0; |
---|
| 1148 | int eno = 0; |
---|
| 1149 | size_t bytes_in_utf8 = buf_size; |
---|
| 1150 | size_t bytes_in_buf; |
---|
| 1151 | uint16_t entry_string[MSDOS_LFN_LEN_PER_ENTRY]; |
---|
| 1152 | |
---|
| 1153 | retval = msdos_get_utf16_string_from_long_entry ( |
---|
| 1154 | entry, |
---|
| 1155 | entry_string, |
---|
| 1156 | sizeof (entry_string), |
---|
| 1157 | is_first_entry |
---|
| 1158 | ); |
---|
| 1159 | |
---|
| 1160 | if (retval >= 0) { |
---|
| 1161 | bytes_in_buf = retval; |
---|
| 1162 | eno = (*converter->handler->utf16_to_utf8) ( |
---|
| 1163 | converter, |
---|
| 1164 | &entry_string[0], |
---|
| 1165 | bytes_in_buf, |
---|
| 1166 | &entry_utf8_buf[0], |
---|
| 1167 | &bytes_in_utf8); |
---|
| 1168 | if ( eno == 0 ) { |
---|
| 1169 | retval = bytes_in_utf8; |
---|
| 1170 | } |
---|
| 1171 | } |
---|
[0a7278e] | 1172 | |
---|
[d2e0bb3] | 1173 | if (eno != 0) { |
---|
| 1174 | retval = -1; |
---|
| 1175 | errno = eno; |
---|
| 1176 | } |
---|
[0a7278e] | 1177 | |
---|
[d2e0bb3] | 1178 | return retval; |
---|
| 1179 | } |
---|
[a5305f6b] | 1180 | |
---|
[d2e0bb3] | 1181 | static ssize_t msdos_short_entry_to_utf8_name ( |
---|
| 1182 | rtems_dosfs_convert_control *converter, |
---|
| 1183 | const char *entry, |
---|
| 1184 | uint8_t *buf, |
---|
| 1185 | const size_t buf_size) |
---|
| 1186 | { |
---|
| 1187 | char char_buf[MSDOS_NAME_MAX_WITH_DOT]; |
---|
| 1188 | int eno = 0; |
---|
| 1189 | size_t bytes_converted = buf_size; |
---|
| 1190 | ssize_t bytes_written = msdos_format_dirent_with_dot(char_buf, entry); |
---|
| 1191 | |
---|
| 1192 | if (bytes_written > 0) { |
---|
| 1193 | if (char_buf[0] == 0x05) |
---|
| 1194 | char_buf[0] = 0xE5; |
---|
| 1195 | |
---|
| 1196 | eno = (*converter->handler->codepage_to_utf8) ( |
---|
| 1197 | converter, |
---|
| 1198 | char_buf, |
---|
| 1199 | bytes_written, |
---|
| 1200 | buf, |
---|
| 1201 | &bytes_converted); |
---|
| 1202 | if (eno == 0) |
---|
| 1203 | bytes_written = bytes_converted; |
---|
| 1204 | } else { |
---|
| 1205 | eno = EINVAL; |
---|
| 1206 | } |
---|
| 1207 | |
---|
| 1208 | if (eno != 0) { |
---|
| 1209 | bytes_written = -1; |
---|
| 1210 | errno = eno; |
---|
| 1211 | } |
---|
| 1212 | |
---|
| 1213 | return bytes_written; |
---|
| 1214 | } |
---|
[07d6fd5] | 1215 | |
---|
[d2e0bb3] | 1216 | static ssize_t |
---|
| 1217 | msdos_compare_entry_against_filename ( |
---|
| 1218 | rtems_dosfs_convert_control *converter, |
---|
| 1219 | const uint8_t *entry, |
---|
| 1220 | const size_t entry_size, |
---|
| 1221 | const uint8_t *filename, |
---|
| 1222 | const size_t filename_size_remaining, |
---|
| 1223 | bool *is_matching) |
---|
| 1224 | { |
---|
| 1225 | ssize_t size_remaining = filename_size_remaining; |
---|
| 1226 | int eno = 0; |
---|
[83a4cbb] | 1227 | uint8_t entry_normalized[MSDOS_LFN_ENTRY_SIZE_UTF8]; |
---|
[d2e0bb3] | 1228 | size_t bytes_in_entry_normalized = sizeof ( entry_normalized ); |
---|
| 1229 | |
---|
| 1230 | eno = (*converter->handler->utf8_normalize_and_fold) ( |
---|
| 1231 | converter, |
---|
| 1232 | &entry[0], |
---|
| 1233 | entry_size, |
---|
| 1234 | &entry_normalized[0], |
---|
| 1235 | &bytes_in_entry_normalized); |
---|
| 1236 | if (eno == 0) { |
---|
| 1237 | #if MSDOS_FIND_PRINT > 1 |
---|
| 1238 | printf ( "MSFS:[6] entry_normalized:%s" |
---|
| 1239 | "name:%s\n", |
---|
| 1240 | entry, |
---|
| 1241 | filename ); |
---|
[07d6fd5] | 1242 | #endif |
---|
[d2e0bb3] | 1243 | if (bytes_in_entry_normalized <= size_remaining) { |
---|
| 1244 | size_remaining = size_remaining - bytes_in_entry_normalized; |
---|
| 1245 | if (0 == memcmp ( &entry_normalized[0], |
---|
| 1246 | &filename[size_remaining], |
---|
| 1247 | bytes_in_entry_normalized)) { |
---|
| 1248 | *is_matching = true; |
---|
| 1249 | } else { |
---|
| 1250 | *is_matching = false; |
---|
| 1251 | size_remaining = filename_size_remaining; |
---|
| 1252 | } |
---|
| 1253 | |
---|
| 1254 | } |
---|
| 1255 | else { |
---|
| 1256 | *is_matching = false; |
---|
| 1257 | } |
---|
| 1258 | } |
---|
| 1259 | |
---|
| 1260 | if (eno != 0) { |
---|
| 1261 | size_remaining = -1; |
---|
| 1262 | errno = eno; |
---|
| 1263 | } |
---|
| 1264 | |
---|
| 1265 | return size_remaining; |
---|
| 1266 | } |
---|
| 1267 | |
---|
| 1268 | static int |
---|
| 1269 | msdos_find_file_in_directory ( |
---|
| 1270 | const uint8_t *filename_converted, |
---|
| 1271 | const size_t name_len_for_compare, |
---|
| 1272 | const size_t name_len_for_save, |
---|
| 1273 | const msdos_name_type_t name_type, |
---|
| 1274 | msdos_fs_info_t *fs_info, |
---|
| 1275 | fat_file_fd_t *fat_fd, |
---|
| 1276 | const uint32_t bts2rd, |
---|
| 1277 | const bool create_node, |
---|
| 1278 | const unsigned int fat_entries, |
---|
| 1279 | char *name_dir_entry, |
---|
| 1280 | fat_dir_pos_t *dir_pos, |
---|
| 1281 | uint32_t *dir_offset, |
---|
| 1282 | uint32_t *empty_space_offset, |
---|
| 1283 | uint32_t *empty_space_entry, |
---|
| 1284 | uint32_t *empty_space_count) |
---|
| 1285 | { |
---|
| 1286 | int rc = RC_OK; |
---|
| 1287 | ssize_t bytes_read; |
---|
| 1288 | uint32_t dir_entry; |
---|
| 1289 | fat_pos_t lfn_start; |
---|
| 1290 | uint8_t lfn_checksum = 0; |
---|
| 1291 | bool entry_matched = false; |
---|
| 1292 | bool empty_space_found = false; |
---|
| 1293 | uint32_t entries_per_block = bts2rd / MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE; |
---|
| 1294 | int lfn_entry = 0; |
---|
[83a4cbb] | 1295 | uint8_t entry_utf8_normalized[MSDOS_LFN_ENTRY_SIZE_UTF8]; |
---|
[d2e0bb3] | 1296 | size_t bytes_in_entry; |
---|
| 1297 | bool filename_matched = false; |
---|
| 1298 | ssize_t filename_size_remaining = name_len_for_compare; |
---|
| 1299 | rtems_dosfs_convert_control *converter = fs_info->converter; |
---|
| 1300 | |
---|
[07d6fd5] | 1301 | /* |
---|
| 1302 | * Scan the directory seeing if the file is present. While |
---|
| 1303 | * doing this see if a suitable location can be found to |
---|
| 1304 | * create the entry if the name is not found. |
---|
| 1305 | */ |
---|
[d2e0bb3] | 1306 | |
---|
| 1307 | lfn_start.cln = lfn_start.ofs = FAT_FILE_SHORT_NAME; |
---|
| 1308 | |
---|
| 1309 | while ( (bytes_read = fat_file_read (&fs_info->fat, fat_fd, (*dir_offset * bts2rd), |
---|
| 1310 | bts2rd, fs_info->cl_buf)) != FAT_EOF |
---|
| 1311 | && rc == RC_OK) |
---|
[f36a7bfc] | 1312 | { |
---|
[07d6fd5] | 1313 | bool remainder_empty = false; |
---|
| 1314 | #if MSDOS_FIND_PRINT |
---|
[d2e0bb3] | 1315 | printf ("MSFS:[2] dir_offset:%li\n", *dir_offset); |
---|
[07d6fd5] | 1316 | #endif |
---|
[0a7278e] | 1317 | |
---|
[d2e0bb3] | 1318 | if (bytes_read < MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE) |
---|
[3899a537] | 1319 | rtems_set_errno_and_return_minus_one(EIO); |
---|
[a5305f6b] | 1320 | |
---|
[d2e0bb3] | 1321 | assert(bytes_read == bts2rd); |
---|
[f36a7bfc] | 1322 | |
---|
[b17ff491] | 1323 | /* have to look at the DIR_NAME as "raw" 8-bit data */ |
---|
[07d6fd5] | 1324 | for (dir_entry = 0; |
---|
[d2e0bb3] | 1325 | dir_entry < bts2rd && rc == RC_OK && (! filename_matched); |
---|
[07d6fd5] | 1326 | dir_entry += MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE) |
---|
[f36a7bfc] | 1327 | { |
---|
[07d6fd5] | 1328 | char* entry = (char*) fs_info->cl_buf + dir_entry; |
---|
| 1329 | |
---|
| 1330 | /* |
---|
| 1331 | * See if the entry is empty or the remainder of the directory is |
---|
[d2e0bb3] | 1332 | * empty ? Localize to make the code read better. |
---|
[07d6fd5] | 1333 | */ |
---|
| 1334 | bool entry_empty = (*MSDOS_DIR_ENTRY_TYPE(entry) == |
---|
| 1335 | MSDOS_THIS_DIR_ENTRY_EMPTY); |
---|
| 1336 | remainder_empty = (*MSDOS_DIR_ENTRY_TYPE(entry) == |
---|
| 1337 | MSDOS_THIS_DIR_ENTRY_AND_REST_EMPTY); |
---|
| 1338 | #if MSDOS_FIND_PRINT |
---|
| 1339 | printf ("MSFS:[3] re:%i ee:%i do:%li de:%li(%ld)\n", |
---|
[d2e0bb3] | 1340 | remainder_empty, entry_empty, *dir_offset, |
---|
[07d6fd5] | 1341 | dir_entry, (dir_entry / MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE)); |
---|
| 1342 | #endif |
---|
| 1343 | /* |
---|
| 1344 | * Remember where the we are, ie the start, so we can come back |
---|
| 1345 | * to here and write the long file name if this is the start of |
---|
| 1346 | * a series of empty entries. If empty_space_count is 0 then |
---|
| 1347 | * we are currently not inside an empty series of entries. It |
---|
| 1348 | * is a count of empty entries. |
---|
| 1349 | */ |
---|
[d2e0bb3] | 1350 | if (*empty_space_count == 0) |
---|
[f36a7bfc] | 1351 | { |
---|
[d2e0bb3] | 1352 | *empty_space_entry = dir_entry; |
---|
| 1353 | *empty_space_offset = *dir_offset; |
---|
[07d6fd5] | 1354 | } |
---|
[f36a7bfc] | 1355 | |
---|
[07d6fd5] | 1356 | if (remainder_empty) |
---|
| 1357 | { |
---|
| 1358 | #if MSDOS_FIND_PRINT |
---|
| 1359 | printf ("MSFS:[3.1] cn:%i esf:%i\n", create_node, empty_space_found); |
---|
| 1360 | #endif |
---|
[a5305f6b] | 1361 | /* |
---|
[07d6fd5] | 1362 | * If just looking and there is no more entries in the |
---|
[f36a7bfc] | 1363 | * directory - return name-not-found |
---|
| 1364 | */ |
---|
[07d6fd5] | 1365 | if (!create_node) |
---|
[d2e0bb3] | 1366 | rc = MSDOS_NAME_NOT_FOUND_ERR; |
---|
[07d6fd5] | 1367 | |
---|
| 1368 | /* |
---|
| 1369 | * Lets go and write the directory entries. If we have not found |
---|
| 1370 | * any available space add the remaining number of entries to any that |
---|
| 1371 | * we may have already found that are just before this entry. If more |
---|
| 1372 | * are needed FAT_EOF is returned by the read and we extend the file. |
---|
| 1373 | */ |
---|
[d2e0bb3] | 1374 | if ( !empty_space_found |
---|
| 1375 | && rc == RC_OK ) |
---|
[07d6fd5] | 1376 | { |
---|
[d2e0bb3] | 1377 | *empty_space_count += |
---|
[07d6fd5] | 1378 | entries_per_block - (dir_entry / MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE); |
---|
[d2e0bb3] | 1379 | empty_space_found = true; |
---|
[07d6fd5] | 1380 | #if MSDOS_FIND_PRINT |
---|
[d2e0bb3] | 1381 | printf ( "MSFS:[3.2] esf:%i esc%"PRIu32"\n", empty_space_found, *empty_space_count ); |
---|
[07d6fd5] | 1382 | #endif |
---|
| 1383 | } |
---|
| 1384 | break; |
---|
| 1385 | } |
---|
| 1386 | else if (entry_empty) |
---|
| 1387 | { |
---|
| 1388 | if (create_node) |
---|
| 1389 | { |
---|
[d2e0bb3] | 1390 | /* |
---|
| 1391 | * Remainder is not empty so is this entry empty ? |
---|
| 1392 | */ |
---|
| 1393 | (*empty_space_count)++; |
---|
[0a7278e] | 1394 | |
---|
[d2e0bb3] | 1395 | if (*empty_space_count == (fat_entries + 1)) |
---|
| 1396 | empty_space_found = true; |
---|
[07d6fd5] | 1397 | } |
---|
| 1398 | #if MSDOS_FIND_PRINT |
---|
| 1399 | printf ("MSFS:[4.1] esc:%li esf:%i\n", |
---|
[d2e0bb3] | 1400 | *empty_space_count, empty_space_found); |
---|
[07d6fd5] | 1401 | #endif |
---|
[a5305f6b] | 1402 | } |
---|
[f36a7bfc] | 1403 | else |
---|
| 1404 | { |
---|
[07d6fd5] | 1405 | /* |
---|
| 1406 | * A valid entry so handle it. |
---|
| 1407 | * |
---|
| 1408 | * If empty space has not been found we need to start the |
---|
| 1409 | * count again. |
---|
| 1410 | */ |
---|
| 1411 | if (create_node && !empty_space_found) |
---|
| 1412 | { |
---|
[d2e0bb3] | 1413 | *empty_space_entry = 0; |
---|
| 1414 | *empty_space_count = 0; |
---|
[07d6fd5] | 1415 | } |
---|
| 1416 | |
---|
| 1417 | /* |
---|
| 1418 | * Check the attribute to see if the entry is for a long |
---|
| 1419 | * file name. |
---|
| 1420 | */ |
---|
| 1421 | if ((*MSDOS_DIR_ATTR(entry) & MSDOS_ATTR_LFN_MASK) == |
---|
| 1422 | MSDOS_ATTR_LFN) |
---|
[f36a7bfc] | 1423 | { |
---|
[d2e0bb3] | 1424 | /* int o;*/ |
---|
[07d6fd5] | 1425 | #if MSDOS_FIND_PRINT |
---|
| 1426 | printf ("MSFS:[4.2] lfn:%c entry:%i checksum:%i\n", |
---|
[8f77f8f] | 1427 | lfn_start.cln == FAT_FILE_SHORT_NAME ? 'f' : 't', |
---|
[07d6fd5] | 1428 | *MSDOS_DIR_ENTRY_TYPE(entry) & MSDOS_LAST_LONG_ENTRY_MASK, |
---|
| 1429 | *MSDOS_DIR_LFN_CHECKSUM(entry)); |
---|
| 1430 | #endif |
---|
| 1431 | /* |
---|
| 1432 | * If we are not already processing a LFN see if this is |
---|
| 1433 | * the first entry of a LFN ? |
---|
| 1434 | */ |
---|
| 1435 | if (lfn_start.cln == FAT_FILE_SHORT_NAME) |
---|
[f36a7bfc] | 1436 | { |
---|
[d2e0bb3] | 1437 | entry_matched = false; |
---|
[07d6fd5] | 1438 | |
---|
| 1439 | /* |
---|
| 1440 | * The first entry must have the last long entry |
---|
| 1441 | * flag set. |
---|
| 1442 | */ |
---|
| 1443 | if ((*MSDOS_DIR_ENTRY_TYPE(entry) & |
---|
| 1444 | MSDOS_LAST_LONG_ENTRY) == 0) |
---|
| 1445 | continue; |
---|
| 1446 | |
---|
| 1447 | /* |
---|
| 1448 | * Does the number of entries in the LFN directory |
---|
| 1449 | * entry match the number we expect for this |
---|
| 1450 | * file name. Note we do not know the number of |
---|
| 1451 | * characters in the entry so this is check further |
---|
| 1452 | * on when the characters are checked. |
---|
| 1453 | */ |
---|
[d2e0bb3] | 1454 | if (fat_entries != (*MSDOS_DIR_ENTRY_TYPE(entry) & |
---|
[07d6fd5] | 1455 | MSDOS_LAST_LONG_ENTRY_MASK)) |
---|
| 1456 | continue; |
---|
[0a7278e] | 1457 | |
---|
[a5305f6b] | 1458 | /* |
---|
[07d6fd5] | 1459 | * Get the checksum of the short entry. |
---|
[f36a7bfc] | 1460 | */ |
---|
[d2e0bb3] | 1461 | lfn_start.cln = *dir_offset; |
---|
[07d6fd5] | 1462 | lfn_start.ofs = dir_entry; |
---|
[d2e0bb3] | 1463 | lfn_entry = fat_entries; |
---|
[07d6fd5] | 1464 | lfn_checksum = *MSDOS_DIR_LFN_CHECKSUM(entry); |
---|
[0a7278e] | 1465 | |
---|
[07d6fd5] | 1466 | #if MSDOS_FIND_PRINT |
---|
| 1467 | printf ("MSFS:[4.3] lfn_checksum:%i\n", |
---|
[d2e0bb3] | 1468 | lfn_checksum); |
---|
[07d6fd5] | 1469 | #endif |
---|
| 1470 | } |
---|
| 1471 | |
---|
| 1472 | /* |
---|
| 1473 | * If the entry number or the check sum do not match |
---|
| 1474 | * forget this series of long directory entries. These |
---|
| 1475 | * could be orphaned entries depending on the history |
---|
| 1476 | * of the disk. |
---|
| 1477 | */ |
---|
| 1478 | if ((lfn_entry != (*MSDOS_DIR_ENTRY_TYPE(entry) & |
---|
| 1479 | MSDOS_LAST_LONG_ENTRY_MASK)) || |
---|
| 1480 | (lfn_checksum != *MSDOS_DIR_LFN_CHECKSUM(entry))) |
---|
| 1481 | { |
---|
| 1482 | #if MSDOS_FIND_PRINT |
---|
| 1483 | printf ("MSFS:[4.4] no match\n"); |
---|
| 1484 | #endif |
---|
| 1485 | lfn_start.cln = FAT_FILE_SHORT_NAME; |
---|
| 1486 | continue; |
---|
| 1487 | } |
---|
| 1488 | #if MSDOS_FIND_PRINT |
---|
| 1489 | printf ("MSFS:[5] lfne:%i\n", lfn_entry); |
---|
| 1490 | #endif |
---|
[d2e0bb3] | 1491 | lfn_entry--; |
---|
[0a7278e] | 1492 | |
---|
[d2e0bb3] | 1493 | bytes_in_entry = msdos_long_entry_to_utf8_name ( |
---|
| 1494 | converter, |
---|
| 1495 | entry, |
---|
| 1496 | (lfn_entry + 1) == fat_entries, |
---|
| 1497 | &entry_utf8_normalized[0], |
---|
| 1498 | sizeof (entry_utf8_normalized)); |
---|
| 1499 | if (bytes_in_entry > 0) { |
---|
| 1500 | filename_size_remaining = msdos_compare_entry_against_filename ( |
---|
| 1501 | converter, |
---|
| 1502 | &entry_utf8_normalized[0], |
---|
| 1503 | bytes_in_entry, |
---|
| 1504 | &filename_converted[0], |
---|
| 1505 | filename_size_remaining, |
---|
| 1506 | &entry_matched); |
---|
| 1507 | |
---|
| 1508 | if (filename_size_remaining < 0 |
---|
| 1509 | || (! entry_matched)) { |
---|
| 1510 | filename_size_remaining = name_len_for_compare; |
---|
[07d6fd5] | 1511 | lfn_start.cln = FAT_FILE_SHORT_NAME; |
---|
| 1512 | } |
---|
[d2e0bb3] | 1513 | } else { |
---|
| 1514 | lfn_start.cln = FAT_FILE_SHORT_NAME; |
---|
| 1515 | entry_matched = false; |
---|
[07d6fd5] | 1516 | } |
---|
| 1517 | } |
---|
| 1518 | else |
---|
| 1519 | { |
---|
[8f77f8f] | 1520 | #if MSDOS_FIND_PRINT |
---|
[d2e0bb3] | 1521 | printf ("MSFS:[9.1] SFN entry, entry_matched:%i\n", entry_matched); |
---|
[8f77f8f] | 1522 | #endif |
---|
[07d6fd5] | 1523 | /* |
---|
| 1524 | * SFN entry found. |
---|
| 1525 | * |
---|
| 1526 | * If a LFN has been found and it matched check the |
---|
| 1527 | * entries have all been found and the checksum is |
---|
| 1528 | * correct. If this is the case return the short file |
---|
| 1529 | * name entry. |
---|
| 1530 | */ |
---|
[d2e0bb3] | 1531 | if (entry_matched) |
---|
[07d6fd5] | 1532 | { |
---|
| 1533 | uint8_t cs = 0; |
---|
| 1534 | uint8_t* p = (uint8_t*) MSDOS_DIR_NAME(entry); |
---|
| 1535 | int i; |
---|
| 1536 | |
---|
| 1537 | for (i = 0; i < MSDOS_SHORT_NAME_LEN; i++, p++) |
---|
| 1538 | cs = ((cs & 1) ? 0x80 : 0) + (cs >> 1) + *p; |
---|
| 1539 | |
---|
| 1540 | if (lfn_entry || (lfn_checksum != cs)) |
---|
[d2e0bb3] | 1541 | entry_matched = false; |
---|
| 1542 | else { |
---|
| 1543 | filename_matched = true; |
---|
| 1544 | rc = msdos_on_entry_found ( |
---|
| 1545 | fs_info, |
---|
| 1546 | fat_fd, |
---|
| 1547 | bts2rd, |
---|
| 1548 | name_dir_entry, |
---|
| 1549 | entry, |
---|
| 1550 | dir_pos, |
---|
| 1551 | dir_offset, |
---|
| 1552 | dir_entry, |
---|
| 1553 | &lfn_start |
---|
| 1554 | ); |
---|
| 1555 | } |
---|
[07d6fd5] | 1556 | |
---|
[8f77f8f] | 1557 | #if MSDOS_FIND_PRINT |
---|
[d2e0bb3] | 1558 | printf ("MSFS:[9.2] checksum, entry_matched:%i, lfn_entry:%i, lfn_checksum:%02x/%02x\n", |
---|
| 1559 | entry_matched, lfn_entry, lfn_checksum, cs); |
---|
[8f77f8f] | 1560 | #endif |
---|
[d2e0bb3] | 1561 | } else { |
---|
| 1562 | bytes_in_entry = MSDOS_SHORT_NAME_LEN + 1; |
---|
| 1563 | bytes_in_entry = msdos_short_entry_to_utf8_name ( |
---|
| 1564 | converter, |
---|
| 1565 | MSDOS_DIR_NAME (entry), |
---|
| 1566 | &entry_utf8_normalized[0], |
---|
| 1567 | bytes_in_entry); |
---|
| 1568 | if (bytes_in_entry > 0) { |
---|
| 1569 | filename_size_remaining = msdos_compare_entry_against_filename ( |
---|
| 1570 | converter, |
---|
| 1571 | &entry_utf8_normalized[0], |
---|
| 1572 | bytes_in_entry, |
---|
| 1573 | &filename_converted[0], |
---|
| 1574 | name_len_for_compare, |
---|
| 1575 | &entry_matched); |
---|
| 1576 | if (entry_matched && filename_size_remaining == 0) { |
---|
| 1577 | filename_matched = true; |
---|
| 1578 | rc = msdos_on_entry_found ( |
---|
| 1579 | fs_info, |
---|
| 1580 | fat_fd, |
---|
| 1581 | bts2rd, |
---|
| 1582 | name_dir_entry, |
---|
| 1583 | entry, |
---|
| 1584 | dir_pos, |
---|
| 1585 | dir_offset, |
---|
| 1586 | dir_entry, |
---|
| 1587 | &lfn_start |
---|
| 1588 | ); |
---|
| 1589 | } |
---|
| 1590 | if (rc == RC_OK && (! filename_matched)) { |
---|
| 1591 | lfn_start.cln = FAT_FILE_SHORT_NAME; |
---|
| 1592 | entry_matched = false; |
---|
| 1593 | filename_size_remaining = name_len_for_compare; |
---|
| 1594 | } |
---|
| 1595 | } else { |
---|
| 1596 | lfn_start.cln = FAT_FILE_SHORT_NAME; |
---|
| 1597 | entry_matched = false; |
---|
| 1598 | filename_size_remaining = name_len_for_compare; |
---|
[07d6fd5] | 1599 | } |
---|
[f36a7bfc] | 1600 | } |
---|
[a5305f6b] | 1601 | } |
---|
| 1602 | } |
---|
| 1603 | } |
---|
[07d6fd5] | 1604 | |
---|
[d2e0bb3] | 1605 | if (filename_matched || remainder_empty) |
---|
[07d6fd5] | 1606 | break; |
---|
| 1607 | |
---|
[d2e0bb3] | 1608 | (*dir_offset)++; |
---|
[a5305f6b] | 1609 | } |
---|
[d2e0bb3] | 1610 | if ( ! filename_matched ) { |
---|
| 1611 | /* |
---|
| 1612 | * If we are not to create the entry return a not found error. |
---|
| 1613 | */ |
---|
| 1614 | if (!create_node) |
---|
| 1615 | rc = MSDOS_NAME_NOT_FOUND_ERR; |
---|
[07d6fd5] | 1616 | |
---|
| 1617 | #if MSDOS_FIND_PRINT |
---|
[d2e0bb3] | 1618 | printf ( "MSFS:[8.1] WRITE do:%"PRIu32" esc:%"PRIu32" eso:%"PRIu32" ese:%"PRIu32"\n", |
---|
| 1619 | *dir_offset, *empty_space_count, *empty_space_offset, *empty_space_entry ); |
---|
[07d6fd5] | 1620 | #endif |
---|
[d2e0bb3] | 1621 | } |
---|
| 1622 | |
---|
| 1623 | return rc; |
---|
| 1624 | } |
---|
| 1625 | |
---|
| 1626 | static int |
---|
| 1627 | msdos_add_file ( |
---|
| 1628 | const char *name_converted, |
---|
| 1629 | const msdos_name_type_t name_type, |
---|
| 1630 | msdos_fs_info_t *fs_info, |
---|
| 1631 | fat_file_fd_t *fat_fd, |
---|
| 1632 | const uint32_t bts2rd, |
---|
| 1633 | const unsigned int fat_entries, |
---|
| 1634 | const char *name_dir_entry, |
---|
| 1635 | fat_dir_pos_t *dir_pos, |
---|
| 1636 | const uint32_t dir_offset, |
---|
| 1637 | const uint32_t empty_space_offset_param, |
---|
| 1638 | const uint32_t empty_space_entry_param, |
---|
| 1639 | const uint32_t empty_space_count |
---|
| 1640 | |
---|
| 1641 | ) |
---|
| 1642 | { |
---|
| 1643 | int ret = 0; |
---|
| 1644 | ssize_t bytes_written = 0; |
---|
| 1645 | uint8_t lfn_checksum = 0; |
---|
| 1646 | uint32_t empty_space_offset = empty_space_offset_param; |
---|
| 1647 | uint32_t empty_space_entry = empty_space_entry_param; |
---|
| 1648 | bool read_cluster = false; |
---|
| 1649 | int lfn_entry = 0; |
---|
| 1650 | fat_pos_t lfn_start; |
---|
| 1651 | uint32_t dir_entry; |
---|
[07d6fd5] | 1652 | |
---|
| 1653 | /* |
---|
| 1654 | * If a long file name calculate the checksum of the short file name |
---|
| 1655 | * data to place in each long file name entry. First set the short |
---|
| 1656 | * file name to the slot of the SFN entry. This will mean no clashes |
---|
| 1657 | * in this directory. |
---|
| 1658 | */ |
---|
| 1659 | if (name_type == MSDOS_NAME_LONG) |
---|
| 1660 | { |
---|
| 1661 | int slot = (((empty_space_offset * bts2rd) + empty_space_entry) / |
---|
[d2e0bb3] | 1662 | MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE) + fat_entries + 1; |
---|
[07d6fd5] | 1663 | msdos_short_name_hex(MSDOS_DIR_NAME(name_dir_entry), slot); |
---|
[8f77f8f] | 1664 | } |
---|
[07d6fd5] | 1665 | |
---|
[d2e0bb3] | 1666 | if (fat_entries) |
---|
[8f77f8f] | 1667 | { |
---|
| 1668 | uint8_t* p = (uint8_t*) MSDOS_DIR_NAME(name_dir_entry); |
---|
| 1669 | int i; |
---|
[07d6fd5] | 1670 | for (i = 0; i < 11; i++, p++) |
---|
| 1671 | lfn_checksum = |
---|
| 1672 | ((lfn_checksum & 1) ? 0x80 : 0) + (lfn_checksum >> 1) + *p; |
---|
| 1673 | } |
---|
| 1674 | |
---|
| 1675 | /* |
---|
| 1676 | * If there is no space available then extend the file. The |
---|
| 1677 | * empty_space_count is a count of empty entries in the currently |
---|
| 1678 | * read cluster so if 0 there is no space. Note, dir_offset will |
---|
| 1679 | * be at the next cluster so we can just make empty_space_offset |
---|
| 1680 | * that value. |
---|
| 1681 | */ |
---|
| 1682 | if (empty_space_count == 0) |
---|
| 1683 | { |
---|
| 1684 | read_cluster = true; |
---|
| 1685 | empty_space_offset = dir_offset; |
---|
| 1686 | empty_space_entry = 0; |
---|
| 1687 | } |
---|
| 1688 | |
---|
| 1689 | /* |
---|
| 1690 | * Have we read past the empty block ? If so go back and read it again. |
---|
| 1691 | */ |
---|
| 1692 | if (dir_offset != empty_space_offset) |
---|
| 1693 | read_cluster = true; |
---|
| 1694 | |
---|
| 1695 | /* |
---|
| 1696 | * Handle the entry writes. |
---|
| 1697 | */ |
---|
| 1698 | lfn_start.cln = lfn_start.ofs = FAT_FILE_SHORT_NAME; |
---|
| 1699 | lfn_entry = 0; |
---|
| 1700 | |
---|
| 1701 | #if MSDOS_FIND_PRINT |
---|
| 1702 | printf ("MSFS:[9] read_cluster:%d eso:%ld ese:%ld\n", |
---|
| 1703 | read_cluster, empty_space_offset, empty_space_entry); |
---|
| 1704 | #endif |
---|
| 1705 | |
---|
| 1706 | /* |
---|
| 1707 | * The one more is the short entry. |
---|
| 1708 | */ |
---|
[d2e0bb3] | 1709 | while (lfn_entry < (fat_entries + 1)) |
---|
[07d6fd5] | 1710 | { |
---|
| 1711 | int length = 0; |
---|
[0a7278e] | 1712 | |
---|
[07d6fd5] | 1713 | if (read_cluster) |
---|
| 1714 | { |
---|
[d2e0bb3] | 1715 | uint32_t new_length; |
---|
[07d6fd5] | 1716 | #if MSDOS_FIND_PRINT |
---|
[d2e0bb3] | 1717 | printf ("MSFS:[9.1] eso:%li\n", empty_space_offset); |
---|
[07d6fd5] | 1718 | #endif |
---|
[d2e0bb3] | 1719 | ret = fat_file_read(&fs_info->fat, fat_fd, |
---|
| 1720 | (empty_space_offset * bts2rd), bts2rd, |
---|
| 1721 | fs_info->cl_buf); |
---|
[07d6fd5] | 1722 | |
---|
[d2e0bb3] | 1723 | if (ret != bts2rd) |
---|
| 1724 | { |
---|
| 1725 | if (ret != FAT_EOF) |
---|
| 1726 | rtems_set_errno_and_return_minus_one(EIO); |
---|
[07d6fd5] | 1727 | |
---|
| 1728 | #if MSDOS_FIND_PRINT |
---|
[d2e0bb3] | 1729 | printf ("MSFS:[9.2] extending file:%li\n", empty_space_offset); |
---|
[07d6fd5] | 1730 | #endif |
---|
[d2e0bb3] | 1731 | ret = fat_file_extend (&fs_info->fat, fat_fd, false, |
---|
| 1732 | empty_space_offset * bts2rd, &new_length); |
---|
[0a7278e] | 1733 | |
---|
[d2e0bb3] | 1734 | if (ret != RC_OK) |
---|
| 1735 | return ret; |
---|
[07d6fd5] | 1736 | |
---|
| 1737 | #if MSDOS_FIND_PRINT |
---|
[d2e0bb3] | 1738 | printf ("MSFS:[9.3] extended: %"PRIu32" <-> %"PRIu32"\n", new_length, empty_space_offset * bts2rd); |
---|
[07d6fd5] | 1739 | #endif |
---|
[d2e0bb3] | 1740 | if (new_length != (empty_space_offset * bts2rd)) |
---|
| 1741 | rtems_set_errno_and_return_minus_one(EIO); |
---|
[07d6fd5] | 1742 | |
---|
[d2e0bb3] | 1743 | memset(fs_info->cl_buf, 0, bts2rd); |
---|
[07d6fd5] | 1744 | |
---|
[d2e0bb3] | 1745 | bytes_written = fat_file_write(&fs_info->fat, fat_fd, |
---|
| 1746 | empty_space_offset * bts2rd, |
---|
| 1747 | bts2rd, fs_info->cl_buf); |
---|
[07d6fd5] | 1748 | #if MSDOS_FIND_PRINT |
---|
[d2e0bb3] | 1749 | printf ("MSFS:[9.4] clear write: %d\n", ret); |
---|
[07d6fd5] | 1750 | #endif |
---|
[d2e0bb3] | 1751 | if (bytes_written == -1) |
---|
| 1752 | return -1; |
---|
| 1753 | else if (bytes_written != bts2rd) |
---|
| 1754 | rtems_set_errno_and_return_minus_one(EIO); |
---|
| 1755 | } |
---|
[07d6fd5] | 1756 | } |
---|
[0a7278e] | 1757 | |
---|
[07d6fd5] | 1758 | #if MSDOS_FIND_PRINT |
---|
| 1759 | printf ("MSFS:[10] eso:%li\n", empty_space_offset); |
---|
| 1760 | #endif |
---|
[0a7278e] | 1761 | |
---|
[07d6fd5] | 1762 | for (dir_entry = empty_space_entry; |
---|
| 1763 | dir_entry < bts2rd; |
---|
| 1764 | dir_entry += MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE) |
---|
| 1765 | { |
---|
| 1766 | char* entry = (char*) fs_info->cl_buf + dir_entry; |
---|
| 1767 | char* p; |
---|
| 1768 | const char* n; |
---|
| 1769 | int i; |
---|
[3ae5950] | 1770 | char fill = 0; |
---|
[07d6fd5] | 1771 | |
---|
| 1772 | length += MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE; |
---|
| 1773 | lfn_entry++; |
---|
[0a7278e] | 1774 | |
---|
[07d6fd5] | 1775 | #if MSDOS_FIND_PRINT |
---|
| 1776 | printf ("MSFS:[10] de:%li(%li) length:%i lfn_entry:%i\n", |
---|
| 1777 | dir_entry, (dir_entry / MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE), |
---|
| 1778 | length, lfn_entry); |
---|
| 1779 | #endif |
---|
| 1780 | /* |
---|
| 1781 | * Time to write the short file name entry. |
---|
| 1782 | */ |
---|
[d2e0bb3] | 1783 | if (lfn_entry == (fat_entries + 1)) |
---|
[07d6fd5] | 1784 | { |
---|
| 1785 | /* get current cluster number */ |
---|
[d2e0bb3] | 1786 | ret = fat_file_ioctl(&fs_info->fat, fat_fd, F_CLU_NUM, |
---|
| 1787 | empty_space_offset * bts2rd, |
---|
| 1788 | &dir_pos->sname.cln); |
---|
| 1789 | if (ret != RC_OK) |
---|
| 1790 | return ret; |
---|
[07d6fd5] | 1791 | |
---|
| 1792 | dir_pos->sname.ofs = dir_entry; |
---|
| 1793 | |
---|
| 1794 | if (lfn_start.cln != FAT_FILE_SHORT_NAME) |
---|
| 1795 | { |
---|
[d2e0bb3] | 1796 | ret = fat_file_ioctl(&fs_info->fat, fat_fd, F_CLU_NUM, |
---|
| 1797 | lfn_start.cln * bts2rd, |
---|
| 1798 | &lfn_start.cln); |
---|
| 1799 | if (ret != RC_OK) |
---|
| 1800 | return ret; |
---|
[07d6fd5] | 1801 | } |
---|
[0a7278e] | 1802 | |
---|
[07d6fd5] | 1803 | dir_pos->lname.cln = lfn_start.cln; |
---|
| 1804 | dir_pos->lname.ofs = lfn_start.ofs; |
---|
| 1805 | |
---|
| 1806 | /* write new node entry */ |
---|
| 1807 | memcpy (entry, (uint8_t *) name_dir_entry, |
---|
| 1808 | MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE); |
---|
| 1809 | break; |
---|
| 1810 | } |
---|
| 1811 | |
---|
| 1812 | /* |
---|
| 1813 | * This is a long file name and we need to write |
---|
| 1814 | * a long file name entry. See if this is the |
---|
| 1815 | * first entry written and if so remember the |
---|
| 1816 | * the location of the long file name. |
---|
| 1817 | */ |
---|
| 1818 | if (lfn_start.cln == FAT_FILE_SHORT_NAME) |
---|
| 1819 | { |
---|
[d2e0bb3] | 1820 | lfn_start.cln = empty_space_offset; |
---|
| 1821 | lfn_start.ofs = dir_entry; |
---|
[07d6fd5] | 1822 | } |
---|
[0a7278e] | 1823 | |
---|
[07d6fd5] | 1824 | /* |
---|
| 1825 | * Clear the entry before loading the data. |
---|
| 1826 | */ |
---|
| 1827 | memset (entry, 0, MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE); |
---|
| 1828 | |
---|
| 1829 | *MSDOS_DIR_LFN_CHECKSUM(entry) = lfn_checksum; |
---|
[0a7278e] | 1830 | |
---|
[07d6fd5] | 1831 | p = entry + 1; |
---|
[83a4cbb] | 1832 | n = name_converted + (fat_entries - lfn_entry) * MSDOS_LFN_ENTRY_SIZE; |
---|
[0a7278e] | 1833 | |
---|
[d2e0bb3] | 1834 | #if MSDOS_FIND_PRINT |
---|
| 1835 | printf ("MSFS:[11] "); |
---|
| 1836 | #endif |
---|
| 1837 | for (i = 0; i < MSDOS_LFN_LEN_PER_ENTRY; ++i) |
---|
[07d6fd5] | 1838 | { |
---|
[d2e0bb3] | 1839 | if (!(*n == 0 && *(n+1) == 0)) |
---|
[3ae5950] | 1840 | { |
---|
| 1841 | *p = *n; |
---|
[d2e0bb3] | 1842 | *(p+1) = *(n+1); |
---|
[3ae5950] | 1843 | } |
---|
| 1844 | else |
---|
| 1845 | { |
---|
| 1846 | p [0] = fill; |
---|
| 1847 | p [1] = fill; |
---|
| 1848 | fill = 0xff; |
---|
| 1849 | } |
---|
[d2e0bb3] | 1850 | n += MSDOS_NAME_LFN_BYTES_PER_CHAR; |
---|
| 1851 | #if MSDOS_FIND_PRINT |
---|
| 1852 | printf ( "'%c''%c'", *p, *(p+1) ); |
---|
| 1853 | #endif |
---|
[0a7278e] | 1854 | |
---|
[07d6fd5] | 1855 | switch (i) |
---|
| 1856 | { |
---|
| 1857 | case 4: |
---|
| 1858 | p += 5; |
---|
| 1859 | break; |
---|
| 1860 | case 10: |
---|
| 1861 | p += 4; |
---|
| 1862 | break; |
---|
| 1863 | default: |
---|
| 1864 | p += 2; |
---|
| 1865 | break; |
---|
| 1866 | } |
---|
| 1867 | } |
---|
[d2e0bb3] | 1868 | #if MSDOS_FIND_PRINT |
---|
| 1869 | printf ( "\n" ); |
---|
| 1870 | #endif |
---|
| 1871 | *MSDOS_DIR_ENTRY_TYPE(entry) = (fat_entries - lfn_entry) + 1; |
---|
[07d6fd5] | 1872 | if (lfn_entry == 1) |
---|
| 1873 | *MSDOS_DIR_ENTRY_TYPE(entry) |= MSDOS_LAST_LONG_ENTRY; |
---|
| 1874 | *MSDOS_DIR_ATTR(entry) |= MSDOS_ATTR_LFN; |
---|
| 1875 | } |
---|
| 1876 | |
---|
[d2e0bb3] | 1877 | bytes_written = fat_file_write(&fs_info->fat, fat_fd, |
---|
| 1878 | (empty_space_offset * bts2rd) + empty_space_entry, |
---|
| 1879 | length, fs_info->cl_buf + empty_space_entry); |
---|
| 1880 | if (bytes_written == -1) |
---|
| 1881 | return -1; |
---|
| 1882 | else if (bytes_written != length) |
---|
[07d6fd5] | 1883 | rtems_set_errno_and_return_minus_one(EIO); |
---|
| 1884 | |
---|
| 1885 | empty_space_offset++; |
---|
| 1886 | empty_space_entry = 0; |
---|
| 1887 | read_cluster = true; |
---|
| 1888 | } |
---|
[d2e0bb3] | 1889 | return ret; |
---|
| 1890 | } |
---|
| 1891 | |
---|
| 1892 | int |
---|
| 1893 | msdos_find_name_in_fat_file ( |
---|
| 1894 | rtems_filesystem_mount_table_entry_t *mt_entry, |
---|
| 1895 | fat_file_fd_t *fat_fd, |
---|
| 1896 | bool create_node, |
---|
| 1897 | const uint8_t *name_utf8, |
---|
| 1898 | int name_utf8_len, |
---|
| 1899 | msdos_name_type_t name_type, |
---|
| 1900 | fat_dir_pos_t *dir_pos, |
---|
| 1901 | char *name_dir_entry) |
---|
| 1902 | { |
---|
| 1903 | int retval = 0; |
---|
| 1904 | msdos_fs_info_t *fs_info = mt_entry->fs_info; |
---|
| 1905 | ssize_t name_len_for_save; |
---|
| 1906 | ssize_t name_len_for_compare; |
---|
| 1907 | uint32_t bts2rd = 0; |
---|
| 1908 | uint32_t empty_space_offset = 0; |
---|
| 1909 | uint32_t empty_space_entry = 0; |
---|
| 1910 | uint32_t empty_space_count = 0; |
---|
| 1911 | uint32_t dir_offset = 0; |
---|
| 1912 | unsigned int fat_entries; |
---|
| 1913 | rtems_dosfs_convert_control *converter = fs_info->converter; |
---|
| 1914 | void *buffer = converter->buffer.data; |
---|
| 1915 | size_t buffer_size = converter->buffer.size; |
---|
| 1916 | |
---|
| 1917 | assert(name_utf8_len > 0); |
---|
| 1918 | |
---|
| 1919 | fat_dir_pos_init(dir_pos); |
---|
| 1920 | |
---|
| 1921 | |
---|
[0a7278e] | 1922 | |
---|
[d2e0bb3] | 1923 | if (FAT_FD_OF_ROOT_DIR(fat_fd) && |
---|
| 1924 | (fs_info->fat.vol.type & (FAT_FAT12 | FAT_FAT16))) |
---|
| 1925 | bts2rd = fat_fd->fat_file_size; |
---|
| 1926 | else |
---|
| 1927 | bts2rd = fs_info->fat.vol.bpc; |
---|
| 1928 | |
---|
| 1929 | switch ( name_type ) { |
---|
| 1930 | case MSDOS_NAME_SHORT: |
---|
| 1931 | name_len_for_compare = msdos_filename_utf8_to_short_name_for_compare ( |
---|
| 1932 | converter, |
---|
| 1933 | name_utf8, |
---|
| 1934 | name_utf8_len, |
---|
| 1935 | buffer, |
---|
| 1936 | MSDOS_SHORT_NAME_LEN); |
---|
| 1937 | if (name_len_for_compare > 0) { |
---|
| 1938 | fat_entries = 0; |
---|
| 1939 | } |
---|
| 1940 | else |
---|
| 1941 | retval = -1; |
---|
| 1942 | break; |
---|
| 1943 | case MSDOS_NAME_LONG: |
---|
| 1944 | name_len_for_save = msdos_filename_utf8_to_long_name_for_save ( |
---|
| 1945 | converter, |
---|
| 1946 | name_utf8, |
---|
| 1947 | name_utf8_len, |
---|
| 1948 | buffer, |
---|
| 1949 | buffer_size); |
---|
| 1950 | if (name_len_for_save > 0) { |
---|
[83a4cbb] | 1951 | fat_entries = (name_len_for_save + MSDOS_LFN_ENTRY_SIZE - 1) |
---|
| 1952 | / MSDOS_LFN_ENTRY_SIZE; |
---|
[d2e0bb3] | 1953 | name_len_for_compare = msdos_filename_utf8_to_long_name_for_compare ( |
---|
| 1954 | converter, |
---|
| 1955 | name_utf8, |
---|
| 1956 | name_utf8_len, |
---|
| 1957 | buffer, |
---|
| 1958 | buffer_size); |
---|
| 1959 | if (0 >= name_len_for_compare) { |
---|
| 1960 | retval = -1; |
---|
| 1961 | } |
---|
| 1962 | } |
---|
| 1963 | else |
---|
| 1964 | retval = -1; |
---|
| 1965 | break; |
---|
| 1966 | default: |
---|
| 1967 | errno = EINVAL; |
---|
| 1968 | retval = -1; |
---|
| 1969 | break; |
---|
| 1970 | } |
---|
| 1971 | if (retval == RC_OK) { |
---|
| 1972 | /* See if the file/directory does already exist */ |
---|
| 1973 | retval = msdos_find_file_in_directory ( |
---|
| 1974 | buffer, |
---|
| 1975 | name_len_for_compare, |
---|
| 1976 | name_len_for_save, |
---|
| 1977 | name_type, |
---|
| 1978 | fs_info, |
---|
| 1979 | fat_fd, |
---|
| 1980 | bts2rd, |
---|
| 1981 | create_node, |
---|
| 1982 | fat_entries, |
---|
| 1983 | name_dir_entry, |
---|
| 1984 | dir_pos, |
---|
| 1985 | &dir_offset, |
---|
| 1986 | &empty_space_offset, |
---|
| 1987 | &empty_space_entry, |
---|
| 1988 | &empty_space_count); |
---|
| 1989 | } |
---|
| 1990 | /* Create a non-existing file/directory if requested */ |
---|
| 1991 | if ( retval == RC_OK |
---|
| 1992 | && create_node) { |
---|
| 1993 | switch (name_type) { |
---|
| 1994 | case MSDOS_NAME_SHORT: |
---|
| 1995 | name_len_for_save = msdos_filename_utf8_to_short_name_for_save ( |
---|
| 1996 | converter, |
---|
| 1997 | name_utf8, |
---|
| 1998 | name_utf8_len, |
---|
| 1999 | buffer, |
---|
| 2000 | MSDOS_SHORT_NAME_LEN); |
---|
| 2001 | if (name_len_for_save > 0 ) { |
---|
| 2002 | fat_entries = 0; |
---|
| 2003 | } |
---|
| 2004 | else |
---|
| 2005 | retval = -1; |
---|
| 2006 | break; |
---|
| 2007 | case MSDOS_NAME_LONG: |
---|
| 2008 | name_len_for_save = msdos_filename_utf8_to_long_name_for_save ( |
---|
| 2009 | converter, |
---|
| 2010 | name_utf8, |
---|
| 2011 | name_utf8_len, |
---|
| 2012 | buffer, |
---|
| 2013 | buffer_size); |
---|
| 2014 | if (name_len_for_save > 0) { |
---|
[83a4cbb] | 2015 | fat_entries = (name_len_for_save + MSDOS_LFN_ENTRY_SIZE - 1) |
---|
| 2016 | / MSDOS_LFN_ENTRY_SIZE; |
---|
[d2e0bb3] | 2017 | } |
---|
| 2018 | else |
---|
| 2019 | retval = -1; |
---|
| 2020 | break; |
---|
| 2021 | default: |
---|
| 2022 | errno = EINVAL; |
---|
| 2023 | retval = -1; |
---|
| 2024 | break; |
---|
| 2025 | } |
---|
[4c9f02d] | 2026 | |
---|
| 2027 | if (retval == RC_OK) |
---|
| 2028 | retval = msdos_add_file ( |
---|
| 2029 | buffer, |
---|
| 2030 | name_type, |
---|
| 2031 | fs_info, |
---|
| 2032 | fat_fd, |
---|
| 2033 | bts2rd, |
---|
| 2034 | fat_entries, |
---|
| 2035 | name_dir_entry, |
---|
| 2036 | dir_pos, |
---|
| 2037 | dir_offset, |
---|
| 2038 | empty_space_offset, |
---|
| 2039 | empty_space_entry, |
---|
| 2040 | empty_space_count |
---|
| 2041 | ); |
---|
[d2e0bb3] | 2042 | } |
---|
| 2043 | |
---|
| 2044 | return retval; |
---|
[f36a7bfc] | 2045 | } |
---|
| 2046 | |
---|
[d2e0bb3] | 2047 | |
---|
[f36a7bfc] | 2048 | /* msdos_find_node_by_cluster_num_in_fat_file -- |
---|
| 2049 | * Find node with specified number of cluster in fat-file. |
---|
| 2050 | * |
---|
[07d6fd5] | 2051 | * Note, not updated in the LFN change because it is only used |
---|
| 2052 | * for . and .. entries and these are always short. |
---|
| 2053 | * |
---|
[f36a7bfc] | 2054 | * PARAMETERS: |
---|
[a5305f6b] | 2055 | * mt_entry - mount table entry |
---|
| 2056 | * fat_fd - fat-file descriptor |
---|
| 2057 | * cl4find - number of cluster to find |
---|
[f36a7bfc] | 2058 | * paux - identify a node location on the disk - |
---|
| 2059 | * cluster num and offset inside the cluster |
---|
| 2060 | * dir_entry - placeholder for found node |
---|
| 2061 | * |
---|
| 2062 | * RETURNS: |
---|
| 2063 | * RC_OK on success, or error code if error occured |
---|
| 2064 | * |
---|
| 2065 | */ |
---|
[e197621] | 2066 | int msdos_find_node_by_cluster_num_in_fat_file( |
---|
[f36a7bfc] | 2067 | rtems_filesystem_mount_table_entry_t *mt_entry, |
---|
| 2068 | fat_file_fd_t *fat_fd, |
---|
[a5305f6b] | 2069 | uint32_t cl4find, |
---|
[07d6fd5] | 2070 | fat_dir_pos_t *dir_pos, |
---|
[f36a7bfc] | 2071 | char *dir_entry |
---|
| 2072 | ) |
---|
| 2073 | { |
---|
[e197621] | 2074 | int rc = RC_OK; |
---|
[f36a7bfc] | 2075 | ssize_t ret = 0; |
---|
| 2076 | msdos_fs_info_t *fs_info = mt_entry->fs_info; |
---|
[a5305f6b] | 2077 | uint32_t bts2rd = 0; |
---|
[f91bbe64] | 2078 | uint32_t i = 0, j = 0; |
---|
[f36a7bfc] | 2079 | |
---|
[a5305f6b] | 2080 | if (FAT_FD_OF_ROOT_DIR(fat_fd) && |
---|
[f36a7bfc] | 2081 | (fs_info->fat.vol.type & (FAT_FAT12 | FAT_FAT16))) |
---|
[a5305f6b] | 2082 | bts2rd = fat_fd->fat_file_size; |
---|
[f36a7bfc] | 2083 | else |
---|
| 2084 | bts2rd = fs_info->fat.vol.bpc; |
---|
| 2085 | |
---|
[c65afce4] | 2086 | while ((ret = fat_file_read(&fs_info->fat, fat_fd, j * bts2rd, bts2rd, |
---|
[f36a7bfc] | 2087 | fs_info->cl_buf)) != FAT_EOF) |
---|
| 2088 | { |
---|
| 2089 | if ( ret < MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE ) |
---|
[3899a537] | 2090 | rtems_set_errno_and_return_minus_one( EIO ); |
---|
[f36a7bfc] | 2091 | |
---|
| 2092 | assert(ret == bts2rd); |
---|
[a5305f6b] | 2093 | |
---|
[f36a7bfc] | 2094 | for (i = 0; i < bts2rd; i += MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE) |
---|
| 2095 | { |
---|
[07d6fd5] | 2096 | char* entry = (char*) fs_info->cl_buf + i; |
---|
[0a7278e] | 2097 | |
---|
[f36a7bfc] | 2098 | /* if this and all rest entries are empty - return not-found */ |
---|
[07d6fd5] | 2099 | if ((*MSDOS_DIR_ENTRY_TYPE(entry)) == |
---|
[f36a7bfc] | 2100 | MSDOS_THIS_DIR_ENTRY_AND_REST_EMPTY) |
---|
| 2101 | return MSDOS_NAME_NOT_FOUND_ERR; |
---|
| 2102 | |
---|
[a5305f6b] | 2103 | /* if this entry is empty - skip it */ |
---|
[07d6fd5] | 2104 | if ((*MSDOS_DIR_ENTRY_TYPE(entry)) == |
---|
[f36a7bfc] | 2105 | MSDOS_THIS_DIR_ENTRY_EMPTY) |
---|
| 2106 | continue; |
---|
| 2107 | |
---|
| 2108 | /* if get a non-empty entry - compare clusters num */ |
---|
[07d6fd5] | 2109 | if (MSDOS_EXTRACT_CLUSTER_NUM(entry) == cl4find) |
---|
[f36a7bfc] | 2110 | { |
---|
| 2111 | /* on success fill aux structure and copy all 32 bytes */ |
---|
[c65afce4] | 2112 | rc = fat_file_ioctl(&fs_info->fat, fat_fd, F_CLU_NUM, j * bts2rd, |
---|
[07d6fd5] | 2113 | &dir_pos->sname.cln); |
---|
[f36a7bfc] | 2114 | if (rc != RC_OK) |
---|
| 2115 | return rc; |
---|
| 2116 | |
---|
[07d6fd5] | 2117 | dir_pos->sname.ofs = i; |
---|
| 2118 | dir_pos->lname.cln = FAT_FILE_SHORT_NAME; |
---|
| 2119 | dir_pos->lname.ofs = FAT_FILE_SHORT_NAME; |
---|
[0a7278e] | 2120 | |
---|
[07d6fd5] | 2121 | memcpy(dir_entry, entry, |
---|
[f36a7bfc] | 2122 | MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE); |
---|
| 2123 | return RC_OK; |
---|
| 2124 | } |
---|
[a5305f6b] | 2125 | } |
---|
[f36a7bfc] | 2126 | j++; |
---|
[a5305f6b] | 2127 | } |
---|
[f36a7bfc] | 2128 | return MSDOS_NAME_NOT_FOUND_ERR; |
---|
| 2129 | } |
---|
[86ef0df] | 2130 | |
---|
| 2131 | int |
---|
| 2132 | msdos_sync(rtems_libio_t *iop) |
---|
| 2133 | { |
---|
| 2134 | int rc = RC_OK; |
---|
| 2135 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
| 2136 | msdos_fs_info_t *fs_info = iop->pathinfo.mt_entry->fs_info; |
---|
| 2137 | |
---|
| 2138 | sc = rtems_semaphore_obtain(fs_info->vol_sema, RTEMS_WAIT, |
---|
| 2139 | MSDOS_VOLUME_SEMAPHORE_TIMEOUT); |
---|
| 2140 | if (sc != RTEMS_SUCCESSFUL) |
---|
| 2141 | rtems_set_errno_and_return_minus_one(EIO); |
---|
| 2142 | |
---|
[0f0db894] | 2143 | rc = fat_sync(&fs_info->fat); |
---|
[86ef0df] | 2144 | |
---|
| 2145 | rtems_semaphore_release(fs_info->vol_sema); |
---|
| 2146 | return rc; |
---|
| 2147 | } |
---|