µ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.h
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
28#ifndef CMSIS_PLUS_POSIX_IO_FILE_H_
29#define CMSIS_PLUS_POSIX_IO_FILE_H_
30
31// ----------------------------------------------------------------------------
32
33#if defined(__cplusplus)
34
35// ----------------------------------------------------------------------------
36
37#if defined(OS_USE_OS_APP_CONFIG_H)
38#include <cmsis-plus/os-app-config.h>
39#endif
40
45
46#include <mutex>
47
48// ----------------------------------------------------------------------------
49
50#pragma GCC diagnostic push
51
52#if defined(__clang__)
53#pragma clang diagnostic ignored "-Wc++98-compat"
54#endif
55
56// ----------------------------------------------------------------------------
57
58namespace os
59{
60 namespace posix
61 {
62 // ------------------------------------------------------------------------
63
64 class file_system;
65 class file_impl;
66
67 // ========================================================================
68
74 class file : public io
75 {
76 // ----------------------------------------------------------------------
77
82 friend class file_system;
83 friend class io;
84
89 // ----------------------------------------------------------------------
95 public:
96
98
103 // The rule of five.
104 file (const file&) = delete;
105 file (file&&) = delete;
106 file&
107 operator= (const file&) = delete;
108 file&
109 operator= (file&&) = delete;
110
115 virtual
116 ~file () override;
117
122 // ----------------------------------------------------------------------
128 public:
129
130 virtual int
131 close (void) override;
132
133 virtual int
134 ftruncate (off_t length);
135
136 virtual int
137 fsync (void);
138
139 virtual int
140 fstatvfs (struct statvfs *buf);
141
142 // ----------------------------------------------------------------------
143 // Support functions.
144
145 class file_system&
146 file_system (void);
147
148 file_impl&
149 impl (void) const;
150
155 // ----------------------------------------------------------------------
156 public:
157
162 // Intrusive node used to link this file to the deferred
163 // deallocation list. Must be public.
164 utils::double_list_links deferred_links_;
165
169 };
170
171 // ========================================================================
172
173 class file_impl : public io_impl
174 {
175 // ----------------------------------------------------------------------
176
181 friend class file;
182
187 // ----------------------------------------------------------------------
193 public:
194
195 file_impl (class file_system& fs);
196
201 // The rule of five.
202 file_impl (const file_impl&) = delete;
203 file_impl (file_impl&&) = delete;
204 file_impl&
205 operator= (const file_impl&) = delete;
206 file_impl&
207 operator= (file_impl&&) = delete;
208
213 virtual
214 ~file_impl () override;
215
220 // ----------------------------------------------------------------------
226 public:
227
228 // Implementations
229
230 virtual int
231 do_ftruncate (off_t length) = 0;
232
233 virtual int
234 do_fsync (void) = 0;
235
236 // ----------------------------------------------------------------------
237 // Support functions.
238
239 class file_system&
240 file_system (void);
241
246 // ----------------------------------------------------------------------
247 protected:
248
253 class file_system& file_system_;
254
258 };
259
260 // ========================================================================
261
262 template<typename T>
264 {
265 // --------------------------------------------------------------------
266
267 public:
268
269 using value_type = T;
270
271 // --------------------------------------------------------------------
272
278 public:
279
281
286 // The rule of five.
287 file_implementable (const file_implementable&) = delete;
290 operator= (const file_implementable&) = delete;
292 operator= (file_implementable&&) = delete;
293
298 virtual
299 ~file_implementable () override;
300
305 // --------------------------------------------------------------------
311 public:
312
313 // Support functions.
314
316 impl (void) const;
317
322 // --------------------------------------------------------------------
323 protected:
324
329 value_type impl_instance_;
330
334 };
335
336 // ========================================================================
337
338 template<typename T, typename L>
339 class file_lockable : public file
340 {
341 // --------------------------------------------------------------------
342
343 public:
344
345 using value_type = T;
346 using lockable_type = L;
347
348 // --------------------------------------------------------------------
349
355 public:
356
357 file_lockable (class file_system& fs, lockable_type& locker);
358
363 // The rule of five.
364 file_lockable (const file_lockable&) = delete;
365 file_lockable (file_lockable&&) = delete;
367 operator= (const file_lockable&) = delete;
369 operator= (file_lockable&&) = delete;
370
375 virtual
376 ~file_lockable () override;
377
382 // --------------------------------------------------------------------
388 public:
389
390 virtual int
391 close (void) override;
392
393 virtual ssize_t
394 read (void* buf, std::size_t nbyte) override;
395
396 virtual ssize_t
397 write (const void* buf, std::size_t nbyte) override;
398
399 virtual ssize_t
400 writev (const struct iovec* iov, int iovcnt) override;
401
402 virtual int
403 vfcntl (int cmd, std::va_list args) override;
404
405 virtual int
406 fstat (struct stat* buf) override;
407
408 virtual off_t
409 lseek (off_t offset, int whence) override;
410
411 virtual int
412 ftruncate (off_t length) override;
413
414 virtual int
415 fsync (void) override;
416
417 // fstatvfs() - must not be locked, since will be locked by the
418 // file system. (otherwise non-recursive mutexes will fail).
419
420 // --------------------------------------------------------------------
421 // Support functions.
422
424 impl (void) const;
425
430 // --------------------------------------------------------------------
431 protected:
432
437 value_type impl_instance_;
438
439 lockable_type& locker_;
440
444 };
445
446 // ==========================================================================
447 } /* namespace posix */
448} /* namespace os */
449
450// ===== Inline & template implementations ====================================
451
452namespace os
453{
454 namespace posix
455 {
456 // ========================================================================
457
458 inline file_system&
460 {
461 return impl ().file_system ();
462 }
463
464 inline file_impl&
465 file::impl (void) const
466 {
467 return static_cast<file_impl&> (impl_);
468 }
469
470 // ========================================================================
471
472 inline class file_system&
474 {
475 return file_system_;
476 }
477
478 // ========================================================================
479
480 template<typename T>
482 file
483 { impl_instance_ }, //
484 impl_instance_
485 { fs }
486 {
487#if defined(OS_TRACE_POSIX_IO_FILE)
488 trace::printf ("file_implementable::%s()=@%p\n", __func__, this);
489#endif
490 }
491
492 template<typename T>
494 {
495#if defined(OS_TRACE_POSIX_IO_FILE)
496 trace::printf ("file_implementable::%s() @%p\n", __func__, this);
497#endif
498 }
499
500 template<typename T>
503 {
504 return static_cast<value_type&> (impl_);
505 }
506
507 // ========================================================================
508
509 template<typename T, typename L>
511 lockable_type& locker) :
512 file
513 { impl_instance_ }, //
514 impl_instance_
515 { fs }, //
516 locker_ (locker)
517 {
518#if defined(OS_TRACE_POSIX_IO_FILE)
519 trace::printf ("file_lockable::%s()=@%p\n", __func__, this);
520#endif
521 }
522
523 template<typename T, typename L>
525 {
526#if defined(OS_TRACE_POSIX_IO_FILE)
527 trace::printf ("file_lockable::%s() @%p\n", __func__, this);
528#endif
529 }
530
531 // ------------------------------------------------------------------------
532
533 template<typename T, typename L>
534 int
536 {
537 std::lock_guard<L> lock
538 { locker_ };
539
540 return file::close ();
541 }
542
543 template<typename T, typename L>
544 ssize_t
545 file_lockable<T, L>::read (void* buf, std::size_t nbyte)
546 {
547 std::lock_guard<L> lock
548 { locker_ };
549
550 return file::read (buf, nbyte);
551 }
552
553 template<typename T, typename L>
554 ssize_t
555 file_lockable<T, L>::write (const void* buf, std::size_t nbyte)
556 {
557 std::lock_guard<L> lock
558 { locker_ };
559
560 return file::write (buf, nbyte);
561 }
562
563 template<typename T, typename L>
564 ssize_t
565 file_lockable<T, L>::writev (const struct iovec* iov, int iovcnt)
566 {
567 std::lock_guard<L> lock
568 { locker_ };
569
570 return file::writev (iov, iovcnt);
571 }
572
573 template<typename T, typename L>
574 int
575 file_lockable<T, L>::vfcntl (int cmd, std::va_list args)
576 {
577 std::lock_guard<L> lock
578 { locker_ };
579
580 return file::vfcntl (cmd, args);
581 }
582
583 template<typename T, typename L>
584 int
586 {
587 std::lock_guard<L> lock
588 { locker_ };
589
590 return file::fstat (buf);
591 }
592
593 template<typename T, typename L>
594 off_t
595 file_lockable<T, L>::lseek (off_t offset, int whence)
596 {
597 std::lock_guard<L> lock
598 { locker_ };
599
600 return file::lseek (offset, whence);
601 }
602
603 template<typename T, typename L>
604 int
606 {
607 std::lock_guard<L> lock
608 { locker_ };
609
610 return file::ftruncate (length);
611 }
612
613 template<typename T, typename L>
614 int
616 {
617 std::lock_guard<L> lock
618 { locker_ };
619
620 return file::fsync ();
621 }
622
623 template<typename T, typename L>
626 {
627 return static_cast<value_type&> (impl_);
628 }
629
630 // ==========================================================================
631 } /* namespace posix */
632} /* namespace os */
633
634#pragma GCC diagnostic pop
635
636// ----------------------------------------------------------------------------
637
638#endif /* __cplusplus */
639
640// ----------------------------------------------------------------------------
641
642#endif /* CMSIS_PLUS_POSIX_IO_FILE_H_ */
virtual ~file_impl() override
Definition file.cpp:140
virtual int do_ftruncate(off_t length)=0
Definition file.cpp:153
class file_system & file_system(void)
Definition file.h:473
virtual int do_fsync(void)=0
Definition file.cpp:162
virtual ~file_implementable() override
Definition file.h:493
value_type & impl(void) const
Definition file.h:502
file_implementable(class file_system &fs)
Definition file.h:481
virtual ssize_t read(void *buf, std::size_t nbyte) override
Definition file.h:545
virtual ~file_lockable() override
Definition file.h:524
virtual off_t lseek(off_t offset, int whence) override
Definition file.h:595
virtual int fstat(struct stat *buf) override
Definition file.h:585
value_type & impl(void) const
Definition file.h:625
file_lockable(class file_system &fs, lockable_type &locker)
Definition file.h:510
virtual ssize_t write(const void *buf, std::size_t nbyte) override
Definition file.h:555
virtual int vfcntl(int cmd, std::va_list args) override
Definition file.h:575
virtual int close(void) override
Definition file.h:535
virtual int ftruncate(off_t length) override
Definition file.h:605
virtual int fsync(void) override
Definition file.h:615
virtual ssize_t writev(const struct iovec *iov, int iovcnt) override
Definition file.h:565
File system class.
File class.
Definition file.h:75
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 off_t lseek(off_t offset, int whence)
Definition io.cpp:453
virtual int fstat(struct stat *buf)
Definition io.cpp:422
virtual ssize_t write(const void *buf, std::size_t nbyte)
Definition io.cpp:280
virtual ssize_t read(void *buf, std::size_t nbyte)
Definition io.cpp:229
virtual ssize_t writev(const struct iovec *iov, int iovcnt)
Definition io.cpp:333
virtual int vfcntl(int cmd, std::va_list args)
Definition io.cpp:387
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:74
int stat(const char *path, struct stat *buf)
System namespace.
Definition uio.h:56