public override void Run(object param) { DriveInfo[] driveInfos = DriveInfo.GetDrives(); Drive[] drives = new Drive[driveInfos.Length]; for (int i = 0; i < driveInfos.Length; i++) { string vLabel = driveInfos[i].VolumeLabel; Queryable<Artefact> q = (Queryable<Artefact>)( from a in _repoProxy.Artefacts//.AsEnumerable() where /*a.GetType() == typeof(Drive)*/ a is Drive && (a as Drive).Label == vLabel select a); // drives[i] = (Drive)q.FirstOrDefault(); // if (q.TotalCount > 0) if (q.Count() > 0) drives[i] = q.ToArray()[0] as Drive; if (drives[i] == null) _repoProxy.Add(drives[i] = new Drive(driveInfos[i])); else if (drives[i].UpdateAge > TimeSpan.FromMinutes(1)) _repoProxy.Update(drives[i].Update(driveInfos[i])); } recursionDepth = -1; Queue<Uri> subDirectories = new Queue<Uri>(new Uri[] { BaseUri }); while (subDirectories.Count > 0) { Uri currentUri = subDirectories.Dequeue(); Drive drive = null; foreach (Drive d in drives) if (currentUri.LocalPath.StartsWith(d.Label)) drive = d; foreach (string relPath in EnumerateFiles(currentUri)) { string absPath = Path.Combine(currentUri.LocalPath, relPath); System.Linq.IQueryable<Artefact> r = from a in _repoProxy.Artefacts where a is File && (a as File).Path == absPath select a; File file = (File)r.AsEnumerable().FirstOrDefault(); if (file == null) _repoProxy.Add(new File(new System.IO.FileInfo(absPath), drive)); else if (file.UpdateAge > TimeSpan.FromMinutes(1)) _repoProxy.Update(new File(new System.IO.FileInfo(absPath), drive) { Id = file.Id, TimeCreated = file.TimeCreated }); } if (RecursionLimit < 0 || ++recursionDepth < RecursionLimit) { foreach (string relPath in EnumerateDirectories(currentUri)) { string absPath = Path.Combine(currentUri.LocalPath, relPath); var r = // System.Linq.IQueryable<Artefact> from a in _repoProxy.Artefacts //.AsEnumerable() where a is Directory && (a as Directory).Path == absPath select a; Directory dir = (Directory)r.AsEnumerable().FirstOrDefault(); if (dir == null) _repoProxy.Add(new Directory(new System.IO.DirectoryInfo(absPath), drive)); else if (dir.UpdateAge > TimeSpan.FromMinutes(1)) _repoProxy.Update(new Directory(new System.IO.DirectoryInfo(absPath), drive) { Id = dir.Id, TimeCreated = dir.TimeCreated }); subDirectories.Enqueue(new Uri(currentUri, relPath)); } } } }
/// <summary> /// Gets the drives. /// </summary> /// <returns>The drives.</returns> public static IQueryable<Drive> GetDrives() { List<Drive> drives = new List<Drive>(); foreach (DriveInfo dInfo in DriveInfo.GetDrives()) { Drive drive = null; try { if (Repository != null) { drive = ((IQueryable<Drive>)Repository.Queryables[typeof(Drive)]).FirstOrDefault((d) => d.Label == dInfo.VolumeLabel); if (drive == null) Repository.Add(drive = new Drive(dInfo)); else Repository.Update(drive.Update()); } else drive = new Drive(dInfo); if (drive == null) throw new NullReferenceException("drive is null"); drives.Add(drive); } catch (UnauthorizedAccessException ex) { // this is OK to continue from, just ignore & omit that drive - for now output for debug though Console.WriteLine("\n{0}\n", ex.ToString()); } } return drives.AsQueryable(); }