Exemplo n.º 1
0
        public virtual NfsPacket Rmdir(uint xid, NfsPacket packet)
        {
            FileHandle fileHandle = new FileHandle(packet);
            string     name       = packet.GetString();

            string dirname = GetNameFromHandle(fileHandle.Handle, xid);
            var    fd      = new FileInfo(Path.Combine(dirname, name));

            // do some correctness checking
            if (fd.Exists == false)
            {
                throw new NFSException(xid, (uint)NfsReply.ERR_NOENT);
            }
            if (NfsFileAttributes.IsDirectory(fd.FullName) == false)
            {
                throw new NFSException(xid, (uint)NfsReply.ERR_NOTDIR);
            }
            // try to remove the directory
            try {
                fd.Delete();
            }
            catch (Exception) {
                throw new NFSException(xid, (uint)NfsReply.ERR_IO);
            }

            NfsPacket reply = new NfsPacket(128);

            reply.AddReplyHeader(xid);
            reply.SetUInt((uint)NfsReply.OK);
            return(reply);
        }
Exemplo n.º 2
0
        public virtual NfsPacket Remove(uint xid, NfsPacket packet)
        {
            FileHandle fileHandle = new FileHandle(packet);
            string     entry      = packet.GetString();

            // open and delete the file
            string dirName = GetNameFromHandle(fileHandle.Handle, xid);
            var    fd      = new FileInfo(Path.Combine(dirName, entry));

            if (fd.Exists == false)
            {
                throw new NFSException(xid, (uint)NfsReply.ERR_NOENT);
            }
            try {
                fd.Delete();
            }
            catch (Exception) {
                throw new NFSException(xid, (uint)NfsReply.ERR_IO);
            }

            // create the reply packet
            NfsPacket reply = new NfsPacket(128);

            reply.AddReplyHeader(xid);
            reply.SetUInt((uint)NfsReply.OK);
            return(reply);
        }
Exemplo n.º 3
0
        public virtual NfsPacket Rename(uint xid, NfsPacket packet)
        {
            // collect arguments from RPC packet
            FileHandle sourceHandle = new FileHandle(packet);
            string     srcentry     = packet.GetString();

            FileHandle destHandle = new FileHandle(packet);
            string     destentry  = packet.GetString();

            // compute the path names specified
            String srcdir  = GetNameFromHandle(sourceHandle.Handle, xid);
            String destdir = GetNameFromHandle(destHandle.Handle, xid);

            FileInfo source = new FileInfo(Path.Combine(srcdir, srcentry));
            FileInfo dest   = new FileInfo(Path.Combine(destdir, destentry));

            if (source.Exists == false)
            {
                throw new NFSException(xid, (uint)NfsReply.ERR_NOENT);
            }

            if (dest.Exists)
            {
                throw new NFSException(xid, (uint)NfsReply.ERR_EXIST);
            }

            try {
                source.MoveTo(dest.FullName);
            }
            catch (Exception) {
                throw new NFSException(xid, (uint)NfsReply.ERR_IO);
            }

            NfsPacket reply = new NfsPacket(128);

            reply.AddReplyHeader(xid);
            reply.SetUInt((uint)NfsReply.OK);
            return(reply);
        }
Exemplo n.º 4
0
        public virtual NfsPacket Create(uint xid, NfsPacket packet)
        {
            try {
                FileHandle dirFH = new FileHandle(packet);
                string entry = packet.GetString();
                string dirName = GetNameFromHandle(dirFH.Handle, xid);
                string path = Path.Combine(dirName, entry);

                // make the file

                if (File.Exists(path)) {
                    throw new NFSException(xid, (uint)NfsReply.ERR_EXIST);
                }

                using (var file = File.Create(path)) { }

                // make a new handle for this file
                FileHandle fh = new FileHandle();
                long handle = HandleManager.Current.GetHandle(path);
                fh.Set(dirFH.Root, (uint)handle, dirFH.ReadOnly);

                // get the attributes of this new file
                NfsFileAttributes fa = new NfsFileAttributes();
                fa.Load(path);

                // create the reply packet
                NfsPacket reply = new NfsPacket(128);
                reply.AddReplyHeader(xid);
                reply.SetUInt((uint)NfsReply.OK);
                fh.Emit(ref reply);
                fa.Emit(ref reply);
                return reply;

            }
            catch (FileNotFoundException) {
                throw new NFSException(xid, (uint)NfsReply.ERR_IO);
            }
            catch (IOException) {
                throw new NFSException(xid, (uint)NfsReply.ERR_IO);
            }
            catch (System.Security.SecurityException) {
                throw new NFSException(xid, (uint)NfsReply.ERR_PERM);
            }
        }
Exemplo n.º 5
0
        public virtual NfsPacket Create(uint xid, NfsPacket packet)
        {
            try {
                FileHandle dirFH   = new FileHandle(packet);
                string     entry   = packet.GetString();
                string     dirName = GetNameFromHandle(dirFH.Handle, xid);
                string     path    = Path.Combine(dirName, entry);

                // make the file

                if (File.Exists(path))
                {
                    throw new NFSException(xid, (uint)NfsReply.ERR_EXIST);
                }

                using (var file = File.Create(path)) { }

                // make a new handle for this file
                FileHandle fh     = new FileHandle();
                long       handle = HandleManager.Current.GetHandle(path);
                fh.Set(dirFH.Root, (uint)handle, dirFH.ReadOnly);

                // get the attributes of this new file
                NfsFileAttributes fa = new NfsFileAttributes();
                fa.Load(path);

                // create the reply packet
                NfsPacket reply = new NfsPacket(128);
                reply.AddReplyHeader(xid);
                reply.SetUInt((uint)NfsReply.OK);
                fh.Emit(ref reply);
                fa.Emit(ref reply);
                return(reply);
            }
            catch (FileNotFoundException) {
                throw new NFSException(xid, (uint)NfsReply.ERR_IO);
            }
            catch (IOException) {
                throw new NFSException(xid, (uint)NfsReply.ERR_IO);
            }
            catch (System.Security.SecurityException) {
                throw new NFSException(xid, (uint)NfsReply.ERR_PERM);
            }
        }
Exemplo n.º 6
0
        public virtual NfsPacket Lookup(NfsPacket packet)
        {
            try {
                FileHandle dir      = new FileHandle(packet);
                String     entry    = packet.GetString();
                String     dirName  = GetNameFromHandle(dir.Handle, packet.XID);
                String     fileName = Path.Combine(dirName, entry);

                Console.WriteLine("Lookup -> " + entry);

                if (!File.Exists(fileName) && !Directory.Exists(fileName))
                {
                    throw new NFSException(packet.XID, (uint)NfsReply.ERR_NOENT);
                }

                NfsFileAttributes attributes = new NfsFileAttributes();
                attributes.Load(fileName);

                // make a FileHandle for this new path
                uint handleId = HandleManager.Current.GetHandle(fileName);

                FileHandle handle = new FileHandle();
                handle.Set(dir.Root, handleId, dir.ReadOnly);

                // make the reply
                NfsPacket reply = new NfsPacket(128);
                reply.AddReplyHeader(packet.XID);
                reply.SetUInt((uint)NfsReply.OK);

                handle.Emit(ref reply);
                attributes.Emit(ref reply);

                return(reply);
            }
            catch (FileNotFoundException) {
                throw new NFSException(packet.XID, (uint)NfsReply.ERR_NOENT);
            }
        }
Exemplo n.º 7
0
        public virtual NfsPacket Mkdir(uint xid, NfsPacket packet)
        {
            try {
                FileHandle fileHandle = new FileHandle(packet);
                string     entry      = packet.GetString();

                string dirName = GetNameFromHandle(fileHandle.Handle, xid);
                string newdir  = Path.Combine(dirName, entry);

                var dir = new DirectoryInfo(newdir);

                if (dir.Exists)
                {
                    throw new NFSException(xid, (uint)NfsReply.ERR_EXIST);
                }

                dir.Create();

                // make a FileHandle for this directory
                long       handle = HandleManager.Current.GetHandle(newdir);
                FileHandle newFH  = new FileHandle();
                newFH.Set(fileHandle.Root, (uint)handle, fileHandle.ReadOnly);

                // get the attributes
                NfsFileAttributes fa = new NfsFileAttributes();
                fa.Load(newdir);

                NfsPacket reply = new NfsPacket(128);
                reply.AddReplyHeader(xid);
                reply.SetUInt((uint)NfsReply.OK);
                newFH.Emit(ref reply);
                fa.Emit(ref reply);
                return(reply);
            }
            catch (FileNotFoundException) {
                throw new NFSException(xid, (uint)NfsReply.ERR_IO);
            }
        }
Exemplo n.º 8
0
        public virtual NfsPacket Rmdir(uint xid, NfsPacket packet)
        {
            FileHandle fileHandle = new FileHandle(packet);
            string name = packet.GetString();

            string dirname = GetNameFromHandle(fileHandle.Handle, xid);
            var fd = new FileInfo(Path.Combine(dirname, name));
            // do some correctness checking
            if (fd.Exists == false) {
                throw new NFSException(xid, (uint)NfsReply.ERR_NOENT);
            }
            if (NfsFileAttributes.IsDirectory(fd.FullName) == false) {
                throw new NFSException(xid, (uint)NfsReply.ERR_NOTDIR);
            }
            // try to remove the directory
            try {
                fd.Delete();
            }
            catch (Exception) {
                throw new NFSException(xid, (uint)NfsReply.ERR_IO);
            }

            NfsPacket reply = new NfsPacket(128);
            reply.AddReplyHeader(xid);
            reply.SetUInt((uint)NfsReply.OK);
            return reply;
        }
Exemplo n.º 9
0
        public virtual NfsPacket Rename(uint xid, NfsPacket packet)
        {
            // collect arguments from RPC packet
            FileHandle sourceHandle = new FileHandle(packet);
            string srcentry = packet.GetString();

            FileHandle destHandle = new FileHandle(packet);
            string destentry = packet.GetString();

            // compute the path names specified
            String srcdir = GetNameFromHandle(sourceHandle.Handle, xid);
            String destdir = GetNameFromHandle(destHandle.Handle, xid);

            FileInfo source = new FileInfo(Path.Combine(srcdir, srcentry));
            FileInfo dest = new FileInfo(Path.Combine(destdir, destentry));

            if (source.Exists == false) {
                throw new NFSException(xid, (uint)NfsReply.ERR_NOENT);
            }

            if (dest.Exists) {
                throw new NFSException(xid, (uint)NfsReply.ERR_EXIST);
            }

            try {
                source.MoveTo(dest.FullName);
            }
            catch (Exception) {
                throw new NFSException(xid, (uint)NfsReply.ERR_IO);
            }

            NfsPacket reply = new NfsPacket(128);
            reply.AddReplyHeader(xid);
            reply.SetUInt((uint)NfsReply.OK);
            return reply;
        }
Exemplo n.º 10
0
        public virtual NfsPacket Remove(uint xid, NfsPacket packet)
        {
            FileHandle fileHandle = new FileHandle(packet);
            string entry = packet.GetString();

            // open and delete the file
            string dirName = GetNameFromHandle(fileHandle.Handle, xid);
            var fd = new FileInfo(Path.Combine(dirName, entry));
            if (fd.Exists == false) {
                throw new NFSException(xid, (uint)NfsReply.ERR_NOENT);
            }
            try {
                fd.Delete();
            }
            catch (Exception) {
                throw new NFSException(xid, (uint)NfsReply.ERR_IO);
            }

            // create the reply packet
            NfsPacket reply = new NfsPacket(128);
            reply.AddReplyHeader(xid);
            reply.SetUInt((uint)NfsReply.OK);
            return reply;
        }
Exemplo n.º 11
0
        public virtual NfsPacket Mkdir(uint xid, NfsPacket packet)
        {
            try {
                FileHandle fileHandle = new FileHandle(packet);
                string entry = packet.GetString();

                string dirName = GetNameFromHandle(fileHandle.Handle, xid);
                string newdir = Path.Combine(dirName, entry);

                var dir = new DirectoryInfo(newdir);

                if (dir.Exists) {
                    throw new NFSException(xid, (uint)NfsReply.ERR_EXIST);
                }

                dir.Create();

                // make a FileHandle for this directory
                long handle = HandleManager.Current.GetHandle(newdir);
                FileHandle newFH = new FileHandle();
                newFH.Set(fileHandle.Root, (uint)handle, fileHandle.ReadOnly);

                // get the attributes
                NfsFileAttributes fa = new NfsFileAttributes();
                fa.Load(newdir);

                NfsPacket reply = new NfsPacket(128);
                reply.AddReplyHeader(xid);
                reply.SetUInt((uint)NfsReply.OK);
                newFH.Emit(ref reply);
                fa.Emit(ref reply);
                return reply;

            }
            catch (FileNotFoundException) {
                throw new NFSException(xid, (uint)NfsReply.ERR_IO);
            }
        }
Exemplo n.º 12
0
        public virtual NfsPacket Lookup(NfsPacket packet)
        {
            try {
                FileHandle dir = new FileHandle(packet);
                String entry = packet.GetString();
                String dirName = GetNameFromHandle(dir.Handle, packet.XID);
                String fileName = Path.Combine(dirName, entry);

                Console.WriteLine("Lookup -> " + entry);

                if (!File.Exists(fileName) && !Directory.Exists(fileName)) {
                    throw new NFSException(packet.XID, (uint)NfsReply.ERR_NOENT);
                }

                NfsFileAttributes attributes = new NfsFileAttributes();
                attributes.Load(fileName);

                // make a FileHandle for this new path
                uint handleId = HandleManager.Current.GetHandle(fileName);

                FileHandle handle = new FileHandle();
                handle.Set(dir.Root, handleId, dir.ReadOnly);

                // make the reply
                NfsPacket reply = new NfsPacket(128);
                reply.AddReplyHeader(packet.XID);
                reply.SetUInt((uint)NfsReply.OK);

                handle.Emit(ref reply);
                attributes.Emit(ref reply);

                return reply;

            }
            catch (FileNotFoundException) {
                throw new NFSException(packet.XID, (uint)NfsReply.ERR_NOENT);
            }
        }