f = CreateFile(namebuf, GENERIC_WRITE, 0, NULL,
f = CreateFile(destfname, GENERIC_WRITE, 0, NULL,
static void sink(char *targ, char *src){    char buf[2048];    char namebuf[2048];    char ch;    int targisdir = 0;    int settime;    int exists;    DWORD attr;    HANDLE f;    unsigned long received;    int wrerror = 0;    unsigned long stat_bytes;    time_t stat_starttime, stat_lasttime;    char *stat_name;    attr = GetFileAttributes(targ);    if (attr != (DWORD) - 1 && (attr & FILE_ATTRIBUTE_DIRECTORY) != 0)	targisdir = 1;    if (targetshouldbedirectory && !targisdir)	bump("%s: Not a directory", targ);    scp_sink_init();    while (1) {	struct scp_sink_action act;	if (scp_get_sink_action(&act))	    return;	if (act.action == SCP_SINK_ENDDIR)	    return;	/* Security fix: ensure the file ends up where we asked for it. */	if (targisdir) {	    char t[2048];	    char *p;	    strcpy(t, targ);	    if (targ[0] != '\0')		strcat(t, "/");	    p = act.name + strlen(act.name);	    while (p > act.name && p[-1] != '/' && p[-1] != '\\')		p--;	    strcat(t, p);	    strcpy(namebuf, t);	} else {	    strcpy(namebuf, targ);	}	attr = GetFileAttributes(namebuf);	exists = (attr != (DWORD) - 1);	if (act.action == SCP_SINK_DIR) {	    if (exists && (attr & FILE_ATTRIBUTE_DIRECTORY) == 0) {		run_err("%s: Not a directory", namebuf);		continue;	    }	    if (!exists) {		if (!CreateDirectory(namebuf, NULL)) {		    run_err("%s: Cannot create directory", namebuf);		    continue;		}	    }	    sink(namebuf, NULL);	    /* can we set the timestamp for directories ? */	    continue;	}	f = CreateFile(namebuf, GENERIC_WRITE, 0, NULL,		       CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);	if (f == INVALID_HANDLE_VALUE) {	    run_err("%s: Cannot create file", namebuf);	    continue;	}	if (scp_accept_filexfer())	    return;	stat_bytes = 0;	stat_starttime = time(NULL);	stat_lasttime = 0;	if ((stat_name = strrchr(namebuf, '/')) == NULL)	    stat_name = namebuf;	else	    stat_name++;	if (strrchr(stat_name, '\\') != NULL)	    stat_name = strrchr(stat_name, '\\') + 1;	received = 0;	while (received < act.size) {	    char transbuf[4096];	    DWORD blksize, read, written;	    blksize = 4096;	    if (blksize > act.size - received)		blksize = act.size - received;	    read = scp_recv_filedata(transbuf, blksize);	    if (read <= 0)		bump("Lost connection");	    if (wrerror)		continue;	    if (!WriteFile(f, transbuf, read, &written, NULL) ||		written != read) {		wrerror = 1;		/* FIXME: in sftp we can actually abort the transfer */		if (statistics)		    printf("\r%-25.25s | %50s\n",			   stat_name,			   "Write error.. waiting for end of file");		continue;	    }	    if (statistics) {		stat_bytes += read;		if (time(NULL) > stat_lasttime ||		    received + read == act.size) {		    stat_lasttime = time(NULL);		    print_stats(stat_name, act.size, stat_bytes,				stat_starttime, stat_lasttime);		}	    }	    received += read;	}	if (act.settime) {	    FILETIME actime, wrtime;	    TIME_POSIX_TO_WIN(act.atime, actime);	    TIME_POSIX_TO_WIN(act.mtime, wrtime);	    SetFileTime(f, NULL, &actime, &wrtime);	}	CloseHandle(f);	if (wrerror) {	    run_err("%s: Write error", namebuf);	    continue;	}	(void) scp_finish_filerecv();    }}