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