#!/bin/bash
###入参1：overlay目录（/ostree/pkgs/ovl-<csum.deployserial>)
###入参2：待处理目录（etc、var)
TMPUPPER="$1/$2-ovl/$2-tmpupper"
UPPER="$1/$2-ovl/$2-upper"
LOWER="$1/$2-ovl/$2-lower"

# # 定义源目录和目的目录
SOURCE_DIR=$TMPUPPER
DEST_DIR=$UPPER

# 临时文件，用于存储字符设备文件的路径
CHAR_DEVICES_FILE=$(mktemp)
# 查找源目录下的字符设备类型文件(删除标记)，并将路径保存到临时文件中
find "$SOURCE_DIR" -type c -printf "%P\n" > "$CHAR_DEVICES_FILE"

####处理tmpupper层修改的数据
Process_modify_data()
{
	logger -t ostree-maintain "Processing file modification conflicts for $TMPUPPER"
	rsync -aAXc  --inplace -H  --numeric-ids --ignore-times --atimes --links --existing --exclude-from=$CHAR_DEVICES_FILE --log-file=/var/log/ostree.log --log-file-format="ostree-maintain: %o %f " $SOURCE_DIR/ "$DEST_DIR/"
	logger -t ostree-maintain "Resolve conflicts in the modified files of $TMPUPPER"
}

####处理tmpupper层删除的数据
Process_delete_data()
{
	logger -t ostree-maintain "Processing file delete conflicts for $TMPUPPER"
	##查找对应文件/目录在那一层新增
	while IFS= read -r line;do
		local upper_path="$UPPER/$line"
		local lower_path="$LOWER/$line"
		local tmpupper_path="$TMPUPPER/$line"
		
		if [ -e "$upper_path" ]; then  ##是由etc-upper新增或修改的文件
			if [ -e "$lower_path" ]; then    ##etc-lower、/etc目录存在，则证明etc-upper目录下知识对该文件修改的，而不是新增。那么直接将删除标记移动到upper即可
				##需要加--delete，否则无法将字符设备文件覆盖同名目录。且目的目录需要是同名文件的上一级（dirname）
				logger -t ostree-maintain "Modify data in the tmpupper (exists in lower):  rsync -a --partial --links --acls --xattrs --delete $tmpupper_path $(dirname "$upper_path")"
				rsync -aAXc  --inplace -H  --numeric-ids --ignore-times --atimes --links --delete --log-file=/var/log/ostree.log --log-file-format="ostree-maintain: %o %f " "$tmpupper_path"  "$(dirname "$upper_path")"
		
			else
				#/etc-lower中不存在，证明是有upper层新建的，删除/etc-upper同级目录下的该文件,
				logger -t ostree-maintain "Delete data in the tmpupper (non-existent in lower): delete $upper_path"
				rm -rf $upper_path
			fi
		fi
	done < "$CHAR_DEVICES_FILE"
	logger -t ostree-maintain "Resolve conflicts in the delete files of $TMPUPPER"
}

Process_opaque_data()
{
	logger -t ostree-maintain "Processing file opaque conflicts for $TMPUPPER"
	local upper_path="$UPPER/ksaf"
	local tmpupper_path="$TMPUPPER/ksaf"

	if [ -e "$tmpupper_path" ]; then
		#使用find递归遍历目录tmpupper_path下的所有文件和目录
		find "$tmpupper_path" \( -type d -o -type f \) -print0 | while IFS= read -r -d '' item; do
			# 检查是否具有trusted.overlay.opaque="y"属性
			if attr=$(getfattr -n trusted.overlay.opaque --only-values "$item" 2>/dev/null) && [ "$attr" = "y" ]; then
				echo "Processing item: $item"
				
				# 计算相对于tmpupper_path的路径
				rel_path="${item#$tmpupper_path/}"
				echo "Relative path: $rel_path"
				
				rsync -aAXc --inplace --force -H --numeric-ids --ignore-times --atimes --links --mkpath --delete --log-file=/var/log/ostree.log --log-file-format="ostree-maintain: %o %f " $item/ "$upper_path/$rel_path"
				echo "Copied: $item to $upper_path/$rel_path"
			fi
		done
		echo "操作完成"
	fi
}

# Process_modify_data
# Process_delete_data
Process_opaque_data

