~mrms/osum

d8699dd800d17ec72c4da18626ea9f0eb5965277 — Marek Ma┼íkarinec 6 months ago 7076fc7
Update directory handling in link
2 files changed, 32 insertions(+), 4 deletions(-)

M os.c
M os.um
M os.c => os.c +29 -4
@@ 59,12 59,37 @@ umc__link(UmkaStackSlot *p, UmkaStackSlot *r)

#ifdef _WIN32
	struct stat st;
	if (stat(target, &st)) {
		r->intVal = errno;
		return;
	{
		char sep = '\\';
		char *link_dir = strrchr(linkpath, '\\');
		if (link_dir == NULL) {
			link_dir = strrchr(linkpath, '/');
			sep = '/';
		}

		if (link_dir == NULL)
			link_dir = linkpath;

		size_t link_start_len = link_dir - linkpath;
		char *target_rel = malloc(link_start_len + 1 + strlen(target) + 1);
		if (target_rel == NULL) {
			r->intVal = ENOMEM;
			return;
		}

		memcpy(target_rel, linkpath, link_start_len);
		target_rel[link_start_len] = sep;
		memcpy(target_rel + link_start_len + 1, target, strlen(target) + 1);

		if (stat(target_rel, &st)) {
			r->intVal = errno;
			return;
		}

		free(target_rel);
	}

	DWORD flag = 0;
	DWORD flag = 0x2;
	if (S_ISDIR(st.st_mode))
		flag |= 0x1;
	r->intVal = CreateSymbolicLinkA(linkpath, target, flag) ? 0 : EAGAIN;

M os.um => os.um +3 -0
@@ 233,5 233,8 @@ fn main() {
        }
        
        mkdirp("test/one/two/three")
        if errno := link("../../two", "test/one/two/three/four"); errno != 0 {
                printf("link failed: %v\n", strerror(errno))
        }
        chmod("test", 0744)
}
\ No newline at end of file