1 /*
2
3 Derby - Class org.apache.derby.io.StorageFile
4
5 Licensed to the Apache Software Foundation (ASF) under one or more
6 contributor license agreements. See the NOTICE file distributed with
7 this work for additional information regarding copyright ownership.
8 The ASF licenses this file to You under the Apache License, Version 2.0
9 (the "License"); you may not use this file except in compliance with
10 the License. You may obtain a copy of the License at
11
12 http://www.apache.org/licenses/LICENSE-2.0
13
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19
20 */
21
22 package org.apache.derby.io;
23
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.net.URL;
29 import java.net.MalformedURLException;
30
31 /**
32 * This interface abstracts file naming. Any method in this interface
33 * that also appears in the java.io.File class should behave as the java.io.File method does.
34 *<p>
35 * When used by the database engine all files will be under either the database
36 * directory, specified by the databaseName argument of the {@link
37 * StorageFactory#init StorageFactory.init} method, or under the temporary file
38 * directory returned by the {@link StorageFactory#getTempDir
39 * StorageFactory.getTempDir} method. All relative path names are relative to
40 * the database directory.
41 *<p>
42 * The database engine will call this interface's methods from its own privilege blocks.
43 *<p>
44 * Different threads may operate on the same underlying file at the same time, either through the
45 * same or different StorageFile objects. The StiFile implementation must be capable of handling this.
46 *<p>
47 * @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html">java.io.File</a>
48 */
49 public interface StorageFile
50 {
51
52 //constant values related to exclusive lock mechanism
53 public static final int NO_FILE_LOCK_SUPPORT = 0;
54 public static final int EXCLUSIVE_FILE_LOCK = 1;
55 public static final int EXCLUSIVE_FILE_LOCK_NOT_AVAILABLE = 2;
56
57 /**
58 * Get the names of all files and sub-directories in the directory named by this path name.
59 *
60 * This method is only used in a writable database.
61 *
62 * @return An array of the names of the files and directories in this
63 * directory denoted by this abstract pathname. The returned array will have length 0
64 * if this directory is empty. Returns null if this StorageFile is not a directory, or
65 * if an I/O error occurs. The return value is undefined if the database is read-only.
66 */
67 public String[] list();
68
69 /**
70 * Determine whether the named file is writable.
71 *
72 * @return <b>true</b> if the file exists and is writable, <b>false</b> if not.
73 */
74 public boolean canWrite();
75
76 /**
77 * Tests whether the named file exists.
78 *
79 * @return <b>true</b> if the named file exists, <b>false</b> if not.
80 */
81 public boolean exists();
82
83 /**
84 * Tests whether the named file is a directory, or not. This is only called in writable storage factories.
85 *
86 * @return <b>true</b> if named file exists and is a directory, <b>false</b> if not.
87 * The return value is undefined if the storage is read-only.
88 */
89 public boolean isDirectory();
90
91 /**
92 * Deletes the named file or empty directory. This method does not delete non-empty directories.
93 *
94 * @return <b>true</b> if the named file or directory is successfully deleted, <b>false</b> if not
95 */
96 public boolean delete();
97
98 /**
99 * Deletes the named file and, if it is a directory, all the files and directories it contains.
100 *
101 * @return <b>true</b> if the named file or directory is successfully deleted, <b>false</b> if not
102 */
103 public boolean deleteAll();
104
105 /**
106 * Converts this StorageFile into a pathname string. The character returned by StorageFactory.getSeparator()
107 * is used to separate the directory and file names in the sequence.
108 *
109 *<p>
110 *<b>The returned path may include the database directory. Therefore it cannot be directly used to make an StorageFile
111 * equivalent to this one.</b>
112 *
113 * @return The pathname as a string.
114 *
115 * @see StorageFactory#getSeparator
116 */
117 public String getPath();
118
119 /**
120 * Converts this StorageFile into a canonical pathname string. The form of the canonical path is system dependent.
121 *
122 * @return The pathname as a string.
123 *
124 * @exception IOException if an I/O error occurred while finding the canonical name
125 */
126 public String getCanonicalPath() throws IOException;
127
128 /**
129 * @return The last segment in the path name, "" if the path name sequence is empty.
130 */
131 public String getName();
132
133 /**
134 * Get a URL representing this file. A valid URL does not indicate the file exists,
135 * it may just be a URL that will fail on opening. Some implementations
136 * return null if the file does not exist.
137 * @throws MalformedURLException File cannot be represented as a URL.
138 */
139 public URL getURL() throws MalformedURLException;
140
141 /**
142 * If the named file does not already exist then create it as an empty normal file.
143 *
144 * The implementation
145 * must synchronize with other threads accessing the same file (in the same or a different process).
146 * If two threads both attempt to create a file with the same name
147 * at the same time then at most one should succeed.
148 *
149 * @return <b>true</b> if this thread's invocation of createNewFile successfully created the named file;
150 * <b>false</b> if not, i.e. <b>false</b> if the named file already exists or if another concurrent thread created it.
151 *
152 * @exception IOException - If the directory does not exist or some other I/O error occurred
153 */
154 public boolean createNewFile() throws IOException;
155
156 /**
157 * Rename the file denoted by this name. Note that StorageFile objects are immutable. This method
158 * renames the underlying file, it does not change this StorageFile object. The StorageFile object denotes the
159 * same name as before, however the exists() method will return false after the renameTo method
160 * executes successfully.
161 *
162 *<p>It is not specified whether this method will succeed if a file already exists under the new name.
163 *
164 * @param newName the new name.
165 *
166 * @return <b>true</b> if the rename succeeded, <b>false</b> if not.
167 */
168 public boolean renameTo( StorageFile newName);
169
170 /**
171 * Creates the named directory.
172 *
173 * @return <b>true</b> if the directory was created; <b>false</b> if not.
174 */
175 public boolean mkdir();
176
177 /**
178 * Creates the named directory, and all nonexistent parent directories.
179 *
180 * @return <b>true</b> if the directory was created, <b>false</b> if not
181 */
182 public boolean mkdirs();
183
184 /**
185 * Returns the length of the named file if it is not a directory. The return value is not specified
186 * if the file is a directory.
187 *
188 * @return The length, in bytes, of the named file if it exists and is not a directory,
189 * 0 if the file does not exist, or any value if the named file is a directory.
190 */
191 public long length();
192
193 /**
194 * Get the name of the parent directory if this name includes a parent.
195 *
196 * @return An StorageFile denoting the parent directory of this StorageFile, if it has a parent, null if
197 * it does not have a parent.
198 */
199 public StorageFile getParentDir();
200
201 /**
202 * Make the named file or directory read-only. This interface does not specify whether this
203 * also makes the file undeletable.
204 *
205 * @return <b>true</b> if the named file or directory was made read-only, or it already was read-only;
206 * <b>false</b> if not.
207 */
208 public boolean setReadOnly();
209
210 /**
211 * Creates an output stream from a file name. If a normal file already exists with this name it
212 * will first be truncated to zero length.
213 *
214 * @return an output stream suitable for writing to the file.
215 *
216 * @exception FileNotFoundException if the file exists but is a directory
217 * rather than a regular file, does not exist but cannot be created, or
218 * cannot be opened for any other reason.
219 */
220 public OutputStream getOutputStream( ) throws FileNotFoundException;
221
222 /**
223 * Creates an output stream from a file name.
224 *
225 * @param append If true then data will be appended to the end of the file, if it already exists.
226 * If false and a normal file already exists with this name the file will first be truncated
227 * to zero length.
228 *
229 * @return an output stream suitable for writing to the file.
230 *
231 * @exception FileNotFoundException if the file exists but is a directory
232 * rather than a regular file, does not exist but cannot be created, or
233 * cannot be opened for any other reason.
234 */
235 public OutputStream getOutputStream( boolean append) throws FileNotFoundException;
236
237 /**
238 * Creates an input stream from a file name.
239 *
240 * @return an input stream suitable for reading from the file.
241 *
242 * @exception FileNotFoundException if the file is not found.
243 */
244 public InputStream getInputStream( ) throws FileNotFoundException;
245
246 /**
247 * Get an exclusive lock with this name. This is used to ensure that two or more JVMs do not open the same database
248 * at the same time. ny StorageFile that has getExclusiveFileLock() called on it is not intended to be read from or written to. It's sole purpose is to provide
249 * a locked entity to avoid multiple instances of Derby accessing the same database.
250 * getExclusiveFileLock() may delete or overwrite any existing file.
251 *
252 * @return EXCLUSIVE_FILE_LOCK_NOT_AVAILABLE if the lock cannot be acquired because it is already held.<br>
253 * EXCLUSIVE_FILE_LOCK if the lock was successfully acquired.<br>
254 * NO_FILE_LOCK_SUPPORT if the system does not support exclusive locks.<br>
255 */
256 public int getExclusiveFileLock();
257
258 /**
259 * Release the resource associated with an earlier acquired exclusive lock
260 * releaseExclusiveFileLock() may delete the file
261
262 * @see #getExclusiveFileLock
263 */
264 public void releaseExclusiveFileLock();
265
266 /**
267 * Get a random access file.
268 *
269 * This method is not called if the StorageFactory is read only. It is unspecified if the StorageFactory
270 * that created it is not a WritableStorageFactory.
271 *
272 * @param mode "r", "rw", "rws", or "rwd". The "rws" and "rwd" modes specify
273 * that the data is to be written to persistent store, consistent with the
274 * java.io.RandomAccessFile class ("synchronized" with the persistent
275 * storage, in the file system meaning of the word "synchronized"). However
276 * the implementation is not required to implement the "rws" or "rwd"
277 * modes. If the "rws" andr "rwd" modes are supported then the supportsRws() method
278 * of the StorageFactory returns true. If supportsRws() returns false then the
279 * implementation may treat "rws" and "rwd" as "rw". It is up to
280 * the user of this interface to call the StorageRandomAccessFile.sync
281 * method if necessary. However, if the "rws" or "rwd" modes are supported and the
282 * RandomAccessFile was opened in "rws" or "rwd" mode then the
283 * implementation of StorageRandomAccessFile.sync need not do anything.
284 *
285 * @return an object that can be used for random access to the file.
286 *
287 * @exception IllegalArgumentException if the mode argument is not equal to one of "r", "rw", "rws", or "rwd".
288 * @exception FileNotFoundException if the file exists but is a directory rather than a regular
289 * file, or cannot be opened or created for any other reason .
290 *
291 * @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/io/RandomAccessFile.html">java.io.RandomAccessFile</a>
292 */
293 public StorageRandomAccessFile getRandomAccessFile( String mode) throws FileNotFoundException;
294 }