source: rtems/cpukit/libfs/src/dosfs/msdos_create.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: 9.2 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup libfs_msdos MSDOS FileSystem
5 *
6 * @brief Create a new MSDOS FileSystem node
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 */
18#ifdef HAVE_CONFIG_H
19#include "config.h"
20#endif
21
22#include <errno.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <rtems/libio_.h>
27#include <time.h>
28
29#include "fat.h"
30#include "fat_fat_operations.h"
31#include "fat_file.h"
32
33#include "msdos.h"
34
35/* msdos_creat_node --
36 *     Create a new node. Determine if the name is a long name. If long we to
37 *     scan the directory to create a short entry.
38 *
39 *
40
41
42
43 *     If a new node is file, FAT 32 Bytes Directory
44 *     Entry Structure is initialized, free space is found in parent
45 *     directory and structure is written to the disk. In case of directory,
46 *     all above steps present and also new cluster is allocated for a
47 *     new directory and dot and dotdot nodes are created in alloceted cluster.
48 *
49 * PARAMETERS:
50 *     parent_loc - parent (directory we are going to create node in)
51 *     type       - new node type (file or directory)
52 *     name       - new node name
53 *     mode       - mode
54 *     link_info  - fs_info of existing node for a pseudo "hard-link"
55 *                  (see msdos_file.c, msdos_link for documentation)
56 *
57 * RETURNS:
58 *     RC_OK on success, or -1 if error occured (errno set appropriately).
59 *
60 */
61int
62msdos_creat_node(const rtems_filesystem_location_info_t  *parent_loc,
63                 fat_file_type_t                          type,
64                 const char                              *name,
65                 int                                      name_len,
66                 mode_t                                   mode,
67                 const fat_file_fd_t                     *link_fd)
68{
69    int               rc = RC_OK;
70    ssize_t           ret = 0;
71    msdos_fs_info_t  *fs_info = parent_loc->mt_entry->fs_info;
72    fat_file_fd_t    *parent_fat_fd = parent_loc->node_access;
73    fat_file_fd_t    *fat_fd = NULL;
74    time_t            now;
75    uint16_t          time_val = 0;
76    uint16_t          date = 0;
77    fat_dir_pos_t     dir_pos;
78    msdos_name_type_t name_type;
79    char              short_node[MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE];
80    char              dot_dotdot[MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE * 2];
81    char              link_node[MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE];
82    uint32_t          sec = 0;
83    uint32_t          byte = 0;
84
85    fat_dir_pos_init(&dir_pos);
86
87    memset(short_node, 0, MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE);
88    memset(dot_dotdot, 0, MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE * 2);
89
90    if (name_len > MSDOS_NAME_MAX_LFN_WITH_DOT) {
91        rtems_set_errno_and_return_minus_one(ENAMETOOLONG);
92    }
93
94    name_type = msdos_long_to_short (fs_info->converter,
95                                     name, name_len,
96                                     MSDOS_DIR_NAME(short_node),
97                                     MSDOS_NAME_MAX);
98    if (name_type == MSDOS_NAME_INVALID) {
99        rtems_set_errno_and_return_minus_one(EINVAL);
100    }
101
102    /* fill reserved field */
103    *MSDOS_DIR_NT_RES(short_node) = MSDOS_RES_NT_VALUE;
104
105    /* set up last write date and time */
106    now = time(NULL);
107    fat_file_set_ctime_mtime(parent_fat_fd, now);
108
109    msdos_date_unix2dos(now, &date, &time_val);
110    *MSDOS_DIR_CRT_TIME(short_node) = CT_LE_W(time_val);
111    *MSDOS_DIR_CRT_DATE(short_node) = CT_LE_W(date);
112    *MSDOS_DIR_WRITE_TIME(short_node) = CT_LE_W(time_val);
113    *MSDOS_DIR_WRITE_DATE(short_node) = CT_LE_W(date);
114    *MSDOS_DIR_LAST_ACCESS_DATE(short_node) = CT_LE_W(date);
115
116    /* initialize directory/file size */
117    *MSDOS_DIR_FILE_SIZE(short_node) = MSDOS_INIT_DIR_SIZE;
118
119    if (type == FAT_DIRECTORY) {
120      *MSDOS_DIR_ATTR(short_node) |= MSDOS_ATTR_DIRECTORY;
121    }
122    else if (type == FAT_HARD_LINK) {
123      /*
124       * when we establish a (temporary) hard link,
125       * we must copy some information from the original
126       * node to the newly created
127       */
128      /*
129       * read the original directory entry
130       */
131      sec = fat_cluster_num_to_sector_num(&fs_info->fat,
132                                          link_fd->dir_pos.sname.cln);
133      sec += (link_fd->dir_pos.sname.ofs >> fs_info->fat.vol.sec_log2);
134      byte = (link_fd->dir_pos.sname.ofs & (fs_info->fat.vol.bps - 1));
135
136      ret = _fat_block_read(&fs_info->fat,
137                            sec, byte, MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE,
138                            link_node);
139      if (ret < 0) {
140          return -1;
141      }
142      /*
143       * copy various attributes
144       */
145      *MSDOS_DIR_ATTR(short_node)          =*MSDOS_DIR_ATTR(link_node);
146      *MSDOS_DIR_CRT_TIME_TENTH(short_node)=*MSDOS_DIR_CRT_TIME_TENTH(link_node);
147      *MSDOS_DIR_CRT_TIME(short_node)      =*MSDOS_DIR_CRT_TIME(link_node);
148      *MSDOS_DIR_CRT_DATE(short_node)      =*MSDOS_DIR_CRT_DATE(link_node);
149
150      /*
151       * copy/set "file size", "first cluster"
152       */
153      *MSDOS_DIR_FILE_SIZE(short_node)     =*MSDOS_DIR_FILE_SIZE(link_node);
154
155      *MSDOS_DIR_FIRST_CLUSTER_LOW(short_node) =
156           *MSDOS_DIR_FIRST_CLUSTER_LOW(link_node);
157      *MSDOS_DIR_FIRST_CLUSTER_HI(short_node) =
158           *MSDOS_DIR_FIRST_CLUSTER_HI(link_node);
159      /*
160       * set "archive bit" due to changes
161       */
162      *MSDOS_DIR_ATTR(short_node) |= MSDOS_ATTR_ARCHIVE;
163    }
164    else { /* regular file... */
165        *MSDOS_DIR_ATTR(short_node) |= MSDOS_ATTR_ARCHIVE;
166    }
167
168    /*
169     * find free space in the parent directory and write new initialized
170     * FAT 32 Bytes Directory Entry Structure to the disk
171     */
172    rc = msdos_get_name_node(parent_loc, true, name, name_len,
173                             name_type, &dir_pos, short_node);
174    if ( rc != RC_OK )
175        return rc;
176
177    /*
178     * if we create a new file we are done, if directory there are more steps
179     * to do
180     */
181    if (type == FAT_DIRECTORY)
182    {
183        uint32_t unused;
184
185        /* open new directory as fat-file */
186        rc = fat_file_open(&fs_info->fat, &dir_pos, &fat_fd);
187        if (rc != RC_OK)
188            goto err;
189
190        /*
191         * we opened fat-file for node we just created, so initialize fat-file
192         * descritor
193         */
194        fat_fd->fat_file_type = FAT_DIRECTORY;
195        fat_fd->size_limit = MSDOS_MAX_DIR_LENGTH;
196        fat_file_set_ctime_mtime(fat_fd, now);
197
198        /* extend it to contain exactly one cluster */
199        rc = fat_file_extend(&fs_info->fat,
200                             fat_fd,
201                             true,
202                             fs_info->fat.vol.bpc,
203                             &unused);
204        if (rc != RC_OK)
205            goto err;
206
207        /*
208         * dot and dotdot entries are identical to new node except the
209         * names
210         */
211        memcpy(DOT_NODE_P(dot_dotdot), short_node,
212               MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE);
213        memcpy(DOTDOT_NODE_P(dot_dotdot), short_node,
214               MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE);
215        memcpy(MSDOS_DIR_NAME(DOT_NODE_P(dot_dotdot)), MSDOS_DOT_NAME,
216               MSDOS_NAME_MAX);
217        memcpy(MSDOS_DIR_NAME(DOTDOT_NODE_P(dot_dotdot)), MSDOS_DOTDOT_NAME,
218               MSDOS_NAME_MAX);
219
220        /* set up cluster num for dotdot entry */
221        /*
222         * here we can ommit FAT32 condition because for all FAT types dirs
223         * right under root dir should contain 0 in dotdot entry but for
224         * FAT12/16 parent_fat_fd->cluster_num always contains such value
225         */
226        if ((FAT_FD_OF_ROOT_DIR(parent_fat_fd)) &&
227            (fs_info->fat.vol.type & FAT_FAT32))
228        {
229            *MSDOS_DIR_FIRST_CLUSTER_LOW(DOTDOT_NODE_P(dot_dotdot)) = 0x0000;
230            *MSDOS_DIR_FIRST_CLUSTER_HI(DOTDOT_NODE_P(dot_dotdot)) = 0x0000;
231        }
232        else
233        {
234            *MSDOS_DIR_FIRST_CLUSTER_LOW(DOTDOT_NODE_P(dot_dotdot)) =
235                CT_LE_W((uint16_t  )((parent_fat_fd->cln) & 0x0000FFFF));
236            *MSDOS_DIR_FIRST_CLUSTER_HI(DOTDOT_NODE_P(dot_dotdot)) =
237                CT_LE_W((uint16_t  )(((parent_fat_fd->cln) & 0xFFFF0000)>>16));
238        }
239
240        /* set up cluster num for dot entry */
241        *MSDOS_DIR_FIRST_CLUSTER_LOW(DOT_NODE_P(dot_dotdot)) =
242                CT_LE_W((uint16_t  )((fat_fd->cln) & 0x0000FFFF));
243        *MSDOS_DIR_FIRST_CLUSTER_HI(DOT_NODE_P(dot_dotdot)) =
244                CT_LE_W((uint16_t  )(((fat_fd->cln) & 0xFFFF0000) >> 16));
245
246        /* write dot and dotdot entries */
247        ret = fat_file_write(&fs_info->fat, fat_fd, 0,
248                             MSDOS_DIRECTORY_ENTRY_STRUCT_SIZE * 2,
249                             (uint8_t *)dot_dotdot);
250        if (ret < 0)
251        {
252            rc = -1;
253            goto error;
254        }
255
256        /* write first cluster num of a new directory to disk */
257        rc = fat_file_write_first_cluster_num(&fs_info->fat, fat_fd);
258        if (rc != RC_OK)
259            goto error;
260
261        fat_file_close(&fs_info->fat, fat_fd);
262    }
263    return RC_OK;
264
265error:
266    fat_file_close(&fs_info->fat, fat_fd);
267
268err:
269    /* mark the used 32bytes structure on the disk as free */
270    msdos_set_first_char4file_name(parent_loc->mt_entry, &dir_pos, 0xE5);
271    return rc;
272}
Note: See TracBrowser for help on using the repository browser.