-
Notifications
You must be signed in to change notification settings - Fork 0
/
JWFileItems.java
139 lines (122 loc) · 3.65 KB
/
JWFileItems.java
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
/*
* Copyright © 2020 Mark Raynsford <code@io7m.com> http://io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package com.io7m.jwheatsheaf.ui.internal;
import com.io7m.jwheatsheaf.api.JWFileKind;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* Functions over file items.
*/
public final class JWFileItems
{
private static final Logger LOG =
LoggerFactory.getLogger(JWFileItems.class);
private JWFileItems()
{
}
/**
* List the given directory, resolving each entry into a file item.
*
* @param directory The directory
* @param withParent {@code true} if the parent directory entry should be included
*
* @return A list of items
*
* @throws IOException On I/O errors
*/
public static List<JWFileItem> listDirectory(
final Path directory,
final boolean withParent)
throws IOException
{
final var items = new ArrayList<JWFileItem>(32);
items.add(resolveFileItem(directory).withDisplayName("."));
if (withParent) {
final var directoryParent = directory.getParent();
if (directoryParent != null) {
items.add(resolveFileItem(directoryParent).withDisplayName(".."));
}
}
try (var stream = Files.list(directory)) {
stream.sorted().forEach(path -> items.add(resolveFileItem(path)));
}
return items;
}
/**
* Resolve the given path into a file item. If the file item cannot be
* resolved due to permissions errors or other I/O errors, a file item
* is returned with identity values in each field (such as 0 for the file
* size).
*
* @param path The input path
*
* @return A file item
*/
public static JWFileItem resolveFileItem(
final Path path)
{
Objects.requireNonNull(path, "path");
try {
return JWFileItem.builder()
.setKind(fileKind(path))
.setModifiedTime(fileTime(path))
.setSize(fileSize(path))
.setPath(path)
.build();
} catch (final IOException e) {
LOG.error("i/o exception during directory listing: ", e);
return JWFileItem.builder()
.setKind(JWFileKind.UNKNOWN)
.setModifiedTime(FileTime.fromMillis(0L))
.setSize(0L)
.setPath(path)
.build();
}
}
private static long fileSize(
final Path path)
throws IOException
{
return Files.size(path);
}
private static FileTime fileTime(
final Path path)
throws IOException
{
return Files.getLastModifiedTime(path);
}
private static JWFileKind fileKind(
final Path path)
{
if (Files.isSymbolicLink(path)) {
return JWFileKind.SYMBOLIC_LINK;
}
if (Files.isDirectory(path)) {
return JWFileKind.DIRECTORY;
}
if (Files.isRegularFile(path)) {
return JWFileKind.REGULAR_FILE;
}
return JWFileKind.UNKNOWN;
}
}