µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
file.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ distribution.
3 * (https://github.com/micro-os-plus)
4 * Copyright (c) 2015-2023 Liviu Ionescu. All rights reserved.
5 *
6 * Permission to use, copy, modify, and/or distribute this software
7 * for any purpose is hereby granted, under the terms of the MIT license.
8 *
9 * If a copy of the license was not distributed with this file, it can
10 * be obtained from https://opensource.org/licenses/mit/.
11 */
12
13#if defined(OS_USE_OS_APP_CONFIG_H)
14#include <cmsis-plus/os-app-config.h>
15#endif
16
19
21
22#include <cerrno>
23
24// ----------------------------------------------------------------------------
25
26#if defined(__clang__)
27#pragma clang diagnostic ignored "-Wc++98-compat"
28#endif
29
30// ----------------------------------------------------------------------------
31
32namespace os
33{
34 namespace posix
35 {
36 // ========================================================================
37
39 io
40 { impl, type::file }
41 {
42#if defined(OS_TRACE_POSIX_IO_FILE)
43 trace::printf ("file::%s()=%p\n", __func__, this);
44#endif
45 }
46
48 {
49#if defined(OS_TRACE_POSIX_IO_FILE)
50 trace::printf ("file::%s() @%p\n", __func__, this);
51#endif
52 }
53
54 // ------------------------------------------------------------------------
55
56 int
58 {
59#if defined(OS_TRACE_POSIX_IO_FILE)
60 trace::printf ("file::%s() @%p\n", __func__, this);
61#endif
62
63 int ret = io::close ();
64
65 // Note: the constructor is not called here.
66
67 // Link the file object to a list kept by the file system.
68 // It will be deallocated at the next open.
70
71 return ret;
72 }
73
74 int
75 file::ftruncate (off_t length)
76 {
77#if defined(OS_TRACE_POSIX_IO_FILE)
78 trace::printf ("file::%s(%u) @%p\n", __func__, length, this);
79#endif
80
81 if (length < 0)
82 {
83 errno = EINVAL;
84 return -1;
85 }
86
87 errno = 0;
88
89 // Execute the implementation specific code.
90 return impl ().do_ftruncate (length);
91 }
92
93 int
95 {
96#if defined(OS_TRACE_POSIX_IO_FILE)
97 trace::printf ("file::%s() @%p\n", __func__, this);
98#endif
99
100 errno = 0;
101
102 // Execute the implementation specific code.
103 return impl ().do_fsync ();
104 }
105
106 int
108 {
109#if defined(OS_TRACE_POSIX_IO_FILE)
110 trace::printf ("file::%s(%p) @%p\n", __func__, buf, this);
111#endif
112
113 errno = 0;
114
115 // Execute the file system code. Might be locked there.
116 return get_file_system ().statvfs (buf);
117 }
118
119 // ========================================================================
120
122 file_system_ (fs)
123 {
124#if defined(OS_TRACE_POSIX_IO_FILE)
125 trace::printf ("file_impl::%s()=%p\n", __func__, this);
126#endif
127 }
128
130 {
131#if defined(OS_TRACE_POSIX_IO_FILE)
132 trace::printf ("file_impl::%s() @%p\n", __func__, this);
133#endif
134 }
135
136 // ------------------------------------------------------------------------
137
138#pragma GCC diagnostic push
139#if defined(__clang__)
140#pragma clang diagnostic ignored "-Wunused-parameter"
141#elif defined(__GNUC__)
142#pragma GCC diagnostic ignored "-Wunused-parameter"
143#endif
144
145 int
147 {
148 errno = ENOSYS; // Not implemented
149 return -1;
150 }
151
152#pragma GCC diagnostic pop
153
154 int
156 {
157 errno = ENOSYS; // Not implemented
158 return -1;
159 }
160
161 // ==========================================================================
162 } /* namespace posix */
163} /* namespace os */
164
165// ----------------------------------------------------------------------------
virtual ~file_impl() override
Definition file.cpp:129
virtual int do_ftruncate(off_t length)=0
Definition file.cpp:146
file_impl(file_system &fs)
Definition file.cpp:121
virtual int do_fsync(void)=0
Definition file.cpp:155
File system class.
virtual int statvfs(struct statvfs *buf)
void add_deferred_file(file *fil)
file_impl & impl(void) const
Definition file.h:493
virtual int fstatvfs(struct statvfs *buf)
Definition file.cpp:107
virtual int ftruncate(off_t length)
Definition file.cpp:75
virtual ~file() override
Definition file.cpp:47
class file_system & get_file_system(void)
Definition file.h:487
virtual int fsync(void)
Definition file.cpp:94
virtual int close(void) override
Definition file.cpp:57
Base I/O class.
Definition io.h:87
virtual int close(void)
Definition io.cpp:165
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:60
System namespace.