OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef SERVICES_FILES_C_LIB_FD_IMPL_H_ | |
6 #define SERVICES_FILES_C_LIB_FD_IMPL_H_ | |
7 | |
8 #include <memory> | |
9 | |
10 #include "files/public/c/mojio_sys_stat.h" | |
11 #include "files/public/c/mojio_sys_types.h" | |
12 #include "mojo/public/cpp/system/macros.h" | |
13 | |
14 namespace mojio { | |
15 | |
16 class ErrnoImpl; | |
17 | |
18 // |FDImpl| is an interface for file-descriptor-like objects, on top of which | |
19 // <unistd.h>-style (and also <stdio.h>-style) functions may be implemented. | |
20 // (For <unistd.h>-style functions, one needs a file descriptor table, since FDs | |
21 // are integers with a prescribed allocation scheme.) | |
22 // | |
23 // <unistd.h> functions with a "boolean" return value (0 on success, -1 on | |
24 // failure) here return a |bool| instead (true on success, false on failure). On | |
25 // failure, all methods set "errno" using the |ErrnoImpl| passed to the | |
26 // constructor. | |
27 class FDImpl { | |
28 public: | |
29 virtual ~FDImpl() {} | |
30 | |
31 // <unistd.h>: | |
32 virtual bool Close() = 0; // May be called only at most once. | |
33 virtual std::unique_ptr<FDImpl> Dup() = 0; | |
34 virtual bool Ftruncate(mojio_off_t length) = 0; | |
35 virtual mojio_off_t Lseek(mojio_off_t offset, int whence) = 0; | |
36 virtual mojio_ssize_t Read(void* buf, size_t count) = 0; | |
37 virtual mojio_ssize_t Write(const void* buf, size_t count) = 0; | |
38 | |
39 // <sys/stat.h>: | |
40 virtual bool Fstat(struct mojio_stat* buf) = 0; | |
41 | |
42 protected: | |
43 // The |ErrnoImpl| must outlive this object and all objects transitively | |
44 // |Dup()|ed from it. | |
45 explicit FDImpl(ErrnoImpl* errno_impl) : errno_impl_(errno_impl) {} | |
46 | |
47 ErrnoImpl* errno_impl() const { return errno_impl_; } | |
48 | |
49 private: | |
50 ErrnoImpl* const errno_impl_; | |
51 | |
52 MOJO_DISALLOW_COPY_AND_ASSIGN(FDImpl); | |
53 }; | |
54 | |
55 } // namespace mojio | |
56 | |
57 #endif // SERVICES_FILES_C_LIB_FD_IMPL_H_ | |
OLD | NEW |