MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
File.cpp
1 /*
2  Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #include <ndb_global.h>
19 
20 #include <util/File.hpp>
21 #include <NdbOut.hpp>
22 
23 //
24 // PUBLIC
25 //
26 time_t
27 File_class::mtime(const char* aFileName)
28 {
29  struct stat s;
30  if (stat(aFileName, &s) != 0)
31  return 0;
32  return s.st_mtime;
33 }
34 
35 bool
36 File_class::exists(const char* aFileName)
37 {
38  struct stat s;
39  if (stat(aFileName, &s) != 0)
40  return false;
41  return true;
42 }
43 
44 off_t
46 {
47  struct stat s;
48  if (fstat(fileno(f), &s) != 0)
49  return 0;
50  return s.st_size;
51 }
52 
53 bool
54 File_class::rename(const char* currFileName, const char* newFileName)
55 {
56  return ::rename(currFileName, newFileName) == 0 ? true : false;
57 }
58 bool
59 File_class::remove(const char* aFileName)
60 {
61  return ::remove(aFileName) == 0 ? true : false;
62 }
63 
65  m_file(NULL),
66  m_fileMode("r")
67 {
68 }
69 
70 File_class::File_class(const char* aFileName, const char* mode) :
71  m_file(NULL),
72  m_fileMode(mode)
73 {
74  BaseString::snprintf(m_fileName, PATH_MAX, "%s", aFileName);
75 }
76 
77 bool
79 {
80  return open(m_fileName, m_fileMode);
81 }
82 
83 bool
84 File_class::open(const char* aFileName, const char* mode)
85 {
86  assert(m_file == NULL); // Not already open
87  if(m_fileName != aFileName){
91  BaseString::snprintf(m_fileName, PATH_MAX, "%s", aFileName);
92  }
93  m_fileMode = mode;
94  bool rc = true;
95  if ((m_file = ::fopen(m_fileName, m_fileMode))== NULL)
96  {
97  rc = false;
98  }
99 
100  return rc;
101 }
102 
103 bool
105 {
106  return (m_file != NULL);
107 }
108 
110 {
111  close();
112 }
113 
114 bool
116 {
117  // Close the file first!
118  close();
119  return File_class::remove(m_fileName);
120 }
121 
122 bool
124 {
125  bool rc = true;
126  int retval = 0;
127 
128  if (m_file != NULL)
129  {
130  ::fflush(m_file);
131  retval = ::fclose(m_file);
132  while ( (retval != 0) && (errno == EINTR) ){
133  retval = ::fclose(m_file);
134  }
135  if( retval == 0){
136  rc = true;
137  }
138  else {
139  rc = false;
140  ndbout_c("ERROR: Close file error in File.cpp for %s",strerror(errno));
141  }
142  }
143  m_file = NULL;
144 
145  return rc;
146 }
147 
148 int
149 File_class::read(void* buf, size_t itemSize, size_t nitems) const
150 {
151  return ::fread(buf, itemSize, nitems, m_file);
152 }
153 
154 int
155 File_class::readChar(char* buf, long start, long length) const
156 {
157  return ::fread((void*)&buf[start], 1, length, m_file);
158 }
159 
160 int
162 {
163  return readChar(buf, 0, (long)strlen(buf));
164 }
165 
166 int
167 File_class::write(const void* buf, size_t size_arg, size_t nitems)
168 {
169  return ::fwrite(buf, size_arg, nitems, m_file);
170 }
171 
172 int
173 File_class::writeChar(const char* buf, long start, long length)
174 {
175  return ::fwrite((const void*)&buf[start], sizeof(char), length, m_file);
176 }
177 
178 int
180 {
181  return writeChar(buf, 0, (long)::strlen(buf));
182 }
183 
184 off_t
186 {
187  return File_class::size(m_file);
188 }
189 
190 const char*
192 {
193  return m_fileName;
194 }
195 
196 int
198 {
199  return ::fflush(m_file);;
200 }