source: rtems/cpukit/libfs/src/dosfs/msdos_file.c @ cfe8f7a

5
Last change on this file since cfe8f7a was cfe8f7a, checked in by Sebastian Huber <sebastian.huber@…>, on 04/27/20 at 14:14:06

doxygen: Switch @brief and @ingroup

This order change fixes the Latex documentation build via Doxygen.

  • Property mode set to 100644
File size: 5.6 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup libfs
5 *
6 * @brief MSDOS File Handlers Implementation
7 */
8
9/*
10 *  Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
11 *  Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru>
12 *
13 *  The license and distribution terms for this file may be
14 *  found in the file LICENSE in this distribution or at
15 *  http://www.rtems.org/license/LICENSE.
16 */
17#ifdef HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include <stdlib.h>
22#include <errno.h>
23
24#include <rtems.h>
25#include <rtems/libio.h>
26#include <rtems/libio_.h>
27
28#include "fat.h"
29#include "fat_fat_operations.h"
30#include "fat_file.h"
31
32#include "msdos.h"
33
34/* msdos_file_read --
35 *     This routine read from file pointed to by file control block into
36 *     the specified data buffer provided by user
37 *
38 * PARAMETERS:
39 *     iop    - file control block
40 *     buffer - buffer  provided by user
41 *     count  - the number of bytes to read
42 *
43 * RETURNS:
44 *     the number of bytes read on success, or -1 if error occured (errno set
45 *     appropriately)
46 */
47ssize_t
48msdos_file_read(rtems_libio_t *iop, void *buffer, size_t count)
49{
50    ssize_t            ret = 0;
51    msdos_fs_info_t   *fs_info = iop->pathinfo.mt_entry->fs_info;
52    fat_file_fd_t     *fat_fd = iop->pathinfo.node_access;
53
54    msdos_fs_lock(fs_info);
55
56    ret = fat_file_read(&fs_info->fat, fat_fd, iop->offset, count,
57                        buffer);
58    if (ret > 0)
59        iop->offset += ret;
60
61    msdos_fs_unlock(fs_info);
62    return ret;
63}
64
65/* msdos_file_write --
66 *     This routine writes the specified data buffer into the file pointed to
67 *     by file control block.
68 *
69 * PARAMETERS:
70 *     iop    - file control block
71 *     buffer - data to write
72 *     count  - count of bytes to write
73 *
74 * RETURNS:
75 *     the number of bytes written on success, or -1 if error occured
76 *     and errno set appropriately
77 */
78ssize_t
79msdos_file_write(rtems_libio_t *iop,const void *buffer, size_t count)
80{
81    ssize_t            ret = 0;
82    msdos_fs_info_t   *fs_info = iop->pathinfo.mt_entry->fs_info;
83    fat_file_fd_t     *fat_fd = iop->pathinfo.node_access;
84
85    msdos_fs_lock(fs_info);
86
87    if (rtems_libio_iop_is_append(iop))
88        iop->offset = fat_fd->fat_file_size;
89
90    ret = fat_file_write(&fs_info->fat, fat_fd, iop->offset, count,
91                         buffer);
92    if (ret < 0)
93    {
94        msdos_fs_unlock(fs_info);
95        return -1;
96    }
97
98    /*
99     * update file size in both fat-file descriptor and file control block if
100     * file was extended
101     */
102    iop->offset += ret;
103    if (iop->offset > fat_fd->fat_file_size)
104        fat_file_set_file_size(fat_fd, (uint32_t) iop->offset);
105
106    if (ret > 0)
107        fat_file_set_ctime_mtime(fat_fd, time(NULL));
108
109    msdos_fs_unlock(fs_info);
110    return ret;
111}
112
113/* msdos_file_stat --
114 *
115 * PARAMETERS:
116 *     loc - node description
117 *     buf - stat buffer provided by user
118 *
119 * RETURNS:
120 *     RC_OK on success, or -1 if error occured (errno set appropriately)
121 */
122int
123msdos_file_stat(
124    const rtems_filesystem_location_info_t *loc,
125    struct stat *buf
126)
127{
128    msdos_fs_info_t   *fs_info = loc->mt_entry->fs_info;
129    fat_file_fd_t     *fat_fd = loc->node_access;
130    uint32_t           cl_mask = fs_info->fat.vol.bpc - 1;
131
132    msdos_fs_lock(fs_info);
133
134    buf->st_dev = fs_info->fat.vol.dev;
135    buf->st_ino = fat_fd->ino;
136    buf->st_mode  = S_IFREG | S_IRWXU | S_IRWXG | S_IRWXO;
137    buf->st_rdev = 0ll;
138    buf->st_size = fat_fd->fat_file_size;
139    buf->st_blocks = ((fat_fd->fat_file_size + cl_mask) & ~cl_mask)
140      >> FAT_SECTOR512_BITS;
141    buf->st_blksize = fs_info->fat.vol.bpc;
142    buf->st_atime = fat_fd->mtime;
143    buf->st_ctime = fat_fd->ctime;
144    buf->st_mtime = fat_fd->mtime;
145
146    msdos_fs_unlock(fs_info);
147    return RC_OK;
148}
149
150/* msdos_file_ftruncate --
151 *     Truncate the file.
152 *
153 * PARAMETERS:
154 *     iop    - file control block
155 *     length - new length
156 *
157 * RETURNS:
158 *     RC_OK on success, or -1 if error occured (errno set appropriately).
159 */
160int
161msdos_file_ftruncate(rtems_libio_t *iop, off_t length)
162{
163    int                rc = RC_OK;
164    msdos_fs_info_t   *fs_info = iop->pathinfo.mt_entry->fs_info;
165    fat_file_fd_t     *fat_fd = iop->pathinfo.node_access;
166    uint32_t old_length;
167
168    msdos_fs_lock(fs_info);
169
170    old_length = fat_fd->fat_file_size;
171    if (length < old_length) {
172        rc = fat_file_truncate(&fs_info->fat, fat_fd, length);
173    } else {
174        uint32_t new_length;
175
176        rc = fat_file_extend(&fs_info->fat,
177                             fat_fd,
178                             true,
179                             length,
180                             &new_length);
181        if (rc == RC_OK && length != new_length) {
182            fat_file_truncate(&fs_info->fat, fat_fd, old_length);
183            errno = ENOSPC;
184            rc = -1;
185        }
186    }
187
188    if (rc == RC_OK)
189    {
190        fat_file_set_file_size(fat_fd, length);
191        fat_file_set_ctime_mtime(fat_fd, time(NULL));
192    }
193
194    msdos_fs_unlock(fs_info);
195
196    return rc;
197}
198
199/* msdos_file_sync --
200 *     Synchronize file - synchronize file data and if file is not removed
201 *     synchronize file metadata.
202 *
203 * PARAMETERS:
204 *     iop - file control block
205 *
206 * RETURNS:
207 *     RC_OK on success, or -1 if error occured (errno set appropriately)
208 */
209int
210msdos_file_sync(rtems_libio_t *iop)
211{
212    int                rc = RC_OK;
213    msdos_fs_info_t   *fs_info = iop->pathinfo.mt_entry->fs_info;
214    fat_file_fd_t     *fat_fd = iop->pathinfo.node_access;
215
216    msdos_fs_lock(fs_info);
217
218    rc = fat_file_update(&fs_info->fat, fat_fd);
219    if (rc != RC_OK)
220    {
221        msdos_fs_unlock(fs_info);
222        return rc;
223    }
224
225    rc = fat_sync(&fs_info->fat);
226
227    msdos_fs_unlock(fs_info);
228
229    return RC_OK;
230}
Note: See TracBrowser for help on using the repository browser.