1fba2635c088b01af59300e8a02611bb788ee3bffchrismair/* 2fba2635c088b01af59300e8a02611bb788ee3bffchrismair * Copyright 2008 the original author or authors. 3fba2635c088b01af59300e8a02611bb788ee3bffchrismair * 4fba2635c088b01af59300e8a02611bb788ee3bffchrismair * Licensed under the Apache License, Version 2.0 (the "License"); 5fba2635c088b01af59300e8a02611bb788ee3bffchrismair * you may not use this file except in compliance with the License. 6fba2635c088b01af59300e8a02611bb788ee3bffchrismair * You may obtain a copy of the License at 7fba2635c088b01af59300e8a02611bb788ee3bffchrismair * 8fba2635c088b01af59300e8a02611bb788ee3bffchrismair * http://www.apache.org/licenses/LICENSE-2.0 9fba2635c088b01af59300e8a02611bb788ee3bffchrismair * 10fba2635c088b01af59300e8a02611bb788ee3bffchrismair * Unless required by applicable law or agreed to in writing, software 11fba2635c088b01af59300e8a02611bb788ee3bffchrismair * distributed under the License is distributed on an "AS IS" BASIS, 12fba2635c088b01af59300e8a02611bb788ee3bffchrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13fba2635c088b01af59300e8a02611bb788ee3bffchrismair * See the License for the specific language governing permissions and 14fba2635c088b01af59300e8a02611bb788ee3bffchrismair * limitations under the License. 15fba2635c088b01af59300e8a02611bb788ee3bffchrismair */ 16fba2635c088b01af59300e8a02611bb788ee3bffchrismairpackage org.mockftpserver.fake.filesystem; 17fba2635c088b01af59300e8a02611bb788ee3bffchrismair 18fba2635c088b01af59300e8a02611bb788ee3bffchrismairimport java.util.List; 19fba2635c088b01af59300e8a02611bb788ee3bffchrismair 20fba2635c088b01af59300e8a02611bb788ee3bffchrismair/** 21fba2635c088b01af59300e8a02611bb788ee3bffchrismair * Interface for a file system for managing files and directories. 22fba2635c088b01af59300e8a02611bb788ee3bffchrismair * 23fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @author Chris Mair 24fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @version $Revision$ - $Date$ 25fba2635c088b01af59300e8a02611bb788ee3bffchrismair */ 26fba2635c088b01af59300e8a02611bb788ee3bffchrismairpublic interface FileSystem { 27fba2635c088b01af59300e8a02611bb788ee3bffchrismair 28fba2635c088b01af59300e8a02611bb788ee3bffchrismair /** 29fba2635c088b01af59300e8a02611bb788ee3bffchrismair * Add the specified file system entry (file or directory) to this file system 30fba2635c088b01af59300e8a02611bb788ee3bffchrismair * 31fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @param entry - the FileSystemEntry to add 32fba2635c088b01af59300e8a02611bb788ee3bffchrismair */ 33fba2635c088b01af59300e8a02611bb788ee3bffchrismair public void add(FileSystemEntry entry); 34fba2635c088b01af59300e8a02611bb788ee3bffchrismair 35fba2635c088b01af59300e8a02611bb788ee3bffchrismair /** 36fba2635c088b01af59300e8a02611bb788ee3bffchrismair * Return the List of FileSystemEntry objects for the files in the specified directory path. If the 37fba2635c088b01af59300e8a02611bb788ee3bffchrismair * path does not refer to a valid directory, then an empty List is returned. 38fba2635c088b01af59300e8a02611bb788ee3bffchrismair * 39fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @param path - the path of the directory whose contents should be returned 40fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @return the List of FileSystemEntry objects for all files in the specified directory may be empty 41fba2635c088b01af59300e8a02611bb788ee3bffchrismair */ 42fba2635c088b01af59300e8a02611bb788ee3bffchrismair public List listFiles(String path); 43fba2635c088b01af59300e8a02611bb788ee3bffchrismair 44fba2635c088b01af59300e8a02611bb788ee3bffchrismair /** 45fba2635c088b01af59300e8a02611bb788ee3bffchrismair * Return the List of filenames in the specified directory path. The returned filenames do not 46fba2635c088b01af59300e8a02611bb788ee3bffchrismair * include a path. If the path does not refer to a valid directory, then an empty List is 47fba2635c088b01af59300e8a02611bb788ee3bffchrismair * returned. 48fba2635c088b01af59300e8a02611bb788ee3bffchrismair * 49fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @param path - the path of the directory whose contents should be returned 50fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @return the List of filenames (not including paths) for all files in the specified directory 51fba2635c088b01af59300e8a02611bb788ee3bffchrismair * may be empty 52fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @throws AssertionError - if path is null 53fba2635c088b01af59300e8a02611bb788ee3bffchrismair */ 54fba2635c088b01af59300e8a02611bb788ee3bffchrismair public List listNames(String path); 55fba2635c088b01af59300e8a02611bb788ee3bffchrismair 56fba2635c088b01af59300e8a02611bb788ee3bffchrismair /** 57fba2635c088b01af59300e8a02611bb788ee3bffchrismair * Delete the file or directory specified by the path. Return true if the file is successfully 58fba2635c088b01af59300e8a02611bb788ee3bffchrismair * deleted, false otherwise. If the path refers to a directory, it must be empty. Return false 59fba2635c088b01af59300e8a02611bb788ee3bffchrismair * if the path does not refer to a valid file or directory or if it is a non-empty directory. 60fba2635c088b01af59300e8a02611bb788ee3bffchrismair * 61fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @param path - the path of the file or directory to delete 62fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @return true if the file or directory is successfully deleted 63fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @throws AssertionError - if path is null 64fba2635c088b01af59300e8a02611bb788ee3bffchrismair */ 65fba2635c088b01af59300e8a02611bb788ee3bffchrismair public boolean delete(String path); 66fba2635c088b01af59300e8a02611bb788ee3bffchrismair 67fba2635c088b01af59300e8a02611bb788ee3bffchrismair /** 68fba2635c088b01af59300e8a02611bb788ee3bffchrismair * Rename the file or directory. Specify the FROM path and the TO path. Throw an exception if the FROM path or 69fba2635c088b01af59300e8a02611bb788ee3bffchrismair * the parent directory of the TO path do not exist; or if the rename fails for another reason. 70fba2635c088b01af59300e8a02611bb788ee3bffchrismair * 71fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @param fromPath - the source (old) path + filename 72fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @param toPath - the target (new) path + filename 73fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @throws AssertionError - if fromPath or toPath is null 74fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @throws FileSystemException - if the rename fails. 75fba2635c088b01af59300e8a02611bb788ee3bffchrismair */ 76fba2635c088b01af59300e8a02611bb788ee3bffchrismair public void rename(String fromPath, String toPath); 77fba2635c088b01af59300e8a02611bb788ee3bffchrismair 78fba2635c088b01af59300e8a02611bb788ee3bffchrismair /** 79fba2635c088b01af59300e8a02611bb788ee3bffchrismair * Return the formatted directory listing entry for the file represented by the specified FileSystemEntry 80fba2635c088b01af59300e8a02611bb788ee3bffchrismair * 81fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @param fileSystemEntry - the FileSystemEntry representing the file or directory entry to be formatted 82fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @return the the formatted directory listing entry 83fba2635c088b01af59300e8a02611bb788ee3bffchrismair */ 84fba2635c088b01af59300e8a02611bb788ee3bffchrismair public String formatDirectoryListing(FileSystemEntry fileSystemEntry); 85fba2635c088b01af59300e8a02611bb788ee3bffchrismair 86fba2635c088b01af59300e8a02611bb788ee3bffchrismair //------------------------------------------------------------------------- 87fba2635c088b01af59300e8a02611bb788ee3bffchrismair // Path-related Methods 88fba2635c088b01af59300e8a02611bb788ee3bffchrismair //------------------------------------------------------------------------- 89fba2635c088b01af59300e8a02611bb788ee3bffchrismair 90fba2635c088b01af59300e8a02611bb788ee3bffchrismair /** 91fba2635c088b01af59300e8a02611bb788ee3bffchrismair * Return true if there exists a file or directory at the specified path 92fba2635c088b01af59300e8a02611bb788ee3bffchrismair * 93fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @param path - the path 94fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @return true if the file/directory exists 95fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @throws AssertionError - if path is null 96fba2635c088b01af59300e8a02611bb788ee3bffchrismair */ 97fba2635c088b01af59300e8a02611bb788ee3bffchrismair public boolean exists(String path); 98fba2635c088b01af59300e8a02611bb788ee3bffchrismair 99fba2635c088b01af59300e8a02611bb788ee3bffchrismair /** 100fba2635c088b01af59300e8a02611bb788ee3bffchrismair * Return true if the specified path designates an existing directory, false otherwise 101fba2635c088b01af59300e8a02611bb788ee3bffchrismair * 102fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @param path - the path 103fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @return true if path is a directory, false otherwise 104fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @throws AssertionError - if path is null 105fba2635c088b01af59300e8a02611bb788ee3bffchrismair */ 106fba2635c088b01af59300e8a02611bb788ee3bffchrismair public boolean isDirectory(String path); 107fba2635c088b01af59300e8a02611bb788ee3bffchrismair 108fba2635c088b01af59300e8a02611bb788ee3bffchrismair /** 109fba2635c088b01af59300e8a02611bb788ee3bffchrismair * Return true if the specified path designates an existing file, false otherwise 110fba2635c088b01af59300e8a02611bb788ee3bffchrismair * 111fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @param path - the path 112fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @return true if path is a file, false otherwise 113fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @throws AssertionError - if path is null 114fba2635c088b01af59300e8a02611bb788ee3bffchrismair */ 115fba2635c088b01af59300e8a02611bb788ee3bffchrismair public boolean isFile(String path); 116fba2635c088b01af59300e8a02611bb788ee3bffchrismair 117fba2635c088b01af59300e8a02611bb788ee3bffchrismair /** 118fba2635c088b01af59300e8a02611bb788ee3bffchrismair * Return true if the specified path designates an absolute file path. What 119fba2635c088b01af59300e8a02611bb788ee3bffchrismair * constitutes an absolute path is dependent on the file system implementation. 120fba2635c088b01af59300e8a02611bb788ee3bffchrismair * 121fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @param path - the path 122fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @return true if path is absolute, false otherwise 123fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @throws AssertionError - if path is null 124fba2635c088b01af59300e8a02611bb788ee3bffchrismair */ 125fba2635c088b01af59300e8a02611bb788ee3bffchrismair public boolean isAbsolute(String path); 126fba2635c088b01af59300e8a02611bb788ee3bffchrismair 127fba2635c088b01af59300e8a02611bb788ee3bffchrismair /** 128fba2635c088b01af59300e8a02611bb788ee3bffchrismair * Build a path from the two path components. Concatenate path1 and path2. Insert the file system-dependent 129fba2635c088b01af59300e8a02611bb788ee3bffchrismair * separator character in between if necessary (i.e., if both are non-empty and path1 does not already 130fba2635c088b01af59300e8a02611bb788ee3bffchrismair * end with a separator character AND path2 does not begin with one). 131fba2635c088b01af59300e8a02611bb788ee3bffchrismair * 132fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @param path1 - the first path component may be null or empty 133fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @param path2 - the second path component may be null or empty 134fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @return the path resulting from concatenating path1 to path2 135fba2635c088b01af59300e8a02611bb788ee3bffchrismair */ 136fba2635c088b01af59300e8a02611bb788ee3bffchrismair public String path(String path1, String path2); 137fba2635c088b01af59300e8a02611bb788ee3bffchrismair 138fba2635c088b01af59300e8a02611bb788ee3bffchrismair /** 139fba2635c088b01af59300e8a02611bb788ee3bffchrismair * Returns the FileSystemEntry object representing the file system entry at the specified path, or null 140fba2635c088b01af59300e8a02611bb788ee3bffchrismair * if the path does not specify an existing file or directory within this file system. 141fba2635c088b01af59300e8a02611bb788ee3bffchrismair * 142fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @param path - the path of the file or directory within this file system 143fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @return the FileSystemEntry containing the information for the file or directory, or else null 144fba2635c088b01af59300e8a02611bb788ee3bffchrismair */ 145fba2635c088b01af59300e8a02611bb788ee3bffchrismair public FileSystemEntry getEntry(String path); 146fba2635c088b01af59300e8a02611bb788ee3bffchrismair 147fba2635c088b01af59300e8a02611bb788ee3bffchrismair /** 148fba2635c088b01af59300e8a02611bb788ee3bffchrismair * Return the parent path of the specified path. If <code>path</code> specifies a filename, 149fba2635c088b01af59300e8a02611bb788ee3bffchrismair * then this method returns the path of the directory containing that file. If <code>path</code> 150fba2635c088b01af59300e8a02611bb788ee3bffchrismair * specifies a directory, the this method returns its parent directory. If <code>path</code> is 151fba2635c088b01af59300e8a02611bb788ee3bffchrismair * empty or does not have a parent component, then return an empty string. 152fba2635c088b01af59300e8a02611bb788ee3bffchrismair * <p/> 153fba2635c088b01af59300e8a02611bb788ee3bffchrismair * All path separators in the returned path are converted to the system-dependent separator character. 154fba2635c088b01af59300e8a02611bb788ee3bffchrismair * 155fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @param path - the path 156fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @return the parent of the specified path, or null if <code>path</code> has no parent 157fba2635c088b01af59300e8a02611bb788ee3bffchrismair * @throws AssertionError - if path is null 158fba2635c088b01af59300e8a02611bb788ee3bffchrismair */ 159fba2635c088b01af59300e8a02611bb788ee3bffchrismair public String getParent(String path); 160fba2635c088b01af59300e8a02611bb788ee3bffchrismair 161fba2635c088b01af59300e8a02611bb788ee3bffchrismair}