// File: /workspace/github_c_makefile_star2_O2/j-hoppe[P]tu58fs/hostdir.c
// Function: hostdir_sync
int hostdir_sync(hostdir_t *_this) {
	// int any_file_change;
	int update_pdp;
	int update_snapshot;
	int i;
	// entry: snapshot contains files in shared dir,
	// as PDP filesystem and hostdir where synched

	// scan hostdir
	snapshot_scan_hostdir(_this);

	// scan PDP image
	snapshot_scan_pdpimage(_this);

	// print state
	if (opt_debug)
		snapshot_print(&_this->snapshot, ferr, 1);
	else if (opt_verbose)
		snapshot_print(&_this->snapshot, ferr, 0);

	update_pdp = 0;
	update_snapshot = 0;
	if (_this->pdp_fs->readonly) {
		// readonly: only sync hostdir from PDP file system
		int hostdir_changed = 0;
		for (i = 0; i < _this->snapshot.file_count; i++) {
			hostdir_file_t *f = &_this->snapshot.file[i];
			if (f->state[side_host] != fs_unchanged)
				hostdir_changed = 1;
		}
		if (hostdir_changed) {
			// short code: rebuild whole hostdir
			info("Unit %d: Device is readonly, reverting changes in shared dir \"%s\".", _this->unit, _this->path) ;
			hostdir_prepare(_this, /*wipe*/1, /*exists*/0, NULL);
			hostdir_from_pdp_fs(_this);
			update_snapshot = 1;
		}
	} else {
		// not readonly: update hostdir and PDP file system
		for (i = 0; i < _this->snapshot.file_count; i++) {
			hostdir_file_t *f = &_this->snapshot.file[i];
			// 16 cases. The cases when one side is unchanged are easy
			if (f->state[side_pdp] == fs_unchanged && f->state[side_host] == fs_unchanged) {
				// do nothing
			} else if (f->state[side_pdp] == fs_unchanged
					&& f->state[side_host] == fs_missing) {
				if (f->pdp_fixed)
					hostdir_file_copy_from_pdp(_this, f); // restore
				else
					update_pdp = 1; // update PDP from hostdir
			} else if (f->state[side_pdp] == fs_unchanged
					&& f->state[side_host] == fs_changed) {
				update_pdp = 1; // update PDP from hostdir
			} else if (f->state[side_pdp] == fs_unchanged
					&& f->state[side_host] == fs_created) {
				update_pdp = 1; // update PDP from hostdir

			} else if (f->state[side_pdp] == fs_missing
					&& f->state[side_host] == fs_unchanged) {
				hostdir_file_delete(_this, f);
				update_snapshot = 1;
			} else if (f->state[side_pdp] == fs_missing && f->state[side_host] == fs_missing) {
				// no need to update hostdir from pdp
				update_snapshot = 1; // but delete file from snapshot!
			} else if (f->state[side_pdp] == fs_missing && f->state[side_host] == fs_changed) {
				if (_this->pdp_priority)
					hostdir_file_delete(_this, f);
				else
					update_pdp = 1;
				update_snapshot = 1;
			} else if (f->state[side_pdp] == fs_missing && f->state[side_host] == fs_created) {
				// the file was created on host and is not yet on PDP
				update_pdp = 1; // force reload
			} else if (f->state[side_pdp] == fs_changed
					&& f->state[side_host] == fs_unchanged) {
				hostdir_file_copy_from_pdp(_this, f);
				update_snapshot = 1;
			} else if (f->state[side_pdp] == fs_changed && f->state[side_host] == fs_missing) {
				if (f->pdp_fixed || _this->pdp_priority)
					hostdir_file_copy_from_pdp(_this, f);
				else
					update_pdp = 1;
				update_snapshot = 1;
			} else if (f->state[side_pdp] == fs_changed && f->state[side_host] == fs_changed) {
				if (_this->pdp_priority)
					hostdir_file_copy_from_pdp(_this, f);
				else
					update_pdp = 1;
				update_snapshot = 1;
			} else if (f->state[side_pdp] == fs_changed && f->state[side_host] == fs_created) {
				if (_this->pdp_priority)
					hostdir_file_copy_from_pdp(_this, f);
				else
					update_pdp = 1;
				update_snapshot = 1;
			} else if (f->state[side_pdp] == fs_created
					&& f->state[side_host] == fs_unchanged) {
				hostdir_file_copy_from_pdp(_this, f);
				update_snapshot = 1;
			} else if (f->state[side_pdp] == fs_created && f->state[side_host] == fs_missing) {
				// new on PDP
				hostdir_file_copy_from_pdp(_this, f);
				update_snapshot = 1;
			} else if (f->state[side_pdp] == fs_created && f->state[side_host] == fs_changed) {
				if (_this->pdp_priority)
					hostdir_file_copy_from_pdp(_this, f);
				else
					update_pdp = 1;
				update_snapshot = 1;
			} else if (f->state[side_pdp] == fs_created && f->state[side_host] == fs_created) {
				if (_this->pdp_priority)
					hostdir_file_copy_from_pdp(_this, f);
				else
					update_pdp = 1;
				update_snapshot = 1;
			}
		}
	}
	if (update_pdp) {
		hostdir_file_t *f;
		// files in the host dir have changed:
		// reload the tu58 image
		hostdir_image_reload(_this);
		// send volum.inf (RT11)
		f = snapshot_file_find(&_this->snapshot, "$VOLUM.INF");
		if (f)
			hostdir_file_copy_from_pdp(_this, f);
		if (opt_verbose)
			info("Unit %d: Updated PDP image with shared dir \"%s\".", _this->unit, _this->path);
	}
	if (update_pdp || update_snapshot) {
		// if file was deleted
		snapshot_init(_this);
		if (opt_verbose)
			info("Unit %d: Scanned content of shared dir \"%s\".", _this->unit, _this->path);
	}

	return ERROR_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
callq 0x9310
movq %rbx, %rdi
callq 0x952a
leaq 0x139d0(%rip), %rax     # 0x1ca74
cmpl $0x0, (%rax)
je 0x90ae
pushq $0x1
popq %rsi
jmp 0x90bc
leaq 0x139af(%rip), %rax     # 0x1ca64
xorl %esi, %esi
cmpl $0x0, (%rax)
je 0x90c8
leaq 0x1010(%rbx), %rdi
callq 0x9667
movq 0x1008(%rbx), %rax
cmpl $0x0, 0x4(%rax)
je 0x913e
movl 0x1018(%rbx), %ecx
xorl %eax, %eax
testl %ecx, %ecx
cmovlel %eax, %ecx
imulq $0x150, %rcx, %rcx      # imm = 0x150
pushq $0x1
popq %rdx
xorl %esi, %esi
cmpq %rax, %rcx
je 0x9106
cmpl $0x0, 0x114c(%rbx,%rax)
cmovnel %edx, %esi
addq $0x150, %rax            # imm = 0x150
jmp 0x90ee
testl %esi, %esi
je 0x92ff
movl (%rbx), %esi
leaq 0x4(%rbx), %rdx
leaq 0xb474(%rip), %rdi      # 0x1458f
xorl %eax, %eax
callq 0x99c3
pushq $0x1
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x882c
movq %rbx, %rdi
callq 0x8b55
jmp 0x92d4
leaq 0x1020(%rbx), %r14
xorl %r15d, %r15d
leaq 0xb191(%rip), %r12      # 0x142e0
pushq $0x1
popq %rdx
xorl %ebp, %ebp
xorl %r13d, %r13d
movslq 0x1018(%rbx), %rax
cmpq %rax, %r15
jge 0x927e
movl 0x128(%r14), %eax
cmpq $0x3, %rax
ja 0x9269
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movl 0x12c(%r14), %eax
cmpq $0x3, %rax
ja 0x9269
leaq 0xb187(%rip), %rcx      # 0x14320
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0x0, 0x148(%r14)
je 0x9248
movq %rbx, %rdi
movq %r14, %rsi
callq 0x96f5
pushq $0x1
popq %rdx
jmp 0x9269
movl 0x12c(%r14), %eax
cmpq $0x3, %rax
ja 0x9269
leaq 0xb125(%rip), %rcx      # 0x14300
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0x0, 0x148(%r14)
jne 0x921a
jmp 0x920d
movl 0x12c(%r14), %eax
cmpq $0x3, %rax
ja 0x9269
leaq 0xb0ec(%rip), %rcx      # 0x142f0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0x0, 0x530a0(%rbx)
movl %edx, %ebp
movl %edx, %eax
je 0x926c
movq %rbx, %rdi
movq %r14, %rsi
callq 0x96f5
jmp 0x9264
movl 0x12c(%r14), %eax
cmpq $0x3, %rax
ja 0x9269
leaq 0xb0d5(%rip), %rsi      # 0x14310
movslq (%rsi,%rax,4), %rcx
addq %rsi, %rcx
jmpq *%rcx
movl %eax, %ebp
jmp 0x9269
movl %edx, %eax
jmp 0x926c
cmpl $0x0, 0x530a0(%rbx)
movl %edx, %ebp
movl %edx, %eax
je 0x926c
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9782
pushq $0x1
popq %rdx
movl %edx, %ebp
movl %r13d, %eax
incq %r15
addq $0x150, %r14            # imm = 0x150
movl %eax, %r13d
jmp 0x9157
testl %r13d, %r13d
je 0x92d0
movq %rbx, %rdi
callq 0x8f7f
leaq 0x1010(%rbx), %rdi
leaq 0xb267(%rip), %rsi      # 0x14500
callq 0x86c8
testq %rax, %rax
je 0x92ae
movq %rbx, %rdi
movq %rax, %rsi
callq 0x96f5
leaq 0x137af(%rip), %rax     # 0x1ca64
cmpl $0x0, (%rax)
je 0x92d4
movl (%rbx), %esi
leaq 0x4(%rbx), %rdx
leaq 0xb30b(%rip), %rdi      # 0x145d2
xorl %eax, %eax
callq 0x99c3
jmp 0x92d4
testl %ebp, %ebp
je 0x92ff
movq %rbx, %rdi
callq 0x97df
leaq 0x13781(%rip), %rax     # 0x1ca64
cmpl $0x0, (%rax)
je 0x92ff
movl (%rbx), %esi
addq $0x4, %rbx
leaq 0xb30e(%rip), %rdi      # 0x14603
movq %rbx, %rdx
xorl %eax, %eax
callq 0x99c3
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq