Fixed JavaIoFileSystem.setLastModifiedTime to actually match its documented behavior (passing -1 modified time should use system time).

--
MOS_MIGRATED_REVID=128489592
diff --git a/src/main/java/com/google/devtools/build/lib/vfs/JavaIoFileSystem.java b/src/main/java/com/google/devtools/build/lib/vfs/JavaIoFileSystem.java
index d2581a2..dead488 100644
--- a/src/main/java/com/google/devtools/build/lib/vfs/JavaIoFileSystem.java
+++ b/src/main/java/com/google/devtools/build/lib/vfs/JavaIoFileSystem.java
@@ -13,10 +13,13 @@
 // limitations under the License.
 package com.google.devtools.build.lib.vfs;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe;
 import com.google.devtools.build.lib.profiler.Profiler;
 import com.google.devtools.build.lib.profiler.ProfilerTask;
 import com.google.devtools.build.lib.unix.FileAccessException;
+import com.google.devtools.build.lib.util.Clock;
+import com.google.devtools.build.lib.util.JavaClock;
 
 import java.io.File;
 import java.io.FileNotFoundException;
@@ -42,12 +45,23 @@
   private static final LinkOption[] NOFOLLOW_LINKS_OPTION =
       new LinkOption[] { LinkOption.NOFOLLOW_LINKS };
 
+  private final Clock clock;
+
   protected static final String ERR_IS_DIRECTORY = " (Is a directory)";
   protected static final String ERR_DIRECTORY_NOT_EMPTY = " (Directory not empty)";
   protected static final String ERR_FILE_EXISTS = " (File exists)";
   protected static final String ERR_NO_SUCH_FILE_OR_DIR = " (No such file or directory)";
   protected static final String ERR_NOT_A_DIRECTORY = " (Not a directory)";
 
+  public JavaIoFileSystem() {
+    this(new JavaClock());
+  }
+
+  @VisibleForTesting
+  JavaIoFileSystem(Clock clock) {
+    this.clock = clock;
+  }
+
   protected File getIoFile(Path path) {
     return new File(path.toString());
   }
@@ -349,7 +363,7 @@
   @Override
   protected void setLastModifiedTime(Path path, long newTime) throws IOException {
     File file = getIoFile(path);
-    if (!file.setLastModified(newTime)) {
+    if (!file.setLastModified(newTime == -1L ? clock.currentTimeMillis() : newTime)) {
       if (!file.exists()) {
         throw new FileNotFoundException(path + ERR_NO_SUCH_FILE_OR_DIR);
       } else if (!file.getParentFile().canWrite()) {
diff --git a/src/test/java/com/google/devtools/build/lib/vfs/JavaIoFileSystemTest.java b/src/test/java/com/google/devtools/build/lib/vfs/JavaIoFileSystemTest.java
index 3251b09..efbd5b0 100644
--- a/src/test/java/com/google/devtools/build/lib/vfs/JavaIoFileSystemTest.java
+++ b/src/test/java/com/google/devtools/build/lib/vfs/JavaIoFileSystemTest.java
@@ -13,6 +13,10 @@
 // limitations under the License.
 package com.google.devtools.build.lib.vfs;
 
+import static org.junit.Assert.assertEquals;
+
+import com.google.devtools.build.lib.testutil.ManualClock;
+
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.JUnit4;
@@ -25,16 +29,34 @@
 @RunWith(JUnit4.class)
 public class JavaIoFileSystemTest extends SymlinkAwareFileSystemTest {
 
+  private ManualClock clock;
+
   @Override
   public FileSystem getFreshFileSystem() {
-    return new JavaIoFileSystem();
+    clock = new ManualClock();
+    return new JavaIoFileSystem(clock);
   }
 
-  // The tests are just inherited from the FileSystemTest
+  // Tests are inherited from the FileSystemTest
 
   // JavaIoFileSystem incorrectly throws a FileNotFoundException for all IO errors. This means that
   // statIfFound incorrectly suppresses those errors.
   @Override
   @Test
   public void testBadPermissionsThrowsExceptionOnStatIfFound() {}
+
+  @Test
+  public void testSetLastModifiedTime() throws Exception {
+    Path file = xEmptyDirectory.getChild("new-file");
+    FileSystemUtils.createEmptyFile(file);
+
+    file.setLastModifiedTime(1000L);
+    assertEquals(1000L, file.getLastModifiedTime());
+    file.setLastModifiedTime(0L);
+    assertEquals(0L, file.getLastModifiedTime());
+
+    clock.advanceMillis(42000L);
+    file.setLastModifiedTime(-1L);
+    assertEquals(42000L, file.getLastModifiedTime());
+  }
 }