µ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 Liviu Ionescu.
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom
12 * the Software is furnished to do so, subject to the following
13 * conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
30
32
33#include <cerrno>
34
35// ----------------------------------------------------------------------------
36
37#if defined(__clang__)
38#pragma clang diagnostic ignored "-Wc++98-compat"
39#endif
40
41// ----------------------------------------------------------------------------
42
43namespace os
44{
45 namespace posix
46 {
47 // ========================================================================
48
50 io
51 { impl, type::file }
52 {
53#if defined(OS_TRACE_POSIX_IO_FILE)
54 trace::printf ("file::%s()=%p\n", __func__, this);
55#endif
56 }
57
59 {
60#if defined(OS_TRACE_POSIX_IO_FILE)
61 trace::printf ("file::%s() @%p\n", __func__, this);
62#endif
63 }
64
65 // ------------------------------------------------------------------------
66
67 int
69 {
70#if defined(OS_TRACE_POSIX_IO_FILE)
71 trace::printf ("file::%s() @%p\n", __func__, this);
72#endif
73
74 int ret = io::close ();
75
76 // Note: the constructor is not called here.
77
78 // Link the file object to a list kept by the file system.
79 // It will be deallocated at the next open.
81
82 return ret;
83 }
84
85 int
86 file::ftruncate (off_t length)
87 {
88#if defined(OS_TRACE_POSIX_IO_FILE)
89 trace::printf ("file::%s(%u) @%p\n", __func__, length, this);
90#endif
91
92 if (length < 0)
93 {
94 errno = EINVAL;
95 return -1;
96 }
97
98 errno = 0;
99
100 // Execute the implementation specific code.
101 return impl ().do_ftruncate (length);
102 }
103
104 int
106 {
107#if defined(OS_TRACE_POSIX_IO_FILE)
108 trace::printf ("file::%s() @%p\n", __func__, this);
109#endif
110
111 errno = 0;
112
113 // Execute the implementation specific code.
114 return impl ().do_fsync ();
115 }
116
117 int
119 {
120#if defined(OS_TRACE_POSIX_IO_FILE)
121 trace::printf ("file::%s(%p) @%p\n", __func__, buf, this);
122#endif
123
124 errno = 0;
125
126 // Execute the file system code. Might be locked there.
127 return file_system ().statvfs (buf);
128 }
129
130 // ========================================================================
131
133 file_system_ (fs)
134 {
135#if defined(OS_TRACE_POSIX_IO_FILE)
136 trace::printf ("file_impl::%s()=%p\n", __func__, this);
137#endif
138 }
139
141 {
142#if defined(OS_TRACE_POSIX_IO_FILE)
143 trace::printf ("file_impl::%s() @%p\n", __func__, this);
144#endif
145 }
146
147 // ------------------------------------------------------------------------
148
149#pragma GCC diagnostic push
150#pragma GCC diagnostic ignored "-Wunused-parameter"
151
152 int
154 {
155 errno = ENOSYS; // Not implemented
156 return -1;
157 }
158
159#pragma GCC diagnostic pop
160
161 int
163 {
164 errno = ENOSYS; // Not implemented
165 return -1;
166 }
167
168 // ==========================================================================
169 } /* namespace posix */
170} /* namespace os */
171
172// ----------------------------------------------------------------------------
virtual ~file_impl() override
Definition file.cpp:140
virtual int do_ftruncate(off_t length)=0
Definition file.cpp:153
file_impl(class file_system &fs)
Definition file.cpp:132
virtual int do_fsync(void)=0
Definition file.cpp:162
File system class.
virtual int statvfs(struct statvfs *buf)
void add_deferred_file(file *fil)
file_impl & impl(void) const
Definition file.h:465
virtual int fstatvfs(struct statvfs *buf)
Definition file.cpp:118
virtual int ftruncate(off_t length)
Definition file.cpp:86
class file_system & file_system(void)
Definition file.h:459
virtual ~file() override
Definition file.cpp:58
virtual int fsync(void)
Definition file.cpp:105
virtual int close(void) override
Definition file.cpp:68
Base I/O class.
Definition io.h:98
virtual int close(void)
Definition io.cpp:176
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:74
System namespace.