1 /***************************************
2  Header file for file function prototypes
3 
4  Part of the Routino routing software.
5  ******************/ /******************
6  This file Copyright 2008-2015, 2019 Andrew M. Bishop
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU Affero General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU Affero General Public License for more details.
17 
18  You should have received a copy of the GNU Affero General Public License
19  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  ***************************************/
21 
22 
23 #ifndef FILES_H
24 #define FILES_H    /*+ To stop multiple inclusions. +*/
25 
26 /* If your system does not have the pread() and pwrite() system calls then you
27  * will need to change this line to the value 0 so that seek() and
28  * read()/write() are used instead of pread()/pwrite(). */
29 
30 #if defined(_MSC_VER) || defined(__MINGW32__)
31 #define HAVE_PREAD_PWRITE 0
32 #else
33 #define HAVE_PREAD_PWRITE 1
34 #endif
35 
36 #if defined(_MSC_VER)
37 #include <io.h>
38 #include <basetsd.h>
39 #define read(fd,address,length)  _read(fd,address,(unsigned int)(length))
40 #define write(fd,address,length) _write(fd,address,(unsigned int)(length))
41 #define ssize_t SSIZE_T
42 #else
43 #include <unistd.h>
44 #endif
45 
46 #if defined(_MSC_VER) || defined(__MINGW32__)
47 #undef lseek
48 #define lseek _lseeki64
49 #endif
50 
51 #include <sys/types.h>
52 #include <inttypes.h>
53 
54 #include "logging.h"
55 
56 
57 /* Types */
58 
59 /*+ A 64-bit file offset since a 32-bit off_t (which is signed) is smaller than a
60     32-bit size_t (which is unsigned) that can be written to or read from a file. +*/
61 typedef int64_t offset_t;
62 
63 
64 /* Functions in files.c */
65 
66 char *FileName(const char *dirname,const char *prefix, const char *name);
67 
68 void *MapFile(const char *filename);
69 void *MapFileWriteable(const char *filename);
70 
71 void *UnmapFile(const void *address);
72 
73 int SlimMapFile(const char *filename);
74 int SlimMapFileWriteable(const char *filename);
75 
76 int SlimUnmapFile(int fd);
77 
78 int OpenFileBufferedNew(const char *filename);
79 int OpenFileBufferedAppend(const char *filename);
80 
81 int ReOpenFileBuffered(const char *filename);
82 
83 int ReplaceFileBuffered(const char *filename,int *oldfd);
84 
85 int WriteFileBuffered(int fd,const void *address,size_t length);
86 int ReadFileBuffered(int fd,void *address,size_t length);
87 
88 int SeekFileBuffered(int fd,offset_t position);
89 int SkipFileBuffered(int fd,offset_t skip);
90 
91 int CloseFileBuffered(int fd);
92 
93 int OpenFile(const char *filename);
94 
95 void CloseFile(int fd);
96 
97 offset_t SizeFile(const char *filename);
98 offset_t SizeFileFD(int fd);
99 int ExistsFile(const char *filename);
100 
101 int DeleteFile(const char *filename);
102 
103 int RenameFile(const char *oldfilename,const char *newfilename);
104 
105 /* Functions in files.h */
106 
107 static inline int SlimReplace(int fd,const void *address,size_t length,offset_t position);
108 static inline int SlimFetch(int fd,void *address,size_t length,offset_t position);
109 
110 
111 /* Inline the frequently called functions */
112 
113 /*++++++++++++++++++++++++++++++++++++++
114   Write data to a file that has been opened for slim mode access.
115 
116   int SlimReplace Returns 0 if OK or something else in case of an error.
117 
118   int fd The file descriptor to write to.
119 
120   const void *address The address of the data to be written.
121 
122   size_t length The length of data to write.
123 
124   offset_t position The position in the file to seek to.
125   ++++++++++++++++++++++++++++++++++++++*/
126 
SlimReplace(int fd,const void * address,size_t length,offset_t position)127 static inline int SlimReplace(int fd,const void *address,size_t length,offset_t position)
128 {
129  /* Seek and write the data */
130 
131 #if HAVE_PREAD_PWRITE
132 
133  if(pwrite(fd,address,length,position)!=(ssize_t)length)
134     return(-1);
135 
136 #else
137 
138  if(lseek(fd,position,SEEK_SET)!=position)
139     return(-1);
140 
141  if(write(fd,address,length)!=(ssize_t)length)
142     return(-1);
143 
144 #endif
145 
146  return(0);
147 }
148 
149 
150 /*++++++++++++++++++++++++++++++++++++++
151   Read data from a file that has been opened for slim mode access.
152 
153   int SlimFetch Returns 0 if OK or something else in case of an error.
154 
155   int fd The file descriptor to read from.
156 
157   void *address The address the data is to be read into.
158 
159   size_t length The length of data to read.
160 
161   offset_t position The position in the file to seek to.
162   ++++++++++++++++++++++++++++++++++++++*/
163 
SlimFetch(int fd,void * address,size_t length,offset_t position)164 static inline int SlimFetch(int fd,void *address,size_t length,offset_t position)
165 {
166  /* Seek and read the data */
167 
168 #if HAVE_PREAD_PWRITE
169 
170  if(pread(fd,address,length,position)!=(ssize_t)length)
171     return(-1);
172 
173 #else
174 
175  if(lseek(fd,position,SEEK_SET)!=position)
176     return(-1);
177 
178  if(read(fd,address,length)!=(ssize_t)length)
179     return(-1);
180 
181 #endif
182 
183  return(0);
184 }
185 
186 
187 #endif /* FILES_H */
188