Search in sources :

Example 1 with Stat

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();
        }
    }
}
Also used : Path(org.apache.hadoop.fs.Path) Stat(org.apache.hadoop.io.nativeio.NativeIO.POSIX.Stat) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with Stat

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();
        }
    }
}
Also used : Stat(org.apache.hadoop.io.nativeio.NativeIO.POSIX.Stat) FileInputStream(java.io.FileInputStream) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with Stat

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());
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) Stat(org.apache.hadoop.io.nativeio.NativeIO.POSIX.Stat) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) POSIX(org.apache.hadoop.io.nativeio.NativeIO.POSIX) Test(org.junit.Test)

Example 4 with Stat

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());
    }
}
Also used : ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) Stat(org.apache.hadoop.io.nativeio.NativeIO.POSIX.Stat) POSIX(org.apache.hadoop.io.nativeio.NativeIO.POSIX) FileOutputStream(java.io.FileOutputStream) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Test(org.junit.Test)

Example 5 with Stat

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();
        }
    }
}
Also used : Stat(org.apache.hadoop.io.nativeio.NativeIO.POSIX.Stat) RandomAccessFile(java.io.RandomAccessFile) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

Stat (org.apache.hadoop.io.nativeio.NativeIO.POSIX.Stat)6 RandomAccessFile (java.io.RandomAccessFile)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 POSIX (org.apache.hadoop.io.nativeio.NativeIO.POSIX)3 Test (org.junit.Test)3 FileInputStream (java.io.FileInputStream)1 ArrayList (java.util.ArrayList)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 FSDataInputStream (org.apache.hadoop.fs.FSDataInputStream)1 Path (org.apache.hadoop.fs.Path)1 UserGroupInformation (org.apache.hadoop.security.UserGroupInformation)1