package org.apache.lucene.mockfile;

/*
 * 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.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileAttribute;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;

/** 
 * Adds extra files/subdirectories when directories are created.
 * <p>
 * Lucene shouldn't care about these, but sometimes operating systems
 * create special files themselves (.DS_Store, thumbs.db, .nfsXXX, ...),
 * so we add them and see what breaks. 
 * <p>
 * When a directory is created, sometimes a file or directory named 
 * "extraNNNN" will be included with it.
 * All other filesystem operations are passed thru as normal.
 */
public class ExtrasFS extends FilterFileSystemProvider {
  final AtomicLong counter = new AtomicLong();
  final Random random;
  
  /** 
   * Create a new instance, wrapping {@code delegate}.
   */
  public ExtrasFS(FileSystem delegate, Random random) {
    super("extras://", delegate);
    this.random = random;
  }

  @Override
  public void createDirectory(Path dir, FileAttribute<?>... attrs) throws IOException {
    super.createDirectory(dir, attrs);
    // ok, we created the directory successfully.
    if (random.nextInt(4) == 0) {
      // lets add a bogus file... if this fails, we don't care, its best effort.
      try {
        Path target = dir.resolve("extra" + counter.incrementAndGet());
        if (random.nextBoolean()) {
          super.createDirectory(target);
        } else {
          Files.createFile(target);
        }
      } catch (Exception ignored) { 
        // best effort
      }
    }
  }
  
  // TODO: would be great if we overrode attributes, so file size was always zero for
  // our fake files. But this is tricky because its hooked into several places. 
  // Currently MDW has a hack so we don't break disk full tests.

}

@@ -1068,16 +1068,78 @@
 cLong;%0A%0A
+import com.carrotsearch.randomizedtesting.RandomizedContext;%0A%0A
 /** %0A * 
@@ -1479,12 +1479,9 @@
 xtra
-NNNN
+0
 %22 wi
@@ -1639,68 +1639,16 @@
 nal 
-AtomicLong counter = new AtomicLong();%0A  final Random random
+int seed
 ;%0A  
@@ -1815,22 +1815,20 @@
    this.
-random
+seed
  = rando
@@ -1828,16 +1828,26 @@
 = random
+.nextInt()
 ;%0A  %7D%0A%0A 
@@ -1983,16 +1983,19 @@
  attrs);
+   
 %0A    // 
@@ -2045,28 +2045,505 @@
     
-if (random.nextInt(4
+%0A    // a little funky: we only look at hashcode (well-defined) of the target class name.%0A    // using a generator won't reproduce, because we are a per-class resource.%0A    // using hashing on filenames won't reproduce, because many of the names rely on other things%0A    // the test class did.%0A    // so a test gets terrorized with extras or gets none at all depending on the initial seed.%0A    int hash = RandomizedContext.current().getTargetClass().toString().hashCode() %5E seed;%0A    if ((hash & 3
 ) ==
@@ -2685,72 +2685,33 @@
 xtra
-%22 + counter.incrementAndGet());%0A        if (random.nextBoolean()
+0%22);%0A        if (hash %3C 0
 ) %7B%0A

make ExtrasFS reproducible