File.cpp 3.66 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   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 */

17
#include <ndb_global.h>
18

19
#include <File.hpp>
unknown's avatar
unknown committed
20

21
#include <NdbOut.hpp>
unknown's avatar
unknown committed
22
#include <my_dir.h>
23 24 25 26

//
// PUBLIC
//
27 28 29 30 31 32 33 34 35 36 37 38
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;
}
39 40

bool 
unknown's avatar
unknown committed
41
File_class::exists(const char* aFileName)
42
{
43 44 45
  MY_STAT stmp;

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

48
off_t
unknown's avatar
unknown committed
49
File_class::size(FILE* f)
50
{
51 52 53
  MY_STAT s;

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

57
  return s.st_size;
58 59 60
}

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

unknown's avatar
unknown committed
71
File_class::File_class() : 
72 73 74 75 76
  m_file(NULL), 
  m_fileMode("r")
{
}

unknown's avatar
unknown committed
77
File_class::File_class(const char* aFileName, const char* mode) :	
78 79 80
  m_file(NULL), 
  m_fileMode(mode)
{
81
  BaseString::snprintf(m_fileName, PATH_MAX, aFileName);
82 83 84
}

bool
unknown's avatar
unknown committed
85
File_class::open()
86 87 88 89 90
{
  return open(m_fileName, m_fileMode);
}

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

bool 
unknown's avatar
unknown committed
114
File_class::remove()
115 116 117
{
  // Close the file first!
  close();
unknown's avatar
unknown committed
118
  return File_class::remove(m_fileName);
119 120 121
}

bool 
unknown's avatar
unknown committed
122
File_class::close()
123 124
{
  bool rc = true;
125 126
  int retval = 0;

127 128 129
  if (m_file != NULL)
  { 
    ::fflush(m_file);
130 131 132 133 134 135 136 137 138 139 140
    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));
    }   
141
  }  
142
  m_file = NULL;
143 144 145 146 147
  
  return rc;
}

int 
unknown's avatar
unknown committed
148
File_class::read(void* buf, size_t itemSize, size_t nitems) const
149 150 151 152 153
{
  return ::fread(buf, itemSize,  nitems, m_file);
}

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

int 
unknown's avatar
unknown committed
160
File_class::readChar(char* buf)
161 162 163 164 165
{
  return readChar(buf, 0, strlen(buf));
}

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

int 
unknown's avatar
unknown committed
178
File_class::writeChar(const char* buf)
179 180 181
{
  return writeChar(buf, 0, ::strlen(buf));
}
182 183

off_t
unknown's avatar
unknown committed
184
File_class::size() const
185
{
unknown's avatar
unknown committed
186
  return File_class::size(m_file);
187 188 189
}

const char* 
unknown's avatar
unknown committed
190
File_class::getName() const
191 192 193 194 195
{
  return m_fileName;
}

int
unknown's avatar
unknown committed
196
File_class::flush() const
197
{
198
  return ::fflush(m_file);;
199
}