Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;
import java.nio.file.FileStore;
import java.nio.file.Files;

import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Shell;
Expand Down Expand Up @@ -50,6 +52,10 @@ public class HardLink {
private static HardLinkCommandGetter getHardLinkCommand;

public final LinkStats linkStats; //not static

private static final String FileAttributeView = "unix";

private static final String FileAttribute = "unix:nlink";

//initialize the command "getters" statically, so can use their
//methods without instantiating the HardLink object
Expand Down Expand Up @@ -203,6 +209,15 @@ public static void createHardLinkMult(File parentDir, String[] fileBaseNames,
parentDir.toPath().resolve(name));
}
}
@VisibleForTesting
static boolean supportsHardLink(File f) {
try {
FileStore store = Files.getFileStore(f.toPath());
return store.supportsFileAttributeView(FileAttributeView);
} catch (IOException e) {
return false;
}
}

/**
* Retrieves the number of links to the specified file.
Expand All @@ -220,6 +235,10 @@ public static int getLinkCount(File fileName) throws IOException {
throw new FileNotFoundException(fileName + " not found.");
}

if (supportsHardLink(fileName)) {
return (int) Files.getAttribute(fileName.toPath(), FileAttribute);
}

// construct and execute shell command
String[] cmd = getHardLinkCommand.linkCount(fileName);
String inpMsg = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.Files;
import java.util.Arrays;

import org.apache.hadoop.test.GenericTestUtils;
Expand Down Expand Up @@ -204,6 +206,8 @@ private String fetchFileContents(File file)
char[] result = Arrays.copyOf(buf, cnt);
return new String(result);
}



/**
* Sanity check the simplest case of HardLink.getLinkCount()
Expand All @@ -219,6 +223,16 @@ public void testGetLinkCount() throws IOException {
assertEquals(1, getLinkCount(x3));
}

@Test
public void testGetLinkCountFromFileAttribute() throws IOException {
assertTrue(supportsHardLink(x1));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

supportsHardLink(x1) may be false if os is not unix style

assertEquals(1, getLinkCount(x1));
assertTrue(supportsHardLink(x2));
assertEquals(1, getLinkCount(x2));
assertTrue(supportsHardLink(x3));
assertEquals(1, getLinkCount(x3));
}

/**
* Test the single-file method HardLink.createHardLink().
* Also tests getLinkCount() with values greater than one.
Expand Down