KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > util > ListUtils


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.util;
19
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import net.sf.drftpd.NoAvailableSlaveException;
28 import net.sf.drftpd.SFVFile;
29 import net.sf.drftpd.SFVFile.SFVStatus;
30 import net.sf.drftpd.master.BaseFtpConnection;
31 import net.sf.drftpd.master.FtpReply;
32 import net.sf.drftpd.remotefile.LinkedRemoteFile;
33 import net.sf.drftpd.remotefile.StaticRemoteFile;
34
35 import org.apache.log4j.Logger;
36
37 /**
38  * @author mog
39  * @version $Id: ListUtils.java,v 1.22.2.1 2004/06/19 23:37:28 mog Exp $
40  */

41 public class ListUtils {
42
43     private static final Logger logger = Logger.getLogger(ListUtils.class);
44
45     public static final String JavaDoc PADDING = " ";
46
47     public static boolean isLegalFileName(String JavaDoc fileName) {
48         if (fileName == null)
49             throw new RuntimeException JavaDoc();
50         return fileName.indexOf("/") == -1
51             && fileName.indexOf("?") == -1
52             && fileName.indexOf('*') == -1
53             && !fileName.equals(".")
54             && !fileName.equals("..");
55     }
56
57     public static List JavaDoc list(
58         LinkedRemoteFile directoryFile,
59         BaseFtpConnection conn) {
60         return list(directoryFile, conn, null);
61     }
62
63     public static List JavaDoc list(
64         LinkedRemoteFile dir,
65         BaseFtpConnection conn,
66         FtpReply response) {
67         ArrayList JavaDoc tempFileList = new ArrayList JavaDoc(dir.getFiles());
68         ArrayList JavaDoc listFiles = new ArrayList JavaDoc();
69         int numOnline = 0;
70         int numTotal = 0;
71         for (Iterator JavaDoc iter = tempFileList.iterator(); iter.hasNext();) {
72             LinkedRemoteFile element = (LinkedRemoteFile) iter.next();
73             if (conn.getConfig() != null
74                 && !conn.getConfig().checkPrivPath(conn.getUserNull(), element)) {
75                 // don't add it
76
continue;
77             }
78             // if (element.isDirectory()) { // can slow listing
79
// try {
80
// int filesleft =
81
// element.lookupSFVFile().getStatus().getMissing();
82
// if (filesleft != 0)
83
// listFiles.add(
84
// new StaticRemoteFile(
85
// null,
86
// element.getName()
87
// + "-"
88
// + filesleft
89
// + "-FILES-MISSING",
90
// "drftpd",
91
// "drftpd",
92
// 0L,
93
// dir.lastModified()));
94
// } catch (IOException e) {
95
// } // errors getting SFV? FINE! We don't care!
96
// // is a directory
97
// // numOnline++; // do not want to include directories in the file count
98
// // numTotal++;
99
// listFiles.add(element);
100
// continue;
101
// } else if (
102
if (!element.isAvailable()) { // directories are always available
103
listFiles.add(
104                     new StaticRemoteFile(
105                         Collections.EMPTY_LIST,
106                         element.getName() + "-OFFLINE",
107                         element.getUsername(),
108                         element.getGroupname(),
109                         element.length(),
110                         element.lastModified()));
111                 numTotal++;
112                 // -OFFLINE and "ONLINE" files will both be present until someoe implements
113
// a way to reupload OFFLINE files.
114
// It could be confusing to the user and/or client if the file doesn't exist, but you can't upload it.
115
}
116             //else element is a file, and is online
117
numOnline++;
118             numTotal++;
119             listFiles.add(element);
120         }
121         String JavaDoc statusDirName = null;
122         try {
123             SFVFile sfvfile = dir.lookupSFVFile();
124             SFVStatus sfvstatus = sfvfile.getStatus();
125             if (sfvfile.size() != 0) {
126                 statusDirName = "[ ";
127                 if (sfvstatus.getMissing() != 0) {
128                     statusDirName += sfvstatus.getMissing()
129                         + " files missing = ";
130                 }
131
132                 statusDirName
133                     += (sfvstatus.getPresent() == 0
134                         ? "0"
135                         : "" + (sfvstatus.getPresent() * 100) / sfvfile.size())
136                     + "% complete";
137
138                 if (sfvstatus.getOffline() != 0) {
139                     statusDirName += " | "
140                         + sfvstatus.getOffline()
141                         + " files offline = "
142                         + ((sfvstatus.getAvailable() * 100)
143                             / sfvstatus.getPresent())
144                         + "% online";
145                 }
146                 statusDirName += " ]";
147
148                 if (statusDirName == null)
149                     throw new RuntimeException JavaDoc();
150                 listFiles.add(
151                     new StaticRemoteFile(
152                         null,
153                         statusDirName,
154                         "drftpd",
155                         "drftpd",
156                         0L,
157                         dir.lastModified()));
158
159                 for (Iterator JavaDoc iter = sfvfile.getNames().iterator();
160                     iter.hasNext();
161                     ) {
162                     String JavaDoc filename = (String JavaDoc) iter.next();
163                     if (!dir.hasFile(filename)) {
164                         //listFiles.remove()
165
listFiles.add(
166                             new StaticRemoteFile(
167                                 Collections.EMPTY_LIST,
168                                 filename + "-MISSING",
169                                 "drftpd",
170                                 "drftpd",
171                                 0L,
172                                 dir.lastModified()));
173                     }
174                 }
175             }
176         } catch (NoAvailableSlaveException e) {
177             logger.warn("No available slaves for SFV file", e);
178         } catch (FileNotFoundException JavaDoc e) {
179             // no sfv file in directory - just skip it
180
} catch (IOException JavaDoc e) {
181             logger.warn("IO error loading SFV file", e);
182         } catch (Throwable JavaDoc e) {
183             logger.warn("zipscript error", e);
184         }
185         return listFiles;
186     }
187
188     public static String JavaDoc padToLength(String JavaDoc value, int length) {
189         if (value.length() >= length)
190             return value;
191         if (PADDING.length() < length)
192             throw new RuntimeException JavaDoc("padding must be longer than length");
193         return PADDING.substring(0, length - value.length()) + value;
194     }
195
196     private ListUtils() {
197     }
198 }
199
Popular Tags