Source code: com/thermidor/util/file/Backup.java
1 package com.thermidor.util.file;
2 import java.util.*;
3 import java.io.*;
4 import gnu.regexp.*;
5 public class Backup {
6 public static final int NONE=0;
7 public static final int NUMBERED=1;
8 public static final int EXISTING=2;
9 public static final int SIMPLE=3;
10 public static final String STR_NONE="none";
11 public static final String STR_NUMBERED="numbered";
12 public static final String STR_EXISTING="existing";
13 public static final String STR_SIMPLE="simple";
14 public static final String[] BACKUPS= {
15 STR_NONE,
16 STR_NUMBERED,
17 STR_EXISTING,
18 STR_SIMPLE
19 };
20 public static final String BACKUP_REGEXP="^\\(.*\\.\\)~\\([[:digit:]]\\+\\)~$";
21 public static class BackupFilenameFilter implements FilenameFilter {
22 private RE _regexp;
23 private String _fileName;
24 public BackupFilenameFilter(String fileName) throws IOException {
25 _fileName=fileName;
26 try {
27 _regexp=new RE(BACKUP_REGEXP,0,RESyntax.RE_SYNTAX_SED);
28 }
29 catch(REException ree) {
30 throw new IOException("could not construct BackupFileNameFilter invalid regexp");
31 }
32 }
33 public boolean accept(File dir,String name) {
34 boolean retval=false;
35 REMatch match=_regexp.getMatch(name);
36 if(match!=null) {
37 String back=null;
38 int numStart=match.getStartIndex(1);
39 int numEnd=match.getEndIndex(1);
40 back=name.substring(numStart,numEnd-1);
41 retval=_fileName.equals(back);
42 }
43 return retval;
44 }
45 public int backupNumber(String name) {
46 int retval=1;
47 REMatch match=_regexp.getMatch(name);
48 int numStart=match.getStartIndex(2);
49 int numEnd=match.getEndIndex(2);
50 retval=Integer.parseInt(name.substring(numStart,numEnd));
51 return retval;
52 }
53 }
54 private static Hashtable BACKUP_TABLE = new Hashtable();
55 static {
56 BACKUP_TABLE.put(STR_NONE,new BackupNone());
57 BACKUP_TABLE.put(STR_NUMBERED,new BackupNumbered());
58 BACKUP_TABLE.put(STR_EXISTING,new BackupExisting());
59 BACKUP_TABLE.put(STR_SIMPLE,new BackupSimple());
60 }
61 public static BackupAction getBackupAction(String mode) throws IOException{
62 BackupAction retval = null;
63 retval=(BackupAction)BACKUP_TABLE.get(mode);
64 if(retval == null) {
65 throw new IOException("unknown backup mode");
66 }
67 return retval;
68 }
69 private static void copyFileRaw(File src,File dest) throws IOException {
70 FileInputStream fis=new FileInputStream(src);
71 FileOutputStream fos = new FileOutputStream(dest);
72 copy(fis,fos);
73 fos.flush();
74 fos.getFD().sync();
75 fos.close();
76 }
77 private static void copy(InputStream in, OutputStream out) throws IOException {
78 byte[] buffer = new byte[1024];
79 int bytesRead = in.read(buffer);
80 while(bytesRead != -1 ) {
81 out.write(buffer, 0, bytesRead);
82 bytesRead = in.read(buffer);
83 }
84 }
85 public static abstract class BackupAction {
86 public abstract void backup(File file,String suffix) throws IOException ;
87 }
88 public static class BackupNone extends BackupAction {
89 public void backup(File file
90 ,String suffix) throws IOException {}
91 }
92 public static class BackupNumbered extends BackupAction {
93 public void backup(File file,String suffix) throws IOException {
94 if(file.exists()) {
95 if(suffix == null) {
96 suffix = DEFAULT_SUFFIX;
97 }
98 File backupFile;
99 BackupFilenameFilter filter=new BackupFilenameFilter(file.getName());
100 String[] list=file.getParentFile().list(filter);
101 int len=list.length;
102 int highest=0;
103 for(int cnt = 0; cnt < len; cnt++) {
104 int temp=filter.backupNumber(list[cnt]);
105 if(temp>highest) {
106 highest=temp;
107 }
108 }
109 highest++;
110 backupFile=new File(file.getPath()+"."+DEFAULT_SUFFIX+highest+DEFAULT_SUFFIX);
111 CPUtil.copyFileRaw(file,backupFile);
112 }
113 }
114 }
115 public static class BackupExisting extends BackupAction {
116 public void backup(File file,String suffix) throws IOException {
117 if(file.exists()) {
118 if(suffix == null) {
119 suffix = DEFAULT_SUFFIX;
120 }
121 BackupFilenameFilter filter=new BackupFilenameFilter(file.getName());
122 String[] list=file.getParentFile().list(filter);
123 File backupFile = null;
124 if(list.length == 0) {
125 backupFile = new File(file.getPath()+suffix);
126 CPUtil.copyFileRaw(file,backupFile);
127 }
128 else {
129 int len=list.length;
130 int highest=0;
131 for(int cnt = 0; cnt < len; cnt++) {
132 int temp=filter.backupNumber(list[cnt]);
133 if(temp>highest) {
134 highest=temp;
135 }
136 }
137 highest++;
138 backupFile=new File(file.getPath()+DEFAULT_SUFFIX+highest+DEFAULT_SUFFIX);
139 CPUtil.copyFileRaw(file,backupFile);
140 }
141 }
142 }
143 }
144 public static String DEFAULT_SUFFIX="~";
145 public static class BackupSimple extends BackupAction {
146 public void backup(File file,String suffix) throws IOException {
147 if(file.exists()) {
148 if(suffix == null) {
149 suffix = DEFAULT_SUFFIX;
150 }
151 File backupFile = new File(file.getPath()+suffix);
152 CPUtil.copyFileRaw(file,backupFile);
153 }
154 }
155 }
156 }
157
158
159
160