~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to storage/ndb/src/common/util/File.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

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