Example #1
0
    static ArrayList RemapUris(LuceneQueryingDriver driver, ArrayList uris)
    {
        // We only need to remap URIs in the file system backend
        if (driver.IndexName != "FileSystemIndex")
        {
            return(uris);
        }

        FileAttributesStore fa_store = new FileAttributesStore(new FileAttributesStore_Mixed(Path.Combine(PathFinder.IndexDir, "FileSystemIndex"), driver.Fingerprint));

        for (int i = 0; i < uris.Count; i++)
        {
            Uri    uri  = (Uri)uris [i];
            string path = uri.LocalPath;

            Beagle.Daemon.FileAttributes attr = fa_store.Read(path);
            if (attr == null)
            {
                Console.WriteLine("No file attribute info for {0}", uri);
                continue;
            }

            Uri internal_uri = new Uri("uid:" + GuidFu.ToShortString(attr.UniqueId) + uri.Fragment);
            uris [i] = internal_uri;
        }

        return(uris);
    }
		public FileAttributes ReadOrCreate (string path, Guid unique_id, out bool created)
		{
			lock (ifas) {
				created = false;

				if (Debug)
					Log.Debug ("Reading or creating attr for {0}", path);

				FileAttributes attr = ifas.Read (path);

				if (attr == null && unique_id == Guid.Empty)
					unique_id = Guid.NewGuid ();

				// If we pass in a Guid that doesn't match the one we found in the
				// the attributes, clobber the old attributes and the old unique Guid.
				if (attr == null
				    || (unique_id != Guid.Empty && unique_id != attr.UniqueId)) {
					// First drop the old attribute, if there is one.
					if (attr != null)
						ifas.Drop (path);

					// Now create the new attribute
					attr = new FileAttributes ();
					attr.UniqueId = unique_id;
					attr.Path = path;
					
					created = true;
				}

				if (Debug)
					Log.Debug ("  {0} attr for {1}", created ? "Created" : "Read existing", path);

				return attr;
			}
		}
		public bool Write (FileAttributes attr)
		{
			if (store_ea.Write (attr)) {
				// Drop any now-outdated information from the sqlite store.
				store_sqlite.Drop (attr.Path);

				return true;
			} else
				return store_sqlite.Write (attr);
		}
Example #4
0
		///////////////////////////////////////////////////////////

		static public DirectoryModel NewRoot (object big_lock, string path, FileAttributes attr)
		{
			path = StringFu.SanitizePath (path);
			
			DirectoryModel root;
			root = new DirectoryModel (attr);

			root.big_lock = big_lock;
			root.rooted_to = FileSystem.GetDirectoryNameRootOk (path);
			root.name = Path.GetFileName (path);

			return root;
		}
Example #5
0
		private DirectoryModel (FileAttributes attr)
		{
			// Always assume an unknown state
			this.state = DirectoryState.Unknown;

			if (attr != null) {
				this.unique_id = attr.UniqueId;
				
				// Since we don't use the Mtime on directories,
				// we can safely store the last crawl time in it.
				this.last_crawl_time = attr.LastWriteTime;
			}
			
			this.cached_full_name = null;
			this.cached_depth = -1;
		}
Example #6
0
    // Remapping hack from DumpIndex
    static Uri RemapUri(LuceneQueryingDriver driver, Uri uri)
    {
        // We only need to remap URIs in the file system backend
        if (driver.IndexName != "FileSystemIndex")
        {
            return(uri);
        }

        FileAttributesStore fa_store = new FileAttributesStore(new FileAttributesStore_Mixed(Path.Combine(PathFinder.IndexDir, "FileSystemIndex"), driver.Fingerprint));

        string path = uri.LocalPath;

        Beagle.Daemon.FileAttributes attr = fa_store.Read(path);
        if (attr == null)
        {
            Console.WriteLine("No file attribute info for {0}", uri);
            return(uri);
        }

        return(new Uri("uid:" + GuidFu.ToShortString(attr.UniqueId) + uri.Fragment));
    }
		public FileAttributes Read (string path)
		{
			if (Disable)
				return null;

			try {
				string tmp = ExtendedAttribute.Get (path);

				if (tmp == null)
					return null;

				string[] csv = tmp.Split (',');

				if (int.Parse (csv [0].Substring (0, 2)) != EA_VERSION
				    || (index_fingerprint != null && csv [0].Substring (3) != index_fingerprint))
					return null;

				FileAttributes attr = new FileAttributes ();
				attr.UniqueId = GuidFu.FromShortString (csv [1]);
				attr.Path = path;
				attr.LastWriteTime = StringFu.StringToDateTime (csv [2]);
				attr.LastAttrTime = StringFu.StringToDateTime (csv [3]);

				if (! String.IsNullOrEmpty (csv [4])) {
					attr.FilterVersion = int.Parse (csv [4].Substring (0, 3));
					attr.FilterName = csv [4].Substring (4);
				}
				
				return attr;

			} catch (Exception e) {
				//Logger.Log.Debug ("Caught exception reading EAs from {0}", path);
				//Logger.Log.Debug (e);
				// FIXME: Do something smarter with the exception.
				return null;
			}
		}
		public bool Write (FileAttributes fa)
		{
			SetPathFlag (fa.Path);
			int ret = 0;
			string filter_name;

			// We need to quote any 's that appear in the strings
			// (in particular, in the path)
			lock (connection) {
				
				// If a transaction has been requested, start it now.
				MaybeStartTransaction ();

				filter_name = fa.FilterName;
				if (filter_name == null)
					filter_name = "";
				filter_name = filter_name.Replace ("'", "''");
				string[] param= new string [] { "@unique_id", "@directory", "@filename", "@last_mtime", "@last_attrtime", "@filter_name", "@filter_version"};
				object[] vals =	new object [] {
								GuidFu.ToShortString (fa.UniqueId),
								fa.Directory.Replace ("'", "''"), fa.Filename.Replace ("'", "''"),
								StringFu.DateTimeToString (fa.LastWriteTime),
								StringFu.DateTimeToString (fa.LastAttrTime),
								filter_name,
								fa.FilterVersion};
				for (int i=0; i < param.Length; i++)
					InsertCommand.Parameters.AddWithValue (param[i], vals[i]);
				
				ret = SqliteUtils.DoNonQuery (InsertCommand);
				
			}

			return (ret != 0);
		}
		private FileAttributes GetFromReader (SqliteDataReader reader)
		{
			FileAttributes attr = new FileAttributes ();

			attr.UniqueId = GuidFu.FromShortString (reader.GetString (0));
			attr.Path = System.IO.Path.Combine (reader.GetString (1), reader.GetString (2));
			attr.LastWriteTime = StringFu.StringToDateTime (reader.GetString (3));
			attr.LastAttrTime = StringFu.StringToDateTime (reader.GetString (4));
			attr.FilterName = reader.GetString (5);
			attr.FilterVersion = int.Parse (reader.GetString (6));

			if (attr.FilterName == "")
				attr.FilterName = null;

			return attr;
		}
		public bool Write (FileAttributes attr)
		{
			if (Disable)
				return false;

			try {
				if (ExtendedAttribute.OldExists (attr.Path, "Fingerprint"))
					DropObsoleteAttributes (attr.Path);

				string fingerprint = String.Format ("{0:00} {1}", EA_VERSION, index_fingerprint);
				string uid = GuidFu.ToShortString (attr.UniqueId);
				string mtime = StringFu.DateTimeToString (attr.LastWriteTime);

				string filter = String.Empty;

				if (attr.HasFilterInfo)
					filter = String.Format ("{0:000} {1}", attr.FilterVersion, attr.FilterName);

				attr.LastAttrTime = DateTime.UtcNow;
				string attrtime = StringFu.DateTimeToString (attr.LastAttrTime);

				string [] csv = {fingerprint, uid, mtime, attrtime, filter};
				ExtendedAttribute.Set (attr.Path, String.Join (",", csv));
							
				return true;
			} catch (IOException e) {
				// An IOException here probably means that we don't have the right
				// permissions to set the EAs.  We just fail silently and return false rather
				// than spewing a bunch of scary exceptions.
				//Logger.Log.Debug (e);
				return false;
			} catch (Exception e) {
				//Logger.Log.Debug (e, "Caught exception writing EAs to {0}", attr.Path);
				// FIXME: Do something smarter with the exception.
				return false;
			}
		}
		// To be used if the last_write_time to use for comparison is not the
		// one obtained from path
		public static bool IsUpToDate (FileAttributes attr, DateTime last_write_time)
		{
			if (attr == null)
				return false;

			return (attr.LastWriteTime >= last_write_time);
		}
		//////////////////////////////////////////////////////////

		public static bool IsUpToDate (string path, FileAttributes attr)
		{
			if (attr == null)
				return false;

			return (attr.LastWriteTime >= FileSystem.GetLastWriteTimeUtc (path));
		}
		public bool Write (FileAttributes attr)
		{
			lock (ifas) {
				if (Debug)
					Log.Debug ("Writing attr for {0}", attr.Path);

				attr.LastAttrTime = DateTime.UtcNow;

				bool success = ifas.Write (attr);

				if (Debug)
					Log.Debug ("  write {0}", success ? "succeeded" : "FAILED");

				return success;
			}
		}
Example #14
0
		public DirectoryModel AddChild (string child_name, FileAttributes attr)
		{
			lock (big_lock) {
				DirectoryModel child;
				child = new DirectoryModel (attr);

				child.name = child_name;
				Attach_Unlocked (child);

				return child;
			}
		}
		private bool RegisterDirectory (string name, DirectoryModel parent, FileAttributes attr)
		{
			string path;
			path = (parent == null) ? name : Path.Combine (parent.FullName, name);

			if (Debug)
				Logger.Log.Debug ("Registered directory '{0}' ({1})", path, attr.UniqueId);

			DateTime mtime = Directory.GetLastWriteTimeUtc (path);

			if (! FileSystem.ExistsByDateTime (mtime)) {
				Log.Debug ("Directory '{0}' ({1}) appears to have gone away", path, attr.UniqueId);
				return false;
			}

			DirectoryModel dir;
			if (parent == null)
				dir = DirectoryModel.NewRoot (big_lock, path, attr);
			else
				dir = parent.AddChild (name, attr);

			if (mtime > attr.LastWriteTime) {
				dir.State = DirectoryState.Dirty;
				if (Debug)
					Logger.Log.Debug ("'{0}' is dirty", path);
			}

			if (Debug) {
				if (dir.IsRoot)
					Logger.Log.Debug ("Created model '{0}'", dir.FullName);
				else
					Logger.Log.Debug ("Created model '{0}' with parent '{1}'", dir.FullName, dir.Parent.FullName);
			}

			// Add any roots we create to the list of roots
			if (dir.IsRoot)
				roots.Add (dir);

			// Add the directory to our by-id hash, and remove any NameInfo
			// we might have cached about it.
			dir_models_by_id [dir.UniqueId] = dir;
			name_info_by_id.Remove (dir.UniqueId);

			// Start watching the directory.
			dir.WatchHandle = event_backend.CreateWatch (path);
			
			// Schedule this directory for crawling.
			if (tree_crawl_task.Add (dir))
				ThisScheduler.Add (tree_crawl_task);

			// Make sure that our file crawling task is active,
			// since presumably we now have something new to crawl.
			ActivateFileCrawling ();

			return true;
		}
		private void ScheduleDirectory (string         name,
						DirectoryModel parent,
						FileAttributes attr,
						bool           is_walkable)
		{
			string path;
			path = (parent == null) ? name : Path.Combine (parent.FullName, name);

			Guid id;
			id = (attr == null) ? Guid.NewGuid () : attr.UniqueId;

			DateTime last_crawl;
			last_crawl = (attr == null) ? DateTime.MinValue : attr.LastWriteTime;

			Indexable indexable;
			indexable = DirectoryToIndexable (path, id, parent);

			if (indexable != null) {
				indexable.LocalState ["Name"] = name;
				indexable.LocalState ["LastCrawl"] = last_crawl;
				indexable.LocalState ["IsWalkable"] = is_walkable;

				Scheduler.Task task;
				task = NewAddTask (indexable);
				task.Priority = Scheduler.Priority.Delayed;
				ThisScheduler.Add (task);
			}
		}