File.cpp 3.72 KB
Newer Older
1 2 3 4
/* Copyright (C) 2003 MySQL AB

   This program is free software; you can redistribute it and/or modify
   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 8 9 10 11 12 13 14 15

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

16
#include <ndb_global.h>
17

18
#include <File.hpp>
joreland@mysql.com's avatar
joreland@mysql.com committed
19

20
#include <NdbOut.hpp>
joreland@mysql.com's avatar
joreland@mysql.com committed
21
#include <my_dir.h>
22 23 24 25

//
// PUBLIC
//
26 27 28 29 30 31 32 33 34 35 36 37
time_t 
File_class::mtime(const char* aFileName)
{
  MY_STAT stmp;
  time_t rc = 0;

  if (my_stat(aFileName, &stmp, MYF(0)) != NULL) {
    rc = stmp.st_mtime;
  }

  return rc;
}
38 39

bool 
tomas@mc05.(none)'s avatar
tomas@mc05.(none) committed
40
File_class::exists(const char* aFileName)
41
{
42 43 44
  MY_STAT stmp;

  return (my_stat(aFileName, &stmp, MYF(0))!=NULL);
45 46
}

47
off_t
tomas@mc05.(none)'s avatar
tomas@mc05.(none) committed
48
File_class::size(FILE* f)
49
{
50 51 52 53 54
  MY_STAT s;

  // Note that my_fstat behaves *differently* than my_stat. ARGGGHH!
  if(my_fstat(::fileno(f), &s, MYF(0)))
    return 0;
55

56
  return s.st_size;
57 58 59
}

bool 
tomas@mc05.(none)'s avatar
tomas@mc05.(none) committed
60
File_class::rename(const char* currFileName, const char* newFileName)
61 62 63 64
{
  return ::rename(currFileName, newFileName) == 0 ? true : false;
}
bool 
tomas@mc05.(none)'s avatar
tomas@mc05.(none) committed
65
File_class::remove(const char* aFileName)
66 67 68 69
{
  return ::remove(aFileName) == 0 ? true : false;
}

tomas@mc05.(none)'s avatar
tomas@mc05.(none) committed
70
File_class::File_class() : 
71 72 73 74 75
  m_file(NULL), 
  m_fileMode("r")
{
}

tomas@mc05.(none)'s avatar
tomas@mc05.(none) committed
76
File_class::File_class(const char* aFileName, const char* mode) :	
77 78 79
  m_file(NULL), 
  m_fileMode(mode)
{
80
  BaseString::snprintf(m_fileName, PATH_MAX, aFileName);
81 82 83
}

bool
tomas@mc05.(none)'s avatar
tomas@mc05.(none) committed
84
File_class::open()
85 86 87 88 89
{
  return open(m_fileName, m_fileMode);
}

bool 
tomas@mc05.(none)'s avatar
tomas@mc05.(none) committed
90
File_class::open(const char* aFileName, const char* mode) 
91 92 93 94 95
{
  if(m_fileName != aFileName){
    /**
     * Only copy if it's not the same string
     */
96
    BaseString::snprintf(m_fileName, PATH_MAX, aFileName);
97 98 99 100 101 102 103 104 105 106
  }
  m_fileMode = mode;
  bool rc = true;
  if ((m_file = ::fopen(m_fileName, m_fileMode))== NULL)
  {
    rc = false;      
  }
  
  return rc;
}
tomas@mc05.(none)'s avatar
tomas@mc05.(none) committed
107
File_class::~File_class()
108 109 110 111 112
{
  close();  
}

bool 
tomas@mc05.(none)'s avatar
tomas@mc05.(none) committed
113
File_class::remove()
114 115 116
{
  // Close the file first!
  close();
tomas@mc05.(none)'s avatar
tomas@mc05.(none) committed
117
  return File_class::remove(m_fileName);
118 119 120
}

bool 
tomas@mc05.(none)'s avatar
tomas@mc05.(none) committed
121
File_class::close()
122 123
{
  bool rc = true;
124 125
  int retval = 0;

126 127 128
  if (m_file != NULL)
  { 
    ::fflush(m_file);
129 130 131 132 133 134 135 136 137 138 139
    retval = ::fclose(m_file);
    while ( (retval != 0) && (errno == EINTR) ){
      retval = ::fclose(m_file);
    }
    if( retval == 0){
      rc = true;
    }
    else {
      rc = false;
      ndbout_c("ERROR: Close file error in File.cpp for %s",strerror(errno));
    } 
140
  }  
141 142
  m_file = NULL;

143 144 145 146
  return rc;
}

int 
tomas@mc05.(none)'s avatar
tomas@mc05.(none) committed
147
File_class::read(void* buf, size_t itemSize, size_t nitems) const
148 149 150 151 152
{
  return ::fread(buf, itemSize,  nitems, m_file);
}

int 
tomas@mc05.(none)'s avatar
tomas@mc05.(none) committed
153
File_class::readChar(char* buf, long start, long length) const
154 155 156 157 158
{
  return ::fread((void*)&buf[start], 1, length, m_file);
}

int 
tomas@mc05.(none)'s avatar
tomas@mc05.(none) committed
159
File_class::readChar(char* buf)
160 161 162 163 164
{
  return readChar(buf, 0, strlen(buf));
}

int 
tomas@mc05.(none)'s avatar
tomas@mc05.(none) committed
165
File_class::write(const void* buf, size_t size, size_t nitems)
166 167 168 169 170
{
  return ::fwrite(buf, size, nitems, m_file);
}
 
int
tomas@mc05.(none)'s avatar
tomas@mc05.(none) committed
171
File_class::writeChar(const char* buf, long start, long length)
172 173 174 175 176
{
  return ::fwrite((const void*)&buf[start], sizeof(char), length, m_file);
}

int 
tomas@mc05.(none)'s avatar
tomas@mc05.(none) committed
177
File_class::writeChar(const char* buf)
178 179 180
{
  return writeChar(buf, 0, ::strlen(buf));
}
181 182

off_t
tomas@mc05.(none)'s avatar
tomas@mc05.(none) committed
183
File_class::size() const
184
{
tomas@mc05.(none)'s avatar
tomas@mc05.(none) committed
185
  return File_class::size(m_file);
186 187 188
}

const char* 
tomas@mc05.(none)'s avatar
tomas@mc05.(none) committed
189
File_class::getName() const
190 191 192 193 194
{
  return m_fileName;
}

int
tomas@mc05.(none)'s avatar
tomas@mc05.(none) committed
195
File_class::flush() const
196 197 198 199 200
{
#if defined NDB_OSE || defined NDB_SOFTOSE
  ::fflush(m_file);
  return ::fsync(::fileno(m_file));
#else
201
  return ::fflush(m_file);;
202 203
#endif
}