/// <summary>Two buffer dirs.</summary> /// <remarks> /// Two buffer dirs. The first dir does not exist & is on a read-only disk; /// The second dir exists & is RW /// getLocalPathForWrite with checkAccess set to false should create a parent /// directory. With checkAccess true, the directory should not be created. /// </remarks> /// <exception cref="System.Exception"/> /// <exception cref="System.IO.IOException"/> public virtual void TestLocalPathForWriteDirCreation() { string dir0 = BuildBufferDir(Root, 0); string dir1 = BuildBufferDir(Root, 1); try { conf.Set(Context, dir0 + "," + dir1); Assert.True(localFs.Mkdirs(new Path(dir1))); BufferRoot.SetReadOnly(); Path p1 = dirAllocator.GetLocalPathForWrite("p1/x", SmallFileSize, conf); Assert.True(localFs.GetFileStatus(p1.GetParent()).IsDirectory() ); Path p2 = dirAllocator.GetLocalPathForWrite("p2/x", SmallFileSize, conf, false); try { localFs.GetFileStatus(p2.GetParent()); } catch (Exception e) { Assert.Equal(e.GetType(), typeof(FileNotFoundException)); } } finally { Shell.ExecCommand(Shell.GetSetPermissionCommand("u+w", false, BufferDirRoot)); RmBufferDirs(); } }
/// <exception cref="System.IO.IOException"/> private void CheckFileCorruption(LocalFileSystem fileSys, Path file, Path fileToCorrupt ) { // corrupt the file RandomAccessFile @out = new RandomAccessFile(new FilePath(fileToCorrupt.ToString( )), "rw"); byte[] buf = new byte[(int)fileSys.GetFileStatus(file).GetLen()]; int corruptFileLen = (int)fileSys.GetFileStatus(fileToCorrupt).GetLen(); NUnit.Framework.Assert.IsTrue(buf.Length >= corruptFileLen); rand.NextBytes(buf); @out.Seek(corruptFileLen / 2); @out.Write(buf, 0, corruptFileLen / 4); @out.Close(); bool gotException = false; InputStream @in = fileSys.Open(file); try { IOUtils.ReadFully(@in, buf, 0, buf.Length); } catch (ChecksumException) { gotException = true; } NUnit.Framework.Assert.IsTrue(gotException); @in.Close(); }
/// <exception cref="System.Exception"/> private void _mkdirs(bool exists, FsPermission before, FsPermission after) { FilePath localDir = MockitoMaker.Make(MockitoMaker.Stub <FilePath>().Returning(exists ).from.Exists()); Org.Mockito.Mockito.When(localDir.Mkdir()).ThenReturn(true); Path dir = Org.Mockito.Mockito.Mock <Path>(); // use default stubs LocalFileSystem fs = MockitoMaker.Make(MockitoMaker.Stub <LocalFileSystem>().Returning (localDir).from.PathToFile(dir)); FileStatus stat = MockitoMaker.Make(MockitoMaker.Stub <FileStatus>().Returning(after ).from.GetPermission()); Org.Mockito.Mockito.When(fs.GetFileStatus(dir)).ThenReturn(stat); try { DiskChecker.MkdirsWithExistsAndPermissionCheck(fs, dir, before); if (!exists) { Org.Mockito.Mockito.Verify(fs).SetPermission(dir, before); } else { Org.Mockito.Mockito.Verify(fs).GetFileStatus(dir); Org.Mockito.Mockito.Verify(stat).GetPermission(); } } catch (DiskChecker.DiskErrorException e) { if (before != after) { Assert.True(e.Message.StartsWith("Incorrect permission")); } } }
/// <exception cref="System.IO.IOException"/> private string ReadFile(string @out) { Path path = new Path(@out); FileStatus stat = lfs.GetFileStatus(path); FSDataInputStream @in = lfs.Open(path); byte[] buffer = new byte[(int)stat.GetLen()]; @in.ReadFully(buffer); @in.Close(); lfs.Delete(path, false); return(Runtime.GetStringForBytes(buffer)); }
public virtual void TestCorruptedChecksum() { Path testPath = new Path(TestRootDir, "testCorruptChecksum"); Path checksumPath = localFs.GetChecksumFile(testPath); // write a file to generate checksum FSDataOutputStream @out = localFs.Create(testPath, true); @out.Write(Runtime.GetBytesForString("testing 1 2 3")); @out.Close(); Assert.True(localFs.Exists(checksumPath)); FileStatus stat = localFs.GetFileStatus(checksumPath); // alter file directly so checksum is invalid @out = localFs.GetRawFileSystem().Create(testPath, true); @out.Write(Runtime.GetBytesForString("testing stale checksum")); @out.Close(); Assert.True(localFs.Exists(checksumPath)); // checksum didn't change on disk Assert.Equal(stat, localFs.GetFileStatus(checksumPath)); Exception e = null; try { localFs.SetVerifyChecksum(true); FileSystemTestHelper.ReadFile(localFs, testPath, 1024); } catch (ChecksumException ce) { e = ce; } finally { NUnit.Framework.Assert.IsNotNull("got checksum error", e); } localFs.SetVerifyChecksum(false); string str = FileSystemTestHelper.ReadFile(localFs, testPath, 1024); Assert.Equal("testing stale checksum", str); }
/// <summary>Create the directory or check permissions if it already exists.</summary> /// <remarks> /// Create the directory or check permissions if it already exists. /// The semantics of mkdirsWithExistsAndPermissionCheck method is different /// from the mkdirs method provided in the Sun's java.io.File class in the /// following way: /// While creating the non-existent parent directories, this method checks for /// the existence of those directories if the mkdir fails at any point (since /// that directory might have just been created by some other process). /// If both mkdir() and the exists() check fails for any seemingly /// non-existent directory, then we signal an error; Sun's mkdir would signal /// an error (return false) if a directory it is attempting to create already /// exists or the mkdir fails. /// </remarks> /// <param name="localFS">local filesystem</param> /// <param name="dir">directory to be created or checked</param> /// <param name="expected">expected permission</param> /// <exception cref="System.IO.IOException"/> public static void MkdirsWithExistsAndPermissionCheck(LocalFileSystem localFS, Path dir, FsPermission expected) { FilePath directory = localFS.PathToFile(dir); bool created = false; if (!directory.Exists()) { created = MkdirsWithExistsCheck(directory); } if (created || !localFS.GetFileStatus(dir).GetPermission().Equals(expected)) { localFS.SetPermission(dir, expected); } }
/// <exception cref="System.IO.IOException"/> public virtual void TestPathEscapes() { Path path = new Path(TestRootDir, "foo%bar"); FileSystemTestHelper.WriteFile(fileSys, path, 1); FileStatus status = fileSys.GetFileStatus(path); Assert.Equal(path.MakeQualified(fileSys), status.GetPath()); CleanupFile(fileSys, path); }
/// <exception cref="System.IO.IOException"/> private void TestFileCorruption(LocalFileSystem fileSys) { // create a file and verify that checksum corruption results in // a checksum exception on LocalFS string dir = PathUtils.GetTestDirName(GetType()); Path file = new Path(dir + "/corruption-test.dat"); Path crcFile = new Path(dir + "/.corruption-test.dat.crc"); WriteFile(fileSys, file); int fileLen = (int)fileSys.GetFileStatus(file).GetLen(); byte[] buf = new byte[fileLen]; InputStream @in = fileSys.Open(file); IOUtils.ReadFully(@in, buf, 0, buf.Length); @in.Close(); // check .crc corruption CheckFileCorruption(fileSys, file, crcFile); fileSys.Delete(file, true); WriteFile(fileSys, file); // check data corrutpion CheckFileCorruption(fileSys, file, file); fileSys.Delete(file, true); }
/// <exception cref="System.IO.IOException"/> internal virtual FsPermission GetPermission(LocalFileSystem fs, Path p) { return(fs.GetFileStatus(p).GetPermission()); }
/// <exception cref="System.IO.IOException"/> internal virtual string GetGroup(LocalFileSystem fs, Path p) { return(fs.GetFileStatus(p).GetGroup()); }