PlatformExporter.java
4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.RepositoryException
* org.apache.commons.io.FileUtils
* org.apache.commons.io.IOUtils
*/
package com.day.jcr.vault.fs.io;
import com.day.jcr.vault.fs.api.AccessType;
import com.day.jcr.vault.fs.api.Artifact;
import com.day.jcr.vault.fs.api.VaultFile;
import com.day.jcr.vault.fs.io.AbstractExporter;
import com.day.jcr.vault.fs.io.ExportInfo;
import com.day.jcr.vault.util.PathUtil;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Map;
import javax.jcr.RepositoryException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
public class PlatformExporter
extends AbstractExporter {
private final File localParent;
private boolean pruneMissing;
public PlatformExporter(File localFile) {
this.localParent = localFile;
}
public boolean pruneMissing() {
return this.pruneMissing;
}
public void setPruneMissing(boolean pruneMissing) {
this.pruneMissing = pruneMissing;
}
public void open() throws IOException, RepositoryException {
this.scan(new File(this.localParent, "jcr_root"));
}
public void close() throws IOException {
if (this.pruneMissing) {
for (ExportInfo.Entry e : this.exportInfo.getEntries().values()) {
if (e.type != ExportInfo.Type.DELETE) continue;
File file = new File(e.path);
FileUtils.deleteQuietly((File)file);
this.track("D", PathUtil.getRelativePath(this.localParent.getAbsolutePath(), e.path));
}
}
}
private void scan(File dir) throws IOException {
File[] files = dir.listFiles();
if (files == null) {
return;
}
for (File file : files) {
String name = file.getName();
if (name.equals(".svn") || name.equals(".vlt")) continue;
if (file.isDirectory()) {
this.exportInfo.update(ExportInfo.Type.RMDIR, file.getPath());
this.scan(file);
continue;
}
this.exportInfo.update(ExportInfo.Type.DELETE, file.getPath());
}
}
public void createDirectory(VaultFile file, String relPath) throws RepositoryException, IOException {
File dir = new File(this.localParent, this.getPlatformFilePath(file, relPath));
this.mkdirs(dir);
this.track("A", PathUtil.getRelativeFilePath(this.localParent.getAbsolutePath(), dir.getAbsolutePath()));
}
public void createDirectory(String relPath) throws IOException {
File dir = new File(this.localParent, relPath);
this.mkdirs(dir);
}
public void writeFile(VaultFile file, String relPath) throws RepositoryException, IOException {
File local = new File(this.localParent, this.getPlatformFilePath(file, relPath));
if (!local.getParentFile().exists()) {
this.mkdirs(local.getParentFile());
}
if (local.exists()) {
this.exportInfo.update(ExportInfo.Type.UPDATE, local.getPath());
} else {
this.exportInfo.update(ExportInfo.Type.ADD, local.getPath());
}
this.track("A", PathUtil.getRelativeFilePath(this.localParent.getAbsolutePath(), local.getAbsolutePath()));
Artifact a = file.getArtifact();
switch (a.getPreferredAccess()) {
case NONE: {
throw new RepositoryException("Artifact has no content.");
}
case SPOOL: {
FileOutputStream out = new FileOutputStream(local);
a.spool(out);
out.close();
break;
}
case STREAM: {
InputStream in = a.getInputStream();
FileOutputStream out = new FileOutputStream(local);
IOUtils.copy((InputStream)in, (OutputStream)out);
in.close();
out.close();
}
}
if (a.getLastModified() >= 0) {
local.setLastModified(a.getLastModified());
}
}
public void writeFile(InputStream in, String relPath) throws IOException {
File local = new File(this.localParent, relPath);
if (!local.getParentFile().exists()) {
this.mkdirs(local.getParentFile());
}
if (local.exists()) {
this.exportInfo.update(ExportInfo.Type.UPDATE, local.getPath());
} else {
this.exportInfo.update(ExportInfo.Type.ADD, local.getPath());
}
FileOutputStream out = new FileOutputStream(local);
IOUtils.copy((InputStream)in, (OutputStream)out);
in.close();
out.close();
}
private void mkdirs(File dir) throws IOException {
dir.mkdirs();
this.exportInfo.update(ExportInfo.Type.MKDIR, dir.getPath());
}
}