Skip to content

Commit 430198d

Browse files
author
justin
committed
Fixed defect in SizeHelper. Added some additional logging.
1 parent 751497c commit 430198d

6 files changed

Lines changed: 79 additions & 10 deletions

File tree

README.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ RELEASE NOTES:
3333

3434
Changes in 4.8.1:
3535
1. Bug fix to FileSystemWriter to handle files that did do not already exist.
36+
2. Bug fix to SizeHelper.
3637

3738
Changes in 4.8:
3839
1. Added additional convenience method to FileHelper.

src/main/java/gov/loc/repository/bagit/BagHelper.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ public static String getVersion(File bagFile) {
5151
}
5252

5353
public static long generatePayloadOctetCount(Bag bag) {
54+
log.debug("Generating payload octet count");
5455
long count = 0;
5556
for(BagFile bagFile : bag.getPayload()) {
5657
count = count + bagFile.getSize();
58+
log.trace(MessageFormat.format("Octet count after adding {0} is {1}", bagFile.getFilepath(), count));
5759
}
5860
return count;
5961
}
@@ -63,16 +65,21 @@ public static String generatePayloadOxum(Bag bag) {
6365
}
6466

6567
public static long generateTagOctetCount(Bag bag) {
68+
log.debug("Generating tag octet count");
6669
long count = 0;
67-
for(BagFile bagFile : bag.getTags()) {
70+
for(BagFile bagFile : bag.getTags()) {
6871
count = count + bagFile.getSize();
72+
log.trace(MessageFormat.format("Octet count after adding {0} is {1}", bagFile.getFilepath(), count));
6973
}
7074
return count;
7175
}
7276

7377
public static String generateBagSize(Bag bag) {
7478
long count = generateTagOctetCount(bag) + generatePayloadOctetCount(bag);
75-
return SizeHelper.getSize(count);
79+
log.trace(MessageFormat.format("Total octet count is {0}", count));
80+
String size = SizeHelper.getSize(count);
81+
log.trace(MessageFormat.format("Size is {0}", size));
82+
return size;
7683
}
7784

7885
public static boolean isPayload(String filepath, BagConstants bagConstants) {

src/main/java/gov/loc/repository/bagit/impl/PreBagImpl.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,18 @@ public Bag makeBagInPlace(Version version, boolean retainBaseDirectory, boolean
7171
//If retainBaseDirectory
7272
File moveToDir = dataDir;
7373
if (retainBaseDirectory) {
74+
log.trace("Retaining base directory");
7475
//Create new base directory in data directory
7576
moveToDir = new File(dataDir, this.dir.getName());
7677
//Move contents of base directory to new base directory
7778
}
78-
log.trace("Move to dir is " + moveToDir);
79+
log.debug(MessageFormat.format("Data directory does not exist so moving files to {0}", moveToDir));
7980
for(File file : FileHelper.normalizeForm(this.dir.listFiles())) {
8081
if (! (file.equals(dataDir) || (file.isDirectory() && this.ignoreDirs.contains(file.getName())))) {
82+
log.trace(MessageFormat.format("Moving {0} to {1}", file, moveToDir));
8183
FileUtils.moveToDirectory(file, moveToDir, true);
84+
} else {
85+
log.trace(MessageFormat.format("Not moving {0}", file));
8286
}
8387
}
8488

@@ -102,10 +106,14 @@ public Bag makeBagInPlace(Version version, boolean retainBaseDirectory, boolean
102106

103107
//Handle empty directories
104108
if (keepEmptyDirectories) {
109+
log.debug("Adding .keep files to empty directories");
105110
this.addKeep(dataDir);
111+
} else {
112+
log.trace("Not adding .keep files to empty directories");
106113
}
107114

108115
//Copy the tags
116+
log.debug("Copying tag files");
109117
for(File tagFile : this.tagFiles) {
110118
log.trace(MessageFormat.format("Copying tag file {0} to {1}", tagFile, this.dir));
111119
try {
@@ -116,10 +124,13 @@ public Bag makeBagInPlace(Version version, boolean retainBaseDirectory, boolean
116124
}
117125

118126
//Create a bag
127+
log.debug(MessageFormat.format("Creating bag by payload files at {0}", this.dir));
119128
Bag bag = this.bagFactory.createBagByPayloadFiles(this.dir, version, this.ignoreDirs);
120129
//Complete the bag
130+
log.debug("Making complete");
121131
bag = bag.makeComplete(completer);
122132
//Write the bag
133+
log.debug("Writing");
123134
return bag.write(new FileSystemWriter(this.bagFactory), this.dir);
124135
}
125136

src/main/java/gov/loc/repository/bagit/transformer/impl/DefaultCompleter.java

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package gov.loc.repository.bagit.transformer.impl;
22

3+
import java.text.MessageFormat;
34
import java.util.Calendar;
45

6+
import org.apache.commons.logging.Log;
7+
import org.apache.commons.logging.LogFactory;
8+
59
import gov.loc.repository.bagit.Bag;
610
import gov.loc.repository.bagit.BagFactory;
711
import gov.loc.repository.bagit.BagInfoTxt;
@@ -12,6 +16,8 @@
1216

1317
public class DefaultCompleter extends LongRunningOperationBase implements Completer {
1418

19+
private static final Log log = LogFactory.getLog(DefaultCompleter.class);
20+
1521
private boolean generateTagManifest = true;
1622
private boolean updatePayloadOxum = true;
1723
private boolean updateBaggingDate = true;
@@ -84,21 +90,37 @@ public void setClearExistingPayloadManifests(boolean clearPayloadManifests) {
8490
}
8591

8692
@Override
87-
public Bag complete(Bag bag) {
93+
public Bag complete(Bag bag) {
94+
log.info(MessageFormat.format("Completing bag at {0}", bag.getFile()));
95+
96+
log.debug("Creating new bag and adding bag files from existing bag");
8897
this.newBag = this.bagFactory.createBag(bag);
8998
this.newBag.putBagFiles(bag.getPayload());
9099
this.newBag.putBagFiles(bag.getTags());
100+
101+
log.debug("Handling bagit.txt");
91102
this.handleBagIt();
103+
104+
log.debug("Handling bag-info.txt");
92105
this.handleBagInfo();
106+
93107
if (this.completePayloadManifests) {
108+
log.debug("Completing payload manifests");
94109
this.handlePayloadManifests();
110+
} else {
111+
log.trace("Not completing payload manifests");
95112
}
113+
96114
if (this.completeTagManifests) {
115+
log.debug("Completing tag manifests");
97116
this.handleTagManifests();
117+
} else {
118+
log.trace("Not completing tag manifests");
98119
}
99120

100121
if (this.isCancelled()) return null;
101122

123+
log.trace("Done completing");
102124
return this.newBag;
103125
}
104126

@@ -120,32 +142,59 @@ protected void handleBagInfo() {
120142
this.newBag.putBagFile(bagInfo);
121143

122144
if (this.updatePayloadOxum) {
145+
log.debug("Generating payload-oxum");
123146
bagInfo.generatePayloadOxum(this.newBag);
147+
} else {
148+
log.trace("Not generating payload-oxum");
124149
}
150+
125151
if (this.updateBaggingDate) {
152+
log.debug("Setting bagging date");
126153
bagInfo.setBaggingDate(Calendar.getInstance().getTime());
154+
} else {
155+
log.trace("Not setting bagging date");
127156
}
157+
128158
if (this.updateBagSize) {
159+
log.debug("Generating bag size");
129160
bagInfo.generateBagSize(this.newBag);
161+
} else {
162+
log.debug("Not generating bag size");
130163
}
131164

132165
}
133166

134167
protected void handleTagManifests() {
135168
if (this.clearTagManifests) {
169+
log.debug("Clearing tag manifests");
136170
this.helper.clearManifests(this.newBag, this.newBag.getTagManifests());
171+
} else {
172+
log.trace("Not clearing tag manifests");
137173
}
174+
175+
log.debug("Cleaning tag manifests");
138176
this.helper.cleanManifests(this.newBag, this.newBag.getTagManifests());
177+
139178
if (this.generateTagManifest) {
179+
log.debug("Generating tag manifests");
140180
this.helper.handleManifest(this.newBag, this.tagManifestAlgorithm, ManifestHelper.getTagManifestFilename(this.tagManifestAlgorithm, this.newBag.getBagConstants()), this.newBag.getTags(), this.nonDefaultManifestSeparator);
181+
} else {
182+
log.trace("Generating tag manifests");
141183
}
142184
}
143185

144-
protected void handlePayloadManifests() {
186+
protected void handlePayloadManifests() {
145187
if (this.clearPayloadManifests) {
188+
log.debug("Clearing payload manifests");
146189
this.helper.clearManifests(this.newBag, this.newBag.getPayloadManifests());
190+
} else {
191+
log.trace("Not clearing payload manifests");
147192
}
193+
194+
log.debug("Cleaning payload manifests");
148195
this.helper.cleanManifests(this.newBag, this.newBag.getPayloadManifests());
196+
197+
log.debug("Generating payload manifests");
149198
this.helper.handleManifest(this.newBag, this.payloadManifestAlgorithm, ManifestHelper.getPayloadManifestFilename(this.payloadManifestAlgorithm, this.newBag.getBagConstants()),this.newBag.getPayload(), this.nonDefaultManifestSeparator);
150199
}
151200

@@ -156,6 +205,5 @@ public String getNonDefaultManifestSeparator() {
156205
public void setNonDefaultManifestSeparator(String manifestSeparator) {
157206
this.nonDefaultManifestSeparator = manifestSeparator;
158207
}
159-
160208

161209
}

src/main/java/gov/loc/repository/bagit/utilities/SizeHelper.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ public static String getSize(long octets) {
3232
String format = "#.#";
3333
double size = octets/div;
3434
String sizeString = (new DecimalFormat(format)).format(size);
35-
while (sizeString.endsWith("0")) {
36-
format += "#";
37-
sizeString = (new DecimalFormat(format)).format(size);
35+
if (size % 1 != 0) {
36+
while (sizeString.endsWith("0")) {
37+
format += "#";
38+
sizeString = (new DecimalFormat(format)).format(size);
39+
}
3840
}
3941
return sizeString + " " + unit;
4042
}

src/test/java/gov/loc/repository/bagit/utilities/SizeHelperTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void testGetSize() {
1717
assertEquals("1.1 MB", SizeHelper.getSize(1148576L));
1818
assertEquals("1 GB", SizeHelper.getSize(1073741824L));
1919
assertEquals("1 TB", SizeHelper.getSize(1099511627776L));
20-
20+
assertEquals("300 KB", SizeHelper.getSize(307200L));
2121
}
2222

2323
}

0 commit comments

Comments
 (0)