1 /*
2 Copyright (c) 2014-2021 Martin Cejp
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 
6 Permission is hereby granted, free of charge, to any person or organization
7 obtaining a copy of the software and accompanying documentation covered by
8 this license (the "Software") to use, reproduce, display, distribute,
9 execute, and transmit the Software, and to prepare derivative works of the
10 Software, and to permit third-parties to whom the Software is furnished to
11 do so, all subject to the following:
12 
13 The copyright notices in the Software and this entire statement, including
14 the above license grant, this restriction and the following disclaimer,
15 must be included in all copies of the Software, in whole or in part, and
16 all derivative works of the Software, unless such copies or derivative
17 works are solely in the form of machine-executable object code generated by
18 a source language processor.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 */
28 
29 module dlib.filesystem.posix.file;
30 
31 version (Posix)
32 {
33     import dlib.core.stream;
34     import dlib.core.memory;
35     import dlib.filesystem.filesystem;
36     import dlib.filesystem.posix.common;
37 
38     static import core.sys.posix.unistd;
39 
40     class PosixFile: IOStream
41     {
42         int fd;
43         uint accessFlags;
44         bool eof = false;
45 
46         this(int fd, uint accessFlags)
47         {
48             this.fd = fd;
49             this.accessFlags = accessFlags;
50         }
51 
52         ~this()
53         {
54             close();
55         }
56 
57         override void close()
58         {
59             if (fd != -1)
60             {
61                 core.sys.posix.unistd.close(fd);
62                 fd = -1;
63             }
64         }
65 
66         override bool seekable()
67         {
68             return true;
69         }
70 
71         override StreamPos getPosition()
72         {
73             import core.sys.posix.stdio;
74 
75             return lseek(fd, 0, SEEK_CUR);
76         }
77 
78         override bool setPosition(StreamPos pos)
79         {
80             import core.sys.posix.stdio;
81 
82             return lseek(fd, pos, SEEK_SET) == pos;
83         }
84 
85         override StreamSize size()
86         {
87             import core.sys.posix.stdio;
88 
89             auto off = lseek(fd, 0, SEEK_CUR);
90             auto end = lseek(fd, 0, SEEK_END);
91             lseek(fd, off, SEEK_SET);
92             return end;
93         }
94 
95         override bool readable()
96         {
97             return fd != -1 && (accessFlags & FileSystem.read) && !eof;
98         }
99 
100         override size_t readBytes(void* buffer, size_t count)
101         {
102             immutable size_t got = core.sys.posix.unistd.read(fd, buffer, count);
103 
104             if (count > got)
105                 eof = true;
106 
107             return got;
108         }
109 
110         override bool writeable()
111         {
112             return fd != -1 && (accessFlags & FileSystem.write);
113         }
114 
115         override size_t writeBytes(const void* buffer, size_t count)
116         {
117             return core.sys.posix.unistd.write(fd, buffer, count);
118         }
119 
120         override void flush()
121         {
122         }
123     }
124 }