Пример #1
0
        private ServerFileData ParseUnixDirLine(string line)
        {
            ServerFileData sfd = new ServerFileData();

            try
            {
                string[] parsed = new string[8];
                int      index  = 0;
                int      position;

                // Parse out the elements
                position = line.IndexOf(' ');
                while (index < parsed.Length)
                {
                    parsed[index] = line.Substring(0, position);
                    line          = line.Substring(position);
                    line          = line.Trim();
                    index++;
                    position = line.IndexOf(' ');
                }
                sfd.fileName = line;

                sfd.permission  = parsed[0];
                sfd.owner       = parsed[2];
                sfd.group       = parsed[3];
                sfd.size        = Convert.ToInt32(parsed[4]);
                sfd.date        = parsed[5] + ' ' + parsed[6] + ' ' + parsed[7];
                sfd.isDirectory = sfd.permission[0] == 'd';
            }
            catch
            {
                sfd.fileName = string.Empty;
            }
            return(sfd);
        }
Пример #2
0
        private ServerFileData ParseDosDirLine(string line)
        {
            ServerFileData sfd = new ServerFileData();

            try
            {
                string[] parsed   = new string[3];
                int      index    = 0;
                int      position = 0;

                // Parse out the elements
                position = line.IndexOf(' ');
                while (index < parsed.Length)
                {
                    parsed[index] = line.Substring(0, position);
                    line          = line.Substring(position);
                    line          = line.Trim();
                    index++;
                    position = line.IndexOf(' ');
                }
                sfd.fileName = line;

                if (parsed[2] != "<DIR>")
                {
                    sfd.size = Convert.ToInt32(parsed[2]);
                }

                sfd.date        = parsed[0] + ' ' + parsed[1];
                sfd.isDirectory = parsed[2] == "<DIR>";
            }
            catch
            {
                sfd.fileName = string.Empty;
            }
            return(sfd);
        }
Пример #3
0
        public ArrayList List()
        {
            Byte[]    bytes          = new Byte[512];
            string    file_list      = "";
            long      bytesgot       = 0;
            int       seconds_passed = 0;
            ArrayList list           = new ArrayList();

            Connect();
            OpenDataSocket();
            SendCommand("LIST");

            ReadResponse();

            //FILIPE MADUREIRA.
            //Added response 125
            switch (response)
            {
            case 125:
            case 150:
                break;

            default:
                CloseDataSocket();
                throw new Exception(responseStr);
            }

            while (data_sock.Available < 1)
            {
                System.Threading.Thread.Sleep(50);
                seconds_passed += 50;
                // this code is just a fail safe option
                // so the code doesn't hang if there is
                // no data comming.
                if (seconds_passed > (timeout / 10))
                {
                    //CloseDataSocket();
                    //throw new Exception("Timed out waiting on server to respond.");

                    //FILIPE MADUREIRA.
                    //If there are no files to list it gives timeout.
                    //So I wait less time and if no data is received, means that there are no files
                    break;//Maybe there are no files
                }
            }

            while (data_sock.Available > 0)
            {
                bytesgot   = data_sock.Receive(bytes, bytes.Length, 0);
                file_list += Encoding.ASCII.GetString(bytes, 0, (int)bytesgot);
                //System.Threading.Thread.Sleep(100);
            }

            CloseDataSocket();

            ReadResponse();
            if (response != 226)
            {
                throw new Exception(responseStr);
            }

            //			foreach(string f in file_list.Split('\n'))
            //			{
            //				if(f.Length > 0 && !Regex.Match(f, "^total").Success)
            //					list.Add(f.Substring(0, f.Length - 1));
            //			}

            try
            {
                ServerFileData sfd        = new ServerFileData();
                string[]       sFile      = file_list.Split('\n');
                int            autodetect = 0;

                foreach (string fileLine in sFile)
                {
                    sfd.fileName = string.Empty;
                    //
                    if (autodetect == 0)
                    {
                        sfd = ParseDosDirLine(fileLine);
                        if (sfd.fileName.Equals(string.Empty))
                        {
                            sfd        = ParseUnixDirLine(fileLine);
                            autodetect = 2;
                        }
                        else
                        {
                            autodetect = 1;
                        }
                    }
                    else
                    if (autodetect == 1)
                    {
                        sfd = ParseDosDirLine(fileLine);
                    }
                    else
                    if (autodetect == 2)
                    {
                        sfd = ParseUnixDirLine(fileLine);
                    }

                    if (!sfd.fileName.Equals(string.Empty))
                    {
                        list.Add(sfd);
                    }
                }
            }
            catch { }

            return(list);
        }
Пример #4
0
        private ServerFileData ParseUnixDirLine(string line)
        {
            ServerFileData sfd = new ServerFileData();

            try
            {
                string[] parsed = new string[8];
                int index = 0;
                int position;

                // Parse out the elements
                position = line.IndexOf(' ');
                while (index < parsed.Length)
                {
                    parsed[index] = line.Substring(0, position);
                    line = line.Substring(position);
                    line = line.Trim();
                    index++;
                    position = line.IndexOf(' ');
                }
                sfd.fileName = line;

                sfd.permission = parsed[0];
                sfd.owner = parsed[2];
                sfd.group = parsed[3];
                sfd.size = Convert.ToInt32(parsed[4]);
                sfd.date = parsed[5] + ' ' + parsed[6] + ' ' + parsed[7];
                sfd.isDirectory = sfd.permission[0] == 'd';
            }
            catch
            {
                sfd.fileName = string.Empty;
            }
            return sfd;
        }
Пример #5
0
        private ServerFileData ParseDosDirLine(string line)
        {
            ServerFileData sfd = new ServerFileData();

            try
            {
                string[] parsed = new string[3];
                int index = 0;
                int position = 0;

                // Parse out the elements
                position = line.IndexOf(' ');
                while (index < parsed.Length)
                {
                    parsed[index] = line.Substring(0, position);
                    line = line.Substring(position);
                    line = line.Trim();
                    index++;
                    position = line.IndexOf(' ');
                }
                sfd.fileName = line;

                if (parsed[2] != "<DIR>")
                    sfd.size = Convert.ToInt32(parsed[2]);

                sfd.date = parsed[0] + ' ' + parsed[1];
                sfd.isDirectory = parsed[2] == "<DIR>";
            }
            catch
            {
                sfd.fileName = string.Empty;
            }
            return sfd;
        }
Пример #6
0
        public ArrayList List()
        {
            Byte[] bytes = new Byte[512];
            string file_list = "";
            long bytesgot = 0;
            int seconds_passed = 0;
            ArrayList list = new ArrayList();

            Connect();
            OpenDataSocket();
            SendCommand("LIST");

            ReadResponse();

            //FILIPE MADUREIRA.
            //Added response 125
            switch (response)
            {
                case 125:
                case 150:
                    break;
                default:
                    CloseDataSocket();
                    throw new Exception(responseStr);
            }

            while (data_sock.Available < 1)
            {
                System.Threading.Thread.Sleep(50);
                seconds_passed += 50;
                // this code is just a fail safe option
                // so the code doesn't hang if there is
                // no data comming.
                if (seconds_passed > (timeout / 10))
                {
                    //CloseDataSocket();
                    //throw new Exception("Timed out waiting on server to respond.");

                    //FILIPE MADUREIRA.
                    //If there are no files to list it gives timeout.
                    //So I wait less time and if no data is received, means that there are no files
                    break;//Maybe there are no files
                }
            }

            while (data_sock.Available > 0)
            {
                bytesgot = data_sock.Receive(bytes, bytes.Length, 0);
                file_list += Encoding.ASCII.GetString(bytes, 0, (int)bytesgot);
                //System.Threading.Thread.Sleep(100);
            }

            CloseDataSocket();

            ReadResponse();
            if (response != 226)
                throw new Exception(responseStr);

            //			foreach(string f in file_list.Split('\n'))
            //			{
            //				if(f.Length > 0 && !Regex.Match(f, "^total").Success)
            //					list.Add(f.Substring(0, f.Length - 1));
            //			}

            try
            {
                ServerFileData sfd = new ServerFileData();
                string[] sFile = file_list.Split('\n');
                int autodetect = 0;

                foreach (string fileLine in sFile)
                {
                    sfd.fileName = string.Empty;
                    //
                    if (autodetect == 0)
                    {
                        sfd = ParseDosDirLine(fileLine);
                        if (sfd.fileName.Equals(string.Empty))
                        {
                            sfd = ParseUnixDirLine(fileLine);
                            autodetect = 2;
                        }
                        else
                            autodetect = 1;
                    }
                    else
                        if (autodetect == 1)
                            sfd = ParseDosDirLine(fileLine);
                        else
                            if (autodetect == 2)
                                sfd = ParseUnixDirLine(fileLine);

                    if (!sfd.fileName.Equals(string.Empty))
                    {
                        list.Add(sfd);
                    }
                }
            }
            catch { }

            return list;
        }