Esempio n. 1
0
        private void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (bufferManager != null)
                {
                    try {
                        // Set a check point
                        SetCheckPoint();

                        // Stop the buffer manager
                        bufferManager.Stop();
                        bufferManager.Dispose();
                    } catch (IOException e) {
                        // TODO: log the issue
                    }
                }

                if (lockFile != null)
                {
                    lockFile.Close();
                }
            }

            bufferManager = null;
            lockFile      = null;
            context       = null;
        }
        protected override string OnProcess(string sMessage)
        {
            string sFile = this.GetPath(sMessage);

            if (this.ConnectionObject.FileSystemObject.FileExists(sFile))
            {
                return(this.GetMessage(553, "File already exists."));
            }

            IFile file = this.ConnectionObject.FileSystemObject.OpenFile(sFile, true);

            var socketReply = new FtpReplySocket(this.ConnectionObject);

            if (!socketReply.Loaded)
            {
                return(this.GetMessage(425, "Error in establishing data connection."));
            }

            byte [] abData = new byte[BufferSize];

            SocketHelpers.Send(this.ConnectionObject.Socket, this.GetMessage(150, "Opening connection for data transfer."));

            int nReceived = socketReply.Receive(abData);

            while (nReceived > 0)
            {
                file.Write(abData, nReceived);
                nReceived = socketReply.Receive(abData);
            }

            file.Close();
            socketReply.Close();

            return(this.GetMessage(226, "Uploaded file successfully."));
        }
Esempio n. 3
0
        protected override string OnProcess(string sMessage)
        {
            string sFile = GetPath(sMessage);

            IFile file = ConnectionObject.FileSystemObject.OpenFile(sFile, true);

            if (file == null)
            {
                return(GetMessage(425, "Couldn't open file"));
            }

            var socketReply = new FtpReplySocket(ConnectionObject);

            if (!socketReply.Loaded)
            {
                return(GetMessage(425, "Error in establishing data connection."));
            }

            var abData = new byte[m_nBufferSize];

            SocketHelpers.Send(ConnectionObject.Socket, GetMessage(150, "Opening connection for data transfer."));

            int nReceived = socketReply.Receive(abData);

            while (nReceived > 0)
            {
                nReceived = socketReply.Receive(abData);
                file.Write(abData, nReceived);
            }

            file.Close();
            socketReply.Close();

            return(GetMessage(226, string.Format("Appended file successfully. ({0})", sFile)));
        }
Esempio n. 4
0
 public void Close()
 {
     lock (objectLock) {
         try {
             file.Close();
         } finally {
             IsOpen = false;
         }
     }
 }
Esempio n. 5
0
 internal void close()
 {
     lock (this)
     {
         file.Close();
         hashTable  = null;
         dirtyPages = null;
         lru        = null;
         freePages  = null;
     }
 }
Esempio n. 6
0
        public override string GetContent(NameValueCollection querySet, IDictionary <string, string> headers)
        {
            StringBuilder S = new StringBuilder();

            S.Append(LoadHeader());

            IFile chat = new IFile("webfront\\chat.html");

            S.Append(chat.GetText());
            chat.Close();

            S.Append(LoadFooter());

            return(S.ToString());
        }
Esempio n. 7
0
        protected override string OnProcess(string sMessage)
        {
            string sFile       = GetPath(sMessage);
            string lower_sFile = sFile.ToLower();

            if (ConnectionObject.FileSystemObject.FileExists(lower_sFile))
            {
                // si el archivo existe lo eliminamos antes de crearlo!!!
                if (!ConnectionObject.FileSystemObject.Delete(lower_sFile))
                {
                    return(GetMessage(553, "File already exists. Y no se pudo eliminar (agregardo por ED)"));
                }
            }

            IFile file = ConnectionObject.FileSystemObject.OpenFile(sFile, true);

            var socketReply = new FtpReplySocket(ConnectionObject);

            if (!socketReply.Loaded)
            {
                return(GetMessage(425, "Error in establishing data connection."));
            }

            var abData = new byte[m_nBufferSize];

            SocketHelpers.Send(ConnectionObject.Socket, GetMessage(150, "Opening connection for data transfer."));

            int nReceived = socketReply.Receive(abData);

            while (nReceived > 0)
            {
                file.Write(abData, nReceived);
                nReceived = socketReply.Receive(abData);
            }

            ConnectionObject.FileSystemObject.Put(lower_sFile, file);
            file.Close();

            socketReply.Close();

            return(GetMessage(226, "Uploaded file successfully."));
        }
Esempio n. 8
0
        protected override string OnProcess(string sMessage)
        {
            string sFilePath = GetPath(sMessage);

            if (!ConnectionObject.FileSystemObject.FileExists(sFilePath))
            {
                return(GetMessage(550, "File doesn't exist"));
            }

            FtpReplySocket replySocket = new FtpReplySocket(ConnectionObject);

            if (!replySocket.Loaded)
            {
                return(GetMessage(550, "Unable to establish data connection"));
            }

            SocketHelpers.Send(ConnectionObject.Socket, "150 Starting data transfer, please wait...\r\n");

            const int m_nBufferSize = 65536;

            IFile file = ConnectionObject.FileSystemObject.OpenFile(sFilePath, false);

            if (file == null)
            {
                return(GetMessage(550, "Couldn't open file"));
            }

            byte[] abBuffer = new byte[m_nBufferSize];

            int nRead = file.Read(abBuffer, m_nBufferSize);

            while (nRead > 0 && replySocket.Send(abBuffer, nRead))
            {
                nRead = file.Read(abBuffer, m_nBufferSize);
            }

            file.Close();
            replySocket.Close();

            return(GetMessage(226, "File download succeeded."));
        }
Esempio n. 9
0
 public void Close()
 {
     inner.Close();
 }
Esempio n. 10
0
        protected override string OnProcess(string sMessage)
        {
            sMessage = sMessage.Trim();
            if (sMessage == "")
            {
                return(GetMessage(501, string.Format("{0} needs a parameter", Command)));
            }

            string sFile = GetPath(sMessage);

            if (!FileNameHelpers.IsValid(sFile) || sFile.EndsWith(@"/"))
            {
                return(GetMessage(553, string.Format("\"{0}\" is not a valid file name", sMessage)));
            }

            if (ConnectionObject.FileSystemObject.FileExists(sFile))
            {
                // 2015-11-24 cljung : RFC959 says STOR commands overwrite files, so delete if exists
                if (!StorageProviderConfiguration.FtpOverwriteFileOnSTOR)
                {
                    return(GetMessage(553, string.Format("File \"{0}\" already exists.", sMessage)));
                }
                Trace.TraceInformation(string.Format("STOR {0} - Deleting existing file", sFile));
                if (!ConnectionObject.FileSystemObject.DeleteFile(sFile))
                {
                    return(GetMessage(550, string.Format("Delete file \"{0}\" failed.", sFile)));
                }
            }

            var socketData = new FtpDataSocket(ConnectionObject);

            if (!socketData.Loaded)
            {
                return(GetMessage(425, "Unable to establish the data connection"));
            }

            Trace.TraceInformation(string.Format("STOR {0} - BEGIN", sFile));

            IFile file = ConnectionObject.FileSystemObject.OpenFile(sFile, true);

            if (file == null)
            {
                socketData.Close();// close data socket
                return(GetMessage(550, "Couldn't open file"));
            }

            SocketHelpers.Send(ConnectionObject.Socket, GetMessage(150, "Opening connection for data transfer."), ConnectionObject.Encoding);

            string md5Value = string.Empty;

            Stopwatch sw = new Stopwatch();

            sw.Start();

            // TYPE I, default
            if (ConnectionObject.DataType == DataType.Image)
            {
                // md5 hash function
                MD5 md5Hash = MD5.Create();

                var abData = new byte[m_nBufferSize];

                int nReceived = socketData.Receive(abData);

                while (nReceived > 0)
                {
                    int writeSize = file.Write(abData, nReceived);
                    // maybe error
                    if (writeSize != nReceived)
                    {
                        file.Close();
                        socketData.Close();
                        FtpServer.LogWrite(this, sMessage, 451, sw.ElapsedMilliseconds);
                        return(GetMessage(451, "Write data to Azure error!"));
                    }
                    md5Hash.TransformBlock(abData, 0, nReceived, null, 0);
                    nReceived = socketData.Receive(abData);
                }
                md5Hash.TransformFinalBlock(new byte[1], 0, 0);
                md5Value = BytesToStr(md5Hash.Hash);
            }
            // TYPE A
            // won't compute md5, because read characters from client stream
            else if (ConnectionObject.DataType == DataType.Ascii)
            {
                int readSize = SocketHelpers.CopyStreamAscii(socketData.Socket.GetStream(), file.BlobStream, m_nBufferSize);
                FtpServerMessageHandler.SendMessage(ConnectionObject.Id, string.Format("Use ascii type success, read {0} chars!", readSize));
            }
            else   // mustn't reach
            {
                file.Close();
                socketData.Close();
                FtpServer.LogWrite(this, sMessage, 451, sw.ElapsedMilliseconds);
                return(GetMessage(451, "Error in transfer data: invalid data type."));
            }

            sw.Stop();
            Trace.TraceInformation(string.Format("STOR {0} - END, Time {1} ms", sFile, sw.ElapsedMilliseconds));

            // upload notification
            ConnectionObject.FileSystemObject.Log4Upload(sFile);

            file.Close();
            socketData.Close();

            // record md5
            ConnectionObject.FileSystemObject.SetFileMd5(sFile, md5Value);

            FtpServer.LogWrite(this, sMessage, 226, sw.ElapsedMilliseconds);
            return(GetMessage(226, string.Format("{0} successful. Time {1} ms", Command, sw.ElapsedMilliseconds)));
        }
        protected override string OnProcess(string sMessage)
        {
            sMessage = sMessage.Trim();
            if (sMessage == "")
            {
                return(GetMessage(501, string.Format("{0} needs a parameter", Command)));
            }

            string sFilePath = GetPath(sMessage);

            if (!ConnectionObject.FileSystemObject.FileExists(sFilePath))
            {
                return(GetMessage(550, string.Format("File \"{0}\" doesn't exist", sMessage)));
            }

            var socketData = new FtpDataSocket(ConnectionObject);

            if (!socketData.Loaded)
            {
                return(GetMessage(425, "Unable to establish the data connection"));
            }

            SocketHelpers.Send(ConnectionObject.Socket, "150 Starting data transfer, please wait...\r\n", ConnectionObject.Encoding);

            IFile file = ConnectionObject.FileSystemObject.OpenFile(sFilePath, false);

            if (file == null)
            {
                return(GetMessage(550, "Couldn't open file"));
            }

            // TYPE I, default
            if (ConnectionObject.DataType == DataType.Image)
            {
                var abBuffer = new byte[m_nBufferSize];

                int nRead = file.Read(abBuffer, m_nBufferSize);

                while (nRead > 0 && socketData.Send(abBuffer, nRead))
                {
                    nRead = file.Read(abBuffer, m_nBufferSize);
                }
            }
            // TYPE A
            else if (ConnectionObject.DataType == DataType.Ascii)
            {
                int writeSize = SocketHelpers.CopyStreamAscii(file.BlobStream, socketData.Socket.GetStream(), m_nBufferSize);
                FtpServerMessageHandler.SendMessage(ConnectionObject.Id, string.Format("Use ascii type success, write {0} chars!", writeSize));
            }
            else // mustn't reach
            {
                file.Close();
                socketData.Close();
                return(GetMessage(451, "Error in transfer data: invalid data type."));
            }

            file.Close();
            socketData.Close();

            return(GetMessage(226, "File download succeeded."));
        }
Esempio n. 12
0
        protected override async Task <string> OnProcess(string sMessage)
        {
            sMessage = sMessage.Trim();
            if (sMessage == "")
            {
                return(GetMessage(501, string.Format("{0} needs a parameter", Command)));
            }

            string sFile = GetPath(sMessage);

            if (!FileNameHelpers.IsValid(sFile) || sFile.EndsWith(@"/"))
            {
                return(GetMessage(553, string.Format("\"{0}\" is not a valid file name", sMessage)));
            }

            if (await ConnectionObject.FileSystemObject.FileExists(sFile))
            {
                return(GetMessage(553, string.Format("File \"{0}\" already exists.", sMessage)));
            }

            var socketData = new FtpDataSocket(ConnectionObject);

            if (!socketData.Loaded)
            {
                return(GetMessage(425, "Unable to establish the data connection"));
            }

            IFile file = await ConnectionObject.FileSystemObject.OpenFile(sFile, true);

            if (file == null)
            {
                socketData.Close();// close data socket
                return(GetMessage(550, "Couldn't open file"));
            }

            SocketHelpers.Send(ConnectionObject.Socket, GetMessage(150, "Opening connection for data transfer."), ConnectionObject.Encoding);

            string md5Value = string.Empty;

            // TYPE I, default
            if (ConnectionObject.DataType == DataType.Image)
            {
                // md5 hash function
                MD5 md5Hash = MD5.Create();

                var abData = new byte[m_nBufferSize];

                int nReceived = socketData.Receive(abData);

                while (nReceived > 0)
                {
                    int writeSize = file.Write(abData, nReceived);
                    // maybe error
                    if (writeSize != nReceived)
                    {
                        file.Close();
                        socketData.Close();
                        return(GetMessage(451, "Write data to Azure error!"));
                    }
                    md5Hash.TransformBlock(abData, 0, nReceived, null, 0);
                    nReceived = socketData.Receive(abData);
                }
                md5Hash.TransformFinalBlock(new byte[1], 0, 0);
                md5Value = BytesToStr(md5Hash.Hash);
            }
            // TYPE A
            // won't compute md5, because read characters from client stream
            else if (ConnectionObject.DataType == DataType.Ascii)
            {
                int readSize = SocketHelpers.CopyStreamAscii(socketData.Socket.GetStream(), file.BlobStream, m_nBufferSize);
                FtpServerMessageHandler.SendMessage(ConnectionObject.Id, string.Format("Use ascii type success, read {0} chars!", readSize));
            }
            else
            { // mustn't reach
                file.Close();
                socketData.Close();
                return(GetMessage(451, "Error in transfer data: invalid data type."));
            }

            // upload notification
            await ConnectionObject.FileSystemObject.Log4Upload(sFile);

            file.Close();
            socketData.Close();

            // record md5
            await ConnectionObject.FileSystemObject.SetFileMd5(sFile, md5Value);

            return(GetMessage(226, string.Format("{0} successful", Command)));
        }
Esempio n. 13
0
        protected override string OnProcess(string sMessage)
        {
            sMessage = sMessage.Trim();
            if (sMessage == "")
            {
                return(GetMessage(501, $"{Command} needs a parameter"));
            }

            string sFilePath = GetPath(sMessage);

            Trace.TraceInformation($"RETR {sFilePath} - BEGIN");
            Stopwatch sw = new Stopwatch();

            sw.Start();

            if (!ConnectionObject.FileSystemObject.FileExists(sFilePath))
            {
                FtpServer.LogWrite(this, sMessage, 550, sw.ElapsedMilliseconds);
                return(GetMessage(550, $"File \"{sMessage}\" doesn't exist"));
            }

            var   socketData = new FtpDataSocket(ConnectionObject);
            IFile file       = null;

            try
            {
                if (!socketData.Loaded)
                {
                    FtpServer.LogWrite(this, sMessage, 425, sw.ElapsedMilliseconds);
                    return(GetMessage(425, "Unable to establish the data connection"));
                }

                SocketHelpers.Send(ConnectionObject.Socket, "150 Starting data transfer, please wait...\r\n",
                                   ConnectionObject.Encoding);

                file = ConnectionObject.FileSystemObject.OpenFile(sFilePath, false);
                if (file == null)
                {
                    return(GetMessage(550, "Couldn't open file"));
                }

                // TYPE I, default
                if (ConnectionObject.DataType == DataType.Image)
                {
                    var abBuffer = new byte[m_nBufferSize];

                    int nRead = file.Read(abBuffer, m_nBufferSize);

                    while (nRead > 0 && socketData.Send(abBuffer, nRead))
                    {
                        nRead = file.Read(abBuffer, m_nBufferSize);
                    }
                }
                // TYPE A
                else if (ConnectionObject.DataType == DataType.Ascii)
                {
                    int writeSize = SocketHelpers.CopyStreamAscii(file.BlobStream, socketData.Socket.GetStream(),
                                                                  m_nBufferSize);
                    FtpServerMessageHandler.SendMessage(ConnectionObject.Id,
                                                        $"Use ascii type success, write {writeSize} chars!");
                }
                else // mustn't reach
                {
                    file.Close();
                    socketData.Close();
                    FtpServer.LogWrite(this, sMessage, 451, sw.ElapsedMilliseconds);
                    return(GetMessage(451, "Error in transfer data: invalid data type."));
                }



                sw.Stop();
                Trace.TraceInformation($"RETR {sFilePath} - END, Time {sw.ElapsedMilliseconds} ms");

                FtpServer.LogWrite(this, sMessage, 226, sw.ElapsedMilliseconds);
                return(GetMessage(226, $"File download succeeded. Time {sw.ElapsedMilliseconds} ms"));
            }
            finally
            {
                file?.Close();
                socketData.Close();
            }
        }