/* RecursiveMove.c generated by valac 0.56.19, the Vala compiler
 * generated from RecursiveMove.vala, do not modify */

/* -*- Mode: Vala; indent-tabs-mode: nil; tab-width: 2 -*-
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 * SPDX-FileCopyrightText: Michael Terry
 */

#include "deja.h"
#include <gio/gio.h>
#include <glib-object.h>
#include <glib.h>
#include <stdlib.h>
#include <string.h>

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

enum  {
	DEJA_DUP_RECURSIVE_MOVE_0_PROPERTY,
	DEJA_DUP_RECURSIVE_MOVE_NUM_PROPERTIES
};
static GParamSpec* deja_dup_recursive_move_properties[DEJA_DUP_RECURSIVE_MOVE_NUM_PROPERTIES];
typedef struct _Block8Data Block8Data;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))

struct _Block8Data {
	int _ref_count_;
	DejaDupRecursiveMove* self;
	gboolean error_occurred;
};

static gpointer deja_dup_recursive_move_parent_class = NULL;

static void deja_dup_recursive_move_progress_callback (DejaDupRecursiveMove* self,
                                                gint64 current_num_bytes,
                                                gint64 total_num_bytes);
static void deja_dup_recursive_move_real_handle_file (DejaDupRecursiveOp* base);
static Block8Data* block8_data_ref (Block8Data* _data8_);
static void block8_data_unref (void * _userdata_);
static void ___lambda31_ (Block8Data* _data8_,
                   GFile* s,
                   GFile* d,
                   const gchar* e);
static void ____lambda31__deja_dup_recursive_op_raise_error (DejaDupRecursiveOp* _sender,
                                                      GFile* src,
                                                      GFile* dst,
                                                      const gchar* errstr,
                                                      gpointer self);
static void _deja_dup_recursive_move_progress_callback_gfile_progress_callback (gint64 current_num_bytes,
                                                                         gint64 total_num_bytes,
                                                                         gpointer self);
static void deja_dup_recursive_move_real_handle_dir (DejaDupRecursiveOp* base);
static void deja_dup_recursive_move_real_finish_dir (DejaDupRecursiveOp* base);
static DejaDupRecursiveOp* deja_dup_recursive_move_real_clone_for_info (DejaDupRecursiveOp* base,
                                                                 GFileInfo* info);
static GType deja_dup_recursive_move_get_type_once (void);

DejaDupRecursiveMove*
deja_dup_recursive_move_construct (GType object_type,
                                   GFile* source,
                                   GFile* dest)
{
	DejaDupRecursiveMove * self = NULL;
	g_return_val_if_fail (source != NULL, NULL);
	g_return_val_if_fail (dest != NULL, NULL);
	self = (DejaDupRecursiveMove*) g_object_new (object_type, "src", source, "dst", dest, NULL);
	return self;
}

DejaDupRecursiveMove*
deja_dup_recursive_move_new (GFile* source,
                             GFile* dest)
{
	return deja_dup_recursive_move_construct (DEJA_DUP_TYPE_RECURSIVE_MOVE, source, dest);
}

static void
deja_dup_recursive_move_progress_callback (DejaDupRecursiveMove* self,
                                           gint64 current_num_bytes,
                                           gint64 total_num_bytes)
{
	g_return_if_fail (self != NULL);
}

static Block8Data*
block8_data_ref (Block8Data* _data8_)
{
	g_atomic_int_inc (&_data8_->_ref_count_);
	return _data8_;
}

static void
block8_data_unref (void * _userdata_)
{
	Block8Data* _data8_;
	_data8_ = (Block8Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data8_->_ref_count_)) {
		DejaDupRecursiveMove* self;
		self = _data8_->self;
		_g_object_unref0 (self);
		g_slice_free (Block8Data, _data8_);
	}
}

static void
___lambda31_ (Block8Data* _data8_,
              GFile* s,
              GFile* d,
              const gchar* e)
{
	DejaDupRecursiveMove* self;
	self = _data8_->self;
	g_return_if_fail (s != NULL);
	g_return_if_fail (d != NULL);
	g_return_if_fail (e != NULL);
	g_signal_emit_by_name ((DejaDupRecursiveOp*) self, "raise-error", s, d, e);
	_data8_->error_occurred = TRUE;
}

static void
____lambda31__deja_dup_recursive_op_raise_error (DejaDupRecursiveOp* _sender,
                                                 GFile* src,
                                                 GFile* dst,
                                                 const gchar* errstr,
                                                 gpointer self)
{
	___lambda31_ (self, src, dst, errstr);
}

static void
_deja_dup_recursive_move_progress_callback_gfile_progress_callback (gint64 current_num_bytes,
                                                                    gint64 total_num_bytes,
                                                                    gpointer self)
{
	deja_dup_recursive_move_progress_callback ((DejaDupRecursiveMove*) self, current_num_bytes, total_num_bytes);
}

static void
deja_dup_recursive_move_real_handle_file (DejaDupRecursiveOp* base)
{
	DejaDupRecursiveMove * self;
	GError* _inner_error0_ = NULL;
	self = (DejaDupRecursiveMove*) base;
	if (((DejaDupRecursiveOp*) self)->dst_type == G_FILE_TYPE_DIRECTORY) {
		Block8Data* _data8_;
		DejaDupRecursiveDelete* del_op = NULL;
		GFile* _tmp0_;
		GFile* _tmp1_;
		DejaDupRecursiveDelete* _tmp2_;
		DejaDupRecursiveDelete* _tmp3_;
		DejaDupRecursiveDelete* _tmp4_;
		_data8_ = g_slice_new0 (Block8Data);
		_data8_->_ref_count_ = 1;
		_data8_->self = g_object_ref (self);
		_data8_->error_occurred = FALSE;
		_tmp0_ = deja_dup_recursive_op_get_dst ((DejaDupRecursiveOp*) self);
		_tmp1_ = _tmp0_;
		_tmp2_ = deja_dup_recursive_delete_new (_tmp1_, NULL, NULL, FALSE);
		del_op = _tmp2_;
		_tmp3_ = del_op;
		g_signal_connect_data ((DejaDupRecursiveOp*) _tmp3_, "raise-error", (GCallback) ____lambda31__deja_dup_recursive_op_raise_error, block8_data_ref (_data8_), (GClosureNotify) block8_data_unref, 0);
		_tmp4_ = del_op;
		deja_dup_recursive_op_start ((DejaDupRecursiveOp*) _tmp4_);
		if (_data8_->error_occurred) {
			_g_object_unref0 (del_op);
			block8_data_unref (_data8_);
			_data8_ = NULL;
			return;
		}
		_g_object_unref0 (del_op);
		block8_data_unref (_data8_);
		_data8_ = NULL;
	}
	{
		GFile* _tmp5_;
		GFile* _tmp6_;
		GFile* _tmp7_;
		GFile* _tmp8_;
		_tmp5_ = deja_dup_recursive_op_get_src ((DejaDupRecursiveOp*) self);
		_tmp6_ = _tmp5_;
		_tmp7_ = deja_dup_recursive_op_get_dst ((DejaDupRecursiveOp*) self);
		_tmp8_ = _tmp7_;
		g_file_move (_tmp6_, _tmp8_, (G_FILE_COPY_ALL_METADATA | G_FILE_COPY_NOFOLLOW_SYMLINKS) | G_FILE_COPY_OVERWRITE, NULL, _deja_dup_recursive_move_progress_callback_gfile_progress_callback, self, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if (g_error_matches (_inner_error0_, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED)) {
				goto __catch0_g_io_error_permission_denied;
			}
			goto __catch0_g_error;
		}
	}
	goto __finally0;
	__catch0_g_io_error_permission_denied:
	{
		g_clear_error (&_inner_error0_);
		{
			GFile* _tmp9_;
			GFile* _tmp10_;
			GFile* _tmp11_;
			GFile* _tmp12_;
			_tmp9_ = deja_dup_recursive_op_get_src ((DejaDupRecursiveOp*) self);
			_tmp10_ = _tmp9_;
			_tmp11_ = deja_dup_recursive_op_get_dst ((DejaDupRecursiveOp*) self);
			_tmp12_ = _tmp11_;
			g_file_copy (_tmp10_, _tmp12_, (G_FILE_COPY_ALL_METADATA | G_FILE_COPY_NOFOLLOW_SYMLINKS) | G_FILE_COPY_OVERWRITE, NULL, _deja_dup_recursive_move_progress_callback_gfile_progress_callback, self, &_inner_error0_);
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				goto __catch1_g_error;
			}
		}
		goto __finally1;
		__catch1_g_error:
		{
			GError* e = NULL;
			GFile* _tmp13_;
			GFile* _tmp14_;
			GFile* _tmp15_;
			GFile* _tmp16_;
			GError* _tmp17_;
			const gchar* _tmp18_;
			e = _inner_error0_;
			_inner_error0_ = NULL;
			_tmp13_ = deja_dup_recursive_op_get_src ((DejaDupRecursiveOp*) self);
			_tmp14_ = _tmp13_;
			_tmp15_ = deja_dup_recursive_op_get_dst ((DejaDupRecursiveOp*) self);
			_tmp16_ = _tmp15_;
			_tmp17_ = e;
			_tmp18_ = _tmp17_->message;
			g_signal_emit_by_name ((DejaDupRecursiveOp*) self, "raise-error", _tmp14_, _tmp16_, _tmp18_);
			_g_error_free0 (e);
		}
		__finally1:
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return;
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GFile* _tmp19_;
		GFile* _tmp20_;
		GFile* _tmp21_;
		GFile* _tmp22_;
		GError* _tmp23_;
		const gchar* _tmp24_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp19_ = deja_dup_recursive_op_get_src ((DejaDupRecursiveOp*) self);
		_tmp20_ = _tmp19_;
		_tmp21_ = deja_dup_recursive_op_get_dst ((DejaDupRecursiveOp*) self);
		_tmp22_ = _tmp21_;
		_tmp23_ = e;
		_tmp24_ = _tmp23_->message;
		g_signal_emit_by_name ((DejaDupRecursiveOp*) self, "raise-error", _tmp20_, _tmp22_, _tmp24_);
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
}

static void
deja_dup_recursive_move_real_handle_dir (DejaDupRecursiveOp* base)
{
	DejaDupRecursiveMove * self;
	gboolean _tmp0_ = FALSE;
	GError* _inner_error0_ = NULL;
	self = (DejaDupRecursiveMove*) base;
	if (((DejaDupRecursiveOp*) self)->dst_type != G_FILE_TYPE_UNKNOWN) {
		_tmp0_ = ((DejaDupRecursiveOp*) self)->dst_type != G_FILE_TYPE_DIRECTORY;
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		{
			GFile* _tmp1_;
			GFile* _tmp2_;
			_tmp1_ = deja_dup_recursive_op_get_dst ((DejaDupRecursiveOp*) self);
			_tmp2_ = _tmp1_;
			g_file_delete (_tmp2_, NULL, &_inner_error0_);
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				goto __catch0_g_error;
			}
		}
		goto __finally0;
		__catch0_g_error:
		{
			GError* e = NULL;
			GFile* _tmp3_;
			GFile* _tmp4_;
			GFile* _tmp5_;
			GFile* _tmp6_;
			GError* _tmp7_;
			const gchar* _tmp8_;
			e = _inner_error0_;
			_inner_error0_ = NULL;
			_tmp3_ = deja_dup_recursive_op_get_src ((DejaDupRecursiveOp*) self);
			_tmp4_ = _tmp3_;
			_tmp5_ = deja_dup_recursive_op_get_dst ((DejaDupRecursiveOp*) self);
			_tmp6_ = _tmp5_;
			_tmp7_ = e;
			_tmp8_ = _tmp7_->message;
			g_signal_emit_by_name ((DejaDupRecursiveOp*) self, "raise-error", _tmp4_, _tmp6_, _tmp8_);
			_g_error_free0 (e);
			return;
		}
		__finally0:
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return;
		}
		((DejaDupRecursiveOp*) self)->dst_type = G_FILE_TYPE_UNKNOWN;
	}
	if (((DejaDupRecursiveOp*) self)->dst_type == G_FILE_TYPE_UNKNOWN) {
		{
			GFile* _tmp9_;
			GFile* _tmp10_;
			_tmp9_ = deja_dup_recursive_op_get_dst ((DejaDupRecursiveOp*) self);
			_tmp10_ = _tmp9_;
			g_file_make_directory (_tmp10_, NULL, &_inner_error0_);
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				goto __catch1_g_error;
			}
		}
		goto __finally1;
		__catch1_g_error:
		{
			GError* e = NULL;
			GFile* _tmp11_;
			GFile* _tmp12_;
			GFile* _tmp13_;
			GFile* _tmp14_;
			GError* _tmp15_;
			const gchar* _tmp16_;
			e = _inner_error0_;
			_inner_error0_ = NULL;
			_tmp11_ = deja_dup_recursive_op_get_src ((DejaDupRecursiveOp*) self);
			_tmp12_ = _tmp11_;
			_tmp13_ = deja_dup_recursive_op_get_dst ((DejaDupRecursiveOp*) self);
			_tmp14_ = _tmp13_;
			_tmp15_ = e;
			_tmp16_ = _tmp15_->message;
			g_signal_emit_by_name ((DejaDupRecursiveOp*) self, "raise-error", _tmp12_, _tmp14_, _tmp16_);
			_g_error_free0 (e);
			return;
		}
		__finally1:
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return;
		}
	}
}

static void
deja_dup_recursive_move_real_finish_dir (DejaDupRecursiveOp* base)
{
	DejaDupRecursiveMove * self;
	GError* _inner_error0_ = NULL;
	self = (DejaDupRecursiveMove*) base;
	{
		GFile* _tmp0_;
		GFile* _tmp1_;
		GFile* _tmp2_;
		GFile* _tmp3_;
		_tmp0_ = deja_dup_recursive_op_get_src ((DejaDupRecursiveOp*) self);
		_tmp1_ = _tmp0_;
		_tmp2_ = deja_dup_recursive_op_get_dst ((DejaDupRecursiveOp*) self);
		_tmp3_ = _tmp2_;
		g_file_copy_attributes (_tmp1_, _tmp3_, G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA, NULL, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		g_clear_error (&_inner_error0_);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
	{
		GFile* _tmp4_;
		GFile* _tmp5_;
		_tmp4_ = deja_dup_recursive_op_get_src ((DejaDupRecursiveOp*) self);
		_tmp5_ = _tmp4_;
		g_file_delete (_tmp5_, NULL, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch1_g_error;
		}
	}
	goto __finally1;
	__catch1_g_error:
	{
		g_clear_error (&_inner_error0_);
	}
	__finally1:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
}

static DejaDupRecursiveOp*
deja_dup_recursive_move_real_clone_for_info (DejaDupRecursiveOp* base,
                                             GFileInfo* info)
{
	DejaDupRecursiveMove * self;
	gchar* child_name = NULL;
	const gchar* _tmp0_;
	gchar* _tmp1_;
	GFile* src_child = NULL;
	GFile* _tmp2_;
	GFile* _tmp3_;
	GFile* _tmp4_;
	GFile* dst_child = NULL;
	GFile* _tmp5_;
	GFile* _tmp6_;
	GFile* _tmp7_;
	DejaDupRecursiveMove* _tmp8_;
	DejaDupRecursiveOp* result;
	self = (DejaDupRecursiveMove*) base;
	g_return_val_if_fail (info != NULL, NULL);
	_tmp0_ = g_file_info_get_name (info);
	_tmp1_ = g_strdup (_tmp0_);
	child_name = _tmp1_;
	_tmp2_ = deja_dup_recursive_op_get_src ((DejaDupRecursiveOp*) self);
	_tmp3_ = _tmp2_;
	_tmp4_ = g_file_get_child (_tmp3_, child_name);
	src_child = _tmp4_;
	_tmp5_ = deja_dup_recursive_op_get_dst ((DejaDupRecursiveOp*) self);
	_tmp6_ = _tmp5_;
	_tmp7_ = g_file_get_child (_tmp6_, child_name);
	dst_child = _tmp7_;
	_tmp8_ = deja_dup_recursive_move_new (src_child, dst_child);
	result = (DejaDupRecursiveOp*) _tmp8_;
	_g_object_unref0 (dst_child);
	_g_object_unref0 (src_child);
	_g_free0 (child_name);
	return result;
}

static void
deja_dup_recursive_move_class_init (DejaDupRecursiveMoveClass * klass,
                                    gpointer klass_data)
{
	deja_dup_recursive_move_parent_class = g_type_class_peek_parent (klass);
	((DejaDupRecursiveOpClass *) klass)->handle_file = (void (*) (DejaDupRecursiveOp*)) deja_dup_recursive_move_real_handle_file;
	((DejaDupRecursiveOpClass *) klass)->handle_dir = (void (*) (DejaDupRecursiveOp*)) deja_dup_recursive_move_real_handle_dir;
	((DejaDupRecursiveOpClass *) klass)->finish_dir = (void (*) (DejaDupRecursiveOp*)) deja_dup_recursive_move_real_finish_dir;
	((DejaDupRecursiveOpClass *) klass)->clone_for_info = (DejaDupRecursiveOp* (*) (DejaDupRecursiveOp*, GFileInfo*)) deja_dup_recursive_move_real_clone_for_info;
}

static void
deja_dup_recursive_move_instance_init (DejaDupRecursiveMove * self,
                                       gpointer klass)
{
}

/**
 * Recursively moves one directory into another, merging files.  And by merge,
 * I mean it overwrites.  It skips any files it can't move and reports an
 * error, but keeps going.
 *
 * This is not optimized for remote files.  It's mostly async, but it does the
 * occasional sync operation.
 */
 G_GNUC_NO_INLINE static GType
deja_dup_recursive_move_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (DejaDupRecursiveMoveClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) deja_dup_recursive_move_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (DejaDupRecursiveMove), 0, (GInstanceInitFunc) deja_dup_recursive_move_instance_init, NULL };
	GType deja_dup_recursive_move_type_id;
	deja_dup_recursive_move_type_id = g_type_register_static (DEJA_DUP_TYPE_RECURSIVE_OP, "DejaDupRecursiveMove", &g_define_type_info, G_TYPE_FLAG_NONE);
	return deja_dup_recursive_move_type_id;
}

GType
deja_dup_recursive_move_get_type (void)
{
	static GType deja_dup_recursive_move_type_id__once = 0;
	if (g_once_init_enter_pointer (&deja_dup_recursive_move_type_id__once)) {
		GType deja_dup_recursive_move_type_id;
		deja_dup_recursive_move_type_id = deja_dup_recursive_move_get_type_once ();
		g_once_init_leave_pointer (&deja_dup_recursive_move_type_id__once, deja_dup_recursive_move_type_id);
	}
	return deja_dup_recursive_move_type_id__once;
}

