+/*< internal >
+ * desktop_file_dir_get_alternative_dir:
+ * @dir: a #DesktopFileDir
+ *
+ * Gets the "alternative" directory to monitor in case the path
+ * doesn't exist.
+ *
+ * If the path exists this will return NULL, otherwise it will return a
+ * parent directory of the path.
+ *
+ * This is used to avoid inotify on a non-existent directory (which
+ * results in polling).
+ *
+ * See https://bugzilla.gnome.org/show_bug.cgi?id=522314 for more info.
+ */
+static gchar *
+desktop_file_dir_get_alternative_dir (DesktopFileDir *dir)
+{
+ gchar *parent;
+
+ /* If the directory itself exists then we need no alternative. */
+ if (g_access (dir->path, R_OK | X_OK) == 0)
+ return NULL;
+
+ /* Otherwise, try the parent directories until we find one. */
+ parent = g_path_get_dirname (dir->path);
+
+ while (g_access (parent, R_OK | X_OK) != 0)
+ {
+ gchar *tmp = parent;
+
+ parent = g_path_get_dirname (tmp);
+
+ /* If somehow we get to '/' or '.' then just stop... */
+ if (g_str_equal (parent, tmp))
+ {
+ g_free (tmp);
+ break;
+ }
+
+ g_free (tmp);
+ }
+
+ return parent;
+}
+