コード例 #1
0
        private void ParseField(string field)
        {
            if (String.IsNullOrEmpty(field))
            {
                throw new FtpsItemParsingException("field cannot be a null or empty value");
            }

            if (field.IndexOf('=') == -1)
            {
                throw new FtpsItemParsingException("field must contain equals '=' value");
            }

            // split the field fact
            string[] fact = field.Split('=');

            if (fact.Length != 2)
            {
                throw new FtpsItemParsingException("field must contain equals '=' value with only a name and value");
            }

            string name  = fact[0].Trim().ToLower();
            string value = fact[1].Trim();

            if (name.Length == 0)
            {
                throw new FtpsItemParsingException("field fact name cannot be empty");
            }

            switch (name)
            {
            case FACT_CHAR_SET:
                _charSet = value;
                break;

            case FACT_CREATE:
                _create = ParseDateTime(value);
                break;

            case FACT_LANG:
                _lang = value;
                break;

            case FACT_MEDIA_TYPE:
                _mediaType = value;
                break;

            case FACT_MODIFY:
                _modified = ParseDateTime(value);
                break;

            case FACT_PERM:
                _perm = ParsePerm(value);
                break;

            case FACT_SIZE:
                _size = ParseSize(value);
                break;

            case FACT_TYPE:
                _itemType = ParseType(value);
                break;

            case FACT_UNIQUE:
                _unique = value;
                break;

            case FACT_EXT_UNIX_GROUP:
                _unixGroup = value;
                break;

            case FACT_EXT_UNIX_MODE:
                _unixMode = ParseInt32(value);
                if (_unixMode != null)
                {
                    _attributes = FtpsUtilities.ModeToAttribute(_unixMode.Value);
                }
                break;

            case FACT_EXT_UNIX_OWNER:
                _unixOwner = value;
                break;

            default:
                break;
            }
        }
コード例 #2
0
        private FtpsItem ParseUnixFormat(string line)
        {
            string attribs  = _unixAttribs.Match(line).ToString();
            string month    = _unixMonth.Match(line).ToString();
            string day      = _unixDay.Match(line).ToString();
            string year     = _unixYear.Match(line).ToString();
            string time     = _unixTime.Match(line).ToString();
            string size     = _unixSize.Match(line).ToString();
            string name     = _unixName.Match(line).ToString().Trim();
            string symbLink = "";

            // ignore the microsoft 'etc' file that IIS uses for WWW users
            if (name == "~ftpsvc~.ckm")
            {
                return(null);
            }

            //  if we find a symbolic link then extract the symbolic link first and then
            //  extract the file name portion
            if (_unixSymbLink.IsMatch(name))
            {
                symbLink = _unixSymbLink.Match(name).ToString();
                name     = name.Substring(0, name.IndexOf("->")).Trim();
            }

            string itemType = _unixType.Match(line).ToString();


            //  if the current year is not given in unix then we need to figure it out.
            //  basically, if a date is within the past 6 months unix will show the
            //  time instead of the year
            if (year.Length == 0)
            {
                int curMonth = DateTime.Today.Month;
                int curYear  = DateTime.Today.Year;

                DateTime result;
                if (DateTime.TryParse(String.Format(CultureInfo.InvariantCulture, "1-{0}-2007", month), out result))
                {
                    if ((curMonth - result.Month) < 0)
                    {
                        year = Convert.ToString(curYear - 1, CultureInfo.InvariantCulture);
                    }
                    else
                    {
                        year = curYear.ToString(CultureInfo.InvariantCulture);
                    }
                }
            }

            DateTime dateObj;

            DateTime.TryParse(String.Format(CultureInfo.InvariantCulture, "{0}-{1}-{2} {3}", day, month, year, time), out dateObj);

            long sizeLng = 0;

            Int64.TryParse(size, out sizeLng);

            FtpItemType itemTypeObj = FtpItemType.Other;

            switch (itemType.ToLower(CultureInfo.InvariantCulture))
            {
            case "l":
                itemTypeObj = FtpItemType.SymbolicLink;
                break;

            case "d":
                itemTypeObj = FtpItemType.Directory;
                break;

            case "-":
                itemTypeObj = FtpItemType.File;
                break;

            case "b":
                itemTypeObj = FtpItemType.BlockSpecialFile;
                break;

            case "c":
                itemTypeObj = FtpItemType.CharacterSpecialFile;
                break;

            case "p":
                itemTypeObj = FtpItemType.NamedSocket;
                break;

            case "s":
                itemTypeObj = FtpItemType.DomainSocket;
                break;
            }

            if (name == ".")
            {
                itemTypeObj = FtpItemType.CurrentDirectory;
            }
            if (name == "..")
            {
                itemTypeObj = FtpItemType.ParentDirectory;
            }

            if (itemTypeObj == FtpItemType.Other || name.Trim().Length == 0)
            {
                return(null);
            }
            else
            {
                return(new FtpsItem(name, dateObj, sizeLng, itemTypeObj, attribs, FtpsUtilities.AttributeToMode(attribs), symbLink, line));
            }
        }