Example #1
0
void SFTP::process_write(void)
{
	u_int32_t id;
	u_int64_t off;
	u_int len;
	int handle, status = SSH2_FX_FAILURE;
	char *data;

	id = get_int();
	handle = get_handle();
	off = get_int64();
	data = (char*) get_string(&len);

#ifdef SLOW_DEBUG
	debug("request %u: write \"%s\" (handle %d) off %llu len %d",
	    id, 
      toUTF8 (handle_to_name(handle)).c_str (), 
      handle, (unsigned long long)off, len);
#endif
	HANDLE fh = handle_to_fh(handle);
	if (fh != INVALID_HANDLE_VALUE) {
    LARGE_INTEGER largeOffset;
    largeOffset.QuadPart = off;
		if (!::SetFilePointerEx 
         (fh, largeOffset, NULL, FILE_BEGIN)
        ) 
    {
			status = errno_to_portable(::GetLastError ());
			error("process_write: seek failed");
		} 
    else 
    {
/* XXX ATOMICIO ? */
			DWORD nBytesWritten;
      if (!::WriteFile (fh, data, len, &nBytesWritten, 0)) 
      {
				error("process_write: write failed");
        int err = ::GetLastError ();
        if (err != 0)
				  status = errno_to_portable(err);
        else 
          status = SSH2_FX_FAILURE; //TODO reason?
			} 
      else if ((size_t) nBytesWritten == len) 
      {
				status = SSH2_FX_OK;
				handle_update_write(handle, (ssize_t) nBytesWritten);
			} 
      else 
      {
				debug2("nothing at all written");
			}
		}
	}
	send_status(id, status);
	xfree(data);
}
static void
process_write(void)
{
	u_int32_t id;
	u_int64_t off;
	u_int len;
	int handle, fd, ret, status;
	char *data;

	id = get_int();
	handle = get_handle();
	off = get_int64();
	data = get_string(&len);

	debug("request %u: write \"%s\" (handle %d) off %llu len %d",
	    id, handle_to_name(handle), handle, (unsigned long long)off, len);
	fd = handle_to_fd(handle);
	
	if (fd < 0)
		status = SSH2_FX_FAILURE;
	else if (readonly)
		status = SSH2_FX_PERMISSION_DENIED;
	else {
		if (lseek(fd, off, SEEK_SET) < 0) {
			status = errno_to_portable(errno);
			error("process_write: seek failed");
		} else {
/* XXX ATOMICIO ? */
			ret = write(fd, data, len);
			if (ret < 0) {
				error("process_write: write failed");
				status = errno_to_portable(errno);
			} else if ((size_t)ret == len) {
				status = SSH2_FX_OK;
				handle_update_write(handle, ret);
			} else {
				debug2("nothing at all written");
				status = SSH2_FX_FAILURE;
			}
		}
	}
	send_status(id, status);
	xfree(data);
}
Example #3
0
static void
process_write(u_int32_t id)
{
	u_int64_t off;
	size_t len;
	int r, handle, fd, ret, status;
	u_char *data;

	if ((r = get_handle(iqueue, &handle)) != 0 ||
	    (r = sshbuf_get_u64(iqueue, &off)) != 0 ||
	    (r = sshbuf_get_string(iqueue, &data, &len)) != 0)
		fatal("%s: buffer error: %s", __func__, ssh_err(r));

	debug("request %u: write \"%s\" (handle %d) off %llu len %zu",
	    id, handle_to_name(handle), handle, (unsigned long long)off, len);
	fd = handle_to_fd(handle);

	if (fd < 0)
		status = SSH2_FX_FAILURE;
	else {
		if (!(handle_to_flags(handle) & O_APPEND) &&
				lseek(fd, off, SEEK_SET) < 0) {
			status = errno_to_portable(errno);
			error("process_write: seek failed");
		} else {
/* XXX ATOMICIO ? */
			ret = write(fd, data, len);
			if (ret < 0) {
				error("process_write: write failed");
				status = errno_to_portable(errno);
			} else if ((size_t)ret == len) {
				status = SSH2_FX_OK;
				handle_update_write(handle, ret);
			} else {
				debug2("nothing at all written");
				status = SSH2_FX_FAILURE;
			}
		}
	}
	send_status(id, status);
	free(data);
}