/* 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; }
/* R E C O R D R E P O R T U S A G E */ /*---------------------------------------------------------------------------- %%Function: RecordReportUsage %%Qualified: bedu.bedu:RecordReportUsage.RecordReportUsage %%Contact: rlittle ----------------------------------------------------------------------------*/ public RecordReportUsage(BEPref bep, string sServer, string sShare) { m_bep = bep; if (sServer == null) sServer = ""; m_sServer = Xmlify(sServer); m_sShare = Xmlify(sShare); m_tw = new StreamWriter(bep.RecordFile); }
/* C O N S O L E R E P O R T U S A G E */ /*---------------------------------------------------------------------------- %%Function: ConsoleReportUsage %%Qualified: bedu.bedu:ConsoleReportUsage.ConsoleReportUsage %%Contact: rlittle ----------------------------------------------------------------------------*/ public ConsoleReportUsage(BEPref bep) { m_bep = bep; }
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(); } }
/* G E T D I R S I Z E */ /*---------------------------------------------------------------------------- %%Function: GetDirSize %%Qualified: bedu.BEDir.GetDirSize %%Contact: rlittle ----------------------------------------------------------------------------*/ public void ProcessDir(BEList belExclusions, BEList belInclusions, int cLevel, BEPref bep, IReportUsage iru) { if (m_plbed == null) PopulateDir(); // at this point, we have populated (either because we were preloaded or we just loaded iru.OpenDir(m_sLabel, m_sFullPath); m_cbDirSize = 0; m_cbDirExcl = 0; foreach (BEDir bed in m_plbed) { bed.ProcessDir(belExclusions, belInclusions, cLevel + 1, bep, iru); m_cbDirSize += bed.DirSize; m_cbDirExcl += bed.DirExcl; } foreach (BEFile bef in m_plbef) { string sFullName = bef.FullName; if (bep.DoExclusions) { if ((belExclusions != null && belExclusions.FMatch(bep.ServerName, bep.ServerShare, sFullName, BEList.BoolOp.Or)) || (belInclusions != null && !belInclusions.FMatch(bep.ServerName, bep.ServerShare, sFullName, BEList.BoolOp.Or))) { iru.ReportFile(0, bef.Bytes, m_sLabel, sFullName); m_cbDirExcl += bef.Bytes; continue; } } iru.ReportFile(bef.Bytes, 0, m_sLabel, sFullName); m_cbDirSize += bef.Bytes; } iru.ReportDir(m_cbDirSize, m_cbDirExcl, m_sLabel, cLevel); iru.CloseDir(); }
/* G E T D I R S I Z E F R O M D I R E C T O R Y I N F O */ /*---------------------------------------------------------------------------- %%Function: GetDirSizeFromDirectoryInfo %%Qualified: bedu.BEDir.GetDirSizeFromDirectoryInfo %%Contact: rlittle ----------------------------------------------------------------------------*/ public static long GetDirSizeFromDirectoryInfo(string sLabel, DirectoryInfo di, BEList belExclusions, BEList belInclusions, int cLevel, BEPref bep, IReportUsage iru, out long cBytesExcl) { long cBytes = 0; cBytesExcl = 0; iru.OpenDir(sLabel, di.FullName); DirectoryInfo []rgdi = null; // first, find any directories try { rgdi = di.GetDirectories(); } catch {} if (rgdi != null) { foreach(DirectoryInfo di2 in rgdi) { long cBytesExcl2; cBytes += GetDirSizeFromDirectoryInfo(sLabel + @"\" + di2.Name, di2, belExclusions, belInclusions, cLevel + 1, bep, iru, out cBytesExcl2); cBytesExcl += cBytesExcl2; } } // and now add on files -- this is where we actually check exclusions/inclusions.... FileInfo []rgfi = null; try { rgfi = di.GetFiles(); } catch {} if (rgfi != null) { foreach(FileInfo fi in rgfi) { try { string sFullName = fi.FullName; if (bep.DoExclusions) { if ((belExclusions != null && belExclusions.FMatch(bep.ServerName, bep.ServerShare, sFullName, BEList.BoolOp.Or)) || (belInclusions != null && !belInclusions.FMatch(bep.ServerName, bep.ServerShare, sFullName, BEList.BoolOp.Or))) { if (bep.Verbose) iru.ReportFile(0, fi.Length, sLabel, sFullName); cBytesExcl += fi.Length; continue; } } if (bep.Verbose) iru.ReportFile(fi.Length, 0, sLabel, sFullName); cBytes += fi.Length; } catch (Exception e) { Console.WriteLine(String.Format("cannot process file {0} in directory {1}: {2}", fi.Name, di.Name, e.Message)); } } } iru.ReportDir(cBytes, cBytesExcl, sLabel, cLevel); iru.CloseDir(); return cBytes; }