Ejemplo n.º 1
0
 protected override Errno OnReleaseHandle(string file, OpenedPathInfo info)
 {
     _fileHandles.TryRemove(info.Handle, out var handle);
     handle.Cts.Cancel();
     handle.Dispose();
     return(0);
 }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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);
 }
Ejemplo n.º 4
0
        protected override Errno OnWriteHandle(string file, OpenedPathInfo info, byte[] buf, long offset,
                                               out int bytesRead)
        {
            bytesRead = 0;

            return(Errno.EROFS);
        }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
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);
 }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
0
        protected override Errno OnReleaseDirectory(string path, OpenedPathInfo info)
        {
            IntPtr dp = (IntPtr)info.Handle;

            Syscall.closedir(dp);
            return(0);
        }
Ejemplo n.º 9
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);
        }
Ejemplo n.º 11
0
        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);
        }
Ejemplo n.º 12
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);
        }
Ejemplo n.º 13
0
        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);
        }
Ejemplo n.º 15
0
        public virtual Errno ReleaseDirectory(string path, OpenedPathInfo info)
        {
            IntPtr dp = (IntPtr)info.Handle;

            Syscall.closedir(dp);
            return(0);
        }
Ejemplo n.º 16
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));
 }
Ejemplo n.º 17
0
        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));
        }
Ejemplo n.º 18
0
        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);
        }
Ejemplo n.º 19
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));
        }
Ejemplo n.º 20
0
        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);
        }
Ejemplo n.º 21
0
        protected override Errno OnReadDirectory(string path, OpenedPathInfo fi,
                                                 out IEnumerable <DirectoryEntry> paths)
        {
            var dp = fi.Handle;

            paths = ReadDirectory(dp);
            return(0);
        }
Ejemplo n.º 22
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);
        }
Ejemplo n.º 23
0
 protected override Errno OnOpenHandle(string file, OpenedPathInfo info)
 {
     if (info.OpenAccess != OpenFlags.O_RDONLY)
     {
         return(Errno.EACCES);
     }
     return(0);
 }
Ejemplo n.º 24
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);
        }
Ejemplo n.º 25
0
        public virtual Errno ReadDirectory(string path, OpenedPathInfo fi,
                                           out IEnumerable <DirectoryEntry> paths)
        {
            IntPtr dp = (IntPtr)fi.Handle;

            paths = ReadDirectory(dp);

            return(0);
        }
Ejemplo n.º 26
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);
        }
Ejemplo n.º 27
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);
        }
Ejemplo n.º 28
0
        public virtual Errno ReleaseHandle(string path, OpenedPathInfo info)
        {
            int r = Syscall.close((int)info.Handle);

            if (r == -1)
            {
                return(Stdlib.GetLastError());
            }
            return(0);
        }
Ejemplo n.º 29
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);
        }
Ejemplo n.º 30
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);
        }