/// <summary>Create the ShortCircuitShm.</summary> /// <param name="shmId">The ID to use.</param> /// <param name="stream"> /// The stream that we're going to use to create this /// shared memory segment. /// Although this is a FileInputStream, we are going to /// assume that the underlying file descriptor is writable /// as well as readable. It would be more appropriate to use /// a RandomAccessFile here, but that class does not have /// any public accessor which returns a FileDescriptor, /// unlike FileInputStream. /// </param> /// <exception cref="System.IO.IOException"/> public ShortCircuitShm(ShortCircuitShm.ShmId shmId, FileInputStream stream) { if (!NativeIO.IsAvailable()) { throw new NotSupportedException("NativeIO is not available."); } if (Shell.Windows) { throw new NotSupportedException("DfsClientShm is not yet implemented for Windows." ); } if (@unsafe == null) { throw new NotSupportedException("can't use DfsClientShm because we failed to " + "load misc.Unsafe."); } this.shmId = shmId; this.mmappedLength = GetUsableLength(stream); this.baseAddress = NativeIO.POSIX.Mmap(stream.GetFD(), NativeIO.POSIX.MmapProtRead | NativeIO.POSIX.MmapProtWrite, true, mmappedLength); this.slots = new ShortCircuitShm.Slot[mmappedLength / BytesPerSlot]; this.allocatedSlots = new BitSet(slots.Length); if (Log.IsTraceEnabled()) { Log.Trace("creating " + this.GetType().Name + "(shmId=" + shmId + ", mmappedLength=" + mmappedLength + ", baseAddress=" + string.Format("%x", baseAddress) + ", slots.length=" + slots.Length + ")"); } }
/// <exception cref="System.Exception"/> public virtual void TestPosixFadvise() { if (Path.Windows) { return; } FileInputStream fis = new FileInputStream("/dev/zero"); try { NativeIO.POSIX.Posix_fadvise(fis.GetFD(), 0, 0, NativeIO.POSIX.PosixFadvSequential ); } catch (NotSupportedException) { // we should just skip the unit test on machines where we don't // have fadvise support Assume.AssumeTrue(false); } catch (NativeIOException) { } finally { // ignore this error as FreeBSD returns EBADF even if length is zero fis.Close(); } try { NativeIO.POSIX.Posix_fadvise(fis.GetFD(), 0, 1024, NativeIO.POSIX.PosixFadvSequential ); NUnit.Framework.Assert.Fail("Did not throw on bad file"); } catch (NativeIOException nioe) { Assert.Equal(Errno.Ebadf, nioe.GetErrno()); } try { NativeIO.POSIX.Posix_fadvise(null, 0, 1024, NativeIO.POSIX.PosixFadvSequential); NUnit.Framework.Assert.Fail("Did not throw on null file"); } catch (ArgumentNullException) { } }
/// <exception cref="System.Exception"/> public virtual void TestReadAndWrite() { FilePath path = new FilePath(TestBase, "testReadAndWrite"); path.Mkdirs(); SharedFileDescriptorFactory factory = SharedFileDescriptorFactory.Create("woot_", new string[] { path.GetAbsolutePath() }); FileInputStream inStream = factory.CreateDescriptor("testReadAndWrite", 4096); FileOutputStream outStream = new FileOutputStream(inStream.GetFD()); outStream.Write(101); inStream.GetChannel().Position(0); Assert.Equal(101, inStream.Read()); inStream.Close(); outStream.Close(); FileUtil.FullyDelete(path); }
protected internal static FileInputStream ForceSecureOpenForRead(FilePath f, string expectedOwner, string expectedGroup) { FileInputStream fis = new FileInputStream(f); bool success = false; try { NativeIO.POSIX.Stat stat = NativeIO.POSIX.GetFstat(fis.GetFD()); CheckStat(f, stat.GetOwner(), stat.GetGroup(), expectedOwner, expectedGroup); success = true; return(fis); } finally { if (!success) { fis.Close(); } } }