/* B E D R E A D F R O M X M L */ /*---------------------------------------------------------------------------- %%Function: BedReadFromXml %%Qualified: bedu.bedu.BedReadFromXml %%Contact: rlittle ----------------------------------------------------------------------------*/ public static BEDir BedFromXml(XmlReader xr, BEPref bep) { List<BEDir> plbedStack = new List<BEDir>(); BEDir bed = null; BEDir bedRoot = null; string sLabel = "."; XmlReadState xrs = XmlReadState.Initial; while (true) { xr.MoveToContent(); if (xrs == XmlReadState.Initial) { if (xr.LocalName != "root") throw new Exception(String.Format("illegal element in initial state: {0}", xr.LocalName)); bep.ServerName = xr.GetAttribute("serverPath"); bep.ServerShare = xr.GetAttribute("drive"); xr.ReadStartElement(); xr.MoveToContent(); xrs = XmlReadState.Root; } if (xrs == XmlReadState.DirOrFile || xrs == XmlReadState.Root) { string sFullName = xr.GetAttribute("name"); if (xr.IsStartElement()) { if (xr.LocalName == "dir") { if (xrs == XmlReadState.DirOrFile) { // here we cheat. we know that we're beyond the root, and we know that the // directory WILL NOT have a trailing "\" which means the path code will be // confused and will think the directory name at the end is a filename... sLabel = sLabel + @"\" + Path.GetFileName(sFullName); } if (sFullName[sFullName.Length - 1] != '\\') sFullName += "\\"; BEDir bedNew = new BEDir(sFullName, sLabel); bedNew.InitDirs(); if (bedRoot == null) { bed = bedRoot = bedNew; if (!xr.IsEmptyElement) plbedStack.Add(null); } else { bed.AddDir(bedNew); if (!xr.IsEmptyElement) { plbedStack.Add(bed); bed = bedNew; } } } else if (xr.LocalName == "file") { if (xrs == XmlReadState.Root) throw new Exception(String.Format("illegal element in root state state: {0}", xr.LocalName)); BEFile bef = new BEFile(xr); bed.AddFile(bef); } xr.ReadStartElement(); xrs = XmlReadState.DirOrFile; } else { // we're at an end element if (xrs == XmlReadState.Root) throw new Exception(String.Format("found close element when looking for root dir: {0}", xr.LocalName)); if (xr.LocalName == "dir") { // pop the directory bed = plbedStack[plbedStack.Count - 1]; if (bed != null) sLabel = bed.Label; plbedStack.RemoveAt(plbedStack.Count - 1); xr.ReadEndElement(); xrs = XmlReadState.DirOrFile; } else if (xr.LocalName == "root") { // we're done if (plbedStack.Count != 0) throw new Exception(String.Format("encountered </root> with dirs on the stack: {0}", plbedStack.Count)); xr.ReadEndElement(); xrs = XmlReadState.Done; break; } else { throw new Exception(String.Format("encountered unknown close element: {0}", xr.LocalName)); } } continue; } } // we should be done... return bedRoot; }
static void Main(string[] args) { UnitTest(); string sError; CmdLineConfig clcfg = new CmdLineConfig(new CmdLineSwitch[] { new CmdLineSwitch("r", false, false, "record to a file", "filename", null), new CmdLineSwitch("f", true, false, "fast mode", null, null), new CmdLineSwitch("p", false, false, "playback from a file", "filename", null), new CmdLineSwitch("n", false, false, "name this server (for later playback)", "servername", null), new CmdLineSwitch("h", false, false, "name this server share (for later playback)", "sharename", null), new CmdLineSwitch("v", true, false, "verbose - show each file", null, null), new CmdLineSwitch("z", true, false, "zeros - show directories that are 0 size", null, null), new CmdLineSwitch("d", false, false, "depth - max depth to show", "depth", null), new CmdLineSwitch("0", true, false, "depth 0 - alias for -d 0", null, null), new CmdLineSwitch("1", true, false, "depth 0 - alias for -d 1", null, null), new CmdLineSwitch("2", true, false, "depth 0 - alias for -d 2", null, null), new CmdLineSwitch("3", true, false, "depth 0 - alias for -d 3", null, null), new CmdLineSwitch("x", true, false, "exclusions - use the global exclusion list", null, null), new CmdLineSwitch("X", false, false, "eXclusions - load more exclusions from the named file", "filename", null), new CmdLineSwitch("i", false, false, "inclusions - this is the selection list to match against", "filename", null) } ); BEList belExclusions = new BEList(); BEList belSelection = new BEList(); BEDir bedProgram = new BEDir(Assembly.GetExecutingAssembly().Location, ""); BEPref bep = new BEPref(belSelection, belExclusions, bedProgram.Dir); bep.MaxDepth=2; CmdLine cl = new CmdLine(clcfg); if (!cl.FParse(args, bep, null, out sError)) { Console.WriteLine(sError); cl.Usage(OutputLine); return; } if (belSelection.Empty) belSelection = null; BEDir bedRoot = new BEDir(Environment.CurrentDirectory + @"\", "."); // make sure we append a "/" because otherwise we won't know its a directory long cBytesExcl; // bedRoot.GetDirSize(".", bedRoot.DirInfo, belExclusions, null, 0, bep, out cBytesExcl); // are we recording, playing back, or what? IReportUsage iru; if (bep.Fast) { iru = new ConsoleReportUsage(bep); BEDir.GetDirSizeFromDirectoryInfo(".", bedRoot.DirInfo, belExclusions, belSelection, 0, bep, iru, out cBytesExcl); } else { if (bep.Playback) { bedRoot = BedFromXml(XmlReader.Create(new StreamReader(bep.RecordFile)), bep); // load the file here... } if (bep.Record) { iru = new RecordReportUsage(bep, bep.ServerName, bep.ServerShare); } else { iru = new ConsoleReportUsage(bep); } iru.WritePrologue(); bedRoot.ProcessDir(belExclusions, belSelection, 0, bep, iru); iru.WriteEpilogue(); iru.Flush(); } }
/* C O M P A R E D I R */ /*---------------------------------------------------------------------------- %%Function: CompareDir %%Qualified: bedu.BEDir.CompareDir %%Contact: rlittle ----------------------------------------------------------------------------*/ public static bool CompareDir(BEDir bed1, BEDir bed2) { if (!CompareDirData(bed1, bed2)) return false; if (bed1.m_plbed.Count != bed2.m_plbed.Count) return false; if (bed1.m_plbef.Count != bed2.m_plbef.Count) return false; for (int i = 0; i < bed1.m_plbed.Count; i++) { if (!CompareDir(bed1.m_plbed[i], bed2.m_plbed[i])) return false; } for (int i = 0; i < bed1.m_plbef.Count; i++) { if (!BEFile.CompareFile(bed1.m_plbef[i], bed2.m_plbef[i])) return false; } return true; }
/* P O P U L A T E D I R */ /*---------------------------------------------------------------------------- %%Function: PopulateDir %%Qualified: bedu.BEDir.PopulateDir %%Contact: rlittle ----------------------------------------------------------------------------*/ public void PopulateDir() { m_plbed = new List<BEDir>(); m_plbef = new List<BEFile>(); m_cbDirSize = 0; m_cbDirExcl = 0; // build up the directory information DirectoryInfo []rgdi = null; // first, find any directories try { rgdi = DirInfo.GetDirectories(); } catch {} if (rgdi != null) { foreach(DirectoryInfo di2 in rgdi) { BEDir bed = new BEDir(di2, m_sLabel + @"\" + di2.Name); bed.PopulateDir(); AddDir(bed); } } // and now add on files -- this is where we actually check exclusions/inclusions.... FileInfo []rgfi = null; try { rgfi = DirInfo.GetFiles(); } catch {} if (rgfi != null) { foreach(FileInfo fi in rgfi) { try { AddFile(new BEFile(fi)); } catch (Exception e) { Console.WriteLine(String.Format("cannot process file {0} in directory {1}: {2}", fi.Name, DirInfo.Name, e.Message)); } } } }
public void AddDir(BEDir bed) { m_plbed.Add(bed); }
/* C O M P A R E D I R D A T A */ /*---------------------------------------------------------------------------- %%Function: CompareDirData %%Qualified: bedu.BEDir.CompareDirData %%Contact: rlittle ----------------------------------------------------------------------------*/ public static bool CompareDirData(BEDir bed1, BEDir bed2) { if (String.Compare(bed1.m_sName, bed2.m_sName, true) != 0) return false; if (String.Compare(bed1.m_sFullPath, bed2.m_sFullPath, true) != 0) return false; if (String.Compare(bed1.m_sLabel, bed2.m_sLabel, true) != 0) return false; return true; }