use of org.apache.hadoop.io.nativeio.NativeIO.POSIX.Stat in project hadoop by apache.
the class SecureIOUtils method forceSecureOpenFSDataInputStream.
/**
* Same as openFSDataInputStream except that it will run even if security is
* off. This is used by unit tests.
*/
@VisibleForTesting
protected static FSDataInputStream forceSecureOpenFSDataInputStream(File file, String expectedOwner, String expectedGroup) throws IOException {
final FSDataInputStream in = rawFilesystem.open(new Path(file.getAbsolutePath()));
boolean success = false;
try {
Stat stat = NativeIO.POSIX.getFstat(in.getFileDescriptor());
checkStat(file, stat.getOwner(), stat.getGroup(), expectedOwner, expectedGroup);
success = true;
return in;
} finally {
if (!success) {
in.close();
}
}
}
use of org.apache.hadoop.io.nativeio.NativeIO.POSIX.Stat in project hadoop by apache.
the class SecureIOUtils method forceSecureOpenForRead.
/**
* Same as openForRead() except that it will run even if security is off.
* This is used by unit tests.
*/
@VisibleForTesting
protected static FileInputStream forceSecureOpenForRead(File f, String expectedOwner, String expectedGroup) throws IOException {
FileInputStream fis = new FileInputStream(f);
boolean success = false;
try {
Stat stat = NativeIO.POSIX.getFstat(fis.getFD());
checkStat(f, stat.getOwner(), stat.getGroup(), expectedOwner, expectedGroup);
success = true;
return fis;
} finally {
if (!success) {
fis.close();
}
}
}
use of org.apache.hadoop.io.nativeio.NativeIO.POSIX.Stat in project hadoop by apache.
the class TestNativeIO method testFstatClosedFd.
@Test(timeout = 30000)
public void testFstatClosedFd() throws Exception {
FileOutputStream fos = new FileOutputStream(new File(TEST_DIR, "testfstat2"));
fos.close();
try {
NativeIO.POSIX.Stat stat = NativeIO.POSIX.getFstat(fos.getFD());
} catch (NativeIOException nioe) {
LOG.info("Got expected exception", nioe);
assertEquals(Errno.EBADF, nioe.getErrno());
}
}
use of org.apache.hadoop.io.nativeio.NativeIO.POSIX.Stat in project hadoop by apache.
the class TestNativeIO method testMultiThreadedFstat.
/**
* Test for races in fstat usage
*
* NOTE: this test is likely to fail on RHEL 6.0 which has a non-threadsafe
* implementation of getpwuid_r.
*/
@Test(timeout = 30000)
public void testMultiThreadedFstat() throws Exception {
assumeNotWindows();
final FileOutputStream fos = new FileOutputStream(new File(TEST_DIR, "testfstat"));
final AtomicReference<Throwable> thrown = new AtomicReference<Throwable>();
List<Thread> statters = new ArrayList<Thread>();
for (int i = 0; i < 10; i++) {
Thread statter = new Thread() {
@Override
public void run() {
long et = Time.now() + 5000;
while (Time.now() < et) {
try {
NativeIO.POSIX.Stat stat = NativeIO.POSIX.getFstat(fos.getFD());
assertEquals(System.getProperty("user.name"), stat.getOwner());
assertNotNull(stat.getGroup());
assertTrue(!stat.getGroup().isEmpty());
assertEquals("Stat mode field should indicate a regular file", S_IFREG, stat.getMode() & S_IFMT);
} catch (Throwable t) {
thrown.set(t);
}
}
}
};
statters.add(statter);
statter.start();
}
for (Thread t : statters) {
t.join();
}
fos.close();
if (thrown.get() != null) {
throw new RuntimeException(thrown.get());
}
}
use of org.apache.hadoop.io.nativeio.NativeIO.POSIX.Stat in project hadoop by apache.
the class SecureIOUtils method forceSecureOpenForRandomRead.
/**
* Same as openForRandomRead except that it will run even if security is off.
* This is used by unit tests.
*/
@VisibleForTesting
protected static RandomAccessFile forceSecureOpenForRandomRead(File f, String mode, String expectedOwner, String expectedGroup) throws IOException {
RandomAccessFile raf = new RandomAccessFile(f, mode);
boolean success = false;
try {
Stat stat = NativeIO.POSIX.getFstat(raf.getFD());
checkStat(f, stat.getOwner(), stat.getGroup(), expectedOwner, expectedGroup);
success = true;
return raf;
} finally {
if (!success) {
raf.close();
}
}
}
Aggregations