Skip to content

Commit

Permalink
Fix last modified time not being updated on linux (#203)
Browse files Browse the repository at this point in the history
Fix test, clean things
  • Loading branch information
gnodet authored Apr 26, 2022
1 parent 49773f1 commit ee5fe41
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,19 +153,17 @@ private void flushBuffer( ByteBuffer writeBuffer ) throws IOException
@Override
public void close() throws IOException
{
flush();
long position = channel.position();
if ( position != channel.size() )
if ( channel.isOpen() )
{
if ( !modified )
flush();
long position = channel.position();
if ( position != channel.size() )
{
FileTime now = FileTime.from( Instant.now() );
Files.setLastModifiedTime( path, now );
modified = true;
channel.truncate( position );
}
channel.truncate( position );
channel.close();
}
channel.close();
}

public boolean isModified()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -39,7 +38,6 @@ public class CachingOutputStreamTest

Path tempDir;
Path checkLastModified;
FileTime lm;

@Before
public void setup() throws IOException
Expand All @@ -48,19 +46,18 @@ public void setup() throws IOException
Files.createDirectories( dir );
tempDir = Files.createTempDirectory( dir, "temp-" );
checkLastModified = tempDir.resolve( ".check" );
Files.newOutputStream( checkLastModified ).close();
lm = Files.getLastModifiedTime( checkLastModified );
}

private void waitLastModified() throws IOException, InterruptedException
{
Files.newOutputStream( checkLastModified ).close();
FileTime lm = Files.getLastModifiedTime( checkLastModified );
while ( true )
{
Files.newOutputStream( checkLastModified ).close();
FileTime nlm = Files.getLastModifiedTime( checkLastModified );
if ( !Objects.equals( nlm, lm ) )
{
lm = nlm;
break;
}
Thread.sleep( 10 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
Expand All @@ -38,7 +37,6 @@ public class CachingWriterTest

Path tempDir;
Path checkLastModified;
FileTime lm;

@Before
public void setup() throws IOException
Expand All @@ -47,19 +45,18 @@ public void setup() throws IOException
Files.createDirectories( dir );
tempDir = Files.createTempDirectory( dir, "temp-" );
checkLastModified = tempDir.resolve( ".check" );
Files.newOutputStream( checkLastModified ).close();
lm = Files.getLastModifiedTime( checkLastModified );
}

private void waitLastModified() throws IOException, InterruptedException
{
Files.newOutputStream( checkLastModified ).close();
FileTime lm = Files.getLastModifiedTime( checkLastModified );
while ( true )
{
Files.newOutputStream( checkLastModified ).close();
FileTime nlm = Files.getLastModifiedTime( checkLastModified );
if ( !Objects.equals( nlm, lm ) )
{
lm = nlm;
break;
}
Thread.sleep( 10 );
Expand Down

0 comments on commit ee5fe41

Please sign in to comment.