Skip to content

Commit 89fea41

Browse files
tulinkryVladimir Kotal
authored andcommitted
removing the size for a directory in directory listing (#1617)
fixes #1552
1 parent 0647a5e commit 89fea41

File tree

2 files changed

+123
-54
lines changed

2 files changed

+123
-54
lines changed

src/org/opensolaris/opengrok/web/DirectoryListing.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
*/
4545
public class DirectoryListing {
4646

47+
protected final static String DIRECTORY_SIZE_PLACEHOLDER = "-";
4748
private final EftarFileReader desc;
4849
private final long now;
4950

@@ -81,9 +82,11 @@ private void printDateSize(Writer out, File child, Date modTime,
8182
out.write(dateFormatter.format(lastm));
8283
}
8384
out.write("</td><td>");
84-
// if (isDir) {
85+
if (child.isDirectory()) {
86+
out.write(DIRECTORY_SIZE_PLACEHOLDER);
87+
} else {
8588
out.write(Util.readableSize(child.length()));
86-
// }
89+
}
8790
out.write("</td>");
8891
}
8992

test/org/opensolaris/opengrok/web/DirectoryListingTest.java

Lines changed: 118 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* CDDL HEADER END
1818
*/
1919

20-
/*
20+
/*
2121
* Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opensolaris.opengrok.web;
@@ -28,51 +28,108 @@
2828
import java.io.StringWriter;
2929
import java.text.SimpleDateFormat;
3030
import java.util.Arrays;
31+
import java.util.List;
3132
import javax.xml.parsers.DocumentBuilder;
3233
import javax.xml.parsers.DocumentBuilderFactory;
3334
import org.junit.After;
3435
import org.junit.AfterClass;
3536
import org.junit.Before;
3637
import org.junit.BeforeClass;
3738
import org.junit.Test;
39+
import org.opensolaris.opengrok.configuration.RuntimeEnvironment;
40+
import org.opensolaris.opengrok.history.RepositoryFactory;
3841
import org.opensolaris.opengrok.util.FileUtilities;
3942
import org.w3c.dom.Document;
4043
import org.w3c.dom.Element;
4144
import org.w3c.dom.Node;
4245
import org.w3c.dom.NodeList;
46+
4347
import static org.junit.Assert.*;
44-
import org.opensolaris.opengrok.configuration.RuntimeEnvironment;
45-
import org.opensolaris.opengrok.history.RepositoryFactory;
4648

4749
/**
4850
* JUnit test to test that the DirectoryListing produce the expected result
4951
*/
5052
public class DirectoryListingTest {
5153

54+
/**
55+
* Indication of that the file was a directory and so that the size given by
56+
* the FS is platform dependent.
57+
*/
58+
private static final int DIRECTORY_INTERNAL_SIZE = -2;
59+
/**
60+
* Indication of unparseable file size.
61+
*/
62+
private static final int INVALID_SIZE = -1;
63+
5264
private File directory;
5365
private FileEntry[] entries;
5466
private SimpleDateFormat dateFormatter;
5567

5668
class FileEntry implements Comparable {
69+
5770
String name;
5871
String href;
5972
long lastModified;
60-
int size; // If negative, don't check it.
73+
/**
74+
* May be:
75+
* <pre>
76+
* positive integer - for a file
77+
* -2 - for a directory
78+
* -1 - for an unparseable size
79+
* </pre>
80+
*/
81+
int size;
82+
List<FileEntry> subdirs;
6183

6284
FileEntry() {
6385
dateFormatter = new SimpleDateFormat("dd-MMM-yyyy");
6486
}
6587

66-
FileEntry(String name, String href, long lastModified, int size) {
88+
private FileEntry(String name, String href, long lastModified, int size, List<FileEntry> subdirs) {
89+
this();
6790
this.name = name;
6891
this.href = href;
6992
this.lastModified = lastModified;
7093
this.size = size;
94+
this.subdirs = subdirs;
95+
}
96+
97+
/**
98+
* Creating the directory entry.
99+
*
100+
* @param name name of the file
101+
* @param href href to the file
102+
* @param lastModified date of last modification
103+
* @param subdirs list of sub entries (may be empty)
104+
*/
105+
FileEntry(String name, String href, long lastModified, List<FileEntry> subdirs) {
106+
this(name, href, lastModified, DIRECTORY_INTERNAL_SIZE, subdirs);
107+
assertNotNull(subdirs);
108+
}
109+
110+
/**
111+
* Creating a regular file entry.
112+
*
113+
* @param name name of the file
114+
* @param href href to the file
115+
* @param lastModified date of last modification
116+
* @param size the desired size of the file on the disc
117+
*/
118+
FileEntry(String name, String href, long lastModified, int size) {
119+
this(name, href, lastModified, size, null);
71120
}
72121

73122
private void create() throws Exception {
74123
File file = new File(directory, name);
75-
if (!file.exists()) {
124+
125+
if (subdirs != null && subdirs.size() > 0) {
126+
// this is a directory
127+
assertTrue("Failed to create a directory", file.mkdirs());
128+
for (FileEntry entry : subdirs) {
129+
entry.name = name + File.separator + entry.name;
130+
entry.create();
131+
}
132+
} else {
76133
assertTrue("Failed to create file", file.createNewFile());
77134
}
78135

@@ -82,35 +139,37 @@ private void create() throws Exception {
82139
}
83140

84141
assertTrue("Failed to set modification time",
85-
file.setLastModified(val));
142+
file.setLastModified(val));
86143

87-
if (size > 0) {
88-
FileOutputStream out = new FileOutputStream(file);
89-
byte[] buffer = new byte[size];
90-
out.write(buffer);
91-
out.close();
144+
if (subdirs == null && size > 0) {
145+
try (FileOutputStream out = new FileOutputStream(file)) {
146+
byte[] buffer = new byte[size];
147+
out.write(buffer);
148+
}
92149
}
93150
}
94151

152+
@Override
95153
public int compareTo(Object o) {
96154
int ret = -1;
97155

98156
if (o instanceof FileEntry) {
99157
FileEntry fe = (FileEntry) o;
100158

101159
// @todo verify all attributes!
102-
if (name.compareTo(fe.name) == 0 &&
103-
href.compareTo(fe.href) == 0) {
104-
ret = 0;
105-
}
106-
// Negative size is not verified.
107-
if (size >= 0 && size == fe.size) {
108-
ret = 0;
160+
if (name.compareTo(fe.name) == 0
161+
&& href.compareTo(fe.href) == 0) {
162+
if ( // this is a file so the size must be exact
163+
(subdirs == null && size == fe.size)
164+
// this is a directory so the size must have been "-" char
165+
|| (subdirs != null && size == DIRECTORY_INTERNAL_SIZE)) {
166+
ret = 0;
167+
}
109168
}
110169
}
111-
112170
return ret;
113171
}
172+
114173
}
115174

116175
public DirectoryListingTest() {
@@ -129,28 +188,28 @@ public void setUp() throws Exception {
129188
directory = FileUtilities.createTemporaryDirectory("directory");
130189

131190
entries = new FileEntry[3];
132-
entries[0] = new FileEntry("foo.c", "foo.c", 0, 1);
191+
entries[0] = new FileEntry("foo.c", "foo.c", 0, 112);
133192
entries[1] = new FileEntry("bar.h", "bar.h", Long.MAX_VALUE, 0);
134-
entries[2] = null;
193+
// Will test getSimplifiedPath() behavior for ignored directories.
194+
// Use DIRECTORY_INTERNAL_SIZE value for length so it is checked as the directory
195+
// should contain "-" (DIRECTORY_SIZE_PLACEHOLDER) string.
196+
entries[2] = new FileEntry("subdir", "subdir/", 0, Arrays.asList(
197+
new FileEntry[]{
198+
new FileEntry("SCCS", "SCCS/", 0, Arrays.asList(
199+
new FileEntry[]{
200+
new FileEntry("version", "version", 0, 312)
201+
})
202+
)}
203+
));
135204

136205
for (FileEntry entry : entries) {
137-
if (entry != null) {
138-
entry.create();
139-
}
206+
entry.create();
140207
}
208+
141209
// Create the entry that will be ignored separately.
142210
FileEntry hgtags = new FileEntry(".hgtags", ".hgtags", 0, 1);
143211
hgtags.create();
144212

145-
// Will test getSimplifiedPath() behavior for ignored directories.
146-
// Use negative value for length so it is not checked as the return value
147-
// of length() is unspecified for directories.
148-
entries[2] = new FileEntry("subdir", "subdir/", 0, -1);
149-
File subdir = new File(directory, "subdir");
150-
subdir.mkdir();
151-
File SCCSdir = new File(subdir, "SCCS");
152-
SCCSdir.mkdir();
153-
154213
// Need to populate list of ignored entries for all repository types.
155214
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
156215
RepositoryFactory.setIgnored(env);
@@ -160,14 +219,14 @@ public void setUp() throws Exception {
160219
public void tearDown() throws Exception {
161220
if (directory != null && directory.exists()) {
162221
removeDirectory(directory);
222+
directory.delete();
163223
}
164224
}
165225

166226
private void removeDirectory(File dir) {
167227
File[] childs = dir.listFiles();
168228
if (childs != null) {
169229
for (File f : childs) {
170-
171230
if (f.isDirectory()) {
172231
removeDirectory(f);
173232
}
@@ -177,8 +236,8 @@ private void removeDirectory(File dir) {
177236
}
178237

179238
/**
180-
* Get the href attribute from: &lt;td align="left"&gt;&lt;tt&gt;&lt;a href="foo"
181-
* class="p"&gt;foo&lt;/a&gt;&lt;/tt&gt;&lt;/td&gt;
239+
* Get the href attribute from: &lt;td align="left"&gt;&lt;tt&gt;&lt;a
240+
* href="foo" class="p"&gt;foo&lt;/a&gt;&lt;/tt&gt;&lt;/td&gt;
182241
*
183242
* @param item
184243
* @return
@@ -226,6 +285,7 @@ private String getFilename(Node item) throws Exception {
226285

227286
/**
228287
* Get the LastModified date from the &lt;td&gt;date&lt;/td&gt;
288+
*
229289
* @todo fix the item
230290
* @param item the node representing &lt;td&gt
231291
* @return last modified date of the file
@@ -238,25 +298,36 @@ private long getLastModified(Node item) throws Exception {
238298

239299
String value = val.getNodeValue();
240300
return value.equalsIgnoreCase("Today")
241-
? Long.MAX_VALUE
242-
: dateFormatter.parse(value).getTime();
301+
? Long.MAX_VALUE
302+
: dateFormatter.parse(value).getTime();
243303
}
244304

245305
/**
246306
* Get the size from the: &lt;td&gt;&lt;tt&gt;size&lt;/tt&gt;&lt;/td&gt;
307+
*
247308
* @param item the node representing &lt;td&gt;
248-
* @return The size
249-
* @throws java.lang.Exception if an error occurs
309+
* @return positive integer if the record was a file<br>
310+
* -1 if the size could not be parsed<br>
311+
* -2 if the record was a directory<br>
250312
*/
251-
private int getSize(Node item) throws Exception {
313+
private int getSize(Node item) throws NumberFormatException {
252314
Node val = item.getFirstChild();
253315
assertNotNull(val);
254316
assertEquals(Node.TEXT_NODE, val.getNodeType());
255-
return Integer.parseInt(val.getNodeValue().trim());
317+
if (DirectoryListing.DIRECTORY_SIZE_PLACEHOLDER.equals(val.getNodeValue().trim())) {
318+
// track that it had the DIRECTORY_SIZE_PLACEHOLDER character
319+
return DIRECTORY_INTERNAL_SIZE;
320+
}
321+
try {
322+
return Integer.parseInt(val.getNodeValue().trim());
323+
} catch (NumberFormatException ex) {
324+
return INVALID_SIZE;
325+
}
256326
}
257327

258328
/**
259329
* Validate this file-entry in the table
330+
*
260331
* @param element The &lt;tr&gt; element
261332
* @throws java.lang.Exception
262333
*/
@@ -274,11 +345,7 @@ private void validateEntry(Element element) throws Exception {
274345
entry.name = getFilename(nl.item(1));
275346
entry.href = getHref(nl.item(1));
276347
entry.lastModified = getLastModified(nl.item(3));
277-
try {
278-
entry.size = getSize(nl.item(4));
279-
} catch (Exception e) {
280-
entry.size = -1;
281-
}
348+
entry.size = getSize(nl.item(4));
282349

283350
// Try to look it up in the list of files.
284351
for (int ii = 0; ii < entries.length; ++ii) {
@@ -293,8 +360,8 @@ private void validateEntry(Element element) throws Exception {
293360

294361
/**
295362
* Test directory listing
296-
* @throws java.lang.Exception if an error occurs while generating the
297-
* list.
363+
*
364+
* @throws java.lang.Exception if an error occurs while generating the list.
298365
*/
299366
@Test
300367
public void directoryListing() throws Exception {
@@ -303,7 +370,7 @@ public void directoryListing() throws Exception {
303370

304371
DirectoryListing instance = new DirectoryListing();
305372
instance.listTo("ctx", directory, out, directory.getPath(),
306-
Arrays.asList(directory.list()));
373+
Arrays.asList(directory.list()));
307374

308375
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
309376
assertNotNull("DocumentBuilderFactory is null", factory);
@@ -313,7 +380,6 @@ public void directoryListing() throws Exception {
313380

314381
out.append("</start>\n");
315382
String str = out.toString();
316-
System.out.println(str);
317383
Document document = builder.parse(new ByteArrayInputStream(str.getBytes()));
318384

319385
NodeList nl = document.getElementsByTagName("tr");

0 commit comments

Comments
 (0)