void guac_rdpdr_fs_process_set_rename_info()

in src/protocols/rdp/channels/rdpdr/rdpdr-fs-messages-file-info.c [129:203]


void guac_rdpdr_fs_process_set_rename_info(guac_rdp_common_svc* svc,
        guac_rdpdr_device* device, guac_rdpdr_iorequest* iorequest,
        int length, wStream* input_stream) {

    int result;
    int filename_length;
    wStream* output_stream;
    char destination_path[GUAC_RDP_FS_MAX_PATH];

    /* Check stream size prior to reading. */
    if (Stream_GetRemainingLength(input_stream) < 6) {
        guac_client_log(svc->client, GUAC_LOG_WARNING, "Server Drive Set "
                "Information Request (FileRenameInformation) PDU does not "
                "contain the expected number of bytes.  File redirection "
                "may not work as expected.");
        return;
    }
        
    /* Read structure */
    Stream_Seek_UINT8(input_stream); /* ReplaceIfExists */
    Stream_Seek_UINT8(input_stream); /* RootDirectory */
    Stream_Read_UINT32(input_stream, filename_length); /* FileNameLength */

    if (Stream_GetRemainingLength(input_stream) < filename_length) {
        guac_client_log(svc->client, GUAC_LOG_WARNING, "Server Drive Set "
                "Information Request (FileRenameInformation) PDU does not "
                "contain the expected number of bytes.  File redirection "
                "may not work as expected.");
        return;
    }
    
    /* Convert name to UTF-8 */
    guac_rdp_utf16_to_utf8(Stream_Pointer(input_stream), filename_length/2,
            destination_path, sizeof(destination_path));

    guac_client_log(svc->client, GUAC_LOG_DEBUG, "%s: [file_id=%i]"
            "destination_path=\"%s\"", __func__, iorequest->file_id,
            destination_path);

    /* If file moving to \Download folder, start stream, do not move */
    if (strncmp(destination_path, "\\Download\\", 10) == 0
			&& !((guac_rdp_fs*) device->data)->disable_download) {

        guac_rdp_fs_file* file;

        /* Get file */
        file = guac_rdp_fs_get_file((guac_rdp_fs*) device->data, iorequest->file_id);
        if (file == NULL)
            return;

        /* Initiate download, pretend move succeeded */
        guac_client_for_owner(svc->client, guac_rdp_download_to_user, file->absolute_path);
        output_stream = guac_rdpdr_new_io_completion(device,
                iorequest->completion_id, STATUS_SUCCESS, 4);

    }

    /* Otherwise, rename as requested */
    else {

        result = guac_rdp_fs_rename((guac_rdp_fs*) device->data,
                iorequest->file_id, destination_path);
        if (result < 0)
            output_stream = guac_rdpdr_new_io_completion(device,
                    iorequest->completion_id, guac_rdp_fs_get_status(result), 4);
        else
            output_stream = guac_rdpdr_new_io_completion(device,
                    iorequest->completion_id, STATUS_SUCCESS, 4);

    }

    Stream_Write_UINT32(output_stream, length);
    guac_rdp_common_svc_write(svc, output_stream);

}