KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > remotefile > FileRemoteFile


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package net.sf.drftpd.remotefile;
19
20 import java.io.File JavaDoc;
21 import java.io.FileNotFoundException JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import net.sf.drftpd.FatalException;
29 import net.sf.drftpd.InvalidDirectoryException;
30 import net.sf.drftpd.slave.Root;
31 import net.sf.drftpd.slave.RootBasket;
32
33 /**
34  * A wrapper for java.io.File to the net.sf.drftpd.RemoteFile structure.
35  *
36  * @author mog
37  * @version $Id: FileRemoteFile.java,v 1.37 2004/05/31 02:47:19 mog Exp $
38  */

39 public class FileRemoteFile extends AbstractRemoteFile {
40     RootBasket rootBasket;
41     String JavaDoc path;
42
43     public FileRemoteFile(RootBasket rootBasket) throws IOException JavaDoc {
44         this(rootBasket, "");
45     }
46     private boolean isFile;
47     private boolean isDirectory;
48     private long length;
49
50     public FileRemoteFile(RootBasket rootBasket, String JavaDoc path)
51         throws IOException JavaDoc {
52         //if(path.length() != 0) {
53
// if(path.charAt(path.length()-1) == File.separatorChar) path = path.substring(0, path.length()-1);
54
//}
55
this.path = path;
56         this.rootBasket = rootBasket;
57         //this.slaves = slaves;
58

59         //check that the roots in the rootBasket are in sync
60
boolean first = true;
61         for (Iterator JavaDoc iter = rootBasket.iterator(); iter.hasNext();) {
62             Root root = (Root) iter.next();
63             File JavaDoc file = root.getFile(path);
64
65             if (!file.exists())
66                 continue;
67
68             // if(file.isDirectory() && file.list().length == 0) {
69
// while(file.list().length == 0) {
70
// File file2 = file.getParentFile();
71
// file.delete();
72
// file = file2;
73
// }
74
// continue;
75
// }
76

77             if (first) {
78                 first = false;
79                 isDirectory = file.isDirectory();
80                 isFile = file.isFile();
81                 if ((!isFile() && !isDirectory())
82                     || (isFile() && isDirectory)) {
83                     throw new IOException JavaDoc(
84                         "(!isFile() && !isDirectory()) || (isFile() && isDirectory): "
85                             + isFile()
86                             + isDirectory()
87                             + " "
88                             + path);
89                 }
90                 if (isDirectory()) {
91                     length = 0;
92                 } else {
93                     length = file.length();
94                 }
95             } else {
96                 if (file.isDirectory() != isDirectory)
97                     throw new IOException JavaDoc(
98                         "roots are out of sync, dir&file mix: " + path);
99                 if (file.isFile() != isFile)
100                     throw new IOException JavaDoc(
101                         "roots are out of sync, file&dir mix: " + path);
102                 if (isFile)
103                     throw new IOException JavaDoc("File collision: " + path);
104                 //if(isFile() && file.length() != length) throw new IOException("roots are out of sync, different sizes: "+path);
105
}
106
107             if (!file
108                 .getCanonicalPath()
109                 .equalsIgnoreCase(file.getAbsolutePath())) {
110                 throw new InvalidDirectoryException(
111                     "Not following symlink: " + file.getAbsolutePath());
112             }
113         }
114     }
115
116     private File JavaDoc getFile() {
117         try {
118             return rootBasket.getFile(getPath());
119         } catch (FileNotFoundException JavaDoc ex) {
120             throw new RuntimeException JavaDoc(ex);
121         }
122     }
123
124     public String JavaDoc getName() {
125         return path.substring(path.lastIndexOf(File.separatorChar) + 1).toString();
126     }
127
128     public String JavaDoc getParent() {
129         throw new UnsupportedOperationException JavaDoc();
130         //return file.getParent();
131
}
132
133     public String JavaDoc getPath() {
134         return path;
135         //throw new UnsupportedOperationException();
136
//return file.getPath();
137
}
138
139     public String JavaDoc getGroupname() {
140         return "drftpd";
141     }
142
143     public String JavaDoc getUsername() {
144         return "drftpd";
145     }
146
147     public boolean isDirectory() {
148         return isDirectory;
149     }
150
151     public boolean isFile() {
152         return isFile;
153     }
154
155     public long lastModified() {
156         return this.getFile().lastModified();
157     }
158
159     public long length() {
160         if (isDirectory()) {
161             return 0;
162         }
163         return getFile().length();
164     }
165
166     /**
167      * Returns an array of FileRemoteFile:s representing the contents of the directory this FileRemoteFile represents.
168      */

169     public RemoteFileInterface[] listFiles() {
170         return (RemoteFileInterface[]) getFiles().toArray(
171             new FileRemoteFile[0]);
172     }
173
174     Hashtable JavaDoc filefiles;
175     private void buildFileFiles() {
176         if (filefiles != null)
177             return;
178         filefiles = new Hashtable JavaDoc();
179
180         if (!isDirectory()) {
181             throw new IllegalArgumentException JavaDoc("listFiles() called on !isDirectory()");
182         }
183         for (Iterator JavaDoc iter = rootBasket.iterator(); iter.hasNext();) {
184             Root root = (Root) iter.next();
185             File JavaDoc file = new File JavaDoc(root.getPath() + "/" + path);
186             if (!file.exists())
187                 continue;
188             if (!file.isDirectory())
189                 throw new FatalException(
190                     file.getPath()
191                         + " is not a directory, attempt to getFiles() on it");
192             if (!file.canRead())
193                 throw new FatalException("Cannot read: " + file);
194             File JavaDoc tmpFiles[] = file.listFiles();
195             //returns null if not a dir, blah!
196
if (tmpFiles == null)
197                 throw new NullPointerException JavaDoc(
198                     "list() on " + file + " returned null");
199             for (int i = 0; i < tmpFiles.length; i++) {
200                 try {
201                     if (tmpFiles[i].isDirectory() && isEmpty(tmpFiles[i])) {
202                         continue;
203                     }
204                     FileRemoteFile listfile =
205                         new FileRemoteFile(
206                             rootBasket,
207                             path + File.separatorChar + tmpFiles[i].getName());
208                     filefiles.put(tmpFiles[i].getName(), listfile);
209                 } catch (IOException JavaDoc e) {
210                     e.printStackTrace();
211                 }
212             }
213         }
214         if(!getName().equals("") && filefiles.isEmpty()) throw new RuntimeException JavaDoc();
215     }
216     /**
217      * @see net.sf.drftpd.remotefile.AbstractRemoteFile#getFiles()
218      */

219     public Collection JavaDoc getFiles() {
220         buildFileFiles();
221         return filefiles.values();
222     }
223
224     /**
225      * @return true if directory contained no files and is now deleted, false otherwise.
226      */

227     private static boolean isEmpty(File JavaDoc dir) {
228         File JavaDoc listfiles[] = dir.listFiles();
229         if (listfiles == null)
230             throw new FatalException("Not a directory or IO error: " + dir);
231         for (int i = 0; i < listfiles.length; i++) {
232             File JavaDoc file = listfiles[i];
233             if (file.isFile())
234                 return false;
235         }
236
237         for (int i = 0; i < listfiles.length; i++) {
238             File JavaDoc file = listfiles[i];
239             // parent directory not empty
240
if (!isEmpty(file))
241                 return false;
242         }
243         dir.delete();
244         return true;
245     }
246
247     public Collection JavaDoc getSlaves() {
248         return new ArrayList JavaDoc();
249     }
250
251     public boolean isDeleted() {
252         return false;
253     }
254 }
255
Popular Tags