[f36a7bfc] | 1 | /* |
---|
| 2 | * Miscellaneous routines implementation for MSDOS filesystem |
---|
| 3 | * |
---|
| 4 | * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia |
---|
| 5 | * Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru> |
---|
| 6 | * |
---|
| 7 | * The license and distribution terms for this file may be |
---|
| 8 | * found in the file LICENSE in this distribution or at |
---|
[cf0539b1] | 9 | * http://www.rtems.com/license/LICENSE. |
---|
[f36a7bfc] | 10 | * |
---|
| 11 | * @(#) $Id$ |
---|
| 12 | */ |
---|
| 13 | |
---|
| 14 | #if HAVE_CONFIG_H |
---|
| 15 | #include "config.h" |
---|
| 16 | #endif |
---|
| 17 | |
---|
| 18 | #include <stdlib.h> |
---|
| 19 | #include <sys/time.h> |
---|
| 20 | #include <unistd.h> |
---|
| 21 | #include <string.h> |
---|
| 22 | #include <assert.h> |
---|
| 23 | #include <rtems/libio_.h> |
---|
| 24 | |
---|
| 25 | #include "fat.h" |
---|
| 26 | #include "fat_fat_operations.h" |
---|
| 27 | #include "fat_file.h" |
---|
| 28 | |
---|
| 29 | #include "msdos.h" |
---|
| 30 | |
---|
| 31 | /* msdos_get_token -- |
---|
| 32 | * Routine to get a token (name or separator) from the path. |
---|
| 33 | * |
---|
| 34 | * PARAMETERS: |
---|
| 35 | * path - path to get token from |
---|
| 36 | * ret_token - returned token |
---|
| 37 | * token_len - length of returned token |
---|
| 38 | * |
---|
| 39 | * RETURNS: |
---|
[a5305f6b] | 40 | * token type, token and token length |
---|
[f36a7bfc] | 41 | * |
---|
| 42 | */ |
---|
[a5305f6b] | 43 | msdos_token_types_t |
---|
[f36a7bfc] | 44 | msdos_get_token(const char *path, char *ret_token, int *token_len) |
---|
| 45 | { |
---|
| 46 | int rc = RC_OK; |
---|
| 47 | register int i = 0; |
---|
| 48 | msdos_token_types_t type = MSDOS_NAME; |
---|
[a5305f6b] | 49 | char token[MSDOS_NAME_MAX_WITH_DOT+1]; |
---|
[f36a7bfc] | 50 | register char c; |
---|
| 51 | |
---|
[a5305f6b] | 52 | /* |
---|
[f36a7bfc] | 53 | * Copy a name into token. (Remember NULL is a token.) |
---|
| 54 | */ |
---|
| 55 | c = path[i]; |
---|
[a5305f6b] | 56 | while ( (!msdos_is_separator(c)) && (i <= MSDOS_NAME_MAX_WITH_DOT) ) |
---|
[f36a7bfc] | 57 | { |
---|
| 58 | token[i] = c; |
---|
| 59 | if ( i == MSDOS_NAME_MAX_WITH_DOT ) |
---|
| 60 | return MSDOS_INVALID_TOKEN; |
---|
| 61 | if ( !msdos_is_valid_name_char(c) ) |
---|
[a5305f6b] | 62 | return MSDOS_INVALID_TOKEN; |
---|
[f36a7bfc] | 63 | c = path [++i]; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | /* |
---|
| 67 | * Copy a seperator into token. |
---|
| 68 | */ |
---|
[a5305f6b] | 69 | if ( i == 0 ) |
---|
[f36a7bfc] | 70 | { |
---|
| 71 | token[i] = c; |
---|
[a5305f6b] | 72 | if ( token[i] != '\0' ) |
---|
[f36a7bfc] | 73 | { |
---|
| 74 | i++; |
---|
| 75 | type = MSDOS_CURRENT_DIR; |
---|
[a5305f6b] | 76 | } |
---|
| 77 | else |
---|
[f36a7bfc] | 78 | type = MSDOS_NO_MORE_PATH; |
---|
[a5305f6b] | 79 | } |
---|
| 80 | else if (token[ i-1 ] != '\0') |
---|
[f36a7bfc] | 81 | token[i] = '\0'; |
---|
| 82 | |
---|
| 83 | /* |
---|
| 84 | * Set token_len to the number of characters copied. |
---|
| 85 | */ |
---|
| 86 | *token_len = i; |
---|
| 87 | |
---|
| 88 | /* |
---|
| 89 | * If we copied something that was not a seperator see if |
---|
| 90 | * it was a special name. |
---|
| 91 | */ |
---|
[a5305f6b] | 92 | if ( type == MSDOS_NAME ) |
---|
[f36a7bfc] | 93 | { |
---|
| 94 | if ( strcmp( token, "..") == 0 ) |
---|
| 95 | { |
---|
| 96 | strcpy(ret_token, MSDOS_DOTDOT_NAME); |
---|
| 97 | type = MSDOS_UP_DIR; |
---|
| 98 | return type; |
---|
[a5305f6b] | 99 | } |
---|
[f36a7bfc] | 100 | |
---|
| 101 | if ( strcmp( token, "." ) == 0 ) |
---|
| 102 | { |
---|
| 103 | strcpy(ret_token, MSDOS_DOT_NAME); |
---|
| 104 | type = MSDOS_CURRENT_DIR; |
---|
[a5305f6b] | 105 | return type; |
---|
[f36a7bfc] | 106 | } |
---|
| 107 | |
---|
[c125425] | 108 | rc = msdos_filename_unix2dos(token, *token_len, ret_token); |
---|
[f36a7bfc] | 109 | if ( rc != RC_OK ) |
---|
[a5305f6b] | 110 | return MSDOS_INVALID_TOKEN; |
---|
[f36a7bfc] | 111 | } |
---|
| 112 | ret_token[MSDOS_NAME_MAX] = '\0'; |
---|
| 113 | return type; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | /* msdos_find_name -- |
---|
[a5305f6b] | 118 | * Find the node which correspondes to the name, open fat-file which |
---|
| 119 | * correspondes to the found node and close fat-file which correspondes |
---|
[f36a7bfc] | 120 | * to the node we searched in. |
---|
| 121 | * |
---|
| 122 | * PARAMETERS: |
---|
| 123 | * parent_loc - parent node description |
---|
| 124 | * name - name to find |
---|
| 125 | * |
---|
| 126 | * RETURNS: |
---|
[a5305f6b] | 127 | * RC_OK and updated 'parent_loc' on success, or -1 if error |
---|
[f36a7bfc] | 128 | * occured (errno set apropriately) |
---|
| 129 | * |
---|
| 130 | */ |
---|
[a5305f6b] | 131 | int |
---|
[f36a7bfc] | 132 | msdos_find_name( |
---|
| 133 | rtems_filesystem_location_info_t *parent_loc, |
---|
| 134 | char *name |
---|
| 135 | ) |
---|
| 136 | { |
---|
| 137 | int rc = RC_OK; |
---|
| 138 | msdos_fs_info_t *fs_info = parent_loc->mt_entry->fs_info; |
---|
| 139 | fat_file_fd_t *fat_fd = NULL; |
---|
| 140 | fat_auxiliary_t aux; |
---|
| 141 | unsigned short time_val = 0; |
---|
| 142 | unsigned short date = 0; |
---|
[adc829ed] | 143 | char node_entry[MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE]; |
---|
[a5305f6b] | 144 | |
---|
[f36a7bfc] | 145 | memset(node_entry, 0, MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE); |
---|
[a5305f6b] | 146 | |
---|
| 147 | /* |
---|
| 148 | * find the node which correspondes to the name in the directory pointed by |
---|
[f36a7bfc] | 149 | * 'parent_loc' |
---|
| 150 | */ |
---|
| 151 | rc = msdos_get_name_node(parent_loc, name, &aux, node_entry); |
---|
| 152 | if (rc != RC_OK) |
---|
| 153 | return rc; |
---|
| 154 | |
---|
| 155 | /* open fat-file corresponded to the found node */ |
---|
| 156 | rc = fat_file_open(parent_loc->mt_entry, aux.cln, aux.ofs, &fat_fd); |
---|
| 157 | if (rc != RC_OK) |
---|
| 158 | return rc; |
---|
[a5305f6b] | 159 | |
---|
[f36a7bfc] | 160 | /* |
---|
[a5305f6b] | 161 | * I don't like this if, but: we should do it , or should write new file |
---|
| 162 | * size and first cluster num to the disk after each write operation |
---|
| 163 | * (even if one byte is written - that is TOO non-optimize) because |
---|
| 164 | * otherwise real values of these fields stored in fat-file descriptor |
---|
[f36a7bfc] | 165 | * may be accidentely rewritten with wrong values stored on the disk |
---|
| 166 | */ |
---|
| 167 | if (fat_fd->links_num == 1) |
---|
| 168 | { |
---|
| 169 | fat_fd->info_cln = aux.cln; |
---|
| 170 | fat_fd->info_ofs = aux.ofs; |
---|
| 171 | fat_fd->cln = MSDOS_EXTRACT_CLUSTER_NUM(node_entry); |
---|
| 172 | fat_fd->first_char = *MSDOS_DIR_NAME(node_entry); |
---|
[a5305f6b] | 173 | |
---|
[f36a7bfc] | 174 | time_val = *MSDOS_DIR_WRITE_TIME(node_entry); |
---|
| 175 | date = *MSDOS_DIR_WRITE_DATE(node_entry); |
---|
[a5305f6b] | 176 | |
---|
[f36a7bfc] | 177 | fat_fd->mtime = msdos_date_dos2unix(CF_LE_W(time_val), CF_LE_W(date)); |
---|
[a5305f6b] | 178 | |
---|
[f36a7bfc] | 179 | if ((*MSDOS_DIR_ATTR(node_entry)) & MSDOS_ATTR_DIRECTORY) |
---|
| 180 | { |
---|
| 181 | fat_fd->fat_file_type = FAT_DIRECTORY; |
---|
[a5305f6b] | 182 | fat_fd->size_limit = MSDOS_MAX_DIR_LENGHT; |
---|
| 183 | |
---|
[f36a7bfc] | 184 | rc = fat_file_size(parent_loc->mt_entry, fat_fd); |
---|
| 185 | if (rc != RC_OK) |
---|
| 186 | { |
---|
| 187 | fat_file_close(parent_loc->mt_entry, fat_fd); |
---|
| 188 | return rc; |
---|
| 189 | } |
---|
| 190 | } |
---|
| 191 | else |
---|
| 192 | { |
---|
[a5305f6b] | 193 | fat_fd->fat_file_size = CF_LE_L(*MSDOS_DIR_FILE_SIZE(node_entry)); |
---|
| 194 | fat_fd->fat_file_type = FAT_FILE; |
---|
[f36a7bfc] | 195 | fat_fd->size_limit = MSDOS_MAX_FILE_SIZE; |
---|
| 196 | } |
---|
[a5305f6b] | 197 | |
---|
[f36a7bfc] | 198 | /* these data is not actual for zero-length fat-file */ |
---|
| 199 | fat_fd->map.file_cln = 0; |
---|
| 200 | fat_fd->map.disk_cln = fat_fd->cln; |
---|
[a5305f6b] | 201 | |
---|
| 202 | if ((fat_fd->fat_file_size != 0) && |
---|
[f36a7bfc] | 203 | (fat_fd->fat_file_size <= fs_info->fat.vol.bpc)) |
---|
| 204 | { |
---|
| 205 | fat_fd->map.last_cln = fat_fd->cln; |
---|
| 206 | } |
---|
| 207 | else |
---|
| 208 | { |
---|
| 209 | fat_fd->map.last_cln = FAT_UNDEFINED_VALUE; |
---|
| 210 | } |
---|
[a5305f6b] | 211 | } |
---|
[f36a7bfc] | 212 | |
---|
| 213 | /* close fat-file corresponded to the node we searched in */ |
---|
| 214 | rc = fat_file_close(parent_loc->mt_entry, parent_loc->node_access); |
---|
| 215 | if (rc != RC_OK) |
---|
| 216 | { |
---|
| 217 | fat_file_close(parent_loc->mt_entry, fat_fd); |
---|
| 218 | return rc; |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | /* update node_info_ptr field */ |
---|
| 222 | parent_loc->node_access = fat_fd; |
---|
[a5305f6b] | 223 | |
---|
[f36a7bfc] | 224 | return rc; |
---|
[a5305f6b] | 225 | } |
---|
[f36a7bfc] | 226 | |
---|
| 227 | /* msdos_get_name_node -- |
---|
| 228 | * This routine is used in two ways: for a new mode creation (a) or for |
---|
| 229 | * search the node which correspondes to the name parameter (b). |
---|
[a5305f6b] | 230 | * In case (a) 'name' should be set up to NULL and 'name_dir_entry' should |
---|
| 231 | * point to initialized 32 bytes structure described a new node. |
---|
[f36a7bfc] | 232 | * In case (b) 'name' should contain a valid string. |
---|
| 233 | * |
---|
[a5305f6b] | 234 | * (a): reading fat-file which correspondes to directory we are going to |
---|
| 235 | * create node in. If free slot is found write contents of |
---|
| 236 | * 'name_dir_entry' into it. If reach end of fat-file and no free |
---|
[f36a7bfc] | 237 | * slot found, write 32 bytes to the end of fat-file. |
---|
| 238 | * |
---|
[a5305f6b] | 239 | * (b): reading fat-file which correspondes to directory and trying to |
---|
[f36a7bfc] | 240 | * find slot with the name field == 'name' parameter |
---|
| 241 | * |
---|
| 242 | * |
---|
| 243 | * PARAMETERS: |
---|
| 244 | * parent_loc - node description to create node in or to find name in |
---|
| 245 | * name - NULL or name to find |
---|
| 246 | * paux - identify a node location on the disk - |
---|
[a5305f6b] | 247 | * cluster num and offset inside the cluster |
---|
[f36a7bfc] | 248 | * name_dir_entry - node to create/placeholder for found node (IN/OUT) |
---|
| 249 | * |
---|
| 250 | * RETURNS: |
---|
[a5305f6b] | 251 | * RC_OK, filled aux_struct_ptr and name_dir_entry on success, or -1 if |
---|
[f36a7bfc] | 252 | * error occured (errno set apropriately) |
---|
| 253 | * |
---|
| 254 | */ |
---|
| 255 | int |
---|
| 256 | msdos_get_name_node( |
---|
[a5305f6b] | 257 | rtems_filesystem_location_info_t *parent_loc, |
---|
| 258 | char *name, |
---|
[f36a7bfc] | 259 | fat_auxiliary_t *paux, |
---|
| 260 | char *name_dir_entry |
---|
| 261 | ) |
---|
| 262 | { |
---|
| 263 | int rc = RC_OK; |
---|
| 264 | ssize_t ret = 0; |
---|
| 265 | msdos_fs_info_t *fs_info = parent_loc->mt_entry->fs_info; |
---|
| 266 | fat_file_fd_t *fat_fd = parent_loc->node_access; |
---|
[f91bbe64] | 267 | uint32_t dotdot_cln = 0; |
---|
[f36a7bfc] | 268 | |
---|
| 269 | /* find name in fat-file which correspondes to the directory */ |
---|
| 270 | rc = msdos_find_name_in_fat_file(parent_loc->mt_entry, fat_fd, name, paux, |
---|
| 271 | name_dir_entry); |
---|
| 272 | if ((rc != RC_OK) && (rc != MSDOS_NAME_NOT_FOUND_ERR)) |
---|
| 273 | return rc; |
---|
[a5305f6b] | 274 | |
---|
[f36a7bfc] | 275 | /* if we search for valid name and name not found -> return */ |
---|
| 276 | if ((rc == MSDOS_NAME_NOT_FOUND_ERR) && (name != NULL)) |
---|
| 277 | return rc; |
---|
[a5305f6b] | 278 | |
---|
| 279 | /* |
---|
| 280 | * if we try to create new entry and the directory is not big enough |
---|
| 281 | * currently - try to enlarge directory |
---|
[f36a7bfc] | 282 | */ |
---|
| 283 | if ((rc == MSDOS_NAME_NOT_FOUND_ERR) && (name == NULL)) |
---|
| 284 | { |
---|
[a5305f6b] | 285 | ret = fat_file_write(parent_loc->mt_entry, fat_fd, |
---|
| 286 | fat_fd->fat_file_size, |
---|
| 287 | MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE, |
---|
[b17ff491] | 288 | (uint8_t *)name_dir_entry); |
---|
[f36a7bfc] | 289 | if (ret == -1) |
---|
[a5305f6b] | 290 | return -1; |
---|
[f36a7bfc] | 291 | |
---|
| 292 | /* on success directory is enlarged by a new cluster */ |
---|
| 293 | fat_fd->fat_file_size += fs_info->fat.vol.bpc; |
---|
[a5305f6b] | 294 | |
---|
[f36a7bfc] | 295 | /* get cluster num where a new node located */ |
---|
| 296 | rc = fat_file_ioctl(parent_loc->mt_entry, fat_fd, F_CLU_NUM, |
---|
| 297 | fat_fd->fat_file_size - 1, &paux->cln); |
---|
[a5305f6b] | 298 | |
---|
[f36a7bfc] | 299 | if (rc != RC_OK) |
---|
| 300 | return rc; |
---|
| 301 | |
---|
[a5305f6b] | 302 | /* |
---|
| 303 | * if new cluster allocated succesfully then new node is at very |
---|
| 304 | * beginning of the cluster (offset is computed in bytes) |
---|
[f36a7bfc] | 305 | */ |
---|
| 306 | paux->ofs = 0; |
---|
[a5305f6b] | 307 | return RC_OK; |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | /* |
---|
| 311 | * if we have deal with ".." - it is a special case :((( |
---|
[f36a7bfc] | 312 | * |
---|
| 313 | * Really, we should return cluster num and offset not of ".." slot, but |
---|
| 314 | * slot which correspondes to real directory name. |
---|
| 315 | */ |
---|
| 316 | if ((rc == RC_OK) && (name != NULL)) |
---|
| 317 | { |
---|
| 318 | if (strncmp(name, MSDOS_DOTDOT_NAME, MSDOS_SHORT_NAME_LEN) == 0) |
---|
| 319 | { |
---|
| 320 | dotdot_cln = MSDOS_EXTRACT_CLUSTER_NUM((name_dir_entry)); |
---|
| 321 | |
---|
| 322 | /* are we right under root dir ? */ |
---|
| 323 | if (dotdot_cln == 0) |
---|
[a5305f6b] | 324 | { |
---|
| 325 | /* |
---|
| 326 | * we can relax about first_char field - it never should be |
---|
[f36a7bfc] | 327 | * used for root dir |
---|
| 328 | */ |
---|
| 329 | paux->cln = FAT_ROOTDIR_CLUSTER_NUM; |
---|
| 330 | paux->ofs = 0; |
---|
[a5305f6b] | 331 | } |
---|
[f36a7bfc] | 332 | else |
---|
| 333 | { |
---|
| 334 | rc = msdos_get_dotdot_dir_info_cluster_num_and_offset( |
---|
| 335 | parent_loc->mt_entry, |
---|
| 336 | dotdot_cln, |
---|
| 337 | paux, |
---|
| 338 | name_dir_entry |
---|
| 339 | ); |
---|
| 340 | if (rc != RC_OK) |
---|
| 341 | return rc; |
---|
| 342 | } |
---|
[a5305f6b] | 343 | } |
---|
[f36a7bfc] | 344 | } |
---|
| 345 | return rc; |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | /* |
---|
| 349 | * msdos_get_dotdot_dir_info_cluster_num_and_offset |
---|
| 350 | * |
---|
[a5305f6b] | 351 | * Unfortunately, in general, we cann't work here in fat-file ideologic |
---|
| 352 | * (open fat_file "..", get ".." and ".", open "..", find an entry ...) |
---|
[f36a7bfc] | 353 | * because if we open |
---|
| 354 | * fat-file ".." it may happend that we have two different fat-file |
---|
[a5305f6b] | 355 | * descriptors ( for real name of directory and ".." name ) for a single |
---|
[f36a7bfc] | 356 | * file ( cluster num of both pointers to the same cluster ) |
---|
| 357 | * But...we do it because we protected by semaphore |
---|
[a5305f6b] | 358 | * |
---|
[f36a7bfc] | 359 | */ |
---|
| 360 | |
---|
| 361 | /* msdos_get_dotdot_dir_info_cluster_num_and_offset -- |
---|
[a5305f6b] | 362 | * Get cluster num and offset not of ".." slot, but slot which correspondes |
---|
| 363 | * to real directory name. |
---|
[f36a7bfc] | 364 | * |
---|
| 365 | * PARAMETERS: |
---|
| 366 | * mt_entry - mount table entry |
---|
| 367 | * cln - data cluster num extracted drom ".." slot |
---|
| 368 | * paux - identify a node location on the disk - |
---|
| 369 | * number of cluster and offset inside the cluster |
---|
[a5305f6b] | 370 | * dir_entry - placeholder for found node |
---|
[f36a7bfc] | 371 | * |
---|
| 372 | * RETURNS: |
---|
| 373 | * RC_OK, filled 'paux' and 'dir_entry' on success, or -1 if error occured |
---|
| 374 | * (errno set apropriately) |
---|
| 375 | * |
---|
| 376 | */ |
---|
| 377 | int |
---|
| 378 | msdos_get_dotdot_dir_info_cluster_num_and_offset( |
---|
[a5305f6b] | 379 | rtems_filesystem_mount_table_entry_t *mt_entry, |
---|
[f91bbe64] | 380 | uint32_t cln, |
---|
[f36a7bfc] | 381 | fat_auxiliary_t *paux, |
---|
| 382 | char *dir_entry |
---|
| 383 | ) |
---|
| 384 | { |
---|
| 385 | int rc = RC_OK; |
---|
| 386 | msdos_fs_info_t *fs_info = mt_entry->fs_info; |
---|
| 387 | fat_file_fd_t *fat_fd = NULL; |
---|
[adc829ed] | 388 | char dot_node[MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE]; |
---|
| 389 | char dotdot_node[MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE]; |
---|
[f91bbe64] | 390 | uint32_t cl4find = 0; |
---|
[a5305f6b] | 391 | |
---|
| 392 | memset(dot_node, 0, MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE); |
---|
[f36a7bfc] | 393 | memset(dotdot_node, 0, MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE); |
---|
[a5305f6b] | 394 | |
---|
[f36a7bfc] | 395 | /* |
---|
| 396 | * open fat-file corresponded to ".." |
---|
| 397 | */ |
---|
| 398 | rc = fat_file_open(mt_entry, paux->cln, paux->ofs, &fat_fd); |
---|
| 399 | if (rc != RC_OK) |
---|
| 400 | return rc; |
---|
[a5305f6b] | 401 | |
---|
[f36a7bfc] | 402 | fat_fd->info_cln = paux->cln; |
---|
| 403 | fat_fd->info_ofs = paux->ofs; |
---|
| 404 | fat_fd->cln = cln; |
---|
| 405 | fat_fd->fat_file_type = FAT_DIRECTORY; |
---|
| 406 | fat_fd->size_limit = MSDOS_MAX_DIR_LENGHT; |
---|
[a5305f6b] | 407 | |
---|
[f36a7bfc] | 408 | fat_fd->map.file_cln = 0; |
---|
| 409 | fat_fd->map.disk_cln = fat_fd->cln; |
---|
| 410 | |
---|
| 411 | rc = fat_file_size(mt_entry, fat_fd); |
---|
| 412 | if (rc != RC_OK) |
---|
| 413 | { |
---|
| 414 | fat_file_close(mt_entry, fat_fd); |
---|
| 415 | return rc; |
---|
[a5305f6b] | 416 | } |
---|
| 417 | |
---|
[f36a7bfc] | 418 | /* find "." node in opened directory */ |
---|
[a5305f6b] | 419 | rc = msdos_find_name_in_fat_file(mt_entry, fat_fd, MSDOS_DOT_NAME, paux, |
---|
[f36a7bfc] | 420 | dot_node); |
---|
[a5305f6b] | 421 | |
---|
[f36a7bfc] | 422 | if (rc != RC_OK) |
---|
| 423 | { |
---|
| 424 | fat_file_close(mt_entry, fat_fd); |
---|
| 425 | return rc; |
---|
| 426 | } |
---|
[a5305f6b] | 427 | |
---|
[f36a7bfc] | 428 | /* find ".." node in opened directory */ |
---|
[a5305f6b] | 429 | rc = msdos_find_name_in_fat_file(mt_entry, fat_fd, MSDOS_DOTDOT_NAME, paux, |
---|
[f36a7bfc] | 430 | dotdot_node); |
---|
[a5305f6b] | 431 | |
---|
| 432 | if (rc != RC_OK) |
---|
[f36a7bfc] | 433 | { |
---|
| 434 | fat_file_close(mt_entry, fat_fd); |
---|
| 435 | return rc; |
---|
| 436 | } |
---|
| 437 | |
---|
| 438 | cl4find = MSDOS_EXTRACT_CLUSTER_NUM(dot_node); |
---|
[a5305f6b] | 439 | |
---|
[f36a7bfc] | 440 | /* close fat-file corresponded to ".." directory */ |
---|
| 441 | rc = fat_file_close(mt_entry, fat_fd); |
---|
| 442 | if ( rc != RC_OK ) |
---|
| 443 | return rc; |
---|
| 444 | |
---|
| 445 | if ( (MSDOS_EXTRACT_CLUSTER_NUM(dotdot_node)) == 0) |
---|
| 446 | { |
---|
[a5305f6b] | 447 | /* |
---|
| 448 | * we handle root dir for all FAT types in the same way with the |
---|
[f36a7bfc] | 449 | * ordinary directories ( through fat_file_* calls ) |
---|
| 450 | */ |
---|
| 451 | paux->cln = FAT_ROOTDIR_CLUSTER_NUM; |
---|
| 452 | paux->ofs = 0; |
---|
| 453 | } |
---|
| 454 | |
---|
| 455 | /* open fat-file corresponded to second ".." */ |
---|
| 456 | rc = fat_file_open(mt_entry, paux->cln, paux->ofs, &fat_fd); |
---|
| 457 | if (rc != RC_OK) |
---|
| 458 | return rc; |
---|
| 459 | |
---|
| 460 | fat_fd->info_cln = paux->cln; |
---|
| 461 | fat_fd->info_ofs = paux->ofs; |
---|
| 462 | |
---|
| 463 | if ((MSDOS_EXTRACT_CLUSTER_NUM(dotdot_node)) == 0) |
---|
| 464 | fat_fd->cln = fs_info->fat.vol.rdir_cl; |
---|
| 465 | else |
---|
| 466 | fat_fd->cln = MSDOS_EXTRACT_CLUSTER_NUM(dotdot_node); |
---|
| 467 | |
---|
| 468 | fat_fd->fat_file_type = FAT_DIRECTORY; |
---|
| 469 | fat_fd->size_limit = MSDOS_MAX_DIR_LENGHT; |
---|
[a5305f6b] | 470 | |
---|
[f36a7bfc] | 471 | fat_fd->map.file_cln = 0; |
---|
| 472 | fat_fd->map.disk_cln = fat_fd->cln; |
---|
| 473 | |
---|
| 474 | rc = fat_file_size(mt_entry, fat_fd); |
---|
| 475 | if (rc != RC_OK) |
---|
| 476 | { |
---|
| 477 | fat_file_close(mt_entry, fat_fd); |
---|
| 478 | return rc; |
---|
[a5305f6b] | 479 | } |
---|
| 480 | |
---|
[f36a7bfc] | 481 | /* in this directory find slot with specified cluster num */ |
---|
[a5305f6b] | 482 | rc = msdos_find_node_by_cluster_num_in_fat_file(mt_entry, fat_fd, cl4find, |
---|
[f36a7bfc] | 483 | paux, dir_entry); |
---|
| 484 | if (rc != RC_OK) |
---|
| 485 | { |
---|
| 486 | fat_file_close(mt_entry, fat_fd); |
---|
| 487 | return rc; |
---|
| 488 | } |
---|
| 489 | rc = fat_file_close(mt_entry, fat_fd); |
---|
| 490 | return rc; |
---|
| 491 | } |
---|
| 492 | |
---|
| 493 | |
---|
| 494 | /* msdos_set_dir_wrt_time_and_date -- |
---|
[a5305f6b] | 495 | * Write last write date and time for a file to the disk (to corresponded |
---|
| 496 | * 32bytes node) |
---|
[f36a7bfc] | 497 | * |
---|
| 498 | * PARAMETERS: |
---|
[a5305f6b] | 499 | * mt_entry - mount table entry |
---|
[f36a7bfc] | 500 | * fat_fd - fat-file descriptor |
---|
| 501 | * |
---|
| 502 | * RETURNS: |
---|
| 503 | * RC_OK on success, or -1 if error occured (errno set apropriately). |
---|
| 504 | * |
---|
| 505 | */ |
---|
| 506 | int |
---|
| 507 | msdos_set_dir_wrt_time_and_date( |
---|
[a5305f6b] | 508 | rtems_filesystem_mount_table_entry_t *mt_entry, |
---|
[f36a7bfc] | 509 | fat_file_fd_t *fat_fd |
---|
| 510 | ) |
---|
| 511 | { |
---|
| 512 | ssize_t ret1 = 0, ret2 = 0; |
---|
| 513 | msdos_fs_info_t *fs_info = mt_entry->fs_info; |
---|
| 514 | unsigned short time_val; |
---|
| 515 | unsigned short date; |
---|
[f91bbe64] | 516 | uint32_t sec = 0; |
---|
| 517 | uint32_t byte = 0; |
---|
[a5305f6b] | 518 | |
---|
[f36a7bfc] | 519 | msdos_date_unix2dos(fat_fd->mtime, &time_val, &date); |
---|
[a5305f6b] | 520 | |
---|
| 521 | /* |
---|
[f36a7bfc] | 522 | * calculate input for _fat_block_write: convert (cluster num, offset) to |
---|
| 523 | * (sector num, new offset) |
---|
| 524 | */ |
---|
| 525 | sec = fat_cluster_num_to_sector_num(mt_entry, fat_fd->info_cln); |
---|
| 526 | sec += (fat_fd->info_ofs >> fs_info->fat.vol.sec_log2); |
---|
| 527 | /* byte points to start of 32bytes structure */ |
---|
| 528 | byte = fat_fd->info_ofs & (fs_info->fat.vol.bps - 1); |
---|
[a5305f6b] | 529 | |
---|
[f36a7bfc] | 530 | time_val = CT_LE_W(time_val); |
---|
| 531 | ret1 = _fat_block_write(mt_entry, sec, byte + MSDOS_FILE_WTIME_OFFSET, |
---|
| 532 | 2, (char *)(&time_val)); |
---|
| 533 | date = CT_LE_W(date); |
---|
| 534 | ret2 = _fat_block_write(mt_entry, sec, byte + MSDOS_FILE_WDATE_OFFSET, |
---|
| 535 | 2, (char *)(&date)); |
---|
| 536 | |
---|
| 537 | if ( (ret1 < 0) || (ret2 < 0) ) |
---|
| 538 | return -1; |
---|
| 539 | |
---|
| 540 | return RC_OK; |
---|
| 541 | } |
---|
| 542 | |
---|
| 543 | /* msdos_set_first_cluster_num -- |
---|
[a5305f6b] | 544 | * Write number of first cluster of the file to the disk (to corresponded |
---|
| 545 | * 32bytes slot) |
---|
[f36a7bfc] | 546 | * |
---|
| 547 | * PARAMETERS: |
---|
[a5305f6b] | 548 | * mt_entry - mount table entry |
---|
[f36a7bfc] | 549 | * fat_fd - fat-file descriptor |
---|
| 550 | * |
---|
| 551 | * RETURNS: |
---|
| 552 | * RC_OK on success, or -1 if error occured |
---|
| 553 | * |
---|
| 554 | */ |
---|
| 555 | int |
---|
| 556 | msdos_set_first_cluster_num( |
---|
[a5305f6b] | 557 | rtems_filesystem_mount_table_entry_t *mt_entry, |
---|
[f36a7bfc] | 558 | fat_file_fd_t *fat_fd |
---|
| 559 | ) |
---|
| 560 | { |
---|
| 561 | ssize_t ret1 = 0, ret2 = 0; |
---|
| 562 | msdos_fs_info_t *fs_info = mt_entry->fs_info; |
---|
[f91bbe64] | 563 | uint32_t new_cln = fat_fd->cln; |
---|
| 564 | uint16_t le_cl_low = 0; |
---|
[a5305f6b] | 565 | uint16_t le_cl_hi = 0; |
---|
[f91bbe64] | 566 | uint32_t sec = 0; |
---|
| 567 | uint32_t byte = 0; |
---|
[f36a7bfc] | 568 | |
---|
[a5305f6b] | 569 | /* |
---|
[f36a7bfc] | 570 | * calculate input for _fat_block_write: convert (cluster num, offset) to |
---|
| 571 | * (sector num, new offset) |
---|
| 572 | */ |
---|
| 573 | sec = fat_cluster_num_to_sector_num(mt_entry, fat_fd->info_cln); |
---|
| 574 | sec += (fat_fd->info_ofs >> fs_info->fat.vol.sec_log2); |
---|
| 575 | /* byte from points to start of 32bytes structure */ |
---|
| 576 | byte = fat_fd->info_ofs & (fs_info->fat.vol.bps - 1); |
---|
[a5305f6b] | 577 | |
---|
[f91bbe64] | 578 | le_cl_low = CT_LE_W((uint16_t )(new_cln & 0x0000FFFF)); |
---|
[a5305f6b] | 579 | ret1 = _fat_block_write(mt_entry, sec, |
---|
[f36a7bfc] | 580 | byte + MSDOS_FIRST_CLUSTER_LOW_OFFSET, 2, |
---|
| 581 | (char *)(&le_cl_low)); |
---|
[f91bbe64] | 582 | le_cl_hi = CT_LE_W((uint16_t )((new_cln & 0xFFFF0000) >> 16)); |
---|
[f36a7bfc] | 583 | ret2 = _fat_block_write(mt_entry, sec, |
---|
| 584 | byte + MSDOS_FIRST_CLUSTER_HI_OFFSET, 2, |
---|
| 585 | (char *)(&le_cl_hi)); |
---|
| 586 | if ( (ret1 < 0) || (ret2 < 0) ) |
---|
| 587 | return -1; |
---|
| 588 | |
---|
| 589 | return RC_OK; |
---|
| 590 | } |
---|
| 591 | |
---|
| 592 | |
---|
| 593 | /* msdos_set_file size -- |
---|
[a5305f6b] | 594 | * Write file size of the file to the disk (to corresponded 32bytes slot) |
---|
[f36a7bfc] | 595 | * |
---|
| 596 | * PARAMETERS: |
---|
[a5305f6b] | 597 | * mt_entry - mount table entry |
---|
[f36a7bfc] | 598 | * fat_fd - fat-file descriptor |
---|
| 599 | * |
---|
| 600 | * RETURNS: |
---|
| 601 | * RC_OK on success, or -1 if error occured (errno set apropriately). |
---|
| 602 | * |
---|
| 603 | */ |
---|
| 604 | int |
---|
| 605 | msdos_set_file_size( |
---|
[a5305f6b] | 606 | rtems_filesystem_mount_table_entry_t *mt_entry, |
---|
[f36a7bfc] | 607 | fat_file_fd_t *fat_fd |
---|
| 608 | ) |
---|
| 609 | { |
---|
| 610 | ssize_t ret = 0; |
---|
| 611 | msdos_fs_info_t *fs_info = mt_entry->fs_info; |
---|
[f91bbe64] | 612 | uint32_t le_new_length = 0; |
---|
| 613 | uint32_t sec = 0; |
---|
| 614 | uint32_t byte = 0; |
---|
[f36a7bfc] | 615 | |
---|
| 616 | sec = fat_cluster_num_to_sector_num(mt_entry, fat_fd->info_cln); |
---|
| 617 | sec += (fat_fd->info_ofs >> fs_info->fat.vol.sec_log2); |
---|
| 618 | byte = (fat_fd->info_ofs & (fs_info->fat.vol.bps - 1)); |
---|
| 619 | |
---|
| 620 | le_new_length = CT_LE_L((fat_fd->fat_file_size)); |
---|
| 621 | ret = _fat_block_write(mt_entry, sec, byte + MSDOS_FILE_SIZE_OFFSET, 4, |
---|
| 622 | (char *)(&le_new_length)); |
---|
| 623 | if ( ret < 0 ) |
---|
| 624 | return -1; |
---|
| 625 | |
---|
| 626 | return RC_OK; |
---|
| 627 | } |
---|
| 628 | |
---|
| 629 | /* |
---|
| 630 | * We should not check whether this routine is called for root dir - it |
---|
| 631 | * never can happend |
---|
| 632 | */ |
---|
| 633 | |
---|
| 634 | /* msdos_set_first_char4file_name -- |
---|
[a5305f6b] | 635 | * Write first character of the name of the file to the disk (to |
---|
| 636 | * corresponded 32bytes slot) |
---|
[f36a7bfc] | 637 | * |
---|
| 638 | * PARAMETERS: |
---|
[a5305f6b] | 639 | * mt_entry - mount table entry |
---|
[f36a7bfc] | 640 | * cl - number of cluster |
---|
| 641 | * ofs - offset inside cluster |
---|
| 642 | * fchar - character to set up |
---|
| 643 | * |
---|
| 644 | * RETURNS: |
---|
| 645 | * RC_OK on success, or -1 if error occured (errno set apropriately) |
---|
| 646 | * |
---|
| 647 | */ |
---|
[a5305f6b] | 648 | int |
---|
[f36a7bfc] | 649 | msdos_set_first_char4file_name( |
---|
[a5305f6b] | 650 | rtems_filesystem_mount_table_entry_t *mt_entry, |
---|
[f91bbe64] | 651 | uint32_t cl, |
---|
| 652 | uint32_t ofs, |
---|
[f36a7bfc] | 653 | unsigned char fchar |
---|
| 654 | ) |
---|
| 655 | { |
---|
| 656 | ssize_t ret = 0; |
---|
| 657 | msdos_fs_info_t *fs_info = mt_entry->fs_info; |
---|
[f91bbe64] | 658 | uint32_t sec = 0; |
---|
| 659 | uint32_t byte = 0; |
---|
[f36a7bfc] | 660 | |
---|
| 661 | sec = fat_cluster_num_to_sector_num(mt_entry, cl); |
---|
| 662 | sec += (ofs >> fs_info->fat.vol.sec_log2); |
---|
| 663 | byte = (ofs & (fs_info->fat.vol.bps - 1)); |
---|
| 664 | |
---|
[a5305f6b] | 665 | ret = _fat_block_write(mt_entry, sec, byte + MSDOS_FILE_NAME_OFFSET, 1, |
---|
[f36a7bfc] | 666 | &fchar); |
---|
| 667 | if ( ret < 0) |
---|
| 668 | return -1; |
---|
| 669 | |
---|
| 670 | return RC_OK; |
---|
[a5305f6b] | 671 | } |
---|
[f36a7bfc] | 672 | |
---|
| 673 | /* msdos_dir_is_empty -- |
---|
[a5305f6b] | 674 | * Check whether directory which correspondes to the fat-file descriptor is |
---|
| 675 | * empty. |
---|
[f36a7bfc] | 676 | * |
---|
| 677 | * PARAMETERS: |
---|
[a5305f6b] | 678 | * mt_entry - mount table entry |
---|
| 679 | * fat_fd - fat-file descriptor |
---|
| 680 | * ret_val - placeholder for result |
---|
[f36a7bfc] | 681 | * |
---|
| 682 | * RETURNS: |
---|
| 683 | * RC_OK on success, or -1 if error occured |
---|
| 684 | * |
---|
| 685 | */ |
---|
| 686 | int |
---|
| 687 | msdos_dir_is_empty( |
---|
| 688 | rtems_filesystem_mount_table_entry_t *mt_entry, |
---|
[a5305f6b] | 689 | fat_file_fd_t *fat_fd, |
---|
[f36a7bfc] | 690 | rtems_boolean *ret_val |
---|
| 691 | ) |
---|
| 692 | { |
---|
| 693 | ssize_t ret = 0; |
---|
| 694 | msdos_fs_info_t *fs_info = mt_entry->fs_info; |
---|
[f91bbe64] | 695 | uint32_t j = 0, i = 0; |
---|
[a5305f6b] | 696 | |
---|
[f36a7bfc] | 697 | /* dir is not empty */ |
---|
| 698 | *ret_val = FALSE; |
---|
| 699 | |
---|
| 700 | while ((ret = fat_file_read(mt_entry, fat_fd, j * fs_info->fat.vol.bps, |
---|
[a5305f6b] | 701 | fs_info->fat.vol.bps, |
---|
[f36a7bfc] | 702 | fs_info->cl_buf)) != FAT_EOF) |
---|
| 703 | { |
---|
| 704 | if (ret < MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE) |
---|
| 705 | return -1; |
---|
| 706 | |
---|
| 707 | assert(ret == fs_info->fat.vol.bps); |
---|
[a5305f6b] | 708 | |
---|
[b17ff491] | 709 | /* have to look at the DIR_NAME as "raw" 8-bit data */ |
---|
[a5305f6b] | 710 | for (i = 0; |
---|
| 711 | i < fs_info->fat.vol.bps; |
---|
[f36a7bfc] | 712 | i += MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE) |
---|
| 713 | { |
---|
[b17ff491] | 714 | if (((*(uint8_t *)MSDOS_DIR_NAME(fs_info->cl_buf + i)) == |
---|
[a5305f6b] | 715 | MSDOS_THIS_DIR_ENTRY_EMPTY) || |
---|
| 716 | (strncmp(MSDOS_DIR_NAME((fs_info->cl_buf + i)), MSDOS_DOT_NAME, |
---|
[f36a7bfc] | 717 | MSDOS_SHORT_NAME_LEN) == 0) || |
---|
| 718 | (strncmp(MSDOS_DIR_NAME((fs_info->cl_buf + i)), |
---|
[a5305f6b] | 719 | MSDOS_DOTDOT_NAME, |
---|
[f36a7bfc] | 720 | MSDOS_SHORT_NAME_LEN) == 0)) |
---|
| 721 | continue; |
---|
| 722 | |
---|
[a5305f6b] | 723 | if ((*MSDOS_DIR_NAME(fs_info->cl_buf + i)) == |
---|
[f36a7bfc] | 724 | MSDOS_THIS_DIR_ENTRY_AND_REST_EMPTY) |
---|
| 725 | { |
---|
| 726 | *ret_val = TRUE; |
---|
| 727 | return RC_OK; |
---|
| 728 | } |
---|
| 729 | return RC_OK; |
---|
[a5305f6b] | 730 | } |
---|
| 731 | j++; |
---|
[f36a7bfc] | 732 | } |
---|
| 733 | *ret_val = TRUE; |
---|
| 734 | return RC_OK; |
---|
| 735 | } |
---|
| 736 | |
---|
| 737 | |
---|
| 738 | /* msdos_find_name_in_fat_file -- |
---|
| 739 | * This routine is used in two ways: for a new mode creation (a) or for |
---|
| 740 | * search the node which correspondes to the 'name' parameter (b). |
---|
[a5305f6b] | 741 | * In case (a) name should be set up to NULL and 'name_dir_entry' should |
---|
| 742 | * point to initialized 32 bytes structure described a new node. |
---|
[f36a7bfc] | 743 | * In case (b) 'name' should contain a valid string. |
---|
| 744 | * |
---|
| 745 | * (a): reading fat-file corresponded to directory we are going to create |
---|
| 746 | * node in. If found free slot write contents of name_dir_entry into |
---|
[a5305f6b] | 747 | * it. |
---|
[f36a7bfc] | 748 | * |
---|
| 749 | * (b): reading fat-file corresponded to directory and trying to find slot |
---|
| 750 | * with the name field == name parameter |
---|
| 751 | * |
---|
| 752 | * PARAMETERS: |
---|
[a5305f6b] | 753 | * mt_entry - mount table entry |
---|
| 754 | * fat_fd - fat-file descriptor |
---|
| 755 | * name - NULL or name to find |
---|
[f36a7bfc] | 756 | * paux - identify a node location on the disk - |
---|
| 757 | * number of cluster and offset inside the cluster |
---|
| 758 | * name_dir_entry - node to create/placeholder for found node |
---|
| 759 | * |
---|
| 760 | * RETURNS: |
---|
[a5305f6b] | 761 | * RC_OK on success, or error code if error occured (errno set |
---|
[f36a7bfc] | 762 | * appropriately) |
---|
| 763 | * |
---|
| 764 | */ |
---|
| 765 | int |
---|
| 766 | msdos_find_name_in_fat_file( |
---|
| 767 | rtems_filesystem_mount_table_entry_t *mt_entry, |
---|
[a5305f6b] | 768 | fat_file_fd_t *fat_fd, |
---|
| 769 | char *name, |
---|
[f36a7bfc] | 770 | fat_auxiliary_t *paux, |
---|
| 771 | char *name_dir_entry |
---|
| 772 | ) |
---|
| 773 | { |
---|
| 774 | int rc = RC_OK; |
---|
| 775 | ssize_t ret = 0; |
---|
| 776 | msdos_fs_info_t *fs_info = mt_entry->fs_info; |
---|
[f91bbe64] | 777 | uint32_t i = 0, j = 0; |
---|
| 778 | uint32_t bts2rd = 0; |
---|
[f36a7bfc] | 779 | |
---|
[a5305f6b] | 780 | if (FAT_FD_OF_ROOT_DIR(fat_fd) && |
---|
[f36a7bfc] | 781 | (fs_info->fat.vol.type & (FAT_FAT12 | FAT_FAT16))) |
---|
[a5305f6b] | 782 | bts2rd = fat_fd->fat_file_size; |
---|
[f36a7bfc] | 783 | else |
---|
| 784 | bts2rd = fs_info->fat.vol.bpc; |
---|
[a5305f6b] | 785 | |
---|
| 786 | while ((ret = fat_file_read(mt_entry, fat_fd, (j * bts2rd), bts2rd, |
---|
| 787 | fs_info->cl_buf)) != FAT_EOF) |
---|
[f36a7bfc] | 788 | { |
---|
| 789 | if (ret < MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE) |
---|
| 790 | set_errno_and_return_minus_one(EIO); |
---|
[a5305f6b] | 791 | |
---|
| 792 | assert(ret == bts2rd); |
---|
[f36a7bfc] | 793 | |
---|
[b17ff491] | 794 | /* have to look at the DIR_NAME as "raw" 8-bit data */ |
---|
[f36a7bfc] | 795 | for (i = 0; i < bts2rd; i += MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE) |
---|
| 796 | { |
---|
| 797 | /* is the entry empty ? */ |
---|
[b17ff491] | 798 | if (((*(uint8_t *)MSDOS_DIR_NAME(fs_info->cl_buf + i)) == |
---|
[f36a7bfc] | 799 | MSDOS_THIS_DIR_ENTRY_AND_REST_EMPTY) || |
---|
[b17ff491] | 800 | ((*(uint8_t *)MSDOS_DIR_NAME(fs_info->cl_buf + i)) == |
---|
[f36a7bfc] | 801 | MSDOS_THIS_DIR_ENTRY_EMPTY)) |
---|
| 802 | { |
---|
| 803 | /* whether we are looking for an empty entry */ |
---|
| 804 | if (name == NULL) |
---|
| 805 | { |
---|
| 806 | /* get current cluster number */ |
---|
[a5305f6b] | 807 | rc = fat_file_ioctl(mt_entry, fat_fd, F_CLU_NUM, |
---|
[f36a7bfc] | 808 | j * bts2rd, &paux->cln); |
---|
| 809 | if (rc != RC_OK) |
---|
| 810 | return rc; |
---|
| 811 | |
---|
| 812 | /* offset is computed in bytes */ |
---|
| 813 | paux->ofs = i; |
---|
[a5305f6b] | 814 | |
---|
[f36a7bfc] | 815 | /* write new node entry */ |
---|
[a5305f6b] | 816 | ret = fat_file_write(mt_entry, fat_fd, j * bts2rd + i, |
---|
[f36a7bfc] | 817 | MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE, |
---|
[b17ff491] | 818 | (uint8_t *)name_dir_entry); |
---|
[f36a7bfc] | 819 | if (ret != MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE) |
---|
| 820 | return -1; |
---|
| 821 | |
---|
[a5305f6b] | 822 | /* |
---|
| 823 | * we don't update fat_file_size here - it should not |
---|
| 824 | * increase |
---|
[f36a7bfc] | 825 | */ |
---|
| 826 | return RC_OK; |
---|
| 827 | } |
---|
| 828 | |
---|
[a5305f6b] | 829 | /* |
---|
| 830 | * if name != NULL and there is no more entries in the |
---|
[f36a7bfc] | 831 | * directory - return name-not-found |
---|
| 832 | */ |
---|
[a5305f6b] | 833 | if (((*MSDOS_DIR_NAME(fs_info->cl_buf + i)) == |
---|
[f36a7bfc] | 834 | MSDOS_THIS_DIR_ENTRY_AND_REST_EMPTY)) |
---|
| 835 | return MSDOS_NAME_NOT_FOUND_ERR; |
---|
[a5305f6b] | 836 | } |
---|
[f36a7bfc] | 837 | else |
---|
| 838 | { |
---|
| 839 | /* entry not empty and name != NULL -> compare names */ |
---|
| 840 | if (name != NULL) |
---|
| 841 | { |
---|
[a5305f6b] | 842 | if (strncmp(MSDOS_DIR_NAME((fs_info->cl_buf + i)), name, |
---|
[f36a7bfc] | 843 | MSDOS_SHORT_NAME_LEN) == 0) |
---|
| 844 | { |
---|
[a5305f6b] | 845 | /* |
---|
| 846 | * we get the entry we looked for - fill auxiliary |
---|
[f36a7bfc] | 847 | * structure and copy all 32 bytes of the entry |
---|
| 848 | */ |
---|
[a5305f6b] | 849 | rc = fat_file_ioctl(mt_entry, fat_fd, F_CLU_NUM, |
---|
[f36a7bfc] | 850 | j * bts2rd, &paux->cln); |
---|
| 851 | if (rc != RC_OK) |
---|
| 852 | return rc; |
---|
| 853 | |
---|
| 854 | /* offset is computed in bytes */ |
---|
| 855 | paux->ofs = i; |
---|
| 856 | memcpy(name_dir_entry,(fs_info->cl_buf + i), |
---|
| 857 | MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE); |
---|
| 858 | return RC_OK; |
---|
| 859 | } |
---|
[a5305f6b] | 860 | } |
---|
| 861 | } |
---|
| 862 | } |
---|
[f36a7bfc] | 863 | j++; |
---|
[a5305f6b] | 864 | } |
---|
[f36a7bfc] | 865 | return MSDOS_NAME_NOT_FOUND_ERR; |
---|
| 866 | } |
---|
| 867 | |
---|
| 868 | /* msdos_find_node_by_cluster_num_in_fat_file -- |
---|
| 869 | * Find node with specified number of cluster in fat-file. |
---|
| 870 | * |
---|
| 871 | * PARAMETERS: |
---|
[a5305f6b] | 872 | * mt_entry - mount table entry |
---|
| 873 | * fat_fd - fat-file descriptor |
---|
| 874 | * cl4find - number of cluster to find |
---|
[f36a7bfc] | 875 | * paux - identify a node location on the disk - |
---|
| 876 | * cluster num and offset inside the cluster |
---|
| 877 | * dir_entry - placeholder for found node |
---|
| 878 | * |
---|
| 879 | * RETURNS: |
---|
| 880 | * RC_OK on success, or error code if error occured |
---|
| 881 | * |
---|
| 882 | */ |
---|
| 883 | int |
---|
| 884 | msdos_find_node_by_cluster_num_in_fat_file( |
---|
| 885 | rtems_filesystem_mount_table_entry_t *mt_entry, |
---|
| 886 | fat_file_fd_t *fat_fd, |
---|
[a5305f6b] | 887 | uint32_t cl4find, |
---|
[f36a7bfc] | 888 | fat_auxiliary_t *paux, |
---|
| 889 | char *dir_entry |
---|
| 890 | ) |
---|
| 891 | { |
---|
| 892 | int rc = RC_OK; |
---|
| 893 | ssize_t ret = 0; |
---|
| 894 | msdos_fs_info_t *fs_info = mt_entry->fs_info; |
---|
[a5305f6b] | 895 | uint32_t bts2rd = 0; |
---|
[f91bbe64] | 896 | uint32_t i = 0, j = 0; |
---|
[f36a7bfc] | 897 | |
---|
[a5305f6b] | 898 | if (FAT_FD_OF_ROOT_DIR(fat_fd) && |
---|
[f36a7bfc] | 899 | (fs_info->fat.vol.type & (FAT_FAT12 | FAT_FAT16))) |
---|
[a5305f6b] | 900 | bts2rd = fat_fd->fat_file_size; |
---|
[f36a7bfc] | 901 | else |
---|
| 902 | bts2rd = fs_info->fat.vol.bpc; |
---|
| 903 | |
---|
| 904 | while ((ret = fat_file_read(mt_entry, fat_fd, j * bts2rd, bts2rd, |
---|
| 905 | fs_info->cl_buf)) != FAT_EOF) |
---|
| 906 | { |
---|
| 907 | if ( ret < MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE ) |
---|
| 908 | set_errno_and_return_minus_one( EIO ); |
---|
| 909 | |
---|
| 910 | assert(ret == bts2rd); |
---|
[a5305f6b] | 911 | |
---|
[f36a7bfc] | 912 | for (i = 0; i < bts2rd; i += MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE) |
---|
| 913 | { |
---|
| 914 | /* if this and all rest entries are empty - return not-found */ |
---|
| 915 | if ((*MSDOS_DIR_NAME(fs_info->cl_buf + i)) == |
---|
| 916 | MSDOS_THIS_DIR_ENTRY_AND_REST_EMPTY) |
---|
| 917 | return MSDOS_NAME_NOT_FOUND_ERR; |
---|
| 918 | |
---|
[b17ff491] | 919 | /* have to look at the DIR_NAME as "raw" 8-bit data */ |
---|
[a5305f6b] | 920 | /* if this entry is empty - skip it */ |
---|
[b17ff491] | 921 | if ((*(uint8_t *)MSDOS_DIR_NAME(fs_info->cl_buf + i)) == |
---|
[f36a7bfc] | 922 | MSDOS_THIS_DIR_ENTRY_EMPTY) |
---|
| 923 | continue; |
---|
| 924 | |
---|
| 925 | /* if get a non-empty entry - compare clusters num */ |
---|
| 926 | if (MSDOS_EXTRACT_CLUSTER_NUM((fs_info->cl_buf + i)) == cl4find) |
---|
| 927 | { |
---|
| 928 | /* on success fill aux structure and copy all 32 bytes */ |
---|
[a5305f6b] | 929 | rc = fat_file_ioctl(mt_entry, fat_fd, F_CLU_NUM, j * bts2rd, |
---|
[f36a7bfc] | 930 | &paux->cln); |
---|
| 931 | if (rc != RC_OK) |
---|
| 932 | return rc; |
---|
| 933 | |
---|
| 934 | paux->ofs = i; |
---|
[a5305f6b] | 935 | memcpy(dir_entry, fs_info->cl_buf + i, |
---|
[f36a7bfc] | 936 | MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE); |
---|
| 937 | return RC_OK; |
---|
| 938 | } |
---|
[a5305f6b] | 939 | } |
---|
[f36a7bfc] | 940 | j++; |
---|
[a5305f6b] | 941 | } |
---|
[f36a7bfc] | 942 | return MSDOS_NAME_NOT_FOUND_ERR; |
---|
| 943 | } |
---|