+ /* WRITE_METADATA is special because it can be configured to silently write to ~/.geeqie/...
+ - that means that there are no hard errors and warnings can be disabled
+ - the destination is determined during the check
+ */
+ else if (fd->change->type == FILEDATA_CHANGE_WRITE_METADATA)
+ {
+ /* determine destination file */
+ gboolean have_dest = FALSE;
+ gchar *dest_dir = NULL;
+
+ if (options->metadata.save_in_image_file)
+ {
+ if (file_data_can_write_directly(fd))
+ {
+ /* we can write the file directly */
+ if (access_file(fd->path, W_OK))
+ {
+ have_dest = TRUE;
+ }
+ else
+ {
+ if (options->metadata.warn_on_write_problems)
+ {
+ ret |= CHANGE_WARN_NO_WRITE_PERM;
+ DEBUG_1("Change checked: file is not writable: %s", fd->path);
+ }
+ }
+ }
+ else if (file_data_can_write_sidecar(fd))
+ {
+ /* we can write sidecar */
+ gchar *sidecar = file_data_get_sidecar_path(fd, FALSE);
+ if (access_file(sidecar, W_OK) || (!isname(sidecar) && access_file(dir, W_OK)))
+ {
+ file_data_update_ci_dest(fd, sidecar);
+ have_dest = TRUE;
+ }
+ else
+ {
+ if (options->metadata.warn_on_write_problems)
+ {
+ ret |= CHANGE_WARN_NO_WRITE_PERM;
+ DEBUG_1("Change checked: file is not writable: %s", sidecar);
+ }
+ }
+ g_free(sidecar);
+ }
+ }
+
+ if (!have_dest)
+ {
+ /* write private metadata file under ~/.geeqie */
+
+ /* If an existing metadata file exists, we will try writing to
+ * it's location regardless of the user's preference.
+ */
+ gchar *metadata_path = NULL;
+#ifdef HAVE_EXIV2
+ /* but ignore XMP if we are not able to write it */
+ metadata_path = cache_find_location(CACHE_TYPE_XMP_METADATA, fd->path);
+#endif
+ if (!metadata_path) metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path);
+
+ if (metadata_path && !access_file(metadata_path, W_OK))
+ {
+ g_free(metadata_path);
+ metadata_path = NULL;
+ }
+
+ if (!metadata_path)
+ {
+ mode_t mode = 0755;
+
+ dest_dir = cache_get_location(CACHE_TYPE_METADATA, fd->path, FALSE, &mode);
+ if (recursive_mkdir_if_not_exists(dest_dir, mode))
+ {
+ gchar *filename = g_strconcat(fd->name, options->metadata.save_legacy_format ? GQ_CACHE_EXT_METADATA : GQ_CACHE_EXT_XMP_METADATA, NULL);
+
+ metadata_path = g_build_filename(dest_dir, filename, NULL);
+ g_free(filename);
+ }
+ }
+ if (access_file(metadata_path, W_OK) || (!isname(metadata_path) && access_file(dest_dir, W_OK)))
+ {
+ file_data_update_ci_dest(fd, metadata_path);
+ have_dest = TRUE;
+ }
+ else
+ {
+ ret |= CHANGE_NO_WRITE_PERM_DEST;
+ DEBUG_1("Change checked: file is not writable: %s", metadata_path);
+ }
+ g_free(metadata_path);
+ }
+ g_free(dest_dir);
+ }