KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
21 import java.io.LineNumberReader JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.io.Reader JavaDoc;
24 import java.io.Writer JavaDoc;
25 import java.text.ParseException JavaDoc;
26 import java.text.SimpleDateFormat JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Date JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.StringTokenizer JavaDoc;
33
34 import net.sf.drftpd.Checksum;
35 import net.sf.drftpd.master.RemoteSlave;
36 import net.sf.drftpd.master.config.FtpConfig;
37 import net.sf.drftpd.remotefile.LinkedRemoteFile.NonExistingFile;
38
39 /**
40  * @author mog
41  * @version $Id: MLSTSerialize.java,v 1.29 2004/05/12 00:45:10 mog Exp $
42  */

43 public class MLSTSerialize {
44     public static final SimpleDateFormat JavaDoc timeval =
45         new SimpleDateFormat JavaDoc("yyyyMMddHHmmss.SSS");
46
47     public static void serialize(LinkedRemoteFileInterface dir, Writer JavaDoc out) {
48         serialize(dir, new PrintWriter JavaDoc(out));
49     }
50     public static void serialize(LinkedRemoteFileInterface dir, PrintWriter JavaDoc out) {
51         out.println(dir.getPath() + ":");
52         for (Iterator JavaDoc iter = dir.getMap().values().iterator();
53             iter.hasNext();
54             ) {
55             LinkedRemoteFile file = (LinkedRemoteFile) iter.next();
56             out.println(toMLST(file));
57         }
58         out.println();
59
60         //Iterator iter = dir.getFiles().iterator();
61
for (Iterator JavaDoc iter = dir.getMap().values().iterator();
62             iter.hasNext();
63             ) {
64             LinkedRemoteFileInterface file = (LinkedRemoteFileInterface) iter.next();
65             if (file.isDirectory())
66                 serialize(file, out);
67         }
68     }
69
70     public static String JavaDoc toMLST(RemoteFileInterface file) {
71         StringBuffer JavaDoc ret = new StringBuffer JavaDoc();
72         if (file.isLink()) {
73             ret.append("type=unix.slink:" + file.getLinkPath() + ";");
74         } else if (file.isFile()) {
75             ret.append("type=file;");
76         } else if (file.isDirectory()) {
77             ret.append("type=dir;");
78         } else {
79             throw new RuntimeException JavaDoc("type");
80         }
81
82         if (file.getCheckSumCached() != 0)
83             ret.append(
84                 "x.crc32="
85                     + Checksum.formatChecksum(file.getCheckSumCached())
86                     + ";");
87
88         ret.append("size=" + file.length() + ";");
89         ret.append(
90             "modify=" + timeval.format(new Date JavaDoc(file.lastModified())) + ";");
91
92         ret.append("unix.owner=" + file.getUsername() + ";");
93         ret.append("unix.group=" + file.getGroupname() + ";");
94         if (file.isFile()) {
95             Iterator JavaDoc iter = file.getSlaves().iterator();
96             ret.append("x.slaves=");
97             if (iter.hasNext()) {
98                 ret.append(((RemoteSlave) iter.next()).getName());
99                 while (iter.hasNext()) {
100                     ret.append("," + ((RemoteSlave) iter.next()).getName());
101                 }
102             }
103             ret.append(";");
104         }
105         if (file.isDeleted())
106             ret.append("x.deleted=true;");
107         if (file.getXfertime() != 0)
108             ret.append("x.xfertime=" + file.getXfertime() + ";");
109         ret.append(" " + file.getName());
110         return ret.toString();
111     }
112
113     private static void unserialize(
114         LineNumberReader JavaDoc in,
115         LinkedRemoteFileInterface dir,
116         Hashtable JavaDoc allRslaves,
117         String JavaDoc path)
118         throws IOException JavaDoc {
119
120         for (String JavaDoc line = in.readLine();; line = in.readLine()) {
121             boolean isFile = false;
122             boolean isDir = false;
123
124             if (line == null)
125                 throw new CorruptFileListException("Unexpected EOF");
126             if (line.equals(""))
127                 return;
128             int pos = line.indexOf(' ');
129             if (pos == -1)
130                 throw new CorruptFileListException(
131                     "\"" + line + "\" is in valid");
132             String JavaDoc filename = line.substring(pos + 1);
133             StaticRemoteFile file = new StaticRemoteFile(filename);
134             StringTokenizer JavaDoc st =
135                 new StringTokenizer JavaDoc(line.substring(0, pos), ";");
136             while (st.hasMoreElements()) {
137                 String JavaDoc entry = st.nextToken();
138                 pos = entry.indexOf('=');
139                 if(pos == -1) throw new CorruptFileListException("\""+entry+" is corrupt, line "+in.getLineNumber());
140                 String JavaDoc k = entry.substring(0, pos);
141                 String JavaDoc v = entry.substring(pos + 1);
142                 if ("type".equals(k)) {
143                     //assert v.equals("file") || v.equals("dir") : v;
144
if (v.startsWith("unix.slink:")) {
145                         file.setLink(v.substring("unix.slink:".length()));
146                         isDir = true;
147                     } else {
148                         isFile = "file".equals(v);
149                         isDir = "dir".equals(v);
150                         if (!(isFile || isDir))
151                             throw new RuntimeException JavaDoc("!(isFile || isDir)");
152                     }
153                 } else if ("modify".equals(k)) {
154                     try {
155                         file.setLastModified(timeval.parse(v).getTime());
156                     } catch (ParseException JavaDoc e) {
157                         throw new CorruptFileListException(e);
158                     }
159                 } else if ("x.crc32".equals(k)) {
160                     file.setCheckSum(Long.parseLong(v, 16));
161                 } else if ("unix.owner".equals(k)) {
162                     file.setUsername(v);
163                 } else if ("unix.group".equals(k)) {
164                     file.setGroupname(v);
165                 } else if ("x.deleted".equals(k)) {
166                     if (file.isLink()) {
167                         isFile = true;
168                     }
169                     file.setDeleted(true);
170                 } else if ("size".equals(k)) {
171                     file.setLength(Long.parseLong(v));
172                 } else if ("x.slaves".equals(k)) {
173                     if (file.isLink() && isFile) {
174                         isFile = true;
175                         isDir = false;
176                     }
177                     ArrayList JavaDoc rslaves = new ArrayList JavaDoc();
178                     StringTokenizer JavaDoc st2 = new StringTokenizer JavaDoc(v, ",");
179                     while (st2.hasMoreTokens()) {
180                         String JavaDoc slavename = st2.nextToken();
181                         RemoteSlave rslave =
182                             (RemoteSlave) allRslaves.get(slavename);
183                         if (rslave == null)
184                             continue;
185                         rslaves.add(rslave);
186                     }
187                     file.setRSlaves(rslaves);
188                 } else if ("x.xfertime".equals(k)) {
189                     file.setXfertime(Long.parseLong(v));
190                 }
191             }
192             //if(isFile && !file.isFile()) file.setRSlaves(Collections.EMPTY_LIST);
193
if (isFile != file.isFile() && isDir != file.isDirectory())
194                 throw new CorruptFileListException(
195                     "entry is a file but had no x.slaves entry: " + line);
196
197             dir.putFile(file);
198         }
199     }
200
201     public static LinkedRemoteFile unserialize(
202         FtpConfig conf,
203         Reader JavaDoc in,
204         List JavaDoc rslaves)
205         throws IOException JavaDoc, CorruptFileListException {
206         LinkedRemoteFile root = new LinkedRemoteFile(conf);
207
208         LineNumberReader JavaDoc in2 = new LineNumberReader JavaDoc(in);
209
210         for (String JavaDoc line = in2.readLine();
211             line != null;
212             line = in2.readLine()) {
213
214             if (!line.endsWith(":"))
215                 throw new CorruptFileListException(
216                     "expecting path, not \"" + line+"\" line "+in2.getLineNumber());
217
218             String JavaDoc path = line.substring(0, line.length() - 1);
219             NonExistingFile ret = root.lookupNonExistingFile(path, true);
220             LinkedRemoteFileInterface dir;
221             dir = ret.getFile();
222             if (!ret.exists()) {
223                 throw new CorruptFileListException(path + " doesn't exist");
224             }
225
226             unserialize(in2, dir, RemoteSlave.rslavesToHashtable(rslaves), path);
227         }
228         return root;
229     }
230 }
231
Popular Tags