This repository has been archived by the owner on Aug 14, 2023. It is now read-only.
/
VFileUtils.scala
306 lines (264 loc) · 10.5 KB
/
VFileUtils.scala
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import org.apache.commons.vfs2.provider.AbstractFileProvider
import org.apache.commons.vfs2.{FileSystemManager, FileObject}
/** VFS equivalents of Apache Commons FileUtils required for GitBucket.
* As much as possible, this is a literal translation from Java to Scala. */
object VFileUtils {
import java.io.IOException
import java.nio.charset.Charset
import org.apache.commons.io.{IOUtils, Charsets}
implicit class RichFileObject(fileObject: FileObject) {
import org.apache.commons.vfs2.FileType
import java.io.File
def getName: String = fileObject.getName.getBaseName
def isDirectory: Boolean = fileObject.getType==FileType.FOLDER
def isFile: Boolean = fileObject.getType==FileType.FILE
def listFiles: Array[FileObject] =
if (isDirectory) {
val name = fileObject.getName.getBaseName
val sanitizedFileObject =
if (!"".equals(name) && !name.endsWith(File.separator))
fileObject.resolveFile("/")
else
fileObject
sanitizedFileObject.getChildren
} else
Array.empty
def toFile: File = new File(fileObject.getName.getPath)
/** Removes protocol and S3 bucket, if present */
def toAbsoluteName: String = fileObject.getName.getPathDecoded
/** Removes protocol and S3 bucket, if present */
def toAbsoluteFile: File = fileObject.toFile.getAbsoluteFile
/** Removes protocol and S3 bucket, if present */
def toCanonicalFile: File = {
val canonicalDir: File = fileObject.toFile.getParentFile.getCanonicalFile
new File(canonicalDir + File.separator + fileObject.getName.getBaseName).getCanonicalFile
}
/** Removes protocol and S3 bucket, if present */
def toCanonicalName: String = toCanonicalFile.getCanonicalPath
}
/** Cleans a directory without deleting it.
* @param directory directory to clean
* @throws IOException in case cleaning is unsuccessful */
def cleanDirectory(directory: FileObject): Unit = {
if (!directory.exists) {
val message = directory + " does not exist"
throw new IllegalArgumentException(message)
}
if (!directory.isDirectory) {
val message = directory + " is not a directory"
throw new IllegalArgumentException(message)
}
val files = directory.listFiles
if (files == null) {
// null if security restricted
throw new IOException("Failed to list contents of " + directory)
}
var exception: IOException = null;
files foreach { file =>
try {
forceDelete(file)
} catch {
case ioe: IOException =>
exception = ioe
}
}
if (null != exception) {
throw exception
}
}
/**
* Copies a whole directory to a new location preserving the file dates.
* <p>
* This method copies the specified directory and all its child
* directories and files to the specified destination.
* The destination is the new location and name of the directory.
* <p>
* The destination directory is created if it does not exist.
* If the destination directory did exist, then this method merges
* the source with the destination, with the source taking precedence.
* <p>
* <strong>Note:</strong> This method tries to preserve the files' last
* modified date/times using {@link File#setLastModified(long)}, however
* it is not guaranteed that those operations will succeed.
* If the modification operation fails, no indication is provided.
*
* @param srcDir an existing directory to copy, must not be {@code null}
* @param destDir the new directory, must not be {@code null}
*
* @throws NullPointerException if source or destination is {@code null}
* @throws IOException if source or destination is invalid
* @throws IOException if an IO error occurs during copying
* @since 1.1
*/
def copyDirectory(srcDir: FileObject, destDir: FileObject): Unit =
copyDirectory(srcDir, destDir, preserveFileDate = true)
/**
* Copies a whole directory to a new location.
* <p>
* This method copies the contents of the specified source directory
* to within the specified destination directory.
* <p>
* The destination directory is created if it does not exist.
* If the destination directory did exist, then this method merges
* the source with the destination, with the source taking precedence.
* <p>
* <strong>Note:</strong> Setting <code>preserveFileDate</code> to
* {@code true} tries to preserve the files' last modified
* date/times using {@link File#setLastModified(long)}, however it is
* not guaranteed that those operations will succeed.
* If the modification operation fails, no indication is provided.
*
* @param srcDir an existing directory to copy, must not be {@code null}
* @param destDir the new directory, must not be {@code null}
* @param preserveFileDate true if the file date of the copy
* should be the same as the original
*
* @throws NullPointerException if source or destination is {@code null}
* @throws IOException if source or destination is invalid
* @throws IOException if an IO error occurs during copying
* @since 1.1
*/
def copyDirectory(srcDir: FileObject, destDir:FileObject, preserveFileDate: Boolean): Unit = {
import org.apache.commons.vfs2.FileSelector
import org.apache.commons.vfs2.FileSelectInfo
val fileSelector = new FileSelector() {
override def includeFile(fileInfo: FileSelectInfo): Boolean = true
override def traverseDescendents(fileInfo: FileSelectInfo): Boolean = true
}
destDir.copyFrom(srcDir, fileSelector) // org.apache.commons.vfs2.FileSystemException: Could not find files in "s3://mslinntest/testDir".
}
/** Deletes a directory recursively.
* @param directory directory to delete
* @throws IOException in case deletion is unsuccessful */
def deleteDirectory(directory: FileObject): Unit = {
if (!directory.exists)
return
if (!isSymlink(directory))
cleanDirectory(directory)
if (!directory.delete()) {
val message = "Unable to delete directory " + directory + "."
throw new IOException(message)
}
}
def deleteQuietly(fileObject: FileObject): Boolean = {
if (fileObject == null)
return false
try {
if (fileObject.isDirectory) {
cleanDirectory(fileObject)
}
} catch {
case ignored:Exception =>
}
try {
fileObject.delete()
} catch {
case ignored: Exception =>
false
}
}
def forceDelete(file: FileObject): Unit = {
if (file.isDirectory) {
deleteDirectory(file)
} else {
val filePresent = file.exists
if (!file.delete()) {
if (!filePresent) {
throw new java.io.FileNotFoundException("File does not exist: " + file)
}
val message = "Unable to delete file: " + file
throw new IOException(message)
}
}
}
/**
* Determines whether the specified file is a Symbolic Link on a local file system rather than an actual file.
* <p>
* Will not return true if there is a Symbolic Link anywhere in the path,
* only if the specific file is.
* <p>
* <b>Note:</b> the current implementation always returns {@code false} if the system
* is detected as Windows using {@link FilenameUtils#isSystemWindows()}
*
* @param fileObject the file to check
* @return true if the file is a Symbolic Link
* @throws IOException if an IO error occurs while checking the file
* @since 2.0
*/
def isSymlink(fileObject: FileObject): Boolean = {
import java.io.File
import org.apache.commons.vfs2.provider.local.LocalFile
import org.apache.commons.io.FileUtils
if (fileObject == null)
throw new NullPointerException("File must not be null")
fileObject match {
case localFile: LocalFile =>
false
case file =>
try {
val rootFile = fileObject.getFileSystem.getRootName.getRoot
val fileName = rootFile + fileObject.getName.getPathDecoded
val file = new File(fileName)
FileUtils.isSymlink(file)
} catch {
case e: Exception =>
throw new RuntimeException("Error checking if symlink", e)
}
}
}
/**
* Reads the contents of a file into a String. The file is always closed.
*
* @param fileObject
* the file to read, must not be {@code null}
* @param encoding
* the encoding to use, {@code null} means platform default
* @return the file contents, never {@code null}
* @throws IOException
* in case of an I/O error
* @throws UnsupportedCharsetException
* thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
* supported.
* @since 2.3
*/
def readFileToString(fileObject: FileObject, encoding: String): String =
readFileToString(fileObject, Charsets.toCharset(encoding))
/**
* Reads the contents of a file into a String.
* The file is always closed.
*
* @param fileObject the file to read, must not be {@code null}
* @param encoding the encoding to use, {@code null} means platform default
* @return the file contents, never {@code null}
* @throws IOException in case of an I/O error
* @since 2.3
*/
def readFileToString(fileObject: FileObject, encoding: Charset): String =
IOUtils.toString(fileObject.getContent.getInputStream, encoding)
def writeByteArrayToFile(fileObject: FileObject, data: Array[Byte]): Unit =
writeByteArrayToFile(fileObject, data, append = false)
def writeByteArrayToFile(fileObject: FileObject, data: Array[Byte], append: Boolean): Unit = {
import java.io.OutputStream
import org.apache.commons.io.IOUtils
var out: OutputStream = null
try {
out = fileObject.getContent.getOutputStream(append)
out.write(data)
out.close() // don't swallow close Exception if copy completes normally
} finally {
IOUtils.closeQuietly(out)
}
}
}
class VFileUtils(provider: AbstractFileProvider, fsManager: FileSystemManager) {
import com.intridea.io.vfs.provider.s3.S3FileProvider
import org.apache.commons.vfs2.provider.local.DefaultLocalFileProvider
import org.apache.commons.vfs2.FileObject
// TODO delete this
val resolveFile: String => FileObject = provider match {
case s3Provider: S3FileProvider =>
fsManager.resolveFile(_: String)
case localProvider: DefaultLocalFileProvider =>
fsManager.resolveFile(_: String)
}
}