protected override Errno OnReleaseHandle(string file, OpenedPathInfo info) { _fileHandles.TryRemove(info.Handle, out var handle); handle.Cts.Cancel(); handle.Dispose(); return(0); }
protected override Errno OnReadHandle(string path, OpenedPathInfo fi, byte[] buf, long offset, out int bytesWritten) { if (!_fileHandles.TryGetValue(fi.Handle, out var handle)) { bytesWritten = 0; return(Errno.ENOENT); } async Task <int> ReadAsync() { var stream = handle.Stream; var ct = handle.Cts.Token; var semaphore = handle.Semaphore; try { await semaphore.WaitAsync(ct); stream.Seek(offset, SeekOrigin.Begin); return(await stream.ReadAsync(buf.AsMemory(), ct)); } catch (OperationCanceledException) { return(0); } finally { semaphore.Release(); } } bytesWritten = ReadAsync().Result; return(0); }
protected override Errno OnReleaseHandle(string file, OpenedPathInfo info) { try { Entry fsEntry = walker.SearchEntry(file.Substring(1)); if (fsEntry == null) { return(Errno.ENOENT); } if (fsEntry.Kind != EntryKind.ZipFileEntry) { return(Errno.EIO); } ReleaseOpenedZipEntry(file); } catch (DirectoryNotFoundException) { return(Errno.ENOTDIR); } catch (IOException) { return(Errno.EIO); } return(0); }
protected override Errno OnWriteHandle(string file, OpenedPathInfo info, byte[] buf, long offset, out int bytesRead) { bytesRead = 0; return(Errno.EROFS); }
protected override Errno OnReadDirectory(string path, OpenedPathInfo fi, out IEnumerable <DirectoryEntry> paths) { IntPtr dp = Syscall.opendir(basedir + path); if (dp == IntPtr.Zero) { paths = null; return(Stdlib.GetLastError()); } Dirent de; List <DirectoryEntry> entries = new List <DirectoryEntry> (); while ((de = Syscall.readdir(dp)) != null) { DirectoryEntry e = new DirectoryEntry(de.d_name); e.Stat.st_ino = de.d_ino; e.Stat.st_mode = (FilePermissions)(de.d_type << 12); entries.Add(e); } Syscall.closedir(dp); paths = entries; return(0); }
protected override Errno OnReadDirectory(string path, OpenedPathInfo fi, out IEnumerable <DirectoryEntry> paths) { var(error, resultPaths) = WithLogging( () => { var rawNames = fileSystemRepository.ReadDirectoryContent(path); rawNames.Add(new DirectoryEntry(".") { Stat = new Stat { st_mode = FilePermissions.S_IFDIR | FilePermissions.ACCESSPERMS } }); rawNames.Add(new DirectoryEntry("..") { Stat = new Stat { st_mode = FilePermissions.S_IFDIR | FilePermissions.ACCESSPERMS } }); return(Result.Ok(rawNames)); }, $"OnReadDirectory({path})", result => $"{string.Join(";", result?.Select(x => x.Name) ?? new string[0])}"); paths = resultPaths; return(error); }
protected override Errno OnWriteHandle( string path, OpenedPathInfo info, byte[] buf, long offset, out int bytesWritten) { var(error, result) = WithLogging( () => fileSystemRepository.ReadFile(path, info.OpenFlags).Then(file => { using var dataStream = new MemoryStream(); if (file.Data.Length > 0) { dataStream.Write(file.Data, 0, file.Data.Length); } dataStream.Seek(offset, SeekOrigin.Begin); dataStream.Write(buf, 0, buf.Length); var written = buf.Length; file.Data = dataStream.ToArray(); fileSystemRepository.WriteFile(file); return(Result.Ok(written)); }), $"OnWriteHandle({path}, {info.OpenAccess}, {info.OpenFlags}, {offset})", res => $"{res}"); bytesWritten = result; return(error); }
protected override Errno OnReleaseDirectory(string path, OpenedPathInfo info) { IntPtr dp = (IntPtr)info.Handle; Syscall.closedir(dp); return(0); }
protected override Errno OnReadDirectory(string path, OpenedPathInfo fi, out IEnumerable <DirectoryEntry> subPaths) { Logger.WriteLineIf(LogLevel.Verbose, _fslog_props, string.Format("OnReadDirectory, path={0}, handle={1}", path, fi.Handle)); return(this._rfs.ReadDirectory(path, fi, out subPaths)); }
protected override Errno OnReadDirectory(string path, OpenedPathInfo fi, out IEnumerable <DirectoryEntry> paths) { var result = _fileSystem.Ls(path, out var items).ToErrno(); paths = items.ToList().ConvertAll(x => new DirectoryEntry(x.Name)); return(result); }
protected override Errno OnWriteHandle(string file, OpenedPathInfo info, byte[] buf, long offset, out int bytesRead) { Debug.WriteLine("OnWriteHandle: {0} off={1}", file, offset); bytesRead = -1; Stream strm = GetStream(info.Handle); if (strm == null || strm.Position != offset) { return(Errno.EIO); } lock (strm) { if (!strm.CanWrite) { return(Errno.EINVAL); } try { strm.Write(buf, 0, buf.Length); } catch (Exception e) { Debug.WriteLine(e); throw e; } } bytesRead = buf.Length; return(0); }
/// <summary> /// Opens the handle. /// </summary> /// <param name="path">The path.</param> /// <param name="info">The info.</param> public override Errno OpenHandle(string path, OpenedPathInfo info) { Errno retVal; if (info.OpenAccess == OpenFlags.O_RDONLY) { String readPath = _pathFactory.CreateVirtualPath4Read(new VirtualRawPath(path)); retVal = base.OpenHandle(readPath, info); } else { // This is a write. // @TODO Write to an existing file is not supported by BitTorrent. // It has to be enforced somewhere. String writePath = _pathFactory.CreateVirtualPath4Write(new VirtualRawPath(path)); retVal = base.OpenHandle(writePath, info); } // Add to filesys context. VirtualPath vp = VirtualPath.CreateFromRawString(path); VirtualFile vf = _fileManager.ReadVirtualFile(vp); FileAccess fa = IOUtil.OpenFlags2FileAccess(info.OpenAccess); var openFileInfo = new OpenFileInfo(info.Handle, vp, vf, fa); _filesysContext.AddOpenFile(info.Handle, openFileInfo); return(retVal); }
protected override unsafe Errno OnReadHandle(string path, OpenedPathInfo info, byte[] buf, long offset, out int bytesRead) { Logger.WriteLineIf(LogLevel.Verbose, _fslog_props, string.Format( "OnReadHandle, path={0}, handle={1}, buflength={2}, offset={3}", path, info.Handle, buf.Length, offset)); var eventArgs = new ReadFileEventArgs(new VirtualRawPath(path), buf, offset, info.Handle); if (this.ReadingFile != null) { try { ReadingFile(this, eventArgs); } catch (Exception ex) { Logger.WriteLineIf(LogLevel.Error, _log_props, string.Format( "Exception thrown when handling ReadingFile event. Exception: {0}", ex)); throw; } } bytesRead = eventArgs.BytesRead; Logger.WriteLineIf(LogLevel.Verbose, _fslog_props, string.Format("::Read {0} bytes.", bytesRead)); // @TODO FIXME: Now always return 0 but should handle error cases. return(0); }
protected override Errno OnReadHandleUnsafe(string file, OpenedPathInfo info, IntPtr buf, ulong size, long offset, out int bytesWritten) { var r = _fileSystem.Read(file, offset, size, out var buffer, out bytesWritten).ToErrno(); Marshal.Copy(buffer, 0, buf, (int)size); return(r); }
public virtual Errno ReleaseDirectory(string path, OpenedPathInfo info) { IntPtr dp = (IntPtr)info.Handle; Syscall.closedir(dp); return(0); }
protected override Errno OnCreateHandle(string path, OpenedPathInfo info, FilePermissions mode) { Logger.WriteLineIf(LogLevel.Verbose, _log_props, string.Format( "OnCreateHandle, path={0}, openflags={1}, filepermission={2}, OpenAccess={3}", path, info.OpenFlags, mode, info.OpenAccess)); return(this._rfs.CreateHandle(path, info, mode)); }
protected override unsafe Errno OnWriteHandle(string path, OpenedPathInfo info, byte[] buf, long offset, out int bytesWritten) { Logger.WriteLineIf(LogLevel.Verbose, _fslog_props, string.Format("OnWriteHandle, path={0}, handle={1}, buflength={2}, offset={3}", path, info.Handle, buf.Length, offset)); return(this._rfs.WriteHandle(path, info, buf, offset, out bytesWritten)); }
protected override Errno OnReadHandle(string file, OpenedPathInfo info, byte[] buf, long offset, out int bytesWritten) { Debug.WriteLine("OnReadHandle: {0} off={1}", file, offset); bytesWritten = -1; Stream strm = GetStream(info.Handle); if (strm == null) { return(Errno.EIO); } lock (strm) { if (!strm.CanRead) { return(Errno.EINVAL); } strm.Seek(offset, SeekOrigin.Begin); try { bytesWritten = strm.Read(buf, 0, buf.Length); } catch (Exception e) { Debug.WriteLine(e); throw e; } } return(0); }
public override Errno ReleaseHandle(string path, OpenedPathInfo info) { _filesysContext.RemoveOpenFile(info.Handle); VirtualPath vp = VirtualPath.CreateFromRawString(path); UpdateFileSizeInVirtualFile(vp); return(base.ReleaseHandle(path, info)); }
protected override Errno OnLockHandle(string path, OpenedPathInfo info, FcntlCommand cmd, ref Flock @lock) { logger.Info($"OnLockHandle({path}, {info}, {cmd}, {@lock.l_type})..."); var error = OnOpenHandle(path, info); logger.Info($"OnLockHandle({path}, {info}, {cmd}, {@lock.l_type}) -> {error}"); return(error); }
protected override Errno OnReadDirectory(string path, OpenedPathInfo fi, out IEnumerable <DirectoryEntry> paths) { var dp = fi.Handle; paths = ReadDirectory(dp); return(0); }
protected override Errno OnLockHandle(string file, OpenedPathInfo info, FcntlCommand cmd, ref Flock @lock) { Flock _lock = @lock; Errno e = ProcessFile(basedir + file, info.OpenFlags, fd => Syscall.fcntl(fd, cmd, ref _lock)); @lock = _lock; return(e); }
protected override Errno OnOpenHandle(string file, OpenedPathInfo info) { if (info.OpenAccess != OpenFlags.O_RDONLY) { return(Errno.EACCES); } return(0); }
protected override Errno OnGetHandleStatus(string path, OpenedPathInfo info, out Stat buf) { Logger.WriteLineIf(LogLevel.Verbose, _fslog_props, string.Format("OnGetHandleStatus, path={0}, handle={1}", path, info.Handle)); Errno ret = this._rfs.GetHandleStatus(path, info, out buf); Logger.WriteLineIf(LogLevel.Verbose, _fslog_props, string.Format("::Handle Status: Size={0}", buf.st_size)); return(ret); }
public virtual Errno ReadDirectory(string path, OpenedPathInfo fi, out IEnumerable <DirectoryEntry> paths) { IntPtr dp = (IntPtr)fi.Handle; paths = ReadDirectory(dp); return(0); }
protected override Errno OnLockHandle(string file, OpenedPathInfo info, FcntlCommand cmd, ref Flock @lock) { int r = Syscall.fcntl((int)info.Handle, cmd, ref @lock); if (r == -1) { return(Stdlib.GetLastError()); } return(0); }
public virtual Errno GetHandleStatus(string path, OpenedPathInfo info, out Stat buf) { int r = Syscall.fstat((int)info.Handle, out buf); if (r == -1) { return(Stdlib.GetLastError()); } return(0); }
public virtual Errno ReleaseHandle(string path, OpenedPathInfo info) { int r = Syscall.close((int)info.Handle); if (r == -1) { return(Stdlib.GetLastError()); } return(0); }
public virtual Errno TruncateHandle(string path, OpenedPathInfo info, long size) { int r = Syscall.ftruncate((int)info.Handle, size); if (r == -1) { return(Stdlib.GetLastError()); } return(0); }
protected override Errno OnGetHandleStatus(string path, OpenedPathInfo info, out Stat buf) { int r = Syscall.fstat((int)info.Handle, out buf); if (r == -1) { return(Stdlib.GetLastError()); } return(0); }