1 /*
2    Copyright (C) 2003-2006 MySQL AB, 2008 Sun Microsystems, Inc.
3     All rights reserved. Use is subject to license terms.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License, version 2.0,
7    as published by the Free Software Foundation.
8 
9    This program is also distributed with certain software (including
10    but not limited to OpenSSL) that is licensed under separate terms,
11    as designated in a particular file or component or in included license
12    documentation.  The authors of MySQL hereby grant you an additional
13    permission to link the program and your derivative works with the
14    separately licensed software that they have included with MySQL.
15 
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License, version 2.0, for more details.
20 
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
24 */
25 
26 #ifndef FILE_H
27 #define FILE_H
28 
29 #include <ndb_global.h>
30 
31 /**
32  * This class provides a file abstraction . It has operations
33  * to create, read, write and delete a file.
34  *
35  * @version #@ $Id: File.hpp,v 1.5 2002/04/26 13:15:38 ejonore Exp $
36  */
37 class File_class
38 {
39 public:
40   /**
41    * Returns time for last contents modification of a file.
42    *
43    * @param aFileName a filename to check.
44    * @return the time for last contents modificaton of the file.
45    */
46   static time_t mtime(const char* aFileName);
47 
48   /**
49    * Returns true if the file exist.
50    *
51    * @param aFileName a filename to check.
52    * @return true if the file exists.
53    */
54   static bool exists(const char* aFileName);
55 
56   /**
57    * Returns the size of a file.
58    *
59    * @param f a pointer to a FILE descriptor.
60    * @return the size of the file.
61    */
62   static off_t size(FILE* f);
63 
64   /**
65    * Renames a file.
66    *
67    * @param currFileName the current name of the file.
68    * @param newFileName the new name of the file.
69    * @return true if successful.
70    */
71   static bool rename(const char* currFileName, const char* newFileName);
72 
73   /**
74    * Removes/deletes a file.
75    *
76    * @param aFileName the file to remove.
77    * @return true if successful.
78    */
79   static bool remove(const char* aFileName);
80 
81   /**
82    * Default constructor.
83    */
84   File_class();
85 
86   /**
87    * Creates a new File  with the specified filename and file mode.
88    * The real file itself will not be created unless open() is called!
89    *
90    * To see the available file modes use 'man 3 fopen'.
91    *
92    * @param aFileName a filename.
93    * @param mode the mode which the file should be opened/created with, default "r".
94    */
95   File_class(const char* aFileName, const char* mode = "r");
96 
97   /**
98    * Destructor.
99    */
100   ~File_class();
101 
102   /**
103    * Opens/creates the file. If open() fails then 'errno' and perror()
104    * should be used to determine the exact failure cause.
105    *
106    * @return true if successful. Check errno if unsuccessful.
107    */
108   bool open();
109 
110   /**
111    * Opens/creates the file with the specified name and mode.
112    * If open() fails then 'errno' and perror() should be used to
113    * determine the exact failure cause.
114    *
115    * @param aFileName the file to open.
116    * @param mode the file mode to use.
117    * @return true if successful. Check errno if unsuccessful.
118    */
119   bool open(const char* aFileName, const char* mode);
120 
121   /**
122    * Check if the file is open
123    *
124    * @return true if file is open, false if closed
125    */
126   bool is_open();
127 
128   /**
129    * Removes the file.
130    *
131    * @return true if successful.
132    */
133   bool remove();
134 
135   /**
136    * Closes the file, i.e., the FILE descriptor is closed.
137    */
138   bool close();
139 
140   /**
141    * Read from the file. See fread() for more info.
142    *
143    * @param buf the buffer to read into.
144    * @param itemSize the size of each item.
145    * @param nitems read max n number of items.
146    * @return 0 if successful.
147    */
148   int read(void* buf, size_t itemSize, size_t nitems) const;
149 
150   /**
151    * Read char from the file. See fread() for more info.
152    *
153    * @param buf the buffer to read into.
154    * @param start the start index of the buf.
155    * @param length the length of the buffer.
156    * @return 0 if successful.
157    */
158   int readChar(char* buf, long start, long length) const;
159 
160   /**
161    * Read chars from the file. See fread() for more info.
162    *
163    * @param buf the buffer to read into.
164    * @return 0 if successful.
165    */
166   int readChar(char* buf);
167 
168   /**
169    * Write to file. See fwrite() for more info.
170    *
171    * @param buf the buffer to read from.
172    * @param itemSize the size of each item.
173    * @param nitems write max n number of items.
174    * @return 0 if successful.
175    */
176   int write(const void* buf, size_t itemSize, size_t nitems);
177 
178   /**
179    * Write chars to file. See fwrite() for more info.
180    *
181    * @param buf the buffer to read from.
182    * @param start the start index of the buf.
183    * @param length the length of the buffer.
184    * @return 0 if successful.
185    */
186   int writeChar(const char* buf, long start, long length);
187 
188   /**
189    * Write chars to file. See fwrite() for more info.
190    *
191    * @param buf the buffer to read from.
192    * @return 0 if successful.
193    */
194   int writeChar(const char* buf);
195 
196   /**
197    * Returns the file size.
198    *
199    * @return the file size.
200    */
201   off_t size() const;
202 
203   /**
204    * Returns the filename.
205    *
206    * @return the filename.
207    */
208   const char* getName() const;
209 
210   /**
211    * Flush the buffer.
212    *
213    * @return 0 if successful.
214    */
215   int flush() const;
216 
217 private:
218   FILE* m_file;
219   char m_fileName[PATH_MAX];
220   const char* m_fileMode;
221   /* Prohibit */
222   File_class (const File_class& aCopy);
223   File_class operator = (const File_class&);
224   bool operator == (const File_class&);
225 };
226 #endif
227 
228