1 18 package net.sf.drftpd.remotefile; 19 20 import java.io.IOException ; 21 import java.io.LineNumberReader ; 22 import java.io.PrintWriter ; 23 import java.io.Reader ; 24 import java.io.Writer ; 25 import java.text.ParseException ; 26 import java.text.SimpleDateFormat ; 27 import java.util.ArrayList ; 28 import java.util.Date ; 29 import java.util.Hashtable ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.StringTokenizer ; 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 43 public class MLSTSerialize { 44 public static final SimpleDateFormat timeval = 45 new SimpleDateFormat ("yyyyMMddHHmmss.SSS"); 46 47 public static void serialize(LinkedRemoteFileInterface dir, Writer out) { 48 serialize(dir, new PrintWriter (out)); 49 } 50 public static void serialize(LinkedRemoteFileInterface dir, PrintWriter out) { 51 out.println(dir.getPath() + ":"); 52 for (Iterator 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 for (Iterator 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 toMLST(RemoteFileInterface file) { 71 StringBuffer ret = new StringBuffer (); 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 ("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 (file.lastModified())) + ";"); 91 92 ret.append("unix.owner=" + file.getUsername() + ";"); 93 ret.append("unix.group=" + file.getGroupname() + ";"); 94 if (file.isFile()) { 95 Iterator 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 in, 115 LinkedRemoteFileInterface dir, 116 Hashtable allRslaves, 117 String path) 118 throws IOException { 119 120 for (String 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 filename = line.substring(pos + 1); 133 StaticRemoteFile file = new StaticRemoteFile(filename); 134 StringTokenizer st = 135 new StringTokenizer (line.substring(0, pos), ";"); 136 while (st.hasMoreElements()) { 137 String entry = st.nextToken(); 138 pos = entry.indexOf('='); 139 if(pos == -1) throw new CorruptFileListException("\""+entry+" is corrupt, line "+in.getLineNumber()); 140 String k = entry.substring(0, pos); 141 String v = entry.substring(pos + 1); 142 if ("type".equals(k)) { 143 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 ("!(isFile || isDir)"); 152 } 153 } else if ("modify".equals(k)) { 154 try { 155 file.setLastModified(timeval.parse(v).getTime()); 156 } catch (ParseException 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 rslaves = new ArrayList (); 178 StringTokenizer st2 = new StringTokenizer (v, ","); 179 while (st2.hasMoreTokens()) { 180 String 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() && 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 in, 204 List rslaves) 205 throws IOException , CorruptFileListException { 206 LinkedRemoteFile root = new LinkedRemoteFile(conf); 207 208 LineNumberReader in2 = new LineNumberReader (in); 209 210 for (String 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 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 |