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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
// Copyright (C) 2009 Red Hat, Inc.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
package net.sourceforge.jnlp.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import static net.sourceforge.jnlp.runtime.Translator.R;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.io.Writer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import net.sourceforge.jnlp.runtime.JNLPRuntime;
/**
* This class contains a few file-related utility functions.
*
* @author Omair Majid
*/
public final class FileUtils {
/**
* list of characters not allowed in filenames
*/
private static final char INVALID_CHARS[] = { '\\', '/', ':', '*', '?', '"', '<', '>', '|' };
private static final char SANITIZED_CHAR = '_';
/**
* Clean up a string by removing characters that can't appear in a local
* file name.
*
* @param path
* the path to sanitize
* @return a sanitized version of the input which is suitable for using as a
* file path
*/
public static String sanitizePath(String path) {
for (int i = 0; i < INVALID_CHARS.length; i++)
if (INVALID_CHARS[i] != File.separatorChar)
if (-1 != path.indexOf(INVALID_CHARS[i]))
path = path.replace(INVALID_CHARS[i], SANITIZED_CHAR);
return path;
}
/**
* Given an input, return a sanitized form of the input suitable for use as
* a file/directory name
*
* @param filename the filename to sanitize.
* @return a sanitized version of the input
*/
public static String sanitizeFileName(String filename) {
for (int i = 0; i < INVALID_CHARS.length; i++)
if (-1 != filename.indexOf(INVALID_CHARS[i]))
filename = filename.replace(INVALID_CHARS[i], SANITIZED_CHAR);
return filename;
}
/**
* Creates a new directory with minimum permissions. The directory is not
* readable or writable by anyone other than the owner. The parent
* directories are not created; they must exist before this is called.
*
* @throws IOException
*/
public static void createRestrictedDirectory(File directory) throws IOException {
createRestrictedFile(directory, true, true);
}
/**
* Creates a new file with minimum permissions. The file is not readable or
* writable by anyone other than the owner. If writeableByOnwer is false,
* even the owner can not write to it.
*
* @throws IOException
*/
public static void createRestrictedFile(File file, boolean writableByOwner) throws IOException {
createRestrictedFile(file, false, writableByOwner);
}
/**
* Tries to create the ancestor directories of file f. Throws
* an IOException if it can't be created (but not if it was
* already there).
* @param f
* @param eMsg - the message to use for the exception. null
* if the file name is to be used.
* @throws IOException if the directory can't be created and doesn't exist.
*/
public static void createParentDir(File f, String eMsg) throws IOException {
File parent = f.getParentFile();
if (!parent.isDirectory() && !parent.mkdirs()) {
throw new IOException(R("RCantCreateDir",
eMsg == null ? parent : eMsg));
}
}
/**
* Tries to create the ancestor directories of file f. Throws
* an IOException if it can't be created (but not if it was
* already there).
* @param f
* @throws IOException if the directory can't be created and doesn't exist.
*/
public static void createParentDir(File f) throws IOException {
createParentDir(f, null);
}
/**
* Tries to delete file f. If the file exists but couldn't be deleted,
* print an error message to stderr with the file name, or eMsg if eMsg
* is not null.
* @param f the file to be deleted
* @param eMsg the message to print on failure (or null to print the
* the file name).
*/
public static void deleteWithErrMesg(File f, String eMsg) {
if (f.exists()) {
if (!f.delete()) {
System.err.println(R("RCantDeleteFile", eMsg == null ? f : eMsg));
}
}
}
/**
* Tries to delete file f. If the file exists but couldn't be deleted,
* print an error message to stderr with the file name.
* @param f the file to be deleted
*/
public static void deleteWithErrMesg(File f) {
deleteWithErrMesg(f, null);
}
/**
* Creates a new file or directory with minimum permissions. The file is not
* readable or writable by anyone other than the owner. If writeableByOnwer
* is false, even the owner can not write to it. If isDir is true, then the
* directory can be executed by the owner
*
* @throws IOException
*/
private static void createRestrictedFile(File file, boolean isDir, boolean writableByOwner) throws IOException {
File tempFile = null;
tempFile = new File(file.getCanonicalPath() + ".temp");
if (isDir) {
if (!tempFile.mkdir()) {
throw new IOException(R("RCantCreateDir", tempFile));
}
} else {
if (!tempFile.createNewFile()) {
throw new IOException(R("RCantCreateFile", tempFile));
}
}
if (JNLPRuntime.isWindows()) {
// remove all permissions
if (!tempFile.setExecutable(false, false)) {
System.err.println(R("RRemoveXPermFailed", tempFile));
}
if (!tempFile.setReadable(false, false)) {
System.err.println(R("RRemoveRPermFailed", tempFile));
}
if (!tempFile.setWritable(false, false)) {
System.err.println(R("RRemoveWPermFailed", tempFile));
}
// allow owner to read
if (!tempFile.setReadable(true, true)) {
System.err.println(R("RGetRPermFailed", tempFile));
}
// allow owner to write
if (writableByOwner && !tempFile.setWritable(true, true)) {
System.err.println(R("RGetWPermFailed", tempFile));
}
// allow owner to enter directories
if (isDir && !tempFile.setExecutable(true, true)) {
System.err.println(R("RGetXPermFailed", tempFile));
}
// rename this file. Unless the file is moved/renamed, any program that
// opened the file right after it was created might still be able to
// read the data.
if (!tempFile.renameTo(file)) {
System.err.println(R("RCantRename", tempFile, file));
}
} else {
// remove all permissions
if (!tempFile.setExecutable(false, false)) {
throw new IOException(R("RRemoveXPermFailed", tempFile));
}
if (!tempFile.setReadable(false, false)) {
throw new IOException(R("RRemoveRPermFailed", tempFile));
}
if (!tempFile.setWritable(false, false)) {
throw new IOException(R("RRemoveWPermFailed", tempFile));
}
// allow owner to read
if (!tempFile.setReadable(true, true)) {
throw new IOException(R("RGetRPermFailed", tempFile));
}
// allow owner to write
if (writableByOwner && !tempFile.setWritable(true, true)) {
throw new IOException(R("RGetWPermFailed", tempFile));
}
// allow owner to enter directories
if (isDir && !tempFile.setExecutable(true, true)) {
throw new IOException(R("RGetXPermFailed", tempFile));
}
// rename this file. Unless the file is moved/renamed, any program that
// opened the file right after it was created might still be able to
// read the data.
if (!tempFile.renameTo(file)) {
throw new IOException(R("RCantRename", tempFile, file));
}
}
}
/**
* Returns a String that is suitable for using in GUI elements for
* displaying (long) paths to users.
*
* @param path a path that should be shortened
* @return a shortened path suitable for displaying to the user
*/
public static String displayablePath(String path) {
final int DEFAULT_LENGTH = 40;
return displayablePath(path, DEFAULT_LENGTH);
}
/**
* Return a String that is suitable for using in GUI elements for displaying
* paths to users. If the path is longer than visibleChars, it is truncated
* in a display-friendly way
*
* @param path a path that should be shorted
* @param visibleChars the maximum number of characters that path should fit
* into. Also the length of the returned string
* @return a shortened path that contains limited number of chars
*/
public static String displayablePath(String path, int visibleChars) {
/*
* use a very simple method: prefix + "..." + suffix
*
* where prefix is the beginning part of path (as much as we can squeeze in)
* and suffix is the end path of path
*/
if (path == null || path.length() <= visibleChars) {
return path;
}
final String OMITTED = "...";
final int OMITTED_LENGTH = OMITTED.length();
final int MIN_PREFIX_LENGTH = 4;
final int MIN_SUFFIX_LENGTH = 4;
/*
* we want to show things other than OMITTED. if we have too few for
* suffix and prefix, then just return as much as we can of the filename
*/
if (visibleChars < (OMITTED_LENGTH + MIN_PREFIX_LENGTH + MIN_SUFFIX_LENGTH)) {
return path.substring(path.length() - visibleChars);
}
int affixLength = (visibleChars - OMITTED_LENGTH) / 2;
String prefix = path.substring(0, affixLength);
String suffix = path.substring(path.length() - affixLength);
return prefix + OMITTED + suffix;
}
/**
* Recursively delete everything under a directory. Works on either files or
* directories
*
* @param file the file object representing what to delete. Can be either a
* file or a directory.
* @param base the directory under which the file and its subdirectories must be located
* @throws IOException on an io exception or if trying to delete something
* outside the base
*/
public static void recursiveDelete(File file, File base) throws IOException {
if (JNLPRuntime.isDebug()) {
System.err.println("Deleting: " + file);
}
if (!(file.getCanonicalPath().startsWith(base.getCanonicalPath()))) {
throw new IOException("Trying to delete a file outside Netx's basedir: "
+ file.getCanonicalPath());
}
if (file.isDirectory()) {
File[] children = file.listFiles();
for (int i = 0; i < children.length; i++) {
recursiveDelete(children[i], base);
}
}
if (!file.delete()) {
throw new IOException("Unable to delete file: " + file);
}
}
/**
* This will return a lock to the file specified.
*
* @param path File path to file we want to lock.
* @param shared Specify if the lock will be a shared lock.
* @param allowBlock Specify if we should block when we can not get the
* lock. Getting a shared lock will always block.
* @return FileLock if we were successful in getting a lock, otherwise null.
* @throws FileNotFoundException If the file does not exist.
*/
public static FileLock getFileLock(String path, boolean shared, boolean allowBlock) throws FileNotFoundException {
RandomAccessFile rafFile = new RandomAccessFile(path, "rw");
FileChannel fc = rafFile.getChannel();
FileLock lock = null;
try {
if (!shared) {
if (allowBlock) {
lock = fc.lock(0, Long.MAX_VALUE, false);
} else {
lock = fc.tryLock(0, Long.MAX_VALUE, false);
}
} else { // We want shared lock. This will block regardless if allowBlock is true or not.
// Test to see if we can get a shared lock.
lock = fc.lock(0, 1, true); // Block if a non exclusive lock is being held.
if (!lock.isShared()) { // This lock is an exclusive lock. Use alternate solution.
FileLock tempLock = null;
for (long pos = 1; tempLock == null && pos < Long.MAX_VALUE - 1; pos++) {
tempLock = fc.tryLock(pos, 1, false);
}
lock.release();
lock = tempLock; // Get the unique exclusive lock.
}
}
} catch (IOException e) {
e.printStackTrace();
}
return lock;
}
/**
* helping dummy method to save String as file
*
* @param content
* @param f
* @throws IOException
*/
public static void saveFile(String content, File f) throws IOException {
saveFile(content, f, "utf-8");
}
public static void saveFile(String content, File f, String encoding) throws IOException {
Writer output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), encoding));
output.write(content);
output.flush();
output.close();
}
/**
* utility method which can read from any stream as one long String
*
* @param input stream
* @return stream as string
* @throws IOException if connection can't be established or resource does not exist
*/
public static String getContentOfStream(InputStream is, String encoding) throws IOException {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(is, encoding));
StringBuilder sb = new StringBuilder();
while (true) {
String s = br.readLine();
if (s == null) {
break;
}
sb.append(s).append("\n");
}
return sb.toString();
} finally {
is.close();
}
}
/**
* utility method which can read from any stream as one long String
*
* @param input stream
* @return stream as string
* @throws IOException if connection can't be established or resource does not exist
*/
public static String getContentOfStream(InputStream is) throws IOException {
return getContentOfStream(is, "UTF-8");
}
public static String loadFileAsString(File f) throws IOException {
return getContentOfStream(new FileInputStream(f));
}
public static String loadFileAsString(File f, String encoding) throws IOException {
return getContentOfStream(new FileInputStream(f), encoding);
}
}
|