source: rtems/testsuites/fstests/fsdosfssync01/init.c @ 4b8e01f

4.11
Last change on this file since 4b8e01f was 4b8e01f, checked in by Sebastian Huber <sebastian.huber@…>, on Dec 13, 2012 at 10:08:03 AM

dosfs: Add sync_device option for msdos_format()

  • Property mode set to 100644
File size: 3.5 KB
Line 
1/*
2 * Copyright (c) 2012 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Obere Lagerstr. 30
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * The license and distribution terms for this file may be
11 * found in the file LICENSE in this distribution or at
12 * http://www.rtems.com/license/LICENSE.
13 */
14
15#ifdef HAVE_CONFIG_H
16  #include "config.h"
17#endif
18
19#include "tmacros.h"
20
21#include <sys/stat.h>
22#include <errno.h>
23#include <string.h>
24#include <stdint.h>
25#include <unistd.h>
26#include <fcntl.h>
27
28#include <rtems/libio.h>
29#include <rtems/blkdev.h>
30#include <rtems/dosfs.h>
31#include <rtems/ramdisk.h>
32
33static void create_file(const char *file)
34{
35  int fd;
36  int rv;
37
38  fd = creat(file, S_IRWXU | S_IRWXG | S_IRWXO);
39  rtems_test_assert(fd >= 0);
40
41  rv = fsync(fd);
42  rtems_test_assert(rv == 0);
43
44  rv = close(fd);
45  rtems_test_assert(rv == 0);
46}
47
48static void write_to_file(const char *file, bool sync)
49{
50  int fd;
51  char buf [1];
52  ssize_t n;
53  off_t pos;
54  int rv;
55
56  fd = open(file, O_RDWR);
57  rtems_test_assert(fd >= 0);
58
59  n = write(fd, buf, sizeof(buf));
60  rtems_test_assert(n == (ssize_t) sizeof(buf));
61
62  pos = lseek(fd, 0, SEEK_END);
63  rtems_test_assert(pos == sizeof(buf));
64
65  if (sync) {
66    rv = fsync(fd);
67    rtems_test_assert(rv == 0);
68  }
69
70  rv = close(fd);
71  rtems_test_assert(rv == 0);
72}
73
74static void check_file_size(const char *file, off_t size)
75{
76  int fd;
77  off_t pos;
78  int rv;
79
80  fd = open(file, O_RDWR);
81  rtems_test_assert(fd >= 0);
82
83  pos = lseek(fd, 0, SEEK_END);
84  rtems_test_assert(pos == size);
85
86  rv = close(fd);
87  rtems_test_assert(rv == 0);
88}
89
90static void test(const char *rda, const char *mnt, const char *file)
91{
92  static const msdos_format_request_param_t rqdata = {
93    .quick_format = true,
94    .sync_device = true
95  };
96
97  rtems_status_code sc;
98  int disk_fd;
99  int rv;
100
101  sc = rtems_disk_io_initialize();
102  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
103
104  disk_fd = open(rda, O_RDWR);
105  rtems_test_assert(disk_fd >= 0);
106
107  rv = msdos_format(rda, &rqdata);
108  rtems_test_assert(rv == 0);
109
110  rv = mount_and_make_target_path(
111    rda,
112    mnt,
113    RTEMS_FILESYSTEM_TYPE_DOSFS,
114    RTEMS_FILESYSTEM_READ_WRITE,
115    NULL
116  );
117  rtems_test_assert(rv == 0);
118
119  create_file(file);
120  rv = rtems_disk_fd_purge(disk_fd);
121  rtems_test_assert(rv == 0);
122
123  write_to_file(file, false);
124  rv = rtems_disk_fd_purge(disk_fd);
125  rtems_test_assert(rv == 0);
126  check_file_size(file, 0);
127
128  write_to_file(file, true);
129  rv = rtems_disk_fd_purge(disk_fd);
130  rtems_test_assert(rv == 0);
131  check_file_size(file, 1);
132
133  rv = unmount(mnt);
134  rtems_test_assert(rv == 0);
135
136  rv = close(disk_fd);
137  rtems_test_assert(rv == 0);
138}
139
140static void Init(rtems_task_argument arg)
141{
142  puts("\n\n*** TEST FSDOSFSSYNC 1 ***");
143
144  test("/dev/rda", "/mnt", "/mnt/file");
145
146  puts("*** END OF TEST FSDOSFSSYNC 1 ***");
147
148  rtems_test_exit(0);
149}
150
151rtems_ramdisk_config rtems_ramdisk_configuration [] = {
152  { .block_size = 512, .block_num = 1024 }
153};
154
155size_t rtems_ramdisk_configuration_size = 1;
156
157#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
158#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
159#define CONFIGURE_APPLICATION_EXTRA_DRIVERS RAMDISK_DRIVER_TABLE_ENTRY
160#define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK
161
162#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 6
163
164#define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM
165
166#define CONFIGURE_FILESYSTEM_DOSFS
167
168#define CONFIGURE_MAXIMUM_TASKS 2
169
170#define CONFIGURE_EXTRA_TASK_STACKS (8 * 1024)
171
172#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
173
174#define CONFIGURE_INIT
175
176#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.