source: rtems/cpukit/include/rtems/dosfs.h @ 87fdb20

Last change on this file since 87fdb20 was 87fdb20, checked in by Christian Mauderer <christian.mauderer@…>, on 08/03/20 at 12:18:33

dosfs: Fix memory leak on failed mounts.

Currently if mount fails, a converter isn't destroyed. We have to take
care of two cases:

  1. The user doesn't provide a converter.

In this case mounting a dosfs creates a default converter. This patch
makes sure that the converter is destroyed again if mount failes for
this case.

  1. The user provides a converter.

In this case it's not sure that the dosfs specific routines are reached
because mount can fail before that. Therefore the user has to destroy
the converter himself again. This patch adds a documentation for that
and implements it in the media server.

Closes #4042.

  • Property mode set to 100644
File size: 14.0 KB
Line 
1/**
2 * @file
3 *
4 * @brief Application Interface to FAT Filesystem
5 *
6 * @ingroup DOSFS
7 */
8
9/*
10 *  Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
11 *  Author: Eugeny S. Mints <Eugeny.Mints@oktet.ru>
12 *
13 *  Modifications to support UTF-8 in the file system are
14 *  Copyright (c) 2013 embedded brains GmbH.
15 *
16 *  The license and distribution terms for this file may be
17 *  found in the file LICENSE in this distribution or at
18 *  http://www.rtems.org/license/LICENSE.
19 */
20
21#ifndef _RTEMS_DOSFS_H
22#define _RTEMS_DOSFS_H
23
24#include <rtems.h>
25#include <rtems/libio.h>
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31typedef struct rtems_dosfs_convert_control rtems_dosfs_convert_control;
32
33/**
34 * @brief Converts from UTF-8 into a specific code page.
35 *
36 * @param[in,out] self The convert control.
37 * @param[in] src A well-formed UTF-8 string to be converted.
38 * @param[in] src_size The size of the string in bytes (inludes '\\0' if any).
39 * @param[out] dst The address the converted string will get copied to.
40 * @param[in,out] dst_size The size of the buffer in bytes respectively the
41 * number of bytes written to the buffer.
42 *
43 * @retval 0 Successful operation.
44 * @retval EINVAL Conversion was successful, but is not reversible.
45 * @retval ENOMEM Conversion failed (possibly due to insufficient buffer size).
46 */
47typedef int (*rtems_dosfs_utf8_to_codepage)(
48  rtems_dosfs_convert_control *self,
49  const uint8_t               *src,
50  size_t                       src_size,
51  char                        *dst,
52  size_t                      *dst_size
53);
54
55/**
56 * @brief Converts from a specific code page into UTF-8
57 *
58 * @param[in,out] self The convert control.
59 * @param[in] src A well-formed string in code page format.
60 * @param[in] src_size The size of the string in bytes (inludes '\\0' if any).
61 * @param[out] dst The address the converted string will get copied to.
62 * @param[in,out] dst_size The size of the buffer in bytes respectively the
63 * number of bytes written to the buffer.
64 *
65 * @retval 0 Successful operation.
66 * @retval EINVAL Conversion was successful, but is not reversible.
67 * @retval ENOMEM Conversion failed (possibly due to insufficient buffer size).
68 */
69typedef int (*rtems_dosfs_codepage_to_utf8)(
70  rtems_dosfs_convert_control *self,
71  const char                  *src,
72  size_t                       src_size,
73  uint8_t                     *dst,
74  size_t                      *dst_size
75);
76
77/**
78 * @brief Converts from UTF-8 to UTF-16
79 *
80 * @param[in,out] self The convert control.
81 * @param[in] src A well-formed UTF-8 string to be converted.
82 * @param[in] src_size The size of the string in bytes (inludes '\\0' if any).
83 * @param[out] dst The address the converted string will get copied to
84 * @param[in,out] dst_size The size of the buffer in bytes respectively the
85 * number of bytes written to the buffer.
86 *
87 * @retval 0 Successful operation.
88 * @retval EINVAL Conversion was successful, but is not reversible.
89 * @retval ENOMEM Conversion failed (possibly due to insufficient buffer size).
90 */
91typedef int (*rtems_dosfs_utf8_to_utf16)(
92  rtems_dosfs_convert_control *self,
93  const uint8_t               *src,
94  size_t                       src_size,
95  uint16_t                    *dst,
96  size_t                      *dst_size
97);
98
99/**
100 * @brief Converts from UTF-16 to UTF-8.
101 *
102 * @param[in,out] self The convert control.
103 * @param[in] src A well-formed UTF-16 string to be converted.
104 * @param[in] src_size The size of the string in bytes (inludes '\\0' if any).
105 * @param[out] dst The address the converted string will get copied to.
106 * @param[in,out] dst_size The size of the buffer in bytes respectively the
107 * number of bytes written to the buffer
108 *
109 * @retval 0 Successful operation.
110 * @retval EINVAL Conversion was successful, but is not reversible.
111 * @retval ENOMEM Conversion failed (possibly due to insufficient buffer size).
112 */
113typedef int (*rtems_dosfs_utf16_to_utf8)(
114  rtems_dosfs_convert_control *self,
115  const uint16_t              *src,
116  size_t                       src_size,
117  uint8_t                     *dst,
118  size_t                      *dst_size
119);
120
121/**
122 * @brief Converts from UTF-8 to Normalized Form Canonical Decomposition.
123 *
124 * Does canonical decomposition of the UTF-8 string and in addition
125 * also converts upper case alphabetic characters to lower case characters
126 *
127 * @param[in,out] self The convert control.
128 * @param[in] src A well-formed UTF-8 string to be normalized and fold.
129 * @param[in] src_size The size of the string in bytes (inludes '\\0' if any).
130 * @param[out] dst The address the normalized and fold string will get
131 * copied to.
132 * @param[in,out] dst_size The size of the buffer in bytes respectively the
133 * number of bytes written to the buffer.
134 *
135 * @retval 0 Successful operation.
136 * @retval EINVAL Conversion failed.
137 * @retval ENOMEM Conversion failed (possibly due to insufficient buffer size).
138 * @retval EOVERFLOW Conversion failed.
139 * @retval ENOENT Conversion failed.
140 */
141typedef int (*rtems_dosfs_utf8_normalize_and_fold)(
142  rtems_dosfs_convert_control *self,
143  const uint8_t               *src,
144  size_t                       src_size,
145  uint8_t                     *dst,
146  size_t                      *dst_size
147);
148
149/**
150 * @brief Destroys a convert control structure.
151 *
152 * @param[in,out] self The convert control for destruction.
153 */
154typedef void (*rtems_dosfs_convert_destroy)(
155  rtems_dosfs_convert_control *self
156);
157
158/**
159 * @brief FAT filesystem convert handler.
160 */
161typedef struct {
162  rtems_dosfs_utf8_to_codepage        utf8_to_codepage;
163  rtems_dosfs_codepage_to_utf8        codepage_to_utf8;
164  rtems_dosfs_utf8_to_utf16           utf8_to_utf16;
165  rtems_dosfs_utf16_to_utf8           utf16_to_utf8;
166  rtems_dosfs_utf8_normalize_and_fold utf8_normalize_and_fold;
167  rtems_dosfs_convert_destroy         destroy;
168} rtems_dosfs_convert_handler;
169
170typedef struct {
171  void   *data;
172  size_t  size;
173} rtems_dosfs_buffer;
174
175/**
176 * @brief FAT filesystem convert control.
177 *
178 * Short file names are stored in the code page format.  Long file names are
179 * stored as little-endian UTF-16.  The convert control determines the format
180 * conversions to and from the POSIX file name strings.
181 */
182struct rtems_dosfs_convert_control {
183  const rtems_dosfs_convert_handler *handler;
184  rtems_dosfs_buffer                 buffer;
185};
186
187/**
188 * @defgroup DOSFS FAT Filesystem Support
189 *
190 * @ingroup FileSystemTypesAndMount
191 *
192 * @brief FAT file system configuration support, format and mount options.
193 *
194 * A block device can be formatted with a FAT file system with the
195 * msdos_format() function.
196 *
197 * The FAT file system mount operation can be controlled with FAT file system
198 * specific mount options, see @ref rtems_dosfs_mount_options.
199 *
200 * @{
201 */
202
203/**
204 * @brief Semaphore count per FAT filesystem instance.
205 *
206 * This can be used for system configuration via <rtems/confdefs.h>.
207 */
208#define RTEMS_DOSFS_SEMAPHORES_PER_INSTANCE 1
209
210/**
211 * @brief FAT filesystem mount options.
212 */
213typedef struct {
214  /**
215   * @brief Converter implementation for new file system instance.
216   *
217   * Note: If you pass a converter to mount, you have to destroy it yourself if
218   * mount failed. In a good case it is destroyed at unmount.
219   *
220   * Before converters have been added to the RTEMS implementation of the FAT
221   * file system, the implementation was:
222   * - Short names were saved in code page format (as is still the case).
223   * - Long names were not saved in UTF-16 format as mandated by the FAT file
224   *   system specification.  Instead the character in the local encoding was
225   *   stored to the low byte directly and the high byte was set to zero.
226   *
227   * There are a few compatibility issues due to a non-standard conform
228   * implementation of the FAT file system before the UTF-8 support was added.
229   * These following issues affect the default converter and the UTF-8
230   * converter:
231   * - Before UTF-8 support was added, it was possible to create files with the
232   *   the same short name in single case and mixed case in a directory.  It
233   *   was for example possible to have files "ABC" and "aBc" in a single
234   *   directory.  Now this bug is fixed.
235   * - Before UTF-8 support was added, it was possible to create files with a
236   *   name length of slightly more than 255 characters.  Now the
237   *   implementation adheres exactly to the 255 character limit.
238   * - Long file names saved before UTF-8 support was added could contain
239   *   non-ASCII characters in the low byte which was saved for a long name
240   *   character.  With the default converter this means such files can be read
241   *   only by their short file name.  With the UTF-8 converter file names will
242   *   be read correctly as long as the characters written with the old
243   *   implementation were Latin-1 characters.
244   *
245   * The following sample code demonstrates how to mount a file
246   * system with UTF-8 support:
247   * @code
248   * #include <errno.h>
249   * #include <assert.h>
250   * #include <rtems/dosfs.h>
251   * #include <rtems/libio.h>
252   *
253   * static int mount_with_utf8(
254   *   const char *device_file,
255   *   const char *mount_point
256   * )
257   * {
258   *   rtems_dosfs_convert_control *convert_ctrl;
259   *   int                          rv;
260   *
261   *   convert_ctrl = rtems_dosfs_create_utf8_converter( "CP850" );
262   *
263   *   if ( convert_ctrl != NULL ) {
264   *     rtems_dosfs_mount_options mount_opts;
265   *
266   *     memset( &mount_opts, 0, sizeof( mount_opts ) );
267   *     mount_opts.converter = convert_ctrl;
268   *
269   *     rv = mount_and_make_target_path(
270   *       device_file,
271   *       mount_point,
272   *       RTEMS_FILESYSTEM_TYPE_DOSFS,
273   *       RTEMS_FILESYSTEM_READ_WRITE,
274   *       &mount_opts
275   *     );
276   *
277   *     if (rv != 0) {
278   *       (*mount_opts.converter->handler->destroy)(mount_opts.converter);
279   *     }
280   *   } else {
281   *     rv = -1;
282   *     errno = ENOMEM;
283   *   }
284   *
285   *   return rv;
286   * }
287   * @endcode
288   *
289   * In case you do not want UTF-8 support, you can simply pass a NULL pointer
290   * to mount_and_make_target_path() respectively to mount() instead of the
291   * mount_opts address.
292   *
293   * @see rtems_dosfs_create_default_converter() and
294   * rtems_dosfs_create_utf8_converter().
295   */
296  rtems_dosfs_convert_control *converter;
297} rtems_dosfs_mount_options;
298
299/**
300 * @brief Allocates and initializes a default converter.
301 *
302 * This default converter will accept only POSIX file names with pure ASCII
303 * characters. This largely corresponds to the file name handling before the
304 * optional UTF-8 support was added to the RTEMS implementation of the FAT file
305 * system.  This handling is mostly backwards compatible to the previous RTEMS
306 * implementation of the FAT file system.
307 *
308 * For backwards compatibility and the previous RTEMS implementation of the FAT
309 * file system please see also @ref rtems_dosfs_mount_options and mount().
310 *
311 * @retval NULL Something failed.
312 * @retval other Pointer to initialized converter.
313 */
314rtems_dosfs_convert_control *rtems_dosfs_create_default_converter(void);
315
316/**
317 * @brief Allocates and initializes a UTF-8 converter.
318 *
319 * This converter will assume that all file names passed to POSIX file handling
320 * methods are UTF-8 strings and will convert them to the selected code page
321 * for short file names and to UTF-16 for long file names.  This conversion
322 * will be done during reading and writing.  These conversions correspond to
323 * the specification of the FAT file system.  This handling is mostly backwards
324 * compatible to the previous RTEMS implementation of the FAT file system.
325 *
326 * For backwards compatibility and the previous RTEMS implementation of the FAT
327 * file system please see also @ref rtems_dosfs_mount_options and mount().
328 *
329 * One possible issue with this converter is: When reading file names which
330 * have been created with other implementations of the FAT file system, it can
331 * happen that during the conversion to UTF-8 a long file name becomes longer
332 * and exceeds the 255 bytes limit.  In such a case only the short file name
333 * will get read.
334 *
335 * @param[in] codepage The iconv() identification string for the used code
336 * page.
337 *
338 * @retval NULL Something failed.
339 * @retval other Pointer to initialized converter.
340 */
341rtems_dosfs_convert_control *rtems_dosfs_create_utf8_converter(
342  const char *codepage
343);
344
345#define MSDOS_FMT_INFO_LEVEL_NONE   (0)
346#define MSDOS_FMT_INFO_LEVEL_INFO   (1)
347#define MSDOS_FMT_INFO_LEVEL_DETAIL (2)
348#define MSDOS_FMT_INFO_LEVEL_DEBUG  (3)
349
350/**
351 * @brief FAT file system format request parameters.
352 */
353typedef struct {
354  /**
355   * @brief OEM name string or NULL.
356   */
357  const char *OEMName;
358
359  /**
360   * @brief Volume label string or NULL.
361   */
362  const char *VolLabel;
363
364  /**
365   * @brief Sectors per cluster hint.
366   *
367   * The format procedure may choose another value.  Use 0 as default value.
368   */
369  uint32_t sectors_per_cluster;
370
371  /**
372   * @brief Number of FATs hint.
373   *
374   * Use 0 as default value.
375   */
376  uint32_t fat_num;
377
378  /**
379   * @brief Minimum files in root directory for FAT12 and FAT16.
380   *
381   * The format procedure may choose a greater value.  Use 0 as default value.
382   */
383  uint32_t files_per_root_dir;
384
385  /**
386   * @brief Media code.
387   *
388   * Use 0 as default value.  The default media code is 0xf8.
389   */
390  uint8_t media;
391
392  /**
393   * @brief Quick format.
394   *
395   * If set to true, then do not clear data sectors to zero.
396   */
397  bool quick_format;
398
399  /**
400   * @brief Do not align FAT, data cluster, and root directory to a cluster
401   * boundary.
402   */
403  bool skip_alignment;
404
405  /**
406   * @brief Synchronize device after write operations.
407   */
408  bool sync_device;
409
410  /**
411   * @brief The amount of info to output.
412   */
413  int info_level;
414} msdos_format_request_param_t;
415
416/**
417 * @brief Formats a block device with a FAT file system.
418 *
419 * @param[in] devname The block device path.
420 * @param[in] rqdata The FAT file system format request data.  Use NULL for
421 * default parameters.
422 *
423 * @retval 0 Successful operation.
424 * @retval -1 An error occurred.  The @c errno indicates the error.
425 */
426int msdos_format (
427  const char *devname,
428  const msdos_format_request_param_t *rqdata
429);
430
431/** @} */
432
433int rtems_dosfs_initialize(rtems_filesystem_mount_table_entry_t *mt_entry,
434                           const void                           *data);
435
436#ifdef __cplusplus
437}
438#endif
439
440#endif
Note: See TracBrowser for help on using the repository browser.