XenAdmin.Actions.ZipStatusReportAction.SanitizeTarPathMember C# (CSharp) Method

SanitizeTarPathMember() public static method

public static SanitizeTarPathMember ( string member ) : string
member string
return string
        public static string SanitizeTarPathMember(string member)
        {
            // Strip any whitespace, or Windows will do it for us, and we might generate non-unique names
            member = member.Trim();

            foreach (string reserved in forbiddenNames)
            {
                // Names can't be any of com1, com2, or com1.xyz, com2.abc etc.
                if (member.ToUpperInvariant() == reserved.ToUpperInvariant()
                    || member.ToUpperInvariant().StartsWith(reserved.ToUpperInvariant() + "."))
                {
                    member = "_" + member;
                }
            }

            // Allow only 31 < c < 126, excluding  < > : " / \ | ? *
            StringBuilder sb = new StringBuilder(member.Length);
            foreach (char c in member.ToCharArray())
            {
                if (c > 31 && c < 127 && !IsCharExcluded(c))
                {
                    sb.Append(c);
                }
                else
                {
                    sb.Append("_");
                }
            }
            member = sb.ToString();

            // Windows also seems not to like filenames ending '.'
            if (member.EndsWith("."))
            {
                member = member.Substring(0, member.Length - 1) + "_";
            }

            // Don't allow empty filename
            if (member.Length == 0)
            {
                member = "_";
            }

            return member;
        }

Usage Example

        protected override void Run()
        {
            host.Status = HostStatus.compiling;

            string hostname = Helpers.GetName(host.Host);

            hostname = ZipStatusReportAction.SanitizeTarPathMember(hostname);
            // Workaround for excessively long filenames: trim the hostname we use
            if (hostname.Length > 20)
            {
                hostname = hostname.Truncate(20);
            }
            string filename = string.Format("{1}\\{2}-bugtool-{0}.tar", hostname, filepath, timestring);

            string entries_string = String.Join(",", entries);

            log.DebugFormat("Getting system status for {0} on {1}", entries_string, hostname);

            try
            {
                host.Status = HostStatus.compiling;
                if (Session == null)
                {
                    throw new Exception(Messages.CONNECTION_IO_EXCEPTION);
                }

                HTTPHelper.Get(this, false, dataRxDelegate, filename, host.Host.address,
                               (HTTP_actions.get_ssss)HTTP_actions.get_system_status,
                               Session.opaque_ref, entries_string, "tar");

                log.DebugFormat("Getting system status from {0} successful", hostname);

                host.Status          = HostStatus.succeeded;
                base.PercentComplete = 100;
            }
            catch (CancelledException ce)
            {
                log.Info("Getting system status cancelled");

                Description = Messages.ACTION_SYSTEM_STATUS_CANCELLED;
                host.Status = HostStatus.failed;
                host.error  = ce;

                throw;
            }
            catch (Exception e)
            {
                log.Warn(string.Format("Getting system status from {0} failed", hostname), e);

                host.Status = HostStatus.failed;
                host.error  = e;

                Description =
                    Win32.GetHResult(e) == Win32.ERROR_DISK_FULL ?
                    Messages.ACTION_SYSTEM_STATUS_DISK_FULL :
                    Messages.ACTION_SYSTEM_STATUS_FAILED;
            }
        }
All Usage Examples Of XenAdmin.Actions.ZipStatusReportAction::SanitizeTarPathMember