source: rtems/cpukit/libfs/src/dosfs/msdos_dir.c @ 7c007cf

4.104.114.9
Last change on this file since 7c007cf was 7c007cf, checked in by Thomas Doerfler <Thomas.Doerfler@…>, on Jul 10, 2008 at 6:03:51 AM

Added S_IRWXU, S_IRWXG and S_IRWXO to file mode.

  • Property mode set to 100644
File size: 16.4 KB
Line 
1/*
2 *  MSDOS directory handlers implementation
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
9 *  http://www.rtems.com/license/LICENSE.
10 *
11 *  @(#) $Id$
12 */
13#if HAVE_CONFIG_H
14#include "config.h"
15#endif
16
17#include <stdlib.h>
18#include <unistd.h>
19#include <assert.h>
20#include <errno.h>
21#include <rtems/libio_.h>
22#include <sys/types.h>
23#include <sys/stat.h>
24
25#include <dirent.h>
26
27#include "fat.h"
28#include "fat_fat_operations.h"
29#include "fat_file.h"
30
31#include "msdos.h"
32
33/* msdos_dir_open --
34 *     Open fat-file which correspondes to the directory being opened and
35 *     set offset field of file control block to zero.
36 *
37 * PARAMETERS:
38 *     iop        - file control block
39 *     pathname   - name
40 *     flag       - flags
41 *     mode       - mode
42 *
43 * RETURNS:
44 *     RC_OK, if directory opened successfully, or -1 if error occured (errno
45 *     set apropriately)
46 */
47int
48msdos_dir_open(rtems_libio_t *iop, const char *pathname, uint32_t   flag,
49               uint32_t   mode)
50{
51    int                rc = RC_OK;
52    rtems_status_code  sc = RTEMS_SUCCESSFUL;
53    msdos_fs_info_t   *fs_info = iop->pathinfo.mt_entry->fs_info;
54    fat_file_fd_t     *fat_fd = iop->file_info;
55
56    sc = rtems_semaphore_obtain(fs_info->vol_sema, RTEMS_WAIT,
57                                MSDOS_VOLUME_SEMAPHORE_TIMEOUT);
58    if (sc != RTEMS_SUCCESSFUL)
59        set_errno_and_return_minus_one( EIO );
60
61    rc = fat_file_reopen(fat_fd);
62    if (rc != RC_OK)
63    {
64        rtems_semaphore_release(fs_info->vol_sema);
65        return rc;
66    }
67
68    iop->offset = 0;
69    rtems_semaphore_release(fs_info->vol_sema);
70    return RC_OK;
71}
72
73/* msdos_dir_close --
74 *     Close  fat-file which correspondes to the directory being closed
75 *
76 * PARAMETERS:
77 *     iop - file control block
78 *
79 * RETURNS:
80 *     RC_OK, if directory closed successfully, or -1 if error occured (errno
81 *     set apropriately.
82 */
83int
84msdos_dir_close(rtems_libio_t *iop)
85{
86    int                rc = RC_OK;
87    rtems_status_code  sc = RTEMS_SUCCESSFUL;
88    msdos_fs_info_t   *fs_info = iop->pathinfo.mt_entry->fs_info;
89    fat_file_fd_t     *fat_fd = iop->file_info;
90
91    sc = rtems_semaphore_obtain(fs_info->vol_sema, RTEMS_WAIT,
92                                MSDOS_VOLUME_SEMAPHORE_TIMEOUT);
93    if (sc != RTEMS_SUCCESSFUL)
94        set_errno_and_return_minus_one( EIO );
95
96    rc = fat_file_close(iop->pathinfo.mt_entry, fat_fd);
97    if (rc != RC_OK)
98    {
99        rtems_semaphore_release(fs_info->vol_sema);
100        return rc;
101    }
102
103    rtems_semaphore_release(fs_info->vol_sema);
104    return RC_OK;
105}
106
107/*  msdos_format_dirent_with_dot --
108 *      This routine convert a (short) MSDOS filename as present on disk
109 *      (fixed 8+3 characters, filled with blanks, without separator dot)
110 *      to a "normal" format, with between 0 and 8 name chars,
111 *      a separating dot and up to 3 extension characters
112 *   Rules to work:
113 *      - copy any (0-8) "name" part characters that are non-blank
114 *      - if an extension exists, append a dot
115 *      - copy any (0-3) non-blank extension characters
116 *      - append a '\0' (dont count it for the rturn code
117 *
118 * PARAMETERS:
119 *     dst: pointer to destination char array (must be big enough)
120 *     src: pointer to source characters
121 *
122 *
123 * RETURNS:
124 *     the number of bytes (without trailing '\0'(written to destination
125 */
126static ssize_t
127msdos_format_dirent_with_dot(char *dst,const char *src)
128{
129  ssize_t len;
130  int i;
131  const char *src_tmp;
132
133  /*
134   * find last non-blank character of base name
135   */
136  for ((i       =       MSDOS_SHORT_BASE_LEN  ,
137        src_tmp = src + MSDOS_SHORT_BASE_LEN-1);
138       ((i > 0) &&
139        (*src_tmp == ' '));
140       i--,src_tmp--)
141    {};
142  /*
143   * copy base name to destination
144   */
145  src_tmp = src;
146  len = i;
147  while (i-- > 0) {
148    *dst++ = *src_tmp++;
149  }
150  /*
151   * find last non-blank character of extension
152   */
153  for ((i       =                            MSDOS_SHORT_EXT_LEN  ,
154        src_tmp = src + MSDOS_SHORT_BASE_LEN+MSDOS_SHORT_EXT_LEN-1);
155       ((i > 0) &&
156        (*src_tmp == ' '));
157       i--,src_tmp--)
158    {};
159  /*
160   * extension is not empty
161   */
162  if (i > 0) {
163    *dst++ = '.'; /* append dot */
164    len += i + 1; /* extension + dot */
165    src_tmp = src + MSDOS_SHORT_BASE_LEN;
166    while (i-- > 0) {
167      *dst++ = *src_tmp++;
168      len++;
169    }
170  }
171  *dst = '\0'; /* terminate string */
172
173  return len;
174}
175
176/*  msdos_dir_read --
177 *      This routine will read the next directory entry based on the directory
178 *      offset. The offset should be equal to -n- time the size of an
179 *      individual dirent structure. If n is not an integer multiple of the
180 *      sizeof a dirent structure, an integer division will be performed to
181 *      determine directory entry that will be returned in the buffer. Count
182 *      should reflect -m- times the sizeof dirent bytes to be placed in the
183 *      buffer.
184 *      If there are not -m- dirent elements from the current directory
185 *      position to the end of the exisiting file, the remaining entries will
186 *      be placed in the buffer and the returned value will be equal to
187 *      -m actual- times the size of a directory entry.
188 *
189 * PARAMETERS:
190 *     iop    - file control block
191 *     buffer - buffer provided by user
192 *     count  - count of bytes to read
193 *
194 * RETURNS:
195 *     the number of bytes read on success, or -1 if error occured (errno
196 *     set apropriately).
197 */
198ssize_t
199msdos_dir_read(rtems_libio_t *iop, void *buffer, size_t count)
200{
201    int                rc = RC_OK;
202    rtems_status_code  sc = RTEMS_SUCCESSFUL;
203    msdos_fs_info_t   *fs_info = iop->pathinfo.mt_entry->fs_info;
204    fat_file_fd_t     *fat_fd = iop->file_info;
205    fat_file_fd_t     *tmp_fat_fd = NULL;
206    struct dirent      tmp_dirent;
207    uint32_t           start = 0;
208    ssize_t            ret = 0;
209    uint32_t           cmpltd = 0;
210    uint32_t           j = 0, i = 0;
211    uint32_t           bts2rd = 0;
212    uint32_t           cur_cln = 0;
213
214    /*
215     * cast start and count - protect against using sizes that are not exact
216     * multiples of the -dirent- size. These could result in unexpected
217     * results
218     */
219    start = iop->offset / sizeof(struct dirent);
220    count = (count / sizeof(struct dirent)) * sizeof(struct dirent);
221
222    /*
223     * optimization: we know that root directory for FAT12/16 volumes is
224     * sequential set of sectors and any cluster is sequential set of sectors
225     * too, so read such set of sectors is quick operation for low-level IO
226     * layer.
227     */
228    bts2rd = (FAT_FD_OF_ROOT_DIR(fat_fd) &&
229             (fs_info->fat.vol.type & (FAT_FAT12 | FAT_FAT16))) ?
230             fat_fd->fat_file_size                              :
231             fs_info->fat.vol.bpc;
232
233    sc = rtems_semaphore_obtain(fs_info->vol_sema, RTEMS_WAIT,
234                                MSDOS_VOLUME_SEMAPHORE_TIMEOUT);
235    if (sc != RTEMS_SUCCESSFUL)
236        set_errno_and_return_minus_one(EIO);
237
238    while (count > 0)
239    {
240        /*
241         * fat-file is already opened by open call, so read it
242         * Always read directory fat-file from the beggining because of MSDOS
243         * directories feature :( - we should count elements currently
244         * present in the directory because there may be holes :)
245         */
246        ret = fat_file_read(iop->pathinfo.mt_entry, fat_fd, (j * bts2rd),
247                            bts2rd, fs_info->cl_buf);
248        if (ret < MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE)
249        {
250            rtems_semaphore_release(fs_info->vol_sema);
251            set_errno_and_return_minus_one(EIO);
252        }
253
254        for (i = 0; i < ret; i += MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE)
255        {
256            if ((*MSDOS_DIR_NAME(fs_info->cl_buf + i)) ==
257                MSDOS_THIS_DIR_ENTRY_AND_REST_EMPTY)
258            {
259                rtems_semaphore_release(fs_info->vol_sema);
260                return cmpltd;
261            }
262
263            /* have to look at the DIR_NAME as "raw" 8-bit data */
264            if ((*(uint8_t *)MSDOS_DIR_NAME(fs_info->cl_buf + i)) ==
265                MSDOS_THIS_DIR_ENTRY_EMPTY)
266                continue;
267
268            /*
269             * skip active entries until get the entry to start from
270             */
271            if (start)
272            {
273                start--;
274                continue;
275            }
276
277            /*
278             * Move the entry to the return buffer
279             *
280             * unfortunately there is no method to extract ino except to
281             * open fat-file descriptor :( ... so, open it
282             */
283
284            /* get number of cluster we are working with */
285            rc = fat_file_ioctl(iop->pathinfo.mt_entry, fat_fd, F_CLU_NUM,
286                                j * bts2rd, &cur_cln);
287            if (rc != RC_OK)
288            {
289                rtems_semaphore_release(fs_info->vol_sema);
290                return rc;
291            }
292
293            rc = fat_file_open(iop->pathinfo.mt_entry, cur_cln, i,
294                               &tmp_fat_fd);
295            if (rc != RC_OK)
296            {
297                rtems_semaphore_release(fs_info->vol_sema);
298                return rc;
299            }
300
301            tmp_fat_fd->info_cln = cur_cln;
302            tmp_fat_fd->info_ofs = i;
303
304            /* fill in dirent structure */
305            /* XXX: from what and in what d_off should be computed ?! */
306            tmp_dirent.d_off = start + cmpltd;
307            tmp_dirent.d_reclen = sizeof(struct dirent);
308            tmp_dirent.d_ino = tmp_fat_fd->ino;
309            /*
310             * convert dir entry from fixed 8+3 format (without dot)
311             * to 0..8 + 1dot + 0..3 format
312             */
313            tmp_dirent.d_namlen = msdos_format_dirent_with_dot(
314              tmp_dirent.d_name,
315              (char *) fs_info->cl_buf + i); /* src text */
316            memcpy(buffer + cmpltd, &tmp_dirent, sizeof(struct dirent));
317
318            iop->offset = iop->offset + sizeof(struct dirent);
319            cmpltd += (sizeof(struct dirent));
320            count -= (sizeof(struct dirent));
321
322            /* inode number extracted, close fat-file */
323            rc = fat_file_close(iop->pathinfo.mt_entry, tmp_fat_fd);
324            if (rc != RC_OK)
325            {
326                rtems_semaphore_release(fs_info->vol_sema);
327                return rc;
328            }
329
330            if (count <= 0)
331                break;
332        }
333        j++;
334    }
335
336    rtems_semaphore_release(fs_info->vol_sema);
337    return cmpltd;
338}
339
340/* msdos_dir_write --
341 *     no write for directory
342 */
343
344/* msdos_dir_lseek --
345 *
346 *  This routine will behave in one of three ways based on the state of
347 *  argument whence. Based on the state of its value the offset argument will
348 *  be interpreted using one of the following methods:
349 *
350 *     SEEK_SET - offset is the absolute byte offset from the start of the
351 *                logical start of the dirent sequence that represents the
352 *                directory
353 *     SEEK_CUR - offset is used as the relative byte offset from the current
354 *                directory position index held in the iop structure
355 *     SEEK_END - N/A --> This will cause an assert.
356 *
357 * PARAMETERS:
358 *     iop    - file control block
359 *     offset - offset
360 *     whence - predefine directive
361 *
362 * RETURNS:
363 *     RC_OK on success, or -1 if error occured (errno
364 *     set apropriately).
365 */
366int
367msdos_dir_lseek(rtems_libio_t *iop, off_t offset, int whence)
368{
369    switch (whence)
370    {
371        case SEEK_SET:
372        case SEEK_CUR:
373            break;
374        /*
375         * Movement past the end of the directory via lseek is not a
376         * permitted operation
377         */
378        case SEEK_END:
379        default:
380            set_errno_and_return_minus_one( EINVAL );
381            break;
382    }
383    return RC_OK;
384}
385
386/* msdos_dir_stat --
387 *
388 * This routine will obtain the following information concerning the current
389 * directory:
390 *     st_dev      device id
391 *     st_ino      node serial number :)
392 *     st_mode     mode extracted from the node
393 *     st_size     total size in bytes
394 *     st_blksize  blocksize for filesystem I/O
395 *     st_blocks   number of blocks allocated
396 *     stat_mtime  time of last modification
397 *
398 * PARAMETERS:
399 *     loc - this directory
400 *     buf - stat buffer provided by user
401 *
402 * RETURNS:
403 *     RC_OK and filled stat buffer on success, or -1 if error occured (errno
404 *     set apropriately).
405 */
406int
407msdos_dir_stat(
408    rtems_filesystem_location_info_t *loc,
409    struct stat                      *buf
410    )
411{
412    rtems_status_code  sc = RTEMS_SUCCESSFUL;
413    msdos_fs_info_t   *fs_info = loc->mt_entry->fs_info;
414    fat_file_fd_t     *fat_fd = loc->node_access;
415
416    sc = rtems_semaphore_obtain(fs_info->vol_sema, RTEMS_WAIT,
417                                MSDOS_VOLUME_SEMAPHORE_TIMEOUT);
418    if (sc != RTEMS_SUCCESSFUL)
419        set_errno_and_return_minus_one(EIO);
420
421    buf->st_dev = fs_info->fat.vol.dev;
422    buf->st_ino = fat_fd->ino;
423    buf->st_mode  = S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO;
424    buf->st_rdev = 0ll;
425    buf->st_size = fat_fd->fat_file_size;
426    buf->st_blocks = fat_fd->fat_file_size >> FAT_SECTOR512_BITS;
427    buf->st_blksize = fs_info->fat.vol.bps;
428    buf->st_mtime = fat_fd->mtime;
429
430    rtems_semaphore_release(fs_info->vol_sema);
431    return RC_OK;
432}
433
434/* msdos_dir_truncate --
435 *     No truncate for directory.
436 *
437 * PARAMETERS:
438 *
439 * RETURNS:
440 *
441 */
442
443/* msdos_dir_sync --
444 *     The following routine does a syncronization on a MSDOS directory node.
445 *     DIR_WrtTime, DIR_WrtDate and DIR_fileSize fields of 32 Bytes Directory
446 *     Entry Structure should not be updated for directories, so only call
447 *     to corresponding fat-file routine.
448 *
449 * PARAMETERS:
450 *     iop - file control block
451 *
452 * RETURNS:
453 *     RC_OK on success, or -1 if error occured (errno set apropriately).
454 */
455int
456msdos_dir_sync(rtems_libio_t *iop)
457{
458    int                rc = RC_OK;
459    rtems_status_code  sc = RTEMS_SUCCESSFUL;
460    fat_file_fd_t     *fat_fd = iop->file_info;
461    msdos_fs_info_t   *fs_info = iop->pathinfo.mt_entry->fs_info;
462
463    sc = rtems_semaphore_obtain(fs_info->vol_sema, RTEMS_WAIT,
464                                MSDOS_VOLUME_SEMAPHORE_TIMEOUT);
465    if (sc != RTEMS_SUCCESSFUL)
466        set_errno_and_return_minus_one(EIO);
467
468    rc = fat_file_datasync(iop->pathinfo.mt_entry, fat_fd);
469
470    rtems_semaphore_release(fs_info->vol_sema);
471    return rc;
472}
473
474/* msdos_dir_rmnod --
475 *     Remove directory node.
476 *
477 *     Check that this directory node is not opened as fat-file, is empty and
478 *     not filesystem root node. If all this conditions met then delete.
479 *
480 * PARAMETERS:
481 *     pathloc - node description
482 *
483 * RETURNS:
484 *     RC_OK on success, or -1 if error occured (errno set apropriately).
485 */
486int
487msdos_dir_rmnod(rtems_filesystem_location_info_t *pathloc)
488{
489    int                rc = RC_OK;
490    rtems_status_code  sc = RTEMS_SUCCESSFUL;
491    msdos_fs_info_t   *fs_info = pathloc->mt_entry->fs_info;
492    fat_file_fd_t     *fat_fd = pathloc->node_access;
493    rtems_boolean      is_empty = FALSE;
494
495    sc = rtems_semaphore_obtain(fs_info->vol_sema, RTEMS_WAIT,
496                                MSDOS_VOLUME_SEMAPHORE_TIMEOUT);
497    if (sc != RTEMS_SUCCESSFUL)
498        set_errno_and_return_minus_one(EIO);
499
500    /*
501     * We deny attemp to delete open directory (if directory is current
502     * directory we assume it is open one)
503     */
504    if (fat_fd->links_num > 1)
505    {
506        rtems_semaphore_release(fs_info->vol_sema);
507        set_errno_and_return_minus_one(EBUSY);
508    }
509
510    /*
511     * You cannot remove a node that still has children
512     */
513    rc = msdos_dir_is_empty(pathloc->mt_entry, fat_fd, &is_empty);
514    if (rc != RC_OK)
515    {
516        rtems_semaphore_release(fs_info->vol_sema);
517        return rc;
518    }
519
520    if (!is_empty)
521    {
522        rtems_semaphore_release(fs_info->vol_sema);
523        set_errno_and_return_minus_one(ENOTEMPTY);
524    }
525
526    /*
527     * You cannot remove the file system root node.
528     */
529    if (pathloc->mt_entry->mt_fs_root.node_access == pathloc->node_access)
530    {
531        rtems_semaphore_release(fs_info->vol_sema);
532        set_errno_and_return_minus_one(EBUSY);
533    }
534
535    /*
536     * You cannot remove a mountpoint.
537     * not used - mount() not implemenetd yet.
538     */
539
540    /* mark file removed */
541    rc = msdos_set_first_char4file_name(pathloc->mt_entry, fat_fd->info_cln,
542                                        fat_fd->info_ofs,
543                                        MSDOS_THIS_DIR_ENTRY_EMPTY);
544    if (rc != RC_OK)
545    {
546        rtems_semaphore_release(fs_info->vol_sema);
547        return rc;
548    }
549
550    fat_file_mark_removed(pathloc->mt_entry, fat_fd);
551
552    rtems_semaphore_release(fs_info->vol_sema);
553    return rc;
554}
Note: See TracBrowser for help on using the repository browser.