741 |
10 Oct 06 |
olle |
1 |
/* |
1652 |
22 May 07 |
gregory |
$Id$ |
741 |
10 Oct 06 |
olle |
3 |
|
1916 |
31 Aug 07 |
jari |
Copyright (C) 2006 Olle Mansson |
1916 |
31 Aug 07 |
jari |
Copyright (C) 2007 Gregory Vincic |
741 |
10 Oct 06 |
olle |
6 |
|
1652 |
22 May 07 |
gregory |
This file is part of Proteios. |
1652 |
22 May 07 |
gregory |
Available at http://www.proteios.org/ |
741 |
10 Oct 06 |
olle |
9 |
|
1652 |
22 May 07 |
gregory |
Proteios is free software; you can redistribute it and/or modify it |
1652 |
22 May 07 |
gregory |
under the terms of the GNU General Public License as published by |
1652 |
22 May 07 |
gregory |
the Free Software Foundation; either version 2 of the License, or |
1652 |
22 May 07 |
gregory |
(at your option) any later version. |
741 |
10 Oct 06 |
olle |
14 |
|
1652 |
22 May 07 |
gregory |
Proteios is distributed in the hope that it will be useful, but |
1652 |
22 May 07 |
gregory |
WITHOUT ANY WARRANTY; without even the implied warranty of |
1652 |
22 May 07 |
gregory |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
1652 |
22 May 07 |
gregory |
General Public License for more details. |
741 |
10 Oct 06 |
olle |
19 |
|
1652 |
22 May 07 |
gregory |
You should have received a copy of the GNU General Public License |
1652 |
22 May 07 |
gregory |
along with this program; if not, write to the Free Software |
1652 |
22 May 07 |
gregory |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
1652 |
22 May 07 |
gregory |
02111-1307, USA. |
1652 |
22 May 07 |
gregory |
24 |
*/ |
741 |
10 Oct 06 |
olle |
25 |
|
741 |
10 Oct 06 |
olle |
// Xerver Free Web Server |
741 |
10 Oct 06 |
olle |
// Copyright (C) 2002-2005 Omid Rouhani |
741 |
10 Oct 06 |
olle |
28 |
// |
741 |
10 Oct 06 |
olle |
29 |
// |
741 |
10 Oct 06 |
olle |
// This program is free software; you can redistribute it and/or |
741 |
10 Oct 06 |
olle |
// modify it under the terms of the GNU General Public License |
741 |
10 Oct 06 |
olle |
// as published by the Free Software Foundation; either version 2 |
741 |
10 Oct 06 |
olle |
// of the License, or (at your option) any later version. |
741 |
10 Oct 06 |
olle |
34 |
// |
741 |
10 Oct 06 |
olle |
// This program is distributed in the hope that it will be useful, |
741 |
10 Oct 06 |
olle |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
741 |
10 Oct 06 |
olle |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
741 |
10 Oct 06 |
olle |
// GNU General Public License for more details. |
741 |
10 Oct 06 |
olle |
39 |
// |
741 |
10 Oct 06 |
olle |
// You should have received a copy of the GNU General Public License |
741 |
10 Oct 06 |
olle |
// along with this program; if not, write to the Free Software |
741 |
10 Oct 06 |
olle |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
741 |
10 Oct 06 |
olle |
43 |
// |
741 |
10 Oct 06 |
olle |
44 |
// |
741 |
10 Oct 06 |
olle |
45 |
// ############################################################# |
741 |
10 Oct 06 |
olle |
// ## YOU CAN CONTACT THE AUTHOR (OMID ROUHANI) AT: ## |
741 |
10 Oct 06 |
olle |
// ## HTTP://WWW.JAVASCRIPT.NU/XERVER/ ## |
741 |
10 Oct 06 |
olle |
48 |
// ## ## |
741 |
10 Oct 06 |
olle |
// ## IF YOUR SOFTWARE IS NOT RELEASED UNDER THE ## |
741 |
10 Oct 06 |
olle |
// ## GNU GENERAL PUBLIC LICENSE (GPL), ## |
741 |
10 Oct 06 |
olle |
// ## PLEASE DO NOT COPY ANYTHING FROM THIS SOURCE CODE!!! ## |
741 |
10 Oct 06 |
olle |
52 |
// ## ## |
741 |
10 Oct 06 |
olle |
// ## FOR FULL LICENSE, PLEASE READ "XERVER LICENSE". ## |
741 |
10 Oct 06 |
olle |
54 |
// ############################################################# |
741 |
10 Oct 06 |
olle |
55 |
|
1652 |
22 May 07 |
gregory |
56 |
package se.lu.thep.coreftpd.webserver; |
741 |
10 Oct 06 |
olle |
57 |
|
741 |
10 Oct 06 |
olle |
58 |
import java.io.File; |
741 |
10 Oct 06 |
olle |
59 |
import java.util.Arrays; |
741 |
10 Oct 06 |
olle |
60 |
import java.util.Vector; |
741 |
10 Oct 06 |
olle |
61 |
|
741 |
10 Oct 06 |
olle |
62 |
/** |
1652 |
22 May 07 |
gregory |
63 |
* |
1652 |
22 May 07 |
gregory |
* <B>About this class:</B> <BR> |
1652 |
22 May 07 |
gregory |
* This class is not intended to be very general, instead it contains methods |
1652 |
22 May 07 |
gregory |
* that are being used in other classes Xerver uses. However, the class is in no |
1652 |
22 May 07 |
gregory |
* way written just for Xerver, but is general enough to be used in other |
1652 |
22 May 07 |
gregory |
* applications as well. <BR> |
1652 |
22 May 07 |
gregory |
* Given a path, such as "c:\blabla\bla\" you can with this constructor get |
1652 |
22 May 07 |
gregory |
* information about if the path is valid, get a list of all files in this |
1652 |
22 May 07 |
gregory |
* directory or get a list with all folders with read permission in this |
1652 |
22 May 07 |
gregory |
* directory. |
1652 |
22 May 07 |
gregory |
73 |
* |
741 |
10 Oct 06 |
olle |
* @author <a href="http://www.JavaScript.nu/xerver/" TARGET="_top">Omid Rouhani</a> |
741 |
10 Oct 06 |
olle |
* @version 1.0 |
741 |
10 Oct 06 |
olle |
76 |
*/ |
1652 |
22 May 07 |
gregory |
77 |
final public class PathInfo { |
1652 |
22 May 07 |
gregory |
78 |
/* |
1652 |
22 May 07 |
gregory |
* Note, general information: when you make a ".isDirectory()" on a drive |
1652 |
22 May 07 |
gregory |
* without a disk inserted, such as A:\ without a floppy disk, |
1652 |
22 May 07 |
gregory |
* ".isDirectory()" will return false! |
1652 |
22 May 07 |
gregory |
82 |
*/ |
1652 |
22 May 07 |
gregory |
83 |
private File f_currentFolder; // This is NOT null iff "s_path" given to |
1652 |
22 May 07 |
gregory |
// the constructor is a valid AND readable |
1652 |
22 May 07 |
gregory |
// directory (A: without a floppy disk is |
1652 |
22 May 07 |
gregory |
// for example under no circumstances |
1652 |
22 May 07 |
gregory |
// readable). Otherwise this is null. |
1652 |
22 May 07 |
gregory |
88 |
|
1652 |
22 May 07 |
gregory |
89 |
private File[] af_allFiles; // This contains all roots unless a valid AND |
1652 |
22 May 07 |
gregory |
// readable directory is given as "s_path". |
1652 |
22 May 07 |
gregory |
// Otherwise this contains all directories in |
1652 |
22 May 07 |
gregory |
// "s_path". |
1652 |
22 May 07 |
gregory |
93 |
|
1652 |
22 May 07 |
gregory |
94 |
private boolean directoryIsValid; // This is true iff "s_path" is [a valid |
1652 |
22 May 07 |
gregory |
// AND readable directory] OR [null] OR |
1652 |
22 May 07 |
gregory |
96 |
// [""]. |
1652 |
22 May 07 |
gregory |
97 |
|
741 |
10 Oct 06 |
olle |
98 |
private String s_path; |
741 |
10 Oct 06 |
olle |
99 |
|
741 |
10 Oct 06 |
olle |
100 |
/** |
1652 |
22 May 07 |
gregory |
* Creates a <CODE>PathInfo</CODE> with the path given as <CODE>argPath</CODE> |
1652 |
22 May 07 |
gregory |
* (equivalent to <CODE>s_path</CODE>). If <CODE>argPath</CODE> is null |
1652 |
22 May 07 |
gregory |
* a <CODE>PathInfo</CODE> with all roots are created. |
1652 |
22 May 07 |
gregory |
104 |
*/ |
1652 |
22 May 07 |
gregory |
105 |
public PathInfo(String argPath) { |
1652 |
22 May 07 |
gregory |
106 |
s_path = argPath; |
1652 |
22 May 07 |
gregory |
107 |
directoryIsValid = true; // By default it's correct... |
1652 |
22 May 07 |
gregory |
// //directoryIsValid is false "for example" |
1652 |
22 May 07 |
gregory |
// (actually I _think_ it should be "iff") |
1652 |
22 May 07 |
gregory |
// when s_path is [s_path is an invalid |
1652 |
22 May 07 |
gregory |
// path, but not null or ""] OR [when the |
1652 |
22 May 07 |
gregory |
// path is unreadable, such as when you have |
1652 |
22 May 07 |
gregory |
// no floppy disk in drive A:] |
1652 |
22 May 07 |
gregory |
114 |
if (s_path == null) // start with root |
741 |
10 Oct 06 |
olle |
115 |
{ |
1652 |
22 May 07 |
gregory |
116 |
f_currentFolder = null; |
1652 |
22 May 07 |
gregory |
117 |
af_allFiles = File.listRoots(); |
1652 |
22 May 07 |
gregory |
118 |
} else { |
1652 |
22 May 07 |
gregory |
119 |
if (s_path.equals("")) { |
1652 |
22 May 07 |
gregory |
120 |
f_currentFolder = null; |
1652 |
22 May 07 |
gregory |
121 |
af_allFiles = File.listRoots(); |
1652 |
22 May 07 |
gregory |
122 |
} else { |
1652 |
22 May 07 |
gregory |
123 |
f_currentFolder = new File(s_path); |
741 |
10 Oct 06 |
olle |
124 |
if (f_currentFolder.isDirectory() && f_currentFolder.canRead()) |
1652 |
22 May 07 |
gregory |
125 |
af_allFiles = f_currentFolder.listFiles(); |
1652 |
22 May 07 |
gregory |
126 |
else // Invalid path-name |
741 |
10 Oct 06 |
olle |
127 |
{ |
1652 |
22 May 07 |
gregory |
128 |
directoryIsValid = false; // s_path is an invalid path, |
1652 |
22 May 07 |
gregory |
// but not null or "". Or the |
1652 |
22 May 07 |
gregory |
// drive might just be |
1652 |
22 May 07 |
gregory |
// unreadable, such as A: |
1652 |
22 May 07 |
gregory |
// without a floppy disk. |
1652 |
22 May 07 |
gregory |
133 |
f_currentFolder = null; |
1652 |
22 May 07 |
gregory |
134 |
af_allFiles = File.listRoots(); |
741 |
10 Oct 06 |
olle |
135 |
} |
741 |
10 Oct 06 |
olle |
136 |
} |
741 |
10 Oct 06 |
olle |
137 |
} |
741 |
10 Oct 06 |
olle |
138 |
} |
741 |
10 Oct 06 |
olle |
139 |
|
741 |
10 Oct 06 |
olle |
140 |
/** |
1652 |
22 May 07 |
gregory |
* This returns all directories (which you have read permission to) in |
1652 |
22 May 07 |
gregory |
* <CODE>s_path</CODE>. If <CODE>s_path</CODE> is not null (not listing |
1652 |
22 May 07 |
gregory |
* roots), then this also returns one "Up One Level" directory, which can be |
1652 |
22 May 07 |
gregory |
* uesful when listing directories. |
1652 |
22 May 07 |
gregory |
145 |
*/ |
1652 |
22 May 07 |
gregory |
146 |
public MyFile[] getAllDirsWithReadAccess() { |
2142 |
16 Oct 07 |
gregory |
147 |
Vector<File> vec = new Vector<File>(); |
1652 |
22 May 07 |
gregory |
148 |
for (int i = 0, allFilesLength = af_allFiles.length; i < allFilesLength; i++) { |
1652 |
22 May 07 |
gregory |
149 |
File tmpFile = af_allFiles[i]; |
1652 |
22 May 07 |
gregory |
150 |
if (tmpFile.isDirectory() && tmpFile.canRead()) { |
741 |
10 Oct 06 |
olle |
151 |
vec.addElement(new MyFile(tmpFile)); |
741 |
10 Oct 06 |
olle |
152 |
} |
741 |
10 Oct 06 |
olle |
153 |
} |
741 |
10 Oct 06 |
olle |
154 |
|
1652 |
22 May 07 |
gregory |
155 |
MyFile[] returnArray; |
1652 |
22 May 07 |
gregory |
156 |
if (f_currentFolder != null) // readable and valid path |
741 |
10 Oct 06 |
olle |
157 |
{ |
1652 |
22 May 07 |
gregory |
158 |
if (f_currentFolder.getParentFile() != null)// If you can go up one |
1652 |
22 May 07 |
gregory |
// level |
1652 |
22 May 07 |
gregory |
160 |
returnArray = new MyFile[vec.size() + 1]; |
741 |
10 Oct 06 |
olle |
161 |
else |
1652 |
22 May 07 |
gregory |
162 |
returnArray = new MyFile[vec.size()]; |
1652 |
22 May 07 |
gregory |
163 |
} else |
1652 |
22 May 07 |
gregory |
// invalid or unreadable path/drive |
1652 |
22 May 07 |
gregory |
165 |
returnArray = new MyFile[vec.size()]; |
741 |
10 Oct 06 |
olle |
166 |
|
1652 |
22 May 07 |
gregory |
167 |
int vecSize = vec.size(); |
1652 |
22 May 07 |
gregory |
168 |
for (int i = 0; i < vecSize; i++) { |
1652 |
22 May 07 |
gregory |
169 |
returnArray[i] = (MyFile) vec.remove(0); |
741 |
10 Oct 06 |
olle |
170 |
} |
1652 |
22 May 07 |
gregory |
171 |
if (f_currentFolder != null) // readable and valid path |
741 |
10 Oct 06 |
olle |
172 |
{ |
1652 |
22 May 07 |
gregory |
173 |
File tmpParentFile = f_currentFolder.getParentFile(); // Optimization... |
1652 |
22 May 07 |
gregory |
174 |
if (tmpParentFile != null)// If you can go up one level |
741 |
10 Oct 06 |
olle |
175 |
{ |
1652 |
22 May 07 |
gregory |
176 |
int index = returnArray.length - 1; // Optimization... |
1652 |
22 May 07 |
gregory |
177 |
returnArray[index] = new MyFile(tmpParentFile); |
741 |
10 Oct 06 |
olle |
178 |
returnArray[index].isUpOneDir(); |
741 |
10 Oct 06 |
olle |
179 |
} |
741 |
10 Oct 06 |
olle |
180 |
} |
741 |
10 Oct 06 |
olle |
181 |
|
741 |
10 Oct 06 |
olle |
182 |
Arrays.sort(returnArray); |
741 |
10 Oct 06 |
olle |
183 |
return returnArray; |
741 |
10 Oct 06 |
olle |
184 |
} |
741 |
10 Oct 06 |
olle |
185 |
|
741 |
10 Oct 06 |
olle |
186 |
/** |
1652 |
22 May 07 |
gregory |
* Returns "new File(s_path)". Does NOT return null iff "s_path" given to |
1652 |
22 May 07 |
gregory |
* the constructor is a valid AND readable directory (A: without a floppy |
1652 |
22 May 07 |
gregory |
* disk is for example under no circumstances readable). Otherwise this |
1652 |
22 May 07 |
gregory |
* returns null. |
1652 |
22 May 07 |
gregory |
191 |
*/ |
1652 |
22 May 07 |
gregory |
192 |
public File getCurrentFolder() { |
741 |
10 Oct 06 |
olle |
193 |
return f_currentFolder; |
741 |
10 Oct 06 |
olle |
194 |
} |
741 |
10 Oct 06 |
olle |
195 |
|
741 |
10 Oct 06 |
olle |
196 |
/** |
1652 |
22 May 07 |
gregory |
* Returns <CODE>af_allFiles</CODE> (af_allFiles contains all roots unless |
1652 |
22 May 07 |
gregory |
* a valid AND readable directory is given as "s_path". Otherwise |
1652 |
22 May 07 |
gregory |
* af_allFiles contains all directories in "s_path".). |
1652 |
22 May 07 |
gregory |
200 |
*/ |
1652 |
22 May 07 |
gregory |
201 |
public File[] getAllFiles() { |
741 |
10 Oct 06 |
olle |
202 |
return af_allFiles; |
741 |
10 Oct 06 |
olle |
203 |
} |
741 |
10 Oct 06 |
olle |
204 |
|
741 |
10 Oct 06 |
olle |
205 |
/** |
1652 |
22 May 07 |
gregory |
* Returns <CODE>af_allFiles[index]</CODE> (af_allFiles contains all roots |
1652 |
22 May 07 |
gregory |
* unless a valid AND readable directory is given as "s_path". Otherwise |
1652 |
22 May 07 |
gregory |
* af_allFiles contains all directories in "s_path".). |
1652 |
22 May 07 |
gregory |
209 |
*/ |
1652 |
22 May 07 |
gregory |
210 |
public File getAllFiles(int index) { |
741 |
10 Oct 06 |
olle |
211 |
return af_allFiles[index]; |
741 |
10 Oct 06 |
olle |
212 |
} |
741 |
10 Oct 06 |
olle |
213 |
|
741 |
10 Oct 06 |
olle |
214 |
/** |
1652 |
22 May 07 |
gregory |
* Returns true iff "<CODE>s_path</CODE>" is [a valid AND readable |
1652 |
22 May 07 |
gregory |
* directory] OR [null] OR [""]. |
1652 |
22 May 07 |
gregory |
217 |
*/ |
1652 |
22 May 07 |
gregory |
218 |
public boolean getDirectoryIsValid() { |
741 |
10 Oct 06 |
olle |
219 |
return directoryIsValid; |
741 |
10 Oct 06 |
olle |
220 |
} |
741 |
10 Oct 06 |
olle |
221 |
|
741 |
10 Oct 06 |
olle |
222 |
/** |
1652 |
22 May 07 |
gregory |
* Returns <CODE>s_path</CODE>, which is equivalent to <CODE>argPath</CODE> |
1652 |
22 May 07 |
gregory |
* given to the constructor. |
1652 |
22 May 07 |
gregory |
225 |
*/ |
2386 |
14 Nov 07 |
gregory |
226 |
@Override |
1652 |
22 May 07 |
gregory |
227 |
public String toString() { |
741 |
10 Oct 06 |
olle |
228 |
return s_path; |
741 |
10 Oct 06 |
olle |
229 |
} |
741 |
10 Oct 06 |
olle |
230 |
} |