xref: /trafficserver/lib/records/RecFile.cc (revision 4cfd5a73)
1 /** @file
2 
3   Record compatibility definitions
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 
24 #include "tscore/ink_platform.h"
25 #include "tscore/ink_string.h"
26 #include "P_RecFile.h"
27 #include "P_RecDefs.h"
28 #include "P_RecUtils.h"
29 
30 #include <array>
31 
32 //-------------------------------------------------------------------------
33 // RecFileOpenR
34 //-------------------------------------------------------------------------
35 
36 RecHandle
RecFileOpenR(const char * file)37 RecFileOpenR(const char *file)
38 {
39   RecHandle h_file;
40   return ((h_file = ::open(file, O_RDONLY)) < 0) ? REC_HANDLE_INVALID : h_file;
41 }
42 
43 //-------------------------------------------------------------------------
44 // RecFileOpenW
45 //-------------------------------------------------------------------------
46 
47 RecHandle
RecFileOpenW(const char * file)48 RecFileOpenW(const char *file)
49 {
50   RecHandle h_file;
51 
52   if ((h_file = ::open(file, O_WRONLY | O_TRUNC | O_CREAT, 0600)) < 0) {
53     return REC_HANDLE_INVALID;
54   }
55   fcntl(h_file, F_SETFD, FD_CLOEXEC);
56   return h_file;
57 }
58 
59 //-------------------------------------------------------------------------
60 // RecFileSync
61 //-------------------------------------------------------------------------
62 
63 int
RecFileSync(RecHandle h_file)64 RecFileSync(RecHandle h_file)
65 {
66   return (fsync(h_file) == 0) ? REC_ERR_OKAY : REC_ERR_FAIL;
67 }
68 
69 //-------------------------------------------------------------------------
70 // RecFileClose
71 //-------------------------------------------------------------------------
72 
73 int
RecFileClose(RecHandle h_file)74 RecFileClose(RecHandle h_file)
75 {
76   return (close(h_file) == 0) ? REC_ERR_OKAY : REC_ERR_FAIL;
77 }
78 
79 //-------------------------------------------------------------------------
80 // RecSnapFileRead
81 //-------------------------------------------------------------------------
82 
83 int
RecSnapFileRead(RecHandle h_file,char * buf,int size,int * bytes_read)84 RecSnapFileRead(RecHandle h_file, char *buf, int size, int *bytes_read)
85 {
86   if ((*bytes_read = ::pread(h_file, buf, size, VERSION_HDR_SIZE)) <= 0) {
87     *bytes_read = 0;
88     return REC_ERR_FAIL;
89   }
90   return REC_ERR_OKAY;
91 }
92 
93 //-------------------------------------------------------------------------
94 // RecFileRead
95 //-------------------------------------------------------------------------
96 
97 int
RecFileRead(RecHandle h_file,char * buf,int size,int * bytes_read)98 RecFileRead(RecHandle h_file, char *buf, int size, int *bytes_read)
99 {
100   if ((*bytes_read = ::read(h_file, buf, size)) <= 0) {
101     *bytes_read = 0;
102     return REC_ERR_FAIL;
103   }
104   return REC_ERR_OKAY;
105 }
106 
107 //-------------------------------------------------------------------------
108 // RecSnapFileWrite
109 //-------------------------------------------------------------------------
110 
111 int
RecSnapFileWrite(RecHandle h_file,char * buf,int size,int * bytes_written)112 RecSnapFileWrite(RecHandle h_file, char *buf, int size, int *bytes_written)
113 {
114   // First write the version byes for snap file
115   std::array<char, VERSION_HDR_SIZE> VERSION_HDR{{'V', PACKAGE_VERSION[0], PACKAGE_VERSION[2], PACKAGE_VERSION[4], '\0'}};
116   if (::write(h_file, VERSION_HDR.data(), VERSION_HDR_SIZE) < 0) {
117     return REC_ERR_FAIL;
118   }
119 
120   if ((*bytes_written = ::pwrite(h_file, buf, size, VERSION_HDR_SIZE)) < 0) {
121     *bytes_written = 0;
122     return REC_ERR_FAIL;
123   }
124   return REC_ERR_OKAY;
125 }
126 
127 //-------------------------------------------------------------------------
128 // RecFileWrite
129 //-------------------------------------------------------------------------
130 
131 int
RecFileWrite(RecHandle h_file,char * buf,int size,int * bytes_written)132 RecFileWrite(RecHandle h_file, char *buf, int size, int *bytes_written)
133 {
134   if ((*bytes_written = ::write(h_file, buf, size)) < 0) {
135     *bytes_written = 0;
136     return REC_ERR_FAIL;
137   }
138   return REC_ERR_OKAY;
139 }
140 
141 //-------------------------------------------------------------------------
142 // RecFileGetSize
143 //-------------------------------------------------------------------------
144 
145 int
RecFileGetSize(RecHandle h_file)146 RecFileGetSize(RecHandle h_file)
147 {
148   struct stat fileStats;
149   fstat(h_file, &fileStats);
150   return static_cast<int>(fileStats.st_size);
151 }
152 
153 //-------------------------------------------------------------------------
154 // RecFileExists
155 //-------------------------------------------------------------------------
156 
157 int
RecFileExists(const char * file)158 RecFileExists(const char *file)
159 {
160   RecHandle h_file;
161   if ((h_file = RecFileOpenR(file)) == REC_HANDLE_INVALID) {
162     return REC_ERR_FAIL;
163   }
164   RecFileClose(h_file);
165   return REC_ERR_OKAY;
166 }
167