OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | Rev 182 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.utils;
15
 
83 ilm 16
import org.openconcerto.utils.CollectionMap2.Mode;
180 ilm 17
import org.openconcerto.utils.DesktopEnvironment.Gnome;
80 ilm 18
import org.openconcerto.utils.OSFamily.Unix;
17 ilm 19
import org.openconcerto.utils.StringUtils.Escaper;
20
import org.openconcerto.utils.cc.ExnTransformer;
21
import org.openconcerto.utils.cc.IClosure;
22
 
23
import java.awt.Desktop;
24
import java.io.BufferedReader;
25
import java.io.BufferedWriter;
26
import java.io.File;
27
import java.io.FileFilter;
28
import java.io.FileInputStream;
67 ilm 29
import java.io.FileNotFoundException;
17 ilm 30
import java.io.FileOutputStream;
31
import java.io.IOException;
32
import java.io.InputStream;
33
import java.io.InputStreamReader;
34
import java.io.OutputStreamWriter;
35
import java.io.RandomAccessFile;
36
import java.io.Reader;
90 ilm 37
import java.net.URI;
17 ilm 38
import java.net.URL;
39
import java.nio.channels.FileChannel;
67 ilm 40
import java.nio.charset.Charset;
156 ilm 41
import java.nio.charset.StandardCharsets;
144 ilm 42
import java.nio.file.CopyOption;
43
import java.nio.file.DirectoryStream;
44
import java.nio.file.DirectoryStream.Filter;
45
import java.nio.file.FileVisitResult;
46
import java.nio.file.Files;
47
import java.nio.file.LinkOption;
180 ilm 48
import java.nio.file.OpenOption;
144 ilm 49
import java.nio.file.Path;
50
import java.nio.file.SimpleFileVisitor;
51
import java.nio.file.StandardCopyOption;
180 ilm 52
import java.nio.file.StandardOpenOption;
144 ilm 53
import java.nio.file.attribute.BasicFileAttributes;
54
import java.nio.file.attribute.PosixFileAttributeView;
55
import java.nio.file.attribute.PosixFilePermissions;
17 ilm 56
import java.util.ArrayList;
57
import java.util.Arrays;
58
import java.util.Collection;
59
import java.util.Collections;
144 ilm 60
import java.util.EnumSet;
17 ilm 61
import java.util.HashMap;
144 ilm 62
import java.util.HashSet;
17 ilm 63
import java.util.List;
64
import java.util.Map;
83 ilm 65
import java.util.Map.Entry;
180 ilm 66
import java.util.Objects;
17 ilm 67
import java.util.Set;
67 ilm 68
import java.util.logging.Level;
73 ilm 69
import java.util.regex.Pattern;
17 ilm 70
 
71
public final class FileUtils {
72
 
180 ilm 73
    public static void main(String[] args) throws Exception {
74
        final String cmd = args[0];
75
        if ("browseFile".equals(cmd))
76
            browseFile(new File(args[1]));
77
        else if ("browse".equals(cmd))
78
            browse(new URI(args[1]));
79
        else
80
            System.err.println("Unkown command : " + cmd);
81
    }
82
 
17 ilm 83
    private FileUtils() {
84
        // all static
85
    }
86
 
93 ilm 87
    public static void browseFile(final File f) throws IOException {
180 ilm 88
        browse(null, Objects.requireNonNull(f));
89
    }
93 ilm 90
 
180 ilm 91
    private static void browse(URI uri, final File f) throws IOException {
92
        assert (uri == null) != (f == null);
93
        boolean handled = false;
94
        final Desktop.Action action = Desktop.Action.BROWSE;
95
        if (isDesktopDesirable(action) && Desktop.isDesktopSupported()) {
96
            final Desktop d = Desktop.getDesktop();
97
            if (d.isSupported(action)) {
98
                if (uri == null)
99
                    uri = f.getCanonicalFile().toURI();
100
                if (!uri.getScheme().equals("file") && DesktopEnvironment.getDE() instanceof Gnome) {
101
                    ProcessBuilder pb = null;
102
                    final String version = DesktopEnvironment.getDE().getVersion();
103
                    // Ubuntu 12.04, 14.04, 16.04
104
                    if (version.startsWith("3.4.") || version.startsWith("3.10.") || version.startsWith("3.18.")) {
105
                        pb = new ProcessBuilder("gvfs-mount", uri.toASCIIString());
106
                        // Ubuntu 18.04
107
                    } else if (version.startsWith("3.28")) {
108
                        // gio/gio-tool-mount.c#mount() calls g_file_mount_enclosing_volume.
109
                        // TODO find out how glib computes "the volume that contains the file
110
                        // location". E.g why does mount "davs://example.com/webdav/dir/dir" mounts
111
                        // "davs://example.com/webdav/".
112
                        // Return 0 if not yet mounted, 2 if it was already mounted.
113
                        pb = new ProcessBuilder("gio", "mount", uri.toASCIIString());
114
                    }
115
                    if (pb != null) {
116
                        try {
117
                            startDiscardingOutput(pb).waitFor();
118
                        } catch (InterruptedException e) {
119
                            throw new RTInterruptedException("Interrupted while waiting on mount for " + uri, e);
120
                        }
121
                    }
122
                }
123
                d.browse(uri);
124
                handled = true;
17 ilm 125
            }
126
        }
180 ilm 127
        if (!handled) {
128
            // if the caller passed a file use it instead of our converted URI
129
            if (f != null)
130
                openNative(f);
131
            else
132
                openNative(uri);
133
        }
17 ilm 134
    }
135
 
180 ilm 136
    public static boolean isDesktopDesirable(Desktop.Action action) {
137
        // apparently the JRE just checks if gnome libs are available (e.g. open Nautilus in XFCE)
138
        return !(action == Desktop.Action.BROWSE && OSFamily.getInstance() == OSFamily.Linux && !(DesktopEnvironment.getDE() instanceof Gnome));
90 ilm 139
    }
140
 
180 ilm 141
    public static void browse(URI uri) throws IOException {
142
        browse(Objects.requireNonNull(uri), null);
143
    }
144
 
17 ilm 145
    public static void openFile(File f) throws IOException {
156 ilm 146
        if (!f.exists()) {
147
            throw new FileNotFoundException(f.getAbsolutePath() + " not found");
148
        }
17 ilm 149
        if (Desktop.isDesktopSupported()) {
150
            Desktop d = Desktop.getDesktop();
151
            if (d.isSupported(Desktop.Action.OPEN)) {
152
                d.open(f.getCanonicalFile());
153
            } else {
154
                openNative(f);
155
            }
156
        } else {
157
            openNative(f);
158
        }
159
    }
160
 
80 ilm 161
    // immutable, getAbsoluteFile() required otherwise list() returns null
162
    static private final File WD = new File("").getAbsoluteFile();
163
 
164
    static public final File getWD() {
165
        return WD;
166
    }
167
 
17 ilm 168
    /**
169
     * All the files (see {@link File#isFile()}) contained in the passed dir.
170
     *
171
     * @param dir the root directory to search.
172
     * @return a List of String.
173
     */
174
    public static List<String> listR(File dir) {
61 ilm 175
        return listR(dir, REGULAR_FILE_FILTER);
17 ilm 176
    }
177
 
61 ilm 178
    public static List<String> listR(File dir, FileFilter ff) {
179
        return listR_rec(dir, ff, ".");
180
    }
181
 
182
    private static List<String> listR_rec(File dir, FileFilter ff, String prefix) {
17 ilm 183
        if (!dir.isDirectory())
184
            return null;
185
 
186
        final List<String> res = new ArrayList<String>();
187
        final File[] children = dir.listFiles();
188
        for (int i = 0; i < children.length; i++) {
189
            final String newPrefix = prefix + "/" + children[i].getName();
61 ilm 190
            if (ff == null || ff.accept(children[i])) {
17 ilm 191
                res.add(newPrefix);
192
            }
61 ilm 193
            if (children[i].isDirectory()) {
194
                res.addAll(listR_rec(children[i], ff, newPrefix));
195
            }
17 ilm 196
        }
197
        return res;
198
    }
199
 
200
    public static void walk(File dir, IClosure<File> c) {
201
        walk(dir, c, RecursionType.BREADTH_FIRST);
202
    }
203
 
204
    public static void walk(File dir, IClosure<File> c, RecursionType type) {
205
        if (type == RecursionType.BREADTH_FIRST)
206
            c.executeChecked(dir);
207
        if (dir.isDirectory()) {
208
            for (final File child : dir.listFiles()) {
209
                walk(child, c, type);
210
            }
211
        }
212
        if (type == RecursionType.DEPTH_FIRST)
213
            c.executeChecked(dir);
214
    }
215
 
216
    public static final List<File> list(File root, final int depth) {
217
        return list(root, depth, null);
218
    }
219
 
220
    /**
221
     * Finds all files at the specified depth below <code>root</code>.
222
     *
223
     * @param root the base directory
224
     * @param depth the depth of the returned files.
225
     * @param ff a filter, can be <code>null</code>.
226
     * @return a list of files <code>depth</code> levels beneath <code>root</code>.
227
     */
228
    public static final List<File> list(File root, final int depth, final FileFilter ff) {
61 ilm 229
        return list(root, depth, depth, ff);
230
    }
231
 
232
    public static final List<File> list(final File root, final int minDepth, final int maxDepth, final FileFilter ff) {
233
        return list(root, minDepth, maxDepth, ff, false);
234
    }
235
 
236
    public static final List<File> list(final File root, final int minDepth, final int maxDepth, final FileFilter ff, final boolean sort) {
237
        if (minDepth > maxDepth)
238
            throw new IllegalArgumentException(minDepth + " > " + maxDepth);
239
        if (maxDepth < 0)
240
            throw new IllegalArgumentException(maxDepth + " < 0");
17 ilm 241
        if (!root.exists())
242
            return Collections.<File> emptyList();
61 ilm 243
 
244
        final File currentFile = accept(ff, minDepth, maxDepth, root, 0) ? root : null;
245
        if (maxDepth == 0) {
246
            return currentFile == null ? Collections.<File> emptyList() : Collections.singletonList(currentFile);
17 ilm 247
        } else {
61 ilm 248
            final List<File> res = new ArrayList<File>();
249
            final File[] children = root.listFiles();
250
            if (children == null)
17 ilm 251
                throw new IllegalStateException("cannot list " + root);
61 ilm 252
            if (sort)
253
                Arrays.sort(children);
254
            for (final File child : children) {
255
                if (maxDepth > 1 && child.isDirectory()) {
256
                    res.addAll(list(child, minDepth - 1, maxDepth - 1, ff, sort));
257
                } else if (accept(ff, minDepth, maxDepth, child, 1)) {
258
                    res.add(child);
259
                }
17 ilm 260
            }
61 ilm 261
            if (currentFile != null)
262
                res.add(currentFile);
17 ilm 263
            return res;
264
        }
265
    }
266
 
61 ilm 267
    private static final boolean accept(final FileFilter ff, final int minDepth, final int maxDepth, final File f, final int depth) {
268
        return minDepth <= depth && depth <= maxDepth && (ff == null || ff.accept(f));
269
    }
270
 
17 ilm 271
    /**
272
     * Returns the relative path from one file to another in the same filesystem tree. Files are not
273
     * required to exist, see {@link File#getCanonicalPath()}.
274
     *
275
     * @param fromDir the starting directory, eg /a/b/.
276
     * @param to the file to get to, eg /a/x/y.txt.
277
     * @return the relative path, eg "../x/y.txt".
278
     * @throws IOException if an error occurs while canonicalizing the files.
279
     * @throws IllegalArgumentException if fromDir exists and is not directory.
280
     */
281
    public static final String relative(File fromDir, File to) throws IOException {
282
        if (fromDir.exists() && !fromDir.isDirectory())
283
            throw new IllegalArgumentException(fromDir + " is not a directory");
284
 
285
        final File fromF = fromDir.getCanonicalFile();
286
        final File toF = to.getCanonicalFile();
287
        final List<File> toPath = getAncestors(toF);
288
        final List<File> fromPath = getAncestors(fromF);
289
 
290
        // no common ancestor (for example on Windows on 2 different letters)
291
        if (!toPath.get(0).equals(fromPath.get(0))) {
292
            // already canonical
293
            return toF.getPath();
294
        }
295
 
296
        int commonIndex = Math.min(toPath.size(), fromPath.size()) - 1;
297
        boolean found = false;
298
        while (commonIndex >= 0 && !found) {
299
            found = fromPath.get(commonIndex).equals(toPath.get(commonIndex));
300
            if (!found)
301
                commonIndex--;
302
        }
303
 
304
        // on remonte jusqu'à l'ancêtre commun
305
        final List<String> complete = new ArrayList<String>(Collections.nCopies(fromPath.size() - 1 - commonIndex, ".."));
306
        if (complete.isEmpty())
307
            complete.add(".");
308
        // puis on descend vers 'to'
309
        for (File f : toPath.subList(commonIndex + 1, toPath.size())) {
310
            complete.add(f.getName());
311
        }
312
 
313
        return CollectionUtils.join(complete, File.separator);
314
    }
315
 
316
    // return each ancestor of f (including itself)
317
    // eg [/, /folder, /folder/dir] for /folder/dir
318
    public final static List<File> getAncestors(File f) {
319
        final List<File> path = new ArrayList<File>();
320
        File currentF = f;
321
        while (currentF != null) {
322
            path.add(0, currentF);
323
            currentF = currentF.getParentFile();
324
        }
325
        return path;
326
    }
327
 
328
    public final static File addSuffix(File f, String suffix) {
329
        return new File(f.getParentFile(), f.getName() + suffix);
330
    }
331
 
332
    /**
333
     * Prepend a string to a suffix.
334
     *
335
     * @param f the file, e.g. "sample.xml".
336
     * @param toInsert the string to insert in the filename, e.g. "-sql".
337
     * @param suffix the suffix of <code>f</code>, e.g. ".xml".
338
     * @return a new file with <code>toInsert</code> prepended to <code>suffix</code>, e.g.
339
     *         "sample-sql.xml".
340
     */
341
    public final static File prependSuffix(File f, String toInsert, String suffix) {
342
        return new File(f.getParentFile(), removeSuffix(f.getName(), suffix) + toInsert + suffix);
343
    }
344
 
144 ilm 345
    /**
346
     * Prepend a string to a suffix.
347
     *
348
     * @param f the file, e.g. "sample.xml".
349
     * @param toInsert the string to insert in the filename, e.g. "-sql".
350
     * @param suffix the suffix of <code>f</code>, e.g. ".xml".
351
     * @return a new file with <code>toInsert</code> prepended to <code>suffix</code>, e.g.
352
     *         "sample-sql.xml".
353
     */
354
    public final static Path prependSuffix(Path f, String toInsert, String suffix) {
355
        return f.resolveSibling(removeSuffix(f.getFileName().toString(), suffix) + toInsert + suffix);
356
    }
357
 
17 ilm 358
    public final static String removeSuffix(String name, String suffix) {
359
        return name.endsWith(suffix) ? name.substring(0, name.length() - suffix.length()) : name;
360
    }
361
 
362
    /**
363
     * Rename a file if necessary by finding a free name. The tested names are
364
     * <code>name + "_" + i + suffix</code>.
365
     *
366
     * @param parent the directory.
367
     * @param name the base name of the file.
368
     * @param suffix the suffix of the file, e.g. ".ods".
369
     * @return <code>new File(parent, name + suffix)</code> (always non existing) and the new file,
370
     *         (or <code>null</code> if no file was moved).
371
     */
372
    public final static File[] mvOut(final File parent, final String name, final String suffix) {
373
        final File fDest = new File(parent, name + suffix);
374
        final File renamed;
375
        if (fDest.exists()) {
376
            int i = 0;
377
            File free = fDest;
378
            while (free.exists()) {
379
                free = new File(parent, name + "_" + i + suffix);
380
                i++;
381
            }
382
            assert !fDest.equals(free);
383
            if (!fDest.renameTo(free))
384
                throw new IllegalStateException("Couldn't rename " + fDest + " to " + free);
385
            renamed = free;
386
        } else {
387
            renamed = null;
388
        }
389
        assert !fDest.exists();
390
        return new File[] { fDest, renamed };
391
    }
392
 
393
    // ** shell
394
 
395
    /**
396
     * Behave like the 'mv' unix utility, ie handle cross filesystems mv and <code>dest</code> being
397
     * a directory.
398
     *
399
     * @param f the source file.
400
     * @param dest the destination file or directory.
401
     * @return the error or <code>null</code> if there was none.
402
     */
403
    public static String mv(File f, File dest) {
404
        final File canonF;
405
        File canonDest;
406
        try {
407
            canonF = f.getCanonicalFile();
408
            canonDest = dest.getCanonicalFile();
409
        } catch (IOException e) {
410
            return ExceptionUtils.getStackTrace(e);
411
        }
412
        if (canonF.equals(canonDest))
413
            // nothing to do
414
            return null;
415
        if (canonDest.isDirectory())
416
            canonDest = new File(canonDest, canonF.getName());
417
 
418
        final File destF;
419
        if (canonDest.exists())
420
            return canonDest + " exists";
421
        else if (!canonDest.getParentFile().exists())
422
            return "parent of " + canonDest + " does not exist";
423
        else
424
            destF = canonDest;
425
        if (!canonF.renameTo(destF)) {
426
            try {
427
                copyDirectory(canonF, destF);
428
                if (destF.exists())
144 ilm 429
                    rm_R(canonF);
17 ilm 430
            } catch (IOException e) {
431
                return ExceptionUtils.getStackTrace(e);
432
            }
433
        }
434
        return null;
435
    }
436
 
437
    // transferTo() can be limited by a number of factors, like the number of bits of the system
438
    // if mmap is used (e.g. on Linux) or by an arbitrary magic number on Windows : 64Mb - 32Kb
439
    private static final int CHANNEL_MAX_COUNT = Math.min(64 * 1024 * 1024 - 32 * 1024, Integer.MAX_VALUE);
440
 
441
    public static void copyFile(File in, File out) throws IOException {
442
        copyFile(in, out, CHANNEL_MAX_COUNT);
443
    }
444
 
445
    /**
446
     * Copy a file. It is generally not advised to use 0 for <code>maxCount</code> since various
447
     * implementations have size limitations, see {@link #copyFile(File, File)}.
448
     *
449
     * @param in the source file.
450
     * @param out the destination file.
451
     * @param maxCount the number of bytes to copy at a time, 0 meaning size of <code>in</code>.
452
     * @throws IOException if an error occurs.
453
     */
454
    public static void copyFile(File in, File out, long maxCount) throws IOException {
149 ilm 455
        try (final FileInputStream sourceIn = new FileInputStream(in); final FileOutputStream sourceOut = new FileOutputStream(out);) {
83 ilm 456
            final FileChannel sourceChannel = sourceIn.getChannel();
17 ilm 457
            final long size = sourceChannel.size();
83 ilm 458
            if (maxCount == 0)
459
                maxCount = size;
460
            final FileChannel destinationChannel = sourceOut.getChannel();
17 ilm 461
            long position = 0;
462
            while (position < size) {
463
                position += sourceChannel.transferTo(position, maxCount, destinationChannel);
464
            }
465
        }
466
    }
467
 
25 ilm 468
    public static void copyFile(File in, File out, final boolean useTime) throws IOException {
469
        if (!useTime || in.lastModified() != out.lastModified()) {
470
            copyFile(in, out);
471
            if (useTime)
472
                out.setLastModified(in.lastModified());
473
        }
474
    }
475
 
17 ilm 476
    public static void copyDirectory(File in, File out) throws IOException {
477
        copyDirectory(in, out, Collections.<String> emptySet());
478
    }
479
 
480
    public static final Set<String> VersionControl = CollectionUtils.createSet(".svn", "CVS");
481
 
482
    public static void copyDirectory(File in, File out, final Set<String> toIgnore) throws IOException {
25 ilm 483
        copyDirectory(in, out, toIgnore, false);
484
    }
485
 
486
    public static void copyDirectory(File in, File out, final Set<String> toIgnore, final boolean useTime) throws IOException {
17 ilm 487
        if (toIgnore.contains(in.getName()))
488
            return;
489
 
490
        if (in.isDirectory()) {
491
            if (!out.exists()) {
492
                out.mkdir();
493
            }
494
 
495
            String[] children = in.list();
496
            for (int i = 0; i < children.length; i++) {
25 ilm 497
                copyDirectory(new File(in, children[i]), new File(out, children[i]), toIgnore, useTime);
17 ilm 498
            }
499
        } else {
500
            if (!in.getName().equals("Thumbs.db")) {
25 ilm 501
                copyFile(in, out, useTime);
17 ilm 502
            }
503
        }
504
    }
505
 
144 ilm 506
    public static void copyDirectory(Path in, Path out) throws IOException {
507
        copyDirectory(in, out, false);
508
    }
509
 
510
    public static void copyDirectory(Path in, Path out, final boolean hardLink, final CopyOption... copyOptions) throws IOException {
511
        copyDirectory(in, out, Collections.<String> emptySet(), hardLink, false, Arrays.asList(copyOptions));
512
    }
513
 
514
    public static void copyDirectory(Path in, Path out, final Set<String> toIgnore, final boolean hardLink, final boolean followLinks, final List<CopyOption> copyOptions) throws IOException {
515
        final LinkOption[] isDirOptions = followLinks ? new LinkOption[0] : new LinkOption[] { LinkOption.NOFOLLOW_LINKS };
516
        final Set<CopyOption> copyOptionsP = new HashSet<>(copyOptions);
517
        if (followLinks) {
518
            copyOptionsP.remove(LinkOption.NOFOLLOW_LINKS);
519
        } else {
520
            copyOptionsP.add(LinkOption.NOFOLLOW_LINKS);
521
        }
522
        copyDirectory(in, out, toIgnore, isDirOptions, copyOptionsP.toArray(new CopyOption[copyOptionsP.size()]), hardLink);
523
    }
524
 
525
    public static void copyDirectory(Path in, Path out, final Set<String> toIgnore, final LinkOption[] isDirOptions, final CopyOption[] copyOptions, final boolean hardLink) throws IOException {
526
        if (toIgnore.contains(in.getFileName().toString()))
527
            return;
528
 
529
        if (Files.isDirectory(in, isDirOptions)) {
530
            Files.copy(in, out, copyOptions);
531
 
532
            try (final DirectoryStream<Path> dirStream = Files.newDirectoryStream(in)) {
533
                for (final Path child : dirStream) {
534
                    copyDirectory(child, out.resolve(child.getFileName()), toIgnore, isDirOptions, copyOptions, hardLink);
535
                }
536
            }
537
            // fix up modification time of directory when done
538
            // (other attributes have already been copied at creation time)
539
            if (Arrays.asList(copyOptions).contains(StandardCopyOption.COPY_ATTRIBUTES))
540
                Files.setLastModifiedTime(out, Files.getLastModifiedTime(in));
541
        } else {
542
            if (!in.getFileName().toString().equals("Thumbs.db")) {
543
                if (hardLink)
544
                    Files.createLink(out, in);
545
                else
546
                    Files.copy(in, out, copyOptions);
547
            }
548
        }
549
    }
550
 
17 ilm 551
    /**
552
     * Delete recursively the passed directory. If a deletion fails, the method stops attempting to
553
     * delete and returns false.
554
     *
555
     * @param dir the dir to be deleted.
556
     * @return <code>true</code> if all deletions were successful.
144 ilm 557
     * @deprecated callers often forget to check return value, see also {@link #rm_R(Path)}
17 ilm 558
     */
559
    public static boolean rmR(File dir) {
560
        if (dir.isDirectory()) {
561
            File[] children = dir.listFiles();
562
            for (int i = 0; i < children.length; i++) {
563
                boolean success = rmR(children[i]);
564
 
565
                if (!success) {
566
 
567
                    return false;
568
                }
569
            }
570
        }
571
 
572
        // The directory is now empty so delete it
573
        return dir.delete();
574
    }
575
 
25 ilm 576
    public static void rm_R(File dir) throws IOException {
144 ilm 577
        rm_R(dir.toPath());
578
    }
579
 
580
    public static void rm_R(Path dir) throws IOException {
581
        rm_R(dir, false);
582
    }
583
 
584
    /**
585
     * Delete recursively the passed file.
586
     *
587
     * @param dir the file to delete.
588
     * @param mustExist what to do if <code>dir</code> is missing : <code>false</code> if it is not
589
     *        an error, <code>true</code> to fail.
590
     * @throws IOException if a deletion fails.
591
     */
592
    public static void rm_R(final Path dir, final boolean mustExist) throws IOException {
593
        if (!mustExist && !Files.exists(dir))
594
            return;
595
        Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
596
            @Override
597
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
598
                Files.delete(file);
599
                return FileVisitResult.CONTINUE;
25 ilm 600
            }
144 ilm 601
 
602
            @Override
603
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
604
                // The directory is now empty so delete it
605
                Files.delete(dir);
606
                return FileVisitResult.CONTINUE;
607
            }
608
        });
25 ilm 609
    }
610
 
611
    public static void rm(File f) throws IOException {
612
        if (f.exists() && !f.delete())
613
            throw new IOException("cannot delete " + f);
614
    }
615
 
17 ilm 616
    public static final File mkdir_p(File dir) throws IOException {
617
        if (!dir.exists()) {
618
            if (!dir.mkdirs()) {
619
                throw new IOException("cannot create directory " + dir);
620
            }
621
        }
622
        return dir;
623
    }
624
 
625
    /**
626
     * Create all ancestors of <code>f</code>.
627
     *
628
     * @param f any file whose ancestors should be created.
629
     * @return <code>f</code>.
630
     * @throws IOException if ancestors cannot be created.
631
     */
632
    public static final File mkParentDirs(File f) throws IOException {
633
        final File parentFile = f.getParentFile();
634
        if (parentFile != null)
635
            mkdir_p(parentFile);
636
        return f;
637
    }
638
 
639
    // **io
640
 
641
    /**
642
     * Read a file line by line with the default encoding and returns the concatenation of these.
643
     *
644
     * @param f the file to read.
645
     * @return the content of f.
646
     * @throws IOException if a pb occur while reading.
647
     */
648
    public static final String read(File f) throws IOException {
83 ilm 649
        return read(new InputStreamReader(new FileInputStream(f)));
17 ilm 650
    }
651
 
652
    /**
653
     * Read a file line by line and returns the concatenation of these.
654
     *
655
     * @param f the file to read.
83 ilm 656
     * @param charset the encoding of <code>f</code>.
17 ilm 657
     * @return the content of f.
658
     * @throws IOException if a pb occur while reading.
659
     */
660
    public static final String read(File f, String charset) throws IOException {
83 ilm 661
        return read(new InputStreamReader(new FileInputStream(f), charset));
662
    }
663
 
664
    public static final String readUTF8(File f) throws IOException {
665
        return readUTF8(new FileInputStream(f));
666
    }
667
 
156 ilm 668
    public static final String readUTF8(Path p) throws IOException {
669
        return new String(Files.readAllBytes(p), StandardCharsets.UTF_8);
670
    }
671
 
83 ilm 672
    public static final String readUTF8(InputStream ins) throws IOException {
673
        return read(ins, StringUtils.UTF8);
674
    }
675
 
676
    public static final String read(File f, Charset charset) throws IOException {
17 ilm 677
        return read(new FileInputStream(f), charset);
678
    }
679
 
83 ilm 680
    public static final String read(InputStream ins, Charset charset) throws IOException {
17 ilm 681
        final Reader reader;
682
        if (charset == null)
683
            reader = new InputStreamReader(ins);
684
        else
685
            reader = new InputStreamReader(ins, charset);
686
        return read(reader);
687
    }
688
 
689
    public static final String read(final Reader reader) throws IOException {
690
        return read(reader, 8192);
691
    }
692
 
693
    public static final String read(final Reader reader, final int bufferSize) throws IOException {
694
        final StringBuilder sb = new StringBuilder();
695
        final char[] buffer = new char[bufferSize];
696
        final BufferedReader in = new BufferedReader(reader);
697
        try {
698
            while (true) {
699
                final int count = in.read(buffer);
700
                if (count == -1)
701
                    break;
702
                sb.append(buffer, 0, count);
703
            }
704
        } finally {
705
            in.close();
706
        }
707
        return sb.toString();
708
    }
709
 
710
    /**
711
     * Read the whole content of a file.
712
     *
713
     * @param f the file to read.
714
     * @return its content.
715
     * @throws IOException if a pb occur while reading.
144 ilm 716
     * @see Files#readAllBytes(java.nio.file.Path)
17 ilm 717
     */
718
    public static final byte[] readBytes(File f) throws IOException {
144 ilm 719
        // works for /proc files which report 0 size
720
        return Files.readAllBytes(f.toPath());
17 ilm 721
    }
722
 
180 ilm 723
    /**
724
     * Write the passed string with default charset to the passed file, truncating it.
725
     *
726
     * @param s the string.
727
     * @param f the file.
728
     * @throws IOException if an error occurs.
729
     * @deprecated use {@link #writeUTF8(Path, String, OpenOption...)} or
730
     *             {@link #write2Step(String, Path)}
731
     */
17 ilm 732
    public static void write(String s, File f) throws IOException {
180 ilm 733
        write2Step(s, f, Charset.defaultCharset(), false);
17 ilm 734
    }
735
 
180 ilm 736
    public static void writeUTF8(String s, File f) throws IOException {
737
        writeUTF8(f.toPath(), s);
738
    }
739
 
740
    public static void writeUTF8(Path path, String s, OpenOption... options) throws IOException {
741
        Files.write(path, s.getBytes(StandardCharsets.UTF_8), options);
742
    }
743
 
744
    public static void write2Step(String s, File f, Charset charset, boolean append) throws IOException {
745
        write2Step(s, f.toPath(), charset, append);
746
    }
747
 
748
    public static void write2Step(final String s, final Path f) throws IOException {
749
        // UTF_8 default like Files.newBufferedReader()
750
        write2Step(s, f, StandardCharsets.UTF_8);
751
    }
752
 
753
    public static void write2Step(final String s, final Path f, final Charset charset) throws IOException {
754
        write2Step(s, f, charset, false);
755
    }
756
 
757
    public static void write2Step(final String s, final Path f, final Charset charset, final boolean append) throws IOException {
758
        // create temporary file in the same directory so we can move it
759
        final Path tmpFile = Files.createTempFile(f.toAbsolutePath().getParent(), null, null);
760
        try {
761
            // 1. write elsewhere
762
            final byte[] bs = charset == null ? s.getBytes() : s.getBytes(charset);
763
            if (append) {
764
                // REPLACE_EXISTING since tmpFile exists
765
                Files.copy(f, tmpFile, StandardCopyOption.REPLACE_EXISTING);
766
                Files.write(tmpFile, bs, StandardOpenOption.APPEND);
767
            } else {
768
                Files.write(tmpFile, bs);
769
            }
770
            // 2. move into place (cannot use ATOMIC_MOVE because f might exists ; and we would need
771
            // to handle AtomicMoveNotSupportedException)
772
            Files.move(tmpFile, f, StandardCopyOption.REPLACE_EXISTING);
773
            // don't try to delete if move() successful
774
        } catch (RuntimeException | Error | IOException e) {
775
            try {
776
                Files.deleteIfExists(tmpFile);
777
            } catch (Exception e1) {
778
                e.addSuppressed(e1);
779
            }
780
            throw e;
17 ilm 781
        }
782
    }
783
 
784
    /**
67 ilm 785
     * Create a writer for the passed file, and write the XML declaration.
786
     *
787
     * @param f a file
788
     * @return a writer with the same encoding as the XML.
789
     * @throws IOException if an error occurs.
790
     * @see StreamUtils#createXMLWriter(java.io.OutputStream)
791
     */
792
    public static BufferedWriter createXMLWriter(final File f) throws IOException {
174 ilm 793
        FileUtils.mkParentDirs(f);
67 ilm 794
        final FileOutputStream outs = new FileOutputStream(f);
795
        try {
796
            return StreamUtils.createXMLWriter(outs);
797
        } catch (RuntimeException e) {
798
            outs.close();
799
            throw e;
800
        } catch (IOException e) {
801
            outs.close();
802
            throw e;
803
        }
804
    }
805
 
806
    /**
807
     * Create an UTF-8 buffered writer.
808
     *
809
     * @param f the file to write to.
810
     * @return a buffered writer.
811
     * @throws FileNotFoundException if the file cannot be opened.
812
     */
813
    public static BufferedWriter createWriter(final File f) throws FileNotFoundException {
814
        return createWriter(f, StringUtils.UTF8);
815
    }
816
 
817
    public static BufferedWriter createWriter(final File f, final Charset cs) throws FileNotFoundException {
818
        final FileOutputStream outs = new FileOutputStream(f);
819
        try {
820
            return new BufferedWriter(new OutputStreamWriter(outs, cs));
821
        } catch (RuntimeException e) {
822
            try {
823
                outs.close();
824
            } catch (IOException e1) {
825
                e1.printStackTrace();
826
            }
827
            throw e;
828
        }
829
    }
830
 
831
    /**
17 ilm 832
     * Execute the passed transformer with the lock on the passed file.
833
     *
834
     * @param <T> return type.
835
     * @param f the file to lock.
836
     * @param transf what to do on the file.
837
     * @return what <code>transf</code> returns.
144 ilm 838
     * @throws IOException if an error occurs while locking the file.
839
     * @throws X if an error occurs while using the file.
17 ilm 840
     */
144 ilm 841
    public static final <T, X extends Exception> T doWithLock(final File f, ExnTransformer<RandomAccessFile, T, X> transf) throws IOException, X {
842
        mkParentDirs(f);
17 ilm 843
        // don't use FileOutputStream : it truncates the file on creation
144 ilm 844
        // we need write to obtain lock
845
        try (final RandomAccessFile out = new RandomAccessFile(f, "rw")) {
17 ilm 846
            out.getChannel().lock();
144 ilm 847
            return transf.transformChecked(out);
17 ilm 848
        }
144 ilm 849
        // the lock is released on close()
17 ilm 850
    }
851
 
852
    private static final Map<URL, File> files = new HashMap<URL, File>();
853
 
854
    private static final File getShortCutFile() throws IOException {
855
        return getFile(FileUtils.class.getResource("shortcut.vbs"));
856
    }
857
 
858
    // windows cannot execute a string, it demands a file
859
    public static final File getFile(final URL url) throws IOException {
67 ilm 860
        // avoid unnecessary IO if already a file
861
        File urlFile = null;
862
        // inexpensive comparison before trying to convert to URI and call the File constructor
863
        if ("file".equalsIgnoreCase(url.getProtocol())) {
864
            try {
865
                urlFile = new File(url.toURI());
866
            } catch (Exception e) {
867
                Log.get().log(Level.FINER, "couldn't convert to file " + url, e);
868
            }
869
        }
870
        if (urlFile != null)
871
            return urlFile;
872
 
17 ilm 873
        final File shortcutFile;
874
        final File currentFile = files.get(url);
875
        if (currentFile == null || !currentFile.exists()) {
876
            shortcutFile = File.createTempFile("windowsIsLame", ".vbs");
67 ilm 877
            // ATTN if the VM is not terminated normally, the file won't be deleted
878
            // perhaps a thread to delete the file after a certain amount of time
17 ilm 879
            shortcutFile.deleteOnExit();
880
            files.put(url, shortcutFile);
149 ilm 881
            try (final InputStream stream = url.openStream(); final FileOutputStream out = new FileOutputStream(shortcutFile);) {
17 ilm 882
                StreamUtils.copy(stream, out);
883
            }
884
        } else
885
            shortcutFile = currentFile;
886
        return shortcutFile;
887
    }
888
 
889
    /**
890
     * Create a symbolic link from <code>link</code> to <code>target</code>.
891
     *
892
     * @param target the target of the link, eg ".".
893
     * @param link the file to create or replace, eg "l".
894
     * @return the link if the creation was successfull, <code>null</code> otherwise, eg "l.LNK".
895
     * @throws IOException if an error occurs.
896
     */
897
    public static final File ln(final File target, final File link) throws IOException {
898
        final Process ps;
899
        final File res;
80 ilm 900
        if (OSFamily.getInstance() == OSFamily.Windows) {
17 ilm 901
            // using the .vbs since it doesn't depends on cygwin
902
            // and cygwin's ln is weird :
903
            // 1. needs CYGWIN=winsymlinks to create a shortcut, but even then "ln -f" doesn't work
904
            // since it tries to delete l instead of l.LNK
905
            // 2. it sets the system flag so "dir" doesn't show the shortcut (unless you add /AS)
906
            // 3. the shortcut is recognized as a symlink thanks to a special attribute that can get
907
            // lost (e.g. copying in eclipse)
180 ilm 908
            ps = startDiscardingOutput("cscript", getShortCutFile().getAbsolutePath(), link.getAbsolutePath(), target.getCanonicalPath());
17 ilm 909
            res = new File(link.getParentFile(), link.getName() + ".LNK");
910
        } else {
911
            final String rel = FileUtils.relative(link.getAbsoluteFile().getParentFile(), target);
912
            // add -f to replace existing links
913
            // add -n so that ln -sf aDir anExistantLinkToIt succeed
914
            final String[] cmdarray = { "ln", "-sfn", rel, link.getAbsolutePath() };
180 ilm 915
            ps = startDiscardingOutput(cmdarray);
17 ilm 916
            res = link;
917
        }
918
        try {
919
            final int exitValue = ps.waitFor();
920
            if (exitValue == 0)
921
                return res;
922
            else
923
                throw new IOException("Abnormal exit value: " + exitValue);
924
        } catch (InterruptedException e) {
925
            throw ExceptionUtils.createExn(IOException.class, "interrupted", e);
926
        }
927
    }
928
 
929
    /**
930
     * Resolve a symbolic link or a windows shortcut.
931
     *
932
     * @param link the shortcut, e.g. shortcut.lnk.
933
     * @return the target of <code>link</code>, <code>null</code> if not found, e.g. target.txt.
934
     * @throws IOException if an error occurs.
935
     */
936
    public static final File readlink(final File link) throws IOException {
937
        final Process ps;
80 ilm 938
        if (OSFamily.getInstance() == OSFamily.Windows) {
17 ilm 939
            ps = Runtime.getRuntime().exec(new String[] { "cscript", "//NoLogo", getShortCutFile().getAbsolutePath(), link.getAbsolutePath() });
940
        } else {
941
            // add -f to canonicalize
942
            ps = Runtime.getRuntime().exec(new String[] { "readlink", "-f", link.getAbsolutePath() });
943
        }
944
        try {
83 ilm 945
            ps.getErrorStream().close();
149 ilm 946
            final String res;
947
            try (final BufferedReader reader = new BufferedReader(new InputStreamReader(ps.getInputStream()));) {
948
                res = reader.readLine();
949
            }
17 ilm 950
            if (ps.waitFor() != 0 || res == null || res.length() == 0)
951
                return null;
952
            else
953
                return new File(res);
954
        } catch (InterruptedException e) {
955
            throw ExceptionUtils.createExn(IOException.class, "interrupted", e);
956
        }
957
    }
958
 
67 ilm 959
    // from guava/src/com/google/common/io/Files.java
960
    /** Maximum loop count when creating temp directories. */
961
    private static final int TEMP_DIR_ATTEMPTS = 10000;
962
 
17 ilm 963
    /**
67 ilm 964
     * Atomically creates a new directory somewhere beneath the system's temporary directory (as
965
     * defined by the {@code java.io.tmpdir} system property), and returns its name.
966
     *
967
     * <p>
968
     * Use this method instead of {@link File#createTempFile(String, String)} when you wish to
969
     * create a directory, not a regular file. A common pitfall is to call {@code createTempFile},
970
     * delete the file and create a directory in its place, but this leads a race condition which
971
     * can be exploited to create security vulnerabilities, especially when executable files are to
972
     * be written into the directory.
973
     *
974
     * <p>
975
     * This method assumes that the temporary volume is writable, has free inodes and free blocks,
976
     * and that it will not be called thousands of times per second.
977
     *
978
     * @param prefix the prefix string to be used in generating the directory's name.
979
     * @return the newly-created directory.
980
     * @throws IllegalStateException if the directory could not be created.
180 ilm 981
     * @deprecated use
982
     *             {@link Files#createTempDirectory(String, java.nio.file.attribute.FileAttribute...)}
67 ilm 983
     */
984
    public static File createTempDir(final String prefix) {
985
        final File baseDir = new File(System.getProperty("java.io.tmpdir"));
986
        final String baseName = prefix + System.currentTimeMillis() + "-";
987
 
988
        for (int counter = 0; counter < TEMP_DIR_ATTEMPTS; counter++) {
989
            final File tempDir = new File(baseDir, baseName + counter);
990
            if (tempDir.mkdir()) {
991
                return tempDir;
992
            }
993
        }
994
        throw new IllegalStateException("Failed to create directory within " + TEMP_DIR_ATTEMPTS + " attempts (tried " + baseName + "0 to " + baseName + (TEMP_DIR_ATTEMPTS - 1) + ')');
995
    }
996
 
997
    /**
17 ilm 998
     * Tries to open the passed file as if it were graphically opened by the current user (respect
999
     * user's "open with"). If a native way to open the file can't be found, tries the passed list
1000
     * of executables.
1001
     *
1002
     * @param f the file to open.
1003
     * @param executables a list of executables to try, e.g. ["ooffice", "soffice"].
1004
     * @throws IOException if the file can't be opened.
1005
     */
1006
    public static final void open(File f, String[] executables) throws IOException {
156 ilm 1007
        if (!f.exists()) {
1008
            throw new FileNotFoundException(f.getAbsolutePath() + " not found");
1009
        }
17 ilm 1010
        try {
1011
            openNative(f);
1012
        } catch (IOException exn) {
1013
            for (int i = 0; i < executables.length; i++) {
1014
                final String executable = executables[i];
1015
                try {
180 ilm 1016
                    startDiscardingOutput(executable, f.getCanonicalPath());
17 ilm 1017
                    return;
1018
                } catch (IOException e) {
180 ilm 1019
                    exn.addSuppressed(new IOException("unable to open with " + executable, e));
17 ilm 1020
                    // try the next one
1021
                }
1022
            }
180 ilm 1023
            throw new IOException("unable to open " + f, exn);
17 ilm 1024
        }
1025
    }
1026
 
1027
    /**
1028
     * Open the passed file as if it were graphically opened by the current user (user's "open
1029
     * with").
1030
     *
1031
     * @param f the file to open.
1032
     * @throws IOException if f couldn't be opened.
1033
     */
1034
    private static final void openNative(File f) throws IOException {
180 ilm 1035
        openNative(f.getCanonicalPath());
1036
    }
1037
 
1038
    private static final void openNative(URI uri) throws IOException {
1039
        openNative(uri.toASCIIString());
1040
    }
1041
 
1042
    private static final void openNative(String param) throws IOException {
80 ilm 1043
        final OSFamily os = OSFamily.getInstance();
17 ilm 1044
        final String[] cmdarray;
80 ilm 1045
        if (os == OSFamily.Windows) {
180 ilm 1046
            cmdarray = new String[] { "cmd", "/c", "start", "\"\"", param };
80 ilm 1047
        } else if (os == OSFamily.Mac) {
180 ilm 1048
            cmdarray = new String[] { "open", param };
80 ilm 1049
        } else if (os instanceof Unix) {
180 ilm 1050
            cmdarray = new String[] { "xdg-open", param };
17 ilm 1051
        } else {
180 ilm 1052
            throw new IOException("unknown way to open " + param);
17 ilm 1053
        }
1054
        try {
180 ilm 1055
            final Process ps = startDiscardingOutput(cmdarray);
17 ilm 1056
            // can wait since the command return as soon as the native application is launched
1057
            // (i.e. this won't wait 30s for OpenOffice)
83 ilm 1058
            final int res = ps.waitFor();
17 ilm 1059
            if (res != 0)
1060
                throw new IOException("error (" + res + ") executing " + Arrays.asList(cmdarray));
1061
        } catch (InterruptedException e) {
1062
            throw ExceptionUtils.createExn(IOException.class, "interrupted waiting for " + Arrays.asList(cmdarray), e);
1063
        }
1064
    }
1065
 
180 ilm 1066
    private static final Process startDiscardingOutput(final String... command) throws IOException {
1067
        return startDiscardingOutput(new ProcessBuilder(command));
1068
    }
1069
 
1070
    private static final Process startDiscardingOutput(final ProcessBuilder pb) throws IOException {
1071
        final Process res = pb.redirectOutput(ProcessStreams.DISCARD).redirectError(ProcessStreams.DISCARD).start();
1072
        res.getOutputStream().close();
1073
        return res;
1074
    }
1075
 
17 ilm 1076
    static final boolean gnomeRunning() {
1077
        try {
180 ilm 1078
            final Process ps = startDiscardingOutput("pgrep", "-u", System.getProperty("user.name"), "nautilus");
83 ilm 1079
            // no need for output, use exit status
1080
            return ps.waitFor() == 0;
17 ilm 1081
        } catch (Exception e) {
1082
            return false;
1083
        }
1084
    }
1085
 
73 ilm 1086
    public static final String XML_TYPE = "text/xml";
17 ilm 1087
    private static final Map<String, String> ext2mime;
83 ilm 1088
    private static final SetMap<String, String> mime2ext;
1089
 
17 ilm 1090
    static {
83 ilm 1091
        mime2ext = new SetMap<String, String>(Mode.NULL_FORBIDDEN);
1092
        mime2ext.putCollection(XML_TYPE, ".xml");
1093
        mime2ext.putCollection("image/jpeg", ".jpg", ".jpeg");
1094
        mime2ext.putCollection("image/png", ".png");
1095
        mime2ext.putCollection("image/tiff", ".tiff", ".tif");
1096
        mime2ext.putCollection("application/pdf", ".pdf");
1097
 
1098
        mime2ext.putCollection("application/vnd.oasis.opendocument.spreadsheet", ".ods");
1099
        mime2ext.putCollection("application/vnd.oasis.opendocument.text", ".odt");
1100
        mime2ext.putCollection("application/vnd.oasis.opendocument.presentation", ".odp");
1101
        mime2ext.putCollection("application/vnd.oasis.opendocument.graphics", ".odg");
1102
 
17 ilm 1103
        ext2mime = new HashMap<String, String>();
83 ilm 1104
        for (final Entry<String, Set<String>> e : mime2ext.entrySet()) {
1105
            final String m = e.getKey();
1106
            for (final String ext : e.getValue()) {
1107
                if (ext2mime.put(ext, m) != null)
1108
                    Log.get().info("Duplicate extension : " + ext);
1109
            }
1110
        }
17 ilm 1111
    }
1112
 
1113
    /**
142 ilm 1114
     * Try to guess the media type of the passed file name (see
1115
     * <a href="http://www.iana.org/assignments/media-types">iana</a>).
17 ilm 1116
     *
1117
     * @param fname a file name.
1118
     * @return its mime type.
1119
     */
1120
    public static final String findMimeType(String fname) {
1121
        for (final Map.Entry<String, String> e : ext2mime.entrySet()) {
1122
            if (fname.toLowerCase().endsWith(e.getKey()))
1123
                return e.getValue();
1124
        }
1125
        return null;
1126
    }
1127
 
83 ilm 1128
    public static final Set<String> getExtensionsFromMimeType(final String mimetype) {
1129
        return mime2ext.get(mimetype);
1130
    }
1131
 
17 ilm 1132
    /**
57 ilm 1133
     * Return the string after the last dot.
1134
     *
1135
     * @param fname a name, e.g. "test.odt" or "sans".
1136
     * @return the extension, e.g. "odt" or <code>null</code>.
1137
     */
1138
    public static final String getExtension(String fname) {
83 ilm 1139
        return getExtension(fname, false);
1140
    }
1141
 
1142
    public static final String getExtension(final String fname, final boolean withDot) {
57 ilm 1143
        final int lastIndex = fname.lastIndexOf('.');
83 ilm 1144
        return lastIndex < 0 ? null : fname.substring(lastIndex + (withDot ? 0 : 1));
57 ilm 1145
    }
1146
 
1147
    /**
17 ilm 1148
     * Chars not valid in filenames.
1149
     */
1150
    public static final Collection<Character> INVALID_CHARS;
1151
 
1152
    /**
1153
     * An escaper suitable for producing valid filenames.
1154
     */
1155
    public static final Escaper FILENAME_ESCAPER = new StringUtils.Escaper('\'', 'Q');
73 ilm 1156
 
142 ilm 1157
    static private final String WS = "\\p{javaWhitespace}";
1158
    static private final Pattern WS_PATTERN = Pattern.compile(WS + "+");
1159
    static private final Pattern CONTROL_PATTERN = Pattern.compile("[\\p{IsCc}\\p{IsCf}&&[^" + WS + "]]+");
73 ilm 1160
    static private final Pattern INVALID_CHARS_PATTERN;
1161
 
17 ilm 1162
    static {
1163
        // from windows explorer
73 ilm 1164
        // http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
1165
        // Naming Files, Paths, and Namespaces
1166
        // on Mac only '/' and ':', on Linux only '/'
17 ilm 1167
        FILENAME_ESCAPER.add('"', 'D').add(':', 'C').add('/', 'S').add('\\', 'A');
1168
        FILENAME_ESCAPER.add('<', 'L').add('>', 'G').add('*', 'R').add('|', 'P').add('?', 'M');
1169
        INVALID_CHARS = FILENAME_ESCAPER.getEscapedChars();
73 ilm 1170
        INVALID_CHARS_PATTERN = Pattern.compile("[" + CollectionUtils.join(INVALID_CHARS, "") + "]");
17 ilm 1171
    }
1172
 
73 ilm 1173
    /**
1174
     * Sanitize a name. Remove control characters, trim, and replace {@link #INVALID_CHARS} by '_'.
1175
     *
1176
     * @param name an arbitrary name.
1177
     * @return a name suitable for any file system.
1178
     */
1179
    static public final String sanitize(String name) {
142 ilm 1180
        // remove control and format characters (except white spaces)
73 ilm 1181
        name = CONTROL_PATTERN.matcher(name).replaceAll("");
142 ilm 1182
        // only use one regular space (must be done after removing control characters as if they are
1183
        // between spaces we want only one space to remain)
73 ilm 1184
        name = WS_PATTERN.matcher(name).replaceAll(" ");
1185
        // leading and trailing spaces are hard to see (and illegal in Explorer)
1186
        name = name.trim();
1187
 
1188
        // replace all invalid characters with _
1189
        name = INVALID_CHARS_PATTERN.matcher(name).replaceAll("_");
1190
 
1191
        return name;
1192
    }
1193
 
17 ilm 1194
    public static final FileFilter DIR_FILTER = new FileFilter() {
1195
        @Override
1196
        public boolean accept(File f) {
1197
            return f.isDirectory();
1198
        }
1199
    };
1200
    public static final FileFilter REGULAR_FILE_FILTER = new FileFilter() {
1201
        @Override
1202
        public boolean accept(File f) {
1203
            return f.isFile();
1204
        }
1205
    };
144 ilm 1206
    public static final Filter<Path> DIR_PATH_FILTER = new DirectoryStream.Filter<Path>() {
1207
        @Override
1208
        public boolean accept(Path entry) throws IOException {
1209
            return Files.isDirectory(entry, LinkOption.NOFOLLOW_LINKS);
1210
        }
1211
    };
17 ilm 1212
 
1213
    /**
1214
     * Return a filter that select regular files ending in <code>ext</code>.
1215
     *
1216
     * @param ext the end of the name, eg ".xml".
1217
     * @return the corresponding filter.
1218
     */
1219
    public static final FileFilter createEndFileFilter(final String ext) {
1220
        return new FileFilter() {
1221
            @Override
1222
            public boolean accept(File f) {
1223
                return f.isFile() && f.getName().endsWith(ext);
1224
            }
1225
        };
1226
    }
144 ilm 1227
 
1228
    /**
1229
     * How to merge the group and others portion of permissions for non-POSIX FS.
1230
     *
1231
     * @author sylvain
1232
     * @see FileUtils#setFilePermissionsFromPOSIX(File, String, GroupAndOthers)
1233
     */
1234
    public static enum GroupAndOthers {
1235
        REQUIRE_SAME {
1236
            @Override
1237
            protected Set<Permission> getNonEqual(Set<Permission> groupPerms, Set<Permission> otherPerms) {
1238
                throw new IllegalArgumentException("Different permissions : " + groupPerms + " != " + otherPerms);
1239
            }
1240
        },
1241
        PERMISSIVE {
1242
            @Override
1243
            protected Set<Permission> getNonEqual(Set<Permission> groupPerms, Set<Permission> otherPerms) {
1244
                final EnumSet<Permission> res = EnumSet.noneOf(Permission.class);
1245
                res.addAll(groupPerms);
1246
                res.addAll(otherPerms);
1247
                return res;
1248
            }
1249
        },
1250
        OTHERS {
1251
            @Override
1252
            protected Set<Permission> getNonEqual(Set<Permission> groupPerms, Set<Permission> otherPerms) {
1253
                return otherPerms;
1254
            }
1255
        },
1256
        RESTRICTIVE {
1257
            @Override
1258
            protected Set<Permission> getNonEqual(Set<Permission> groupPerms, Set<Permission> otherPerms) {
1259
                final EnumSet<Permission> res = EnumSet.allOf(Permission.class);
1260
                res.retainAll(groupPerms);
1261
                res.retainAll(otherPerms);
1262
                return res;
1263
            }
1264
        };
1265
 
1266
        public final Set<Permission> getPermissions(final Set<Permission> groupPerms, final Set<Permission> otherPerms) {
1267
            if (groupPerms.equals(otherPerms)) {
1268
                return groupPerms;
1269
            } else {
1270
                return getNonEqual(groupPerms, otherPerms);
1271
            }
1272
        }
1273
 
1274
        public final Set<Permission> getPermissions(final String posixPerms) {
1275
            final Set<Permission> groupPerms = Permission.fromString(posixPerms.substring(3, 6));
1276
            final Set<Permission> otherPerms = Permission.fromString(posixPerms.substring(6, 9));
1277
            return this.getPermissions(groupPerms, otherPerms);
1278
        }
1279
 
1280
        protected abstract Set<Permission> getNonEqual(final Set<Permission> groupPerms, final Set<Permission> otherPerms);
1281
 
1282
    }
1283
 
1284
    public static final String setPermissions(final Path p, final String posixPerms) throws IOException {
1285
        return setPermissions(p, posixPerms, GroupAndOthers.RESTRICTIVE);
1286
    }
1287
 
1288
    /**
1289
     * Use {@link PosixFileAttributeView#setPermissions(Set)} if possible, otherwise use
1290
     * {@link #setFilePermissionsFromPOSIX(File, String, GroupAndOthers)}.
1291
     *
1292
     * @param p the path to change.
1293
     * @param posixPerms the new permissions to apply.
1294
     * @param groupAndOthers only for non-POSIX FS, how to merge group and others portion.
1295
     * @return the permission applied, 9 characters for POSIX, 6 for non-POSIX (i.e. 3 for owner, 3
1296
     *         for the rest), <code>null</code> if some permissions couldn't be applied (only on
1297
     *         non-POSIX).
1298
     * @throws IOException if permissions couldn't be applied.
1299
     */
1300
    public static final String setPermissions(final Path p, final String posixPerms, final GroupAndOthers groupAndOthers) throws IOException {
1301
        final String res;
1302
        final PosixFileAttributeView view = Files.getFileAttributeView(p, PosixFileAttributeView.class);
1303
        if (view != null) {
1304
            view.setPermissions(PosixFilePermissions.fromString(posixPerms));
1305
            res = posixPerms;
1306
        } else {
1307
            // final Set<Permission> notOwnerPerms = setFilePermissions(p.toFile(), pfp,
1308
            // groupAndOthers);
1309
            final Set<Permission> notOwnerPerms = setFilePermissionsFromPOSIX(p.toFile(), posixPerms, groupAndOthers);
1310
            res = notOwnerPerms == null ? null : posixPerms.substring(0, 3) + Permission.get3chars(notOwnerPerms);
1311
        }
1312
        return res;
1313
    }
1314
 
1315
    public static final Set<Permission> setFilePermissionsFromPOSIX(final File f, final String posixPerms) {
1316
        return setFilePermissionsFromPOSIX(f, posixPerms, GroupAndOthers.RESTRICTIVE);
1317
    }
1318
 
1319
    /**
1320
     * This method doesn't need POSIX but must merge permissions before applying them.
1321
     *
1322
     * @param f the file to change.
1323
     * @param posixPerms the POSIX permissions to merge.
1324
     * @param groupAndOthers how to merge.
1325
     * @return the merged permissions for the "not owner" portion, or <code>null</code> if some
1326
     *         permissions couldn't be set.
1327
     * @see #setFilePermissions(File, Set, Set)
1328
     */
1329
    public static final Set<Permission> setFilePermissionsFromPOSIX(final File f, final String posixPerms, final GroupAndOthers groupAndOthers) {
1330
        if (posixPerms.length() != 9)
1331
            throw new IllegalArgumentException("Invalid mode : " + posixPerms);
1332
        final Set<Permission> ownerPerms = Permission.fromString(posixPerms.substring(0, 3));
1333
        final Set<Permission> notOwnerPerms = groupAndOthers.getPermissions(posixPerms);
1334
        assert notOwnerPerms != null;
1335
        final boolean success = setFilePermissions(f, ownerPerms, notOwnerPerms);
1336
        return success ? notOwnerPerms : null;
1337
    }
1338
 
1339
    /**
1340
     * Use {@link File} methods to set permissions. This works everywhere but group and others are
1341
     * treated as the same.
1342
     *
1343
     * @param f the file to change.
1344
     * @param owner the permissions for the owner.
1345
     * @param notOwner the permissions for not the owner.
1346
     * @return <code>true</code> if all asked permissions were set.
1347
     * @see File#setReadable(boolean, boolean)
1348
     * @see File#setWritable(boolean, boolean)
1349
     * @see File#setExecutable(boolean, boolean)
1350
     */
1351
    public static final boolean setFilePermissions(final File f, final Set<Permission> owner, final Set<Permission> notOwner) {
1352
        boolean res = setFilePermissions(f, notOwner, false);
1353
        if (!owner.equals(notOwner)) {
1354
            res &= setFilePermissions(f, owner, true);
1355
        }
1356
        return res;
1357
    }
1358
 
1359
    public static final boolean setFilePermissions(final File f, final Set<Permission> perms, final boolean ownerOnly) {
1360
        boolean res = f.setReadable(perms.contains(Permission.READ), ownerOnly);
1361
        res &= f.setWritable(perms.contains(Permission.WRITE), ownerOnly);
1362
        res &= f.setExecutable(perms.contains(Permission.EXECUTE), ownerOnly);
1363
        return res;
1364
    }
1365
 
1366
    public static enum Permission {
1367
        READ, WRITE, EXECUTE;
1368
 
1369
        public static final Permission R = READ;
1370
        public static final Permission W = WRITE;
1371
        public static final Permission X = EXECUTE;
1372
        public static final Map<String, Set<Permission>> FROM_STRING = new HashMap<>();
1373
        public static final Pattern MINUS_PATTERN = Pattern.compile("-+");
1374
        static {
1375
            putString("---", Collections.<Permission> emptySet());
1376
            putString("--x", Collections.singleton(EXECUTE));
1377
            putString("-w-", Collections.singleton(WRITE));
1378
            putString("-wx", EnumSet.of(WRITE, EXECUTE));
1379
            putString("r--", Collections.singleton(READ));
1380
            putString("r-x", EnumSet.of(READ, EXECUTE));
1381
            putString("rw-", EnumSet.of(READ, WRITE));
1382
            putString("rwx", EnumSet.allOf(Permission.class));
1383
        }
1384
 
1385
        static private final void putString(final String str, final EnumSet<Permission> set) {
1386
            putString(str, Collections.unmodifiableSet(set));
1387
        }
1388
 
1389
        static private final void putString(final String str, final Set<Permission> unmodifiableSet) {
1390
            FROM_STRING.put(str, unmodifiableSet);
1391
            FROM_STRING.put(MINUS_PATTERN.matcher(str).replaceAll(""), unmodifiableSet);
1392
        }
1393
 
1394
        static public final Set<Permission> fromString(final String str) {
1395
            final Set<Permission> res = FROM_STRING.get(str);
1396
            if (res == null)
1397
                throw new IllegalArgumentException("Invalid string : " + str);
1398
            return res;
1399
        }
1400
 
1401
        public static final String get3chars(final Set<Permission> perms) {
1402
            return get3chars(perms.contains(READ), perms.contains(WRITE), perms.contains(EXECUTE));
1403
        }
1404
 
1405
        private static final String get3chars(final boolean read, final boolean write, final boolean exec) {
1406
            final StringBuilder sb = new StringBuilder(3);
1407
            sb.append(read ? 'r' : '-');
1408
            sb.append(write ? 'w' : '-');
1409
            sb.append(exec ? 'x' : '-');
1410
            return sb.toString();
1411
        }
1412
    }
17 ilm 1413
}