package org.apache.lucene.store;

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;

import org.apache.lucene.codecs.CodecUtil;
import org.apache.lucene.util.IOUtils;

public class TestHardLinkCopyDirectoryWrapper extends BaseDirectoryTestCase {

  @Override
  protected Directory getDirectory(Path file) throws IOException {
    Directory open = random().nextBoolean() ? newFSDirectory(file) : newDirectory();
    return new HardlinkCopyDirectoryWrapper(open);
  }

  /**
   * Tests that we use hardlinks if possible on Directory#copyFrom
   */
  public void testCopyHardLinks() throws IOException {
    Path tempDir = createTempDir();
    Path dir_1 = tempDir.resolve("dir_1");
    Path dir_2 = tempDir.resolve("dir_2");
    Files.createDirectories(dir_1);
    Files.createDirectories(dir_2);

    Directory luceneDir_1 = newFSDirectory(dir_1);
    Directory luceneDir_2 = newFSDirectory(dir_2);
    try(IndexOutput output = luceneDir_1.createOutput("foo.bar", IOContext.DEFAULT)) {
      CodecUtil.writeHeader(output, "foo", 0);
      output.writeString("hey man, nice shot!");
      CodecUtil.writeFooter(output);
    }
    try {
      Files.createLink(tempDir.resolve("test"), dir_1.resolve("foo.bar"));
      BasicFileAttributes destAttr = Files.readAttributes(tempDir.resolve("test"), BasicFileAttributes.class);
      BasicFileAttributes sourceAttr = Files.readAttributes(dir_1.resolve("foo.bar"), BasicFileAttributes.class);
      assumeTrue("hardlinks are not supported", destAttr.fileKey() != null
          && destAttr.fileKey().equals(sourceAttr.fileKey()));
    } catch (UnsupportedOperationException ex) {
      assumeFalse("hardlinks are not supported", false);
    }

    HardlinkCopyDirectoryWrapper wrapper = new HardlinkCopyDirectoryWrapper(luceneDir_2);
    wrapper.copyFrom(luceneDir_1, "foo.bar", "bar.foo", IOContext.DEFAULT);
    assertTrue(Files.exists(dir_2.resolve("bar.foo")));
    BasicFileAttributes destAttr = Files.readAttributes(dir_2.resolve("bar.foo"), BasicFileAttributes.class);
    BasicFileAttributes sourceAttr = Files.readAttributes(dir_1.resolve("foo.bar"), BasicFileAttributes.class);
    assertEquals(destAttr.fileKey(), sourceAttr.fileKey());
    try(ChecksumIndexInput indexInput = wrapper.openChecksumInput("bar.foo", IOContext.DEFAULT)) {
      CodecUtil.checkHeader(indexInput, "foo", 0, 0);
      assertEquals("hey man, nice shot!", indexInput.readString());
      CodecUtil.checkFooter(indexInput);
    }
    IOUtils.close(luceneDir_1, luceneDir_2);
  }
}

@@ -1786,16 +1786,29 @@
 %0A    try
+ %7B%0A      try 
 (IndexOu
@@ -1877,32 +1877,34 @@
 FAULT)) %7B%0A      
+  
 CodecUtil.writeH
@@ -1934,16 +1934,18 @@
 ;%0A      
+ 
+ 
 output.w
@@ -1977,32 +1977,34 @@
  shot!%22);%0A      
+  
 CodecUtil.writeF
@@ -2014,24 +2014,26 @@
 er(output);%0A
+  
     %7D%0A    tr
@@ -2026,30 +2026,34 @@
       %7D%0A    
+  
 try %7B%0A
+  
       Files.
@@ -2111,24 +2111,26 @@
 foo.bar%22));%0A
+  
       BasicF
@@ -2230,24 +2230,26 @@
 ass);%0A      
+  
 BasicFileAtt
@@ -2346,24 +2346,26 @@
 ass);%0A      
+  
 assumeTrue(%22
@@ -2431,16 +2431,18 @@
         
+ 
+ 
 && destA
@@ -2490,16 +2490,18 @@
 ));%0A    
+  
 %7D catch 
@@ -2533,24 +2533,26 @@
 ption ex) %7B%0A
+  
       assume
@@ -2592,20 +2592,19 @@
 d%22, 
-fals
+tru
 e);%0A    
 %7D%0A%0A 
@@ -2599,19 +2599,23 @@
 e);%0A    
+  
 %7D%0A%0A
+  
     Hard
@@ -2700,16 +2700,18 @@
 2);%0A    
+  
 wrapper.
@@ -2774,24 +2774,26 @@
 FAULT);%0A    
+  
 assertTrue(F
@@ -2832,24 +2832,26 @@
 oo%22)));%0A    
+ 
+ 
 BasicFileAtt
@@ -2940,24 +2940,26 @@
 tes.class);%0A
+  
     BasicFil
@@ -3054,24 +3054,26 @@
 tes.class);%0A
+  
     assertEq
@@ -3124,19 +3124,22 @@
 ));%0A    
+  
 try
+ 
 (Checksu
@@ -3218,24 +3218,26 @@
 DEFAULT)) %7B%0A
+  
       CodecU
@@ -3276,32 +3276,34 @@
 %22, 0, 0);%0A      
+  
 assertEquals(%22he
@@ -3350,24 +3350,26 @@
 g());%0A      
+  
 CodecUtil.ch
@@ -3391,26 +3391,118 @@
 Input);%0A    
+  
 %7D%0A
+    %7D finally %7B%0A      // close them in a finally block we might run into an assume here%0A  
     IOUtils.
@@ -3534,14 +3534,21 @@
 Dir_2);%0A
+    %7D%0A%0A
   %7D%0A%7D%0A

Fix TestHardLinkCopyDirectoryWrapper if an assume is hit all directories must be closed