Skip to content

Commit

Permalink
XCOMMONS-2387: Exception when saving status because file path too long
Browse files Browse the repository at this point in the history
  • Loading branch information
tmortagne committed Oct 31, 2024
1 parent bc70b68 commit 01d4b5d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import javax.inject.Inject;
import javax.inject.Singleton;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
Expand Down Expand Up @@ -343,8 +344,20 @@ private File getJobFolder(List<String> id)
File folder = this.configuration.getStorage();

if (id != null) {
for (String idElement : id) {
folder = new File(folder, encode(idElement));
// Create a different folder for each element
for (String fullIdElement : id) {
// But cut each element is it's bigger than 255 bytes (and not characters) since it's a very common
// limit for a single element of the path among file systems
// To be sure to deal with characters not taking more than 1 byte, we start by encoding it in base 64
String encodedIdElement = Base64.encodeBase64String(fullIdElement.getBytes());
if (encodedIdElement.length() > 255) {
do {
folder = new File(folder, encode(encodedIdElement.substring(0, 255)));
encodedIdElement = encodedIdElement.substring(255);
} while (encodedIdElement.length() > 255);
} else {
folder = new File(folder, encode(encodedIdElement));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@

import javax.inject.Provider;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
Expand Down Expand Up @@ -396,6 +398,17 @@ void storeJobStatusWithNullId()
this.store.store(jobStatus);
}

@Test
void storeJobStatusWithBigId()
{
DefaultRequest request = new DefaultRequest();
request.setId(StringUtils.repeat('a', 768));

JobStatus jobStatus = new DefaultJobStatus("type", request, null, null, null);

this.store.store(jobStatus);
}

@Test
void storeUnserializableJobStatus()
{
Expand Down Expand Up @@ -442,7 +455,8 @@ void storeJobStatusWhenSerializable()

// Verify that the status has been serialized, indirectly verifying that isSerializable() has been called and
// returned true.
assertTrue(new File(this.storeDirectory, "newstatus/status.xml.zip").exists());
assertTrue(new File(this.storeDirectory, Base64.encodeBase64String(id.get(0).getBytes()) + "/status.xml.zip")
.exists());
}

@Test
Expand Down

0 comments on commit 01d4b5d

Please sign in to comment.