24#include <freerdp/config.h>
30#define FUSE_USE_VERSION 30
31#include <fuse_lowlevel.h>
42#include <winpr/string.h>
43#include <winpr/assert.h>
44#include <winpr/interlocked.h>
45#include <winpr/image.h>
46#include <winpr/stream.h>
47#include <winpr/clipboard.h>
48#include <winpr/path.h>
50#include <freerdp/utils/helpers.h>
51#include <freerdp/utils/signal.h>
52#include <freerdp/log.h>
53#include <freerdp/client/cliprdr.h>
54#include <freerdp/channels/channels.h>
55#include <freerdp/channels/cliprdr.h>
57#include <freerdp/client/client_cliprdr_file.h>
59#if defined(WITH_CLIPRDR_FILE_APPLE)
62#include <winpr/file.h>
64#include <freerdp/freerdp.h>
65#include <freerdp/settings.h>
66#include <freerdp/utils/cliprdr_utils.h>
68#include "client_cliprdr_file_apple.h"
71#define NO_CLIP_DATA_ID (UINT64_C(1) << 32)
72#define WIN32_FILETIME_TO_UNIX_EPOCH INT64_C(11644473600)
74#ifdef WITH_DEBUG_CLIPRDR
75#define DEBUG_CLIPRDR(log, ...) WLog_Print(log, WLOG_DEBUG, __VA_ARGS__)
77#define DEBUG_CLIPRDR(log, ...) \
84typedef enum eFuseLowlevelOperationType
86 FUSE_LL_OPERATION_NONE,
87 FUSE_LL_OPERATION_LOOKUP,
88 FUSE_LL_OPERATION_GETATTR,
89 FUSE_LL_OPERATION_READ,
90} FuseLowlevelOperationType;
92typedef struct sCliprdrFuseFile CliprdrFuseFile;
94struct sCliprdrFuseFile
96 CliprdrFuseFile* parent;
100 const char* filename;
102 size_t filename_with_root_len;
103 char* filename_with_root;
113 BOOL has_last_write_time;
114 INT64 last_write_time_unix;
116 BOOL has_clip_data_id;
122 CliprdrFileContext* file_context;
124 CliprdrFuseFile* clip_data_dir;
126 BOOL has_clip_data_id;
128} CliprdrFuseClipDataEntry;
132 CliprdrFileContext* file_context;
134 wArrayList* fuse_files;
137 BOOL has_clip_data_id;
139} FuseFileClearContext;
143 FuseLowlevelOperationType operation_type;
144 CliprdrFuseFile* fuse_file;
155 CliprdrFileContext* context;
163 CliprdrLocalFile* files;
164 CliprdrFileContext* context;
167struct cliprdr_file_context
169#if defined(WITH_FUSE)
171 HANDLE fuse_start_sync;
172 HANDLE fuse_stop_sync;
175 struct fuse_session* fuse_sess;
176#if FUSE_USE_VERSION < 30
177 struct fuse_chan* ch;
180 wHashTable* inode_table;
181 wHashTable* dir_table;
182 wHashTable* clip_data_table;
183 wHashTable* request_table;
185 CliprdrFuseClipDataEntry* clip_data_entry_without_id;
186 UINT32 current_clip_data_id;
189 UINT32 next_clip_data_id;
190 UINT32 next_stream_id;
193#if defined(WITH_CLIPRDR_FILE_APPLE)
196 CRITICAL_SECTION apple_lock;
197 BOOL apple_cancelled;
198 UINT32 apple_generation;
199 HANDLE apple_contents_event;
200 UINT32 apple_stream_id;
201 BOOL apple_contents_success;
202 BYTE* apple_contents_data;
203 UINT32 apple_contents_size;
207 BOOL file_formats_registered;
208 UINT32 file_capability_flags;
210 UINT32 local_lock_id;
212 wHashTable* local_streams;
215 CliprdrClientContext* context;
218 BYTE server_data_hash[WINPR_SHA256_DIGEST_LENGTH];
219 BYTE client_data_hash[WINPR_SHA256_DIGEST_LENGTH];
222#if defined(WITH_FUSE)
223static CliprdrFuseFile* get_fuse_file_by_ino(CliprdrFileContext* file_context, fuse_ino_t fuse_ino);
225static void fuse_file_free(
void* data)
227 CliprdrFuseFile* fuse_file = data;
232 ArrayList_Free(fuse_file->children);
233 free(fuse_file->filename_with_root);
238WINPR_ATTR_FORMAT_ARG(1, 2)
239WINPR_ATTR_MALLOC(fuse_file_free, 1)
241static CliprdrFuseFile* fuse_file_new(WINPR_FORMAT_ARG const
char* fmt, ...)
243 CliprdrFuseFile* file = calloc(1,
sizeof(CliprdrFuseFile));
247 file->children = ArrayList_New(FALSE);
254 va_list ap = WINPR_C_ARRAY_INIT;
257 winpr_vasprintf(&file->filename_with_root, &file->filename_with_root_len, fmt, ap);
264 if (file->filename_with_root && (file->filename_with_root_len > 0))
266 file->filename_len = 0;
267 file->filename = strrchr(file->filename_with_root,
'/') + 1;
269 file->filename_len = strnlen(file->filename, file->filename_with_root_len);
273 fuse_file_free(file);
277static void clip_data_entry_free(
void* data)
279 CliprdrFuseClipDataEntry* clip_data_entry = data;
281 if (!clip_data_entry)
284 if (clip_data_entry->has_clip_data_id)
286 CliprdrFileContext* file_context = clip_data_entry->file_context;
289 WINPR_ASSERT(file_context);
291 unlock_clipboard_data.common.msgType = CB_UNLOCK_CLIPDATA;
292 unlock_clipboard_data.clipDataId = clip_data_entry->clip_data_id;
294 const UINT rc = file_context->context->ClientUnlockClipboardData(file_context->context,
295 &unlock_clipboard_data);
296 if (rc != CHANNEL_RC_OK)
297 WLog_Print(file_context->log, WLOG_DEBUG,
298 "ClientUnlockClipboardData failed with %" PRIu32, rc);
300 clip_data_entry->has_clip_data_id = FALSE;
302 WLog_Print(file_context->log, WLOG_DEBUG,
"Destroyed ClipDataEntry with id %u",
303 clip_data_entry->clip_data_id);
306 free(clip_data_entry);
309static BOOL does_server_support_clipdata_locking(CliprdrFileContext* file_context)
311 WINPR_ASSERT(file_context);
313 return (cliprdr_file_context_remote_get_flags(file_context) & CB_CAN_LOCK_CLIPDATA) != 0;
316static UINT32 get_next_free_clip_data_id(CliprdrFileContext* file_context)
318 UINT32 clip_data_id = 0;
320 WINPR_ASSERT(file_context);
322 HashTable_Lock(file_context->inode_table);
323 clip_data_id = file_context->next_clip_data_id;
324 while (clip_data_id == 0 ||
325 HashTable_GetItemValue(file_context->clip_data_table, (
void*)(uintptr_t)clip_data_id))
328 file_context->next_clip_data_id = clip_data_id + 1;
329 HashTable_Unlock(file_context->inode_table);
334static CliprdrFuseClipDataEntry* clip_data_entry_new(CliprdrFileContext* file_context,
335 BOOL needs_clip_data_id)
337 CliprdrFuseClipDataEntry* clip_data_entry =
nullptr;
340 WINPR_ASSERT(file_context);
342 clip_data_entry = calloc(1,
sizeof(CliprdrFuseClipDataEntry));
343 if (!clip_data_entry)
346 clip_data_entry->file_context = file_context;
347 clip_data_entry->clip_data_id = get_next_free_clip_data_id(file_context);
349 if (!needs_clip_data_id)
350 return clip_data_entry;
352 lock_clipboard_data.common.msgType = CB_LOCK_CLIPDATA;
353 lock_clipboard_data.clipDataId = clip_data_entry->clip_data_id;
355 if (file_context->context->ClientLockClipboardData(file_context->context, &lock_clipboard_data))
357 HashTable_Lock(file_context->inode_table);
358 clip_data_entry_free(clip_data_entry);
359 HashTable_Unlock(file_context->inode_table);
362 clip_data_entry->has_clip_data_id = TRUE;
364 WLog_Print(file_context->log, WLOG_DEBUG,
"Created ClipDataEntry with id %u",
365 clip_data_entry->clip_data_id);
367 return clip_data_entry;
370static BOOL should_remove_fuse_file(CliprdrFuseFile* fuse_file, BOOL all_files,
371 BOOL has_clip_data_id, UINT32 clip_data_id)
376 if (fuse_file->ino == FUSE_ROOT_ID)
378 if (!fuse_file->has_clip_data_id && !has_clip_data_id)
380 if (fuse_file->has_clip_data_id && has_clip_data_id &&
381 (fuse_file->clip_data_id == clip_data_id))
387static BOOL maybe_clear_fuse_request(
const void* key,
void* value,
void* arg)
389 CliprdrFuseRequest* fuse_request = value;
390 FuseFileClearContext* clear_context = arg;
391 CliprdrFileContext* file_context = clear_context->file_context;
392 CliprdrFuseFile* fuse_file = fuse_request->fuse_file;
394 WINPR_ASSERT(file_context);
396 if (!should_remove_fuse_file(fuse_file, clear_context->all_files,
397 clear_context->has_clip_data_id, clear_context->clip_data_id))
400 DEBUG_CLIPRDR(file_context->log,
"Clearing FileContentsRequest for file \"%s\"",
401 fuse_file->filename_with_root);
403 fuse_reply_err(fuse_request->fuse_req, EIO);
404 HashTable_Remove(file_context->request_table, key);
409static BOOL maybe_steal_inode(
const void* key,
void* value,
void* arg)
411 CliprdrFuseFile* fuse_file = value;
412 FuseFileClearContext* clear_context = arg;
413 CliprdrFileContext* file_context = clear_context->file_context;
415 WINPR_ASSERT(file_context);
417 if (should_remove_fuse_file(fuse_file, clear_context->all_files,
418 clear_context->has_clip_data_id, clear_context->clip_data_id))
420 if (!ArrayList_Append(clear_context->fuse_files, fuse_file))
421 WLog_Print(file_context->log, WLOG_ERROR,
422 "Failed to append FUSE file to list for deletion");
424 if (!clear_context->all_files && fuse_file->is_directory)
426 HashTable_Remove(file_context->dir_table, fuse_file->filename_with_root);
428 HashTable_Remove(file_context->inode_table, key);
434static BOOL notify_delete_child(
void* data, WINPR_ATTR_UNUSED
size_t index, va_list ap)
436 CliprdrFuseFile* child = data;
440 CliprdrFileContext* file_context = va_arg(ap, CliprdrFileContext*);
441 CliprdrFuseFile* parent = va_arg(ap, CliprdrFuseFile*);
443 WINPR_ASSERT(file_context);
444 WINPR_ASSERT(parent);
446 WINPR_ASSERT(file_context->fuse_sess);
447 fuse_lowlevel_notify_delete(file_context->fuse_sess, parent->ino, child->ino, child->filename,
448 child->filename_len);
453static BOOL invalidate_inode(
void* data, WINPR_ATTR_UNUSED
size_t index, va_list ap)
455 CliprdrFuseFile* fuse_file = data;
457 WINPR_ASSERT(fuse_file);
459 CliprdrFileContext* file_context = va_arg(ap, CliprdrFileContext*);
460 WINPR_ASSERT(file_context);
461 WINPR_ASSERT(file_context->fuse_sess);
464 ArrayList_ForEach(fuse_file->children, notify_delete_child, file_context, fuse_file);
466 DEBUG_CLIPRDR(file_context->log,
"Invalidating inode %lu for file \"%s\"", fuse_file->ino,
467 fuse_file->filename);
468 fuse_lowlevel_notify_inval_inode(file_context->fuse_sess, fuse_file->ino, 0, 0);
469 WLog_Print(file_context->log, WLOG_DEBUG,
"Inode %lu invalidated", fuse_file->ino);
475static bool clear_selection(CliprdrFileContext* file_context, BOOL all_selections,
476 CliprdrFuseClipDataEntry* clip_data_entry)
479 FuseFileClearContext clear_context = WINPR_C_ARRAY_INIT;
480 CliprdrFuseFile* clip_data_dir =
nullptr;
482 WINPR_ASSERT(file_context);
484 clear_context.file_context = file_context;
485 clear_context.fuse_files = ArrayList_New(FALSE);
486 WINPR_ASSERT(clear_context.fuse_files);
488 wObject* aobj = ArrayList_Object(clear_context.fuse_files);
494 clip_data_dir = clip_data_entry->clip_data_dir;
495 clip_data_entry->clip_data_dir =
nullptr;
497 WINPR_ASSERT(clip_data_dir);
499 CliprdrFuseFile* root_dir = get_fuse_file_by_ino(file_context, FUSE_ROOT_ID);
501 ArrayList_Remove(root_dir->children, clip_data_dir);
503 clear_context.has_clip_data_id = clip_data_dir->has_clip_data_id;
504 clear_context.clip_data_id = clip_data_dir->clip_data_id;
506 clear_context.all_files = all_selections;
508 if (clip_data_entry && clip_data_entry->has_clip_data_id)
509 WLog_Print(file_context->log, WLOG_DEBUG,
"Clearing selection for clipDataId %u",
510 clip_data_entry->clip_data_id);
512 WLog_Print(file_context->log, WLOG_DEBUG,
"Clearing selection%s",
513 all_selections ?
"s" :
"");
515 if (!HashTable_Foreach(file_context->request_table, maybe_clear_fuse_request, &clear_context))
519 HashTable_Clear(file_context->dir_table);
521 if (!HashTable_Foreach(file_context->inode_table, maybe_steal_inode, &clear_context))
523 HashTable_Unlock(file_context->inode_table);
525 if (file_context->fuse_sess)
535 if (!ArrayList_ForEach(clear_context.fuse_files, invalidate_inode, file_context))
537 CliprdrFuseFile* root_dir = get_fuse_file_by_ino(file_context, FUSE_ROOT_ID);
538 if (clip_data_dir && root_dir)
540 fuse_lowlevel_notify_delete(file_context->fuse_sess, root_dir->ino, clip_data_dir->ino,
541 clip_data_dir->filename, clip_data_dir->filename_len);
544 ArrayList_Free(clear_context.fuse_files);
546 HashTable_Lock(file_context->inode_table);
547 if (clip_data_entry && clip_data_entry->has_clip_data_id)
548 WLog_Print(file_context->log, WLOG_DEBUG,
"Selection cleared for clipDataId %u",
549 clip_data_entry->clip_data_id);
551 WLog_Print(file_context->log, WLOG_DEBUG,
"Selection%s cleared", all_selections ?
"s" :
"");
556static bool clear_entry_selection(CliprdrFuseClipDataEntry* clip_data_entry)
558 WINPR_ASSERT(clip_data_entry);
560 if (!clip_data_entry->clip_data_dir)
563 return clear_selection(clip_data_entry->file_context, FALSE, clip_data_entry);
567static bool clear_no_cdi_entry(CliprdrFileContext* file_context)
570 WINPR_ASSERT(file_context);
572 WINPR_ASSERT(file_context->inode_table);
573 HashTable_Lock(file_context->inode_table);
574 if (file_context->clip_data_entry_without_id)
576 res = clear_entry_selection(file_context->clip_data_entry_without_id);
578 clip_data_entry_free(file_context->clip_data_entry_without_id);
579 file_context->clip_data_entry_without_id =
nullptr;
581 HashTable_Unlock(file_context->inode_table);
586static BOOL clear_clip_data_entries(WINPR_ATTR_UNUSED
const void* key,
void* value,
587 WINPR_ATTR_UNUSED
void* arg)
589 return clear_entry_selection(value);
593static UINT clear_cdi_entries(CliprdrFileContext* file_context)
595 UINT res = CHANNEL_RC_OK;
596 WINPR_ASSERT(file_context);
598 HashTable_Lock(file_context->inode_table);
599 if (!HashTable_Foreach(file_context->clip_data_table, clear_clip_data_entries,
nullptr))
600 res = ERROR_INTERNAL_ERROR;
602 HashTable_Clear(file_context->clip_data_table);
603 HashTable_Unlock(file_context->inode_table);
608static UINT prepare_clip_data_entry_with_id(CliprdrFileContext* file_context)
610 CliprdrFuseClipDataEntry* clip_data_entry =
nullptr;
612 WINPR_ASSERT(file_context);
614 clip_data_entry = clip_data_entry_new(file_context, TRUE);
615 if (!clip_data_entry)
617 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to create clipDataEntry");
618 return ERROR_INTERNAL_ERROR;
621 HashTable_Lock(file_context->inode_table);
622 if (!HashTable_Insert(file_context->clip_data_table,
623 (
void*)(uintptr_t)clip_data_entry->clip_data_id, clip_data_entry))
625 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to insert clipDataEntry");
626 clip_data_entry_free(clip_data_entry);
627 HashTable_Unlock(file_context->inode_table);
628 return ERROR_INTERNAL_ERROR;
630 HashTable_Unlock(file_context->inode_table);
634 file_context->current_clip_data_id = clip_data_entry->clip_data_id;
636 return CHANNEL_RC_OK;
639static UINT prepare_clip_data_entry_without_id(CliprdrFileContext* file_context)
641 WINPR_ASSERT(file_context);
642 WINPR_ASSERT(!file_context->clip_data_entry_without_id);
644 file_context->clip_data_entry_without_id = clip_data_entry_new(file_context, FALSE);
645 if (!file_context->clip_data_entry_without_id)
647 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to create clipDataEntry");
648 return ERROR_INTERNAL_ERROR;
651 return CHANNEL_RC_OK;
655#if defined(WITH_CLIPRDR_FILE_APPLE)
659static void apple_new_generation(CliprdrFileContext* file)
663 EnterCriticalSection(&file->apple_lock);
664 file->apple_generation++;
665 (void)SetEvent(file->apple_contents_event);
666 LeaveCriticalSection(&file->apple_lock);
670UINT cliprdr_file_context_notify_new_server_format_list(CliprdrFileContext* file_context)
672 UINT rc = CHANNEL_RC_OK;
673 WINPR_ASSERT(file_context);
674 WINPR_ASSERT(file_context->context);
676#if defined(WITH_FUSE)
677 if (!clear_no_cdi_entry(file_context))
678 return ERROR_INTERNAL_ERROR;
680 rc = clear_cdi_entries(file_context);
681 if (rc != CHANNEL_RC_OK)
684 if (does_server_support_clipdata_locking(file_context))
685 rc = prepare_clip_data_entry_with_id(file_context);
687 rc = prepare_clip_data_entry_without_id(file_context);
689#if defined(WITH_CLIPRDR_FILE_APPLE)
690 apple_new_generation(file_context);
695UINT cliprdr_file_context_notify_new_client_format_list(CliprdrFileContext* file_context)
697 WINPR_ASSERT(file_context);
698 WINPR_ASSERT(file_context->context);
700#if defined(WITH_CLIPRDR_FILE_APPLE)
701 apple_new_generation(file_context);
703#if defined(WITH_FUSE)
704 if (!clear_no_cdi_entry(file_context))
705 return ERROR_INTERNAL_ERROR;
707 return clear_cdi_entries(file_context);
710 return CHANNEL_RC_OK;
713static CliprdrLocalStream* cliprdr_local_stream_new(CliprdrFileContext* context, UINT32 streamID,
714 const char* data,
size_t size);
715static void cliprdr_file_session_terminate(CliprdrFileContext* file, BOOL stop_thread);
716static BOOL local_stream_discard(
const void* key,
void* value,
void* arg);
718WINPR_ATTR_FORMAT_ARG(6, 7)
719static
void writelog(wLog* log, DWORD level, const
char* fname, const
char* fkt,
size_t line,
720 WINPR_FORMAT_ARG const
char* fmt, ...)
722 if (!WLog_IsLevelActive(log, level))
725 va_list ap = WINPR_C_ARRAY_INIT;
727 WLog_PrintTextMessageVA(log, level, line, fname, fkt, fmt, ap);
731#if defined(WITH_FUSE)
732static CliprdrFuseFile* fuse_file_new_root(CliprdrFileContext* file_context);
733static void cliprdr_file_fuse_lookup(fuse_req_t req, fuse_ino_t parent,
const char* name);
734static void cliprdr_file_fuse_getattr(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info* fi);
735static void cliprdr_file_fuse_readdir(fuse_req_t req, fuse_ino_t ino,
size_t size, off_t off,
736 struct fuse_file_info* fi);
737static void cliprdr_file_fuse_read(fuse_req_t req, fuse_ino_t ino,
size_t size, off_t off,
738 struct fuse_file_info* fi);
739static void cliprdr_file_fuse_open(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info* fi);
740static void cliprdr_file_fuse_opendir(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info* fi);
742static const struct fuse_lowlevel_ops cliprdr_file_fuse_oper = {
743 .lookup = cliprdr_file_fuse_lookup,
744 .getattr = cliprdr_file_fuse_getattr,
745 .readdir = cliprdr_file_fuse_readdir,
746 .open = cliprdr_file_fuse_open,
747 .read = cliprdr_file_fuse_read,
748 .opendir = cliprdr_file_fuse_opendir,
751CliprdrFuseFile* get_fuse_file_by_ino(CliprdrFileContext* file_context, fuse_ino_t fuse_ino)
753 WINPR_ASSERT(file_context);
755 return HashTable_GetItemValue(file_context->inode_table, (
void*)(uintptr_t)fuse_ino);
758static CliprdrFuseFile*
759get_fuse_file_by_name_from_parent(WINPR_ATTR_UNUSED CliprdrFileContext* file_context,
760 CliprdrFuseFile* parent,
const char* name)
762 WINPR_ASSERT(file_context);
763 WINPR_ASSERT(parent);
765 for (
size_t i = 0; i < ArrayList_Count(parent->children); ++i)
767 CliprdrFuseFile* child = ArrayList_GetItem(parent->children, i);
771 if (strncmp(name, child->filename, child->filename_len + 1) == 0)
775 DEBUG_CLIPRDR(file_context->log,
"Requested file \"%s\" in directory \"%s\" does not exist",
776 name, parent->filename);
781static CliprdrFuseRequest* cliprdr_fuse_request_new(CliprdrFileContext* file_context,
782 CliprdrFuseFile* fuse_file, fuse_req_t fuse_req,
783 FuseLowlevelOperationType operation_type)
785 CliprdrFuseRequest* fuse_request =
nullptr;
786 UINT32 stream_id = file_context->next_stream_id;
788 WINPR_ASSERT(file_context);
789 WINPR_ASSERT(fuse_file);
791 fuse_request = calloc(1,
sizeof(CliprdrFuseRequest));
794 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to allocate FUSE request for file \"%s\"",
795 fuse_file->filename_with_root);
799 fuse_request->fuse_file = fuse_file;
800 fuse_request->fuse_req = fuse_req;
801 fuse_request->operation_type = operation_type;
803 while (stream_id == 0 ||
804 HashTable_GetItemValue(file_context->request_table, (
void*)(uintptr_t)stream_id))
806 fuse_request->stream_id = stream_id;
808 file_context->next_stream_id = stream_id + 1;
810 if (!HashTable_Insert(file_context->request_table, (
void*)(uintptr_t)fuse_request->stream_id,
813 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to track FUSE request for file \"%s\"",
814 fuse_file->filename_with_root);
822static BOOL request_file_size_async(CliprdrFileContext* file_context, CliprdrFuseFile* fuse_file,
823 fuse_req_t fuse_req, FuseLowlevelOperationType operation_type)
825 CliprdrFuseRequest* fuse_request =
nullptr;
828 WINPR_ASSERT(file_context);
829 WINPR_ASSERT(fuse_file);
831 fuse_request = cliprdr_fuse_request_new(file_context, fuse_file, fuse_req, operation_type);
835 file_contents_request.common.msgType = CB_FILECONTENTS_REQUEST;
836 file_contents_request.streamId = fuse_request->stream_id;
837 file_contents_request.listIndex = fuse_file->list_idx;
838 file_contents_request.dwFlags = FILECONTENTS_SIZE;
839 file_contents_request.cbRequested = 0x8;
840 file_contents_request.haveClipDataId = fuse_file->has_clip_data_id;
841 file_contents_request.clipDataId = fuse_file->clip_data_id;
843 if (file_context->context->ClientFileContentsRequest(file_context->context,
844 &file_contents_request))
846 WLog_Print(file_context->log, WLOG_ERROR,
847 "Failed to send FileContentsRequest for file \"%s\"",
848 fuse_file->filename_with_root);
849 HashTable_Remove(file_context->request_table, (
void*)(uintptr_t)fuse_request->stream_id);
853 DEBUG_CLIPRDR(file_context->log,
"Requested file size for file \"%s\" with stream id %u",
854 fuse_file->filename, fuse_request->stream_id);
861static BOOL write_file_attributes(CliprdrFuseFile* fuse_file,
struct stat* attr)
863 memset(attr, 0,
sizeof(
struct stat));
865 if (!fuse_file || (fuse_file->size > INT64_MAX))
868 attr->st_ino = fuse_file->ino;
869 if (fuse_file->is_directory)
871 attr->st_mode = S_IFDIR | (fuse_file->is_readonly ? 0555 : 0755);
876 attr->st_mode = S_IFREG | (fuse_file->is_readonly ? 0444 : 0644);
878 attr->st_size = WINPR_ASSERTING_INT_CAST(off_t, fuse_file->size);
880 attr->st_uid = getuid();
881 attr->st_gid = getgid();
882 attr->st_atime = attr->st_mtime = attr->st_ctime =
883 (fuse_file->has_last_write_time ? fuse_file->last_write_time_unix : time(
nullptr));
887static void cliprdr_file_fuse_lookup(fuse_req_t fuse_req, fuse_ino_t parent_ino,
const char* name)
889 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
890 CliprdrFuseFile* parent =
nullptr;
891 CliprdrFuseFile* fuse_file =
nullptr;
892 struct fuse_entry_param entry = WINPR_C_ARRAY_INIT;
894 WINPR_ASSERT(file_context);
896 HashTable_Lock(file_context->inode_table);
897 if (!(parent = get_fuse_file_by_ino(file_context, parent_ino)) || !parent->is_directory)
899 HashTable_Unlock(file_context->inode_table);
900 fuse_reply_err(fuse_req, ENOENT);
903 if (!(fuse_file = get_fuse_file_by_name_from_parent(file_context, parent, name)))
905 HashTable_Unlock(file_context->inode_table);
906 fuse_reply_err(fuse_req, ENOENT);
910 DEBUG_CLIPRDR(file_context->log,
"lookup() has been called for \"%s\"", name);
911 DEBUG_CLIPRDR(file_context->log,
"Parent is \"%s\", child is \"%s\"",
912 parent->filename_with_root, fuse_file->filename_with_root);
914 if (!fuse_file->is_directory && !fuse_file->has_size)
919 request_file_size_async(file_context, fuse_file, fuse_req, FUSE_LL_OPERATION_LOOKUP);
920 HashTable_Unlock(file_context->inode_table);
923 fuse_reply_err(fuse_req, EIO);
928 entry.ino = fuse_file->ino;
929 if (!write_file_attributes(fuse_file, &entry.attr))
931 HashTable_Unlock(file_context->inode_table);
932 fuse_reply_err(fuse_req, ERANGE);
935 entry.attr_timeout = 1.0;
936 entry.entry_timeout = 1.0;
937 HashTable_Unlock(file_context->inode_table);
939 fuse_reply_entry(fuse_req, &entry);
942static void cliprdr_file_fuse_getattr(fuse_req_t fuse_req, fuse_ino_t fuse_ino,
943 WINPR_ATTR_UNUSED
struct fuse_file_info* file_info)
945 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
946 CliprdrFuseFile* fuse_file =
nullptr;
947 struct stat attr = WINPR_C_ARRAY_INIT;
949 WINPR_ASSERT(file_context);
951 HashTable_Lock(file_context->inode_table);
952 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
954 HashTable_Unlock(file_context->inode_table);
955 fuse_reply_err(fuse_req, ENOENT);
959 DEBUG_CLIPRDR(file_context->log,
"getattr() has been called for file \"%s\"",
960 fuse_file->filename_with_root);
962 if (!fuse_file->is_directory && !fuse_file->has_size)
967 request_file_size_async(file_context, fuse_file, fuse_req, FUSE_LL_OPERATION_GETATTR);
968 HashTable_Unlock(file_context->inode_table);
971 fuse_reply_err(fuse_req, EIO);
976 if (!write_file_attributes(fuse_file, &attr))
978 fuse_reply_err(fuse_req, ERANGE);
979 HashTable_Unlock(file_context->inode_table);
982 HashTable_Unlock(file_context->inode_table);
984 fuse_reply_attr(fuse_req, &attr, 1.0);
987static void cliprdr_file_fuse_open(fuse_req_t fuse_req, fuse_ino_t fuse_ino,
988 struct fuse_file_info* file_info)
990 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
991 CliprdrFuseFile* fuse_file =
nullptr;
993 WINPR_ASSERT(file_context);
995 HashTable_Lock(file_context->inode_table);
996 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
998 HashTable_Unlock(file_context->inode_table);
999 fuse_reply_err(fuse_req, ENOENT);
1002 if (fuse_file->is_directory)
1004 HashTable_Unlock(file_context->inode_table);
1005 fuse_reply_err(fuse_req, EISDIR);
1008 HashTable_Unlock(file_context->inode_table);
1010 if ((file_info->flags & O_ACCMODE) != O_RDONLY)
1012 fuse_reply_err(fuse_req, EACCES);
1017 file_info->direct_io = 1;
1019 fuse_reply_open(fuse_req, file_info);
1022static BOOL request_file_range_async(CliprdrFileContext* file_context, CliprdrFuseFile* fuse_file,
1023 fuse_req_t fuse_req, off_t offset,
size_t requested_size)
1027 WINPR_ASSERT(file_context);
1028 WINPR_ASSERT(fuse_file);
1030 if (requested_size > UINT32_MAX)
1033 CliprdrFuseRequest* fuse_request =
1034 cliprdr_fuse_request_new(file_context, fuse_file, fuse_req, FUSE_LL_OPERATION_READ);
1038 file_contents_request.common.msgType = CB_FILECONTENTS_REQUEST;
1039 file_contents_request.streamId = fuse_request->stream_id;
1040 file_contents_request.listIndex = fuse_file->list_idx;
1041 file_contents_request.dwFlags = FILECONTENTS_RANGE;
1042 file_contents_request.nPositionLow = (UINT32)(offset & 0xFFFFFFFF);
1043 file_contents_request.nPositionHigh = (UINT32)((offset >> 32) & 0xFFFFFFFF);
1044 file_contents_request.cbRequested = (UINT32)requested_size;
1045 file_contents_request.haveClipDataId = fuse_file->has_clip_data_id;
1046 file_contents_request.clipDataId = fuse_file->clip_data_id;
1048 if (file_context->context->ClientFileContentsRequest(file_context->context,
1049 &file_contents_request))
1051 WLog_Print(file_context->log, WLOG_ERROR,
1052 "Failed to send FileContentsRequest for file \"%s\"",
1053 fuse_file->filename_with_root);
1054 HashTable_Remove(file_context->request_table, (
void*)(uintptr_t)fuse_request->stream_id);
1062 "Requested file range (%zu Bytes at offset %ld) for file \"%s\" with stream id %u",
1063 requested_size, offset, fuse_file->filename, fuse_request->stream_id);
1069static void cliprdr_file_fuse_read(fuse_req_t fuse_req, fuse_ino_t fuse_ino,
size_t size,
1070 off_t offset, WINPR_ATTR_UNUSED
struct fuse_file_info* file_info)
1072 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
1073 CliprdrFuseFile* fuse_file =
nullptr;
1076 WINPR_ASSERT(file_context);
1078 HashTable_Lock(file_context->inode_table);
1079 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
1081 HashTable_Unlock(file_context->inode_table);
1082 fuse_reply_err(fuse_req, ENOENT);
1085 if (fuse_file->is_directory)
1087 HashTable_Unlock(file_context->inode_table);
1088 fuse_reply_err(fuse_req, EISDIR);
1091 if (!fuse_file->has_size || (offset < 0) || ((
size_t)offset > fuse_file->size))
1093 HashTable_Unlock(file_context->inode_table);
1094 fuse_reply_err(fuse_req, EINVAL);
1098 size = MIN(size, 8ULL * 1024ULL * 1024ULL);
1100 result = request_file_range_async(file_context, fuse_file, fuse_req, offset, size);
1101 HashTable_Unlock(file_context->inode_table);
1104 fuse_reply_err(fuse_req, EIO);
1107static void cliprdr_file_fuse_opendir(fuse_req_t fuse_req, fuse_ino_t fuse_ino,
1108 struct fuse_file_info* file_info)
1110 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
1111 CliprdrFuseFile* fuse_file =
nullptr;
1113 WINPR_ASSERT(file_context);
1115 HashTable_Lock(file_context->inode_table);
1116 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
1118 HashTable_Unlock(file_context->inode_table);
1119 fuse_reply_err(fuse_req, ENOENT);
1122 if (!fuse_file->is_directory)
1124 HashTable_Unlock(file_context->inode_table);
1125 fuse_reply_err(fuse_req, ENOTDIR);
1128 HashTable_Unlock(file_context->inode_table);
1130 if ((file_info->flags & O_ACCMODE) != O_RDONLY)
1132 fuse_reply_err(fuse_req, EACCES);
1136 fuse_reply_open(fuse_req, file_info);
1139static void cliprdr_file_fuse_readdir(fuse_req_t fuse_req, fuse_ino_t fuse_ino,
size_t max_size,
1141 WINPR_ATTR_UNUSED
struct fuse_file_info* file_info)
1143 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
1144 CliprdrFuseFile* fuse_file =
nullptr;
1145 CliprdrFuseFile* child =
nullptr;
1146 struct stat attr = WINPR_C_ARRAY_INIT;
1147 size_t written_size = 0;
1148 size_t entry_size = 0;
1149 char* filename =
nullptr;
1150 char* buf =
nullptr;
1152 WINPR_ASSERT(file_context);
1154 HashTable_Lock(file_context->inode_table);
1155 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
1157 HashTable_Unlock(file_context->inode_table);
1158 fuse_reply_err(fuse_req, ENOENT);
1161 if (!fuse_file->is_directory)
1163 HashTable_Unlock(file_context->inode_table);
1164 fuse_reply_err(fuse_req, ENOTDIR);
1168 DEBUG_CLIPRDR(file_context->log,
"Reading directory \"%s\" at offset %ld",
1169 fuse_file->filename_with_root, offset);
1171 if ((offset < 0) || ((
size_t)offset >= ArrayList_Count(fuse_file->children)))
1173 HashTable_Unlock(file_context->inode_table);
1174 fuse_reply_buf(fuse_req,
nullptr, 0);
1178 buf = calloc(max_size,
sizeof(
char));
1181 HashTable_Unlock(file_context->inode_table);
1182 fuse_reply_err(fuse_req, ENOMEM);
1187 for (off_t i = offset; i < 2; ++i)
1191 if (!write_file_attributes(fuse_file, &attr))
1193 HashTable_Unlock(file_context->inode_table);
1194 fuse_reply_err(fuse_req, ERANGE);
1203 if (!write_file_attributes(fuse_file->parent, &attr))
1205 HashTable_Unlock(file_context->inode_table);
1206 fuse_reply_err(fuse_req, ERANGE);
1210 attr.st_ino = fuse_file->parent ? attr.st_ino : FUSE_ROOT_ID;
1211 attr.st_mode = fuse_file->parent ? attr.st_mode : 0555;
1216 WINPR_ASSERT(FALSE);
1223 entry_size = fuse_add_direntry(fuse_req, buf + written_size, max_size - written_size,
1224 filename, &attr, i + 1);
1225 if (entry_size > max_size - written_size)
1228 written_size += entry_size;
1231 for (
size_t j = 0, i = 2; j < ArrayList_Count(fuse_file->children); ++j, ++i)
1233 if (i < (
size_t)offset)
1236 child = ArrayList_GetItem(fuse_file->children, j);
1238 if (!write_file_attributes(child, &attr))
1240 HashTable_Unlock(file_context->inode_table);
1241 fuse_reply_err(fuse_req, ERANGE);
1247 fuse_add_direntry(fuse_req, buf + written_size, max_size - written_size,
1248 child->filename, &attr, WINPR_ASSERTING_INT_CAST(off_t, i + 1));
1249 if (entry_size > max_size - written_size)
1252 written_size += entry_size;
1254 HashTable_Unlock(file_context->inode_table);
1256 fuse_reply_buf(fuse_req, buf, written_size);
1260static void fuse_abort(
int sig,
const char* signame,
void* context)
1262 CliprdrFileContext* file = (CliprdrFileContext*)context;
1266 WLog_Print(file->log, WLOG_INFO,
"signal %s [%d] aborting session", signame, sig);
1267 cliprdr_file_session_terminate(file, FALSE);
1271static DWORD WINAPI cliprdr_file_fuse_thread(LPVOID arg)
1273 CliprdrFileContext* file = (CliprdrFileContext*)arg;
1277 DEBUG_CLIPRDR(file->log,
"Starting fuse with mountpoint '%s'", file->path);
1279 struct fuse_args args = FUSE_ARGS_INIT(0,
nullptr);
1280 fuse_opt_add_arg(&args, file->path);
1281#if defined(__linux__)
1286 fuse_opt_add_arg(&args,
"-oauto_unmount");
1288 file->fuse_sess = fuse_session_new(&args, &cliprdr_file_fuse_oper,
1289 sizeof(cliprdr_file_fuse_oper), (
void*)file);
1290 (void)SetEvent(file->fuse_start_sync);
1292 if (file->fuse_sess !=
nullptr)
1294 if (freerdp_add_signal_cleanup_handler(file, fuse_abort))
1296 if (0 == fuse_session_mount(file->fuse_sess, file->path))
1298 fuse_session_loop(file->fuse_sess);
1299 fuse_session_unmount(file->fuse_sess);
1303 const LONG old = InterlockedExchange(&file->fuse_failed, 1);
1305 file->log, WLOG_WARN,
1306 "cannot mount '%s': files from the server cannot be pasted here (%" PRId32
")",
1309 freerdp_del_signal_cleanup_handler(file, fuse_abort);
1312 WLog_Print(file->log, WLOG_DEBUG,
"Waiting for FUSE stop sync");
1313 if (WaitForSingleObject(file->fuse_stop_sync, INFINITE) == WAIT_FAILED)
1314 WLog_Print(file->log, WLOG_ERROR,
"Failed to wait for stop sync");
1315 fuse_session_destroy(file->fuse_sess);
1317 fuse_opt_free_args(&args);
1319 DEBUG_CLIPRDR(file->log,
"Quitting fuse with mountpoint '%s'", file->path);
1326WINPR_ATTR_FORMAT_ARG(4, 5)
1327static UINT cliprdr_fail_content_reponse(CliprdrFileContext* file_context,
1328 CliprdrFuseRequest* fuse_request, UINT32 streamID,
1329 WINPR_FORMAT_ARG const
char* format, ...)
1331 WINPR_ASSERT(file_context);
1332 WINPR_ASSERT(fuse_request);
1333 WINPR_ASSERT(format);
1335 const DWORD level = WLOG_WARN;
1336 if (WLog_IsLevelActive(file_context->log, level))
1339 va_start(ap, format);
1340 WLog_PrintVA(file_context->log, WLOG_WARN, format, ap);
1344 fuse_reply_err(fuse_request->fuse_req, EIO);
1345 HashTable_Remove(file_context->request_table, (
void*)(uintptr_t)streamID);
1346 HashTable_Unlock(file_context->inode_table);
1347 return CHANNEL_RC_OK;
1350static UINT cliprdr_file_context_server_file_contents_response(
1351 CliprdrClientContext* cliprdr_context,
1354 CliprdrFileContext* file_context =
nullptr;
1355 CliprdrFuseRequest* fuse_request =
nullptr;
1356 struct fuse_entry_param entry = WINPR_C_ARRAY_INIT;
1358 WINPR_ASSERT(cliprdr_context);
1359 WINPR_ASSERT(file_contents_response);
1361 file_context = cliprdr_context->custom;
1362 WINPR_ASSERT(file_context);
1364 HashTable_Lock(file_context->inode_table);
1365 fuse_request = HashTable_GetItemValue(file_context->request_table,
1366 (
void*)(uintptr_t)file_contents_response->streamId);
1369 HashTable_Unlock(file_context->inode_table);
1370 return CHANNEL_RC_OK;
1373 if (!(file_contents_response->common.msgFlags & CB_RESPONSE_OK))
1375 return cliprdr_fail_content_reponse(file_context, fuse_request,
1376 file_contents_response->streamId,
1377 "FileContentsRequests for file \"%s\" was unsuccessful",
1378 fuse_request->fuse_file->filename);
1381 if ((fuse_request->operation_type == FUSE_LL_OPERATION_LOOKUP ||
1382 fuse_request->operation_type == FUSE_LL_OPERATION_GETATTR) &&
1383 file_contents_response->cbRequested !=
sizeof(UINT64))
1385 return cliprdr_fail_content_reponse(
1386 file_context, fuse_request, file_contents_response->streamId,
1387 "Received invalid file size for file \"%s\" from the client",
1388 fuse_request->fuse_file->filename);
1391 if (fuse_request->operation_type == FUSE_LL_OPERATION_LOOKUP ||
1392 fuse_request->operation_type == FUSE_LL_OPERATION_GETATTR)
1394 DEBUG_CLIPRDR(file_context->log,
"Received file size for file \"%s\" with stream id %u",
1395 fuse_request->fuse_file->filename, file_contents_response->streamId);
1397 fuse_request->fuse_file->size =
1398 winpr_Data_Get_UINT64(file_contents_response->requestedData);
1399 fuse_request->fuse_file->has_size = TRUE;
1401 entry.ino = fuse_request->fuse_file->ino;
1402 if (!write_file_attributes(fuse_request->fuse_file, &entry.attr))
1404 return cliprdr_fail_content_reponse(
1405 file_context, fuse_request, file_contents_response->streamId,
1406 "Received invalid file size for file \"%s\" from the client",
1407 fuse_request->fuse_file->filename);
1409 entry.attr_timeout = 1.0;
1410 entry.entry_timeout = 1.0;
1412 else if (fuse_request->operation_type == FUSE_LL_OPERATION_READ)
1414 DEBUG_CLIPRDR(file_context->log,
"Received file range for file \"%s\" with stream id %u",
1415 fuse_request->fuse_file->filename, file_contents_response->streamId);
1420 const FuseLowlevelOperationType operation_type = fuse_request->operation_type;
1421 fuse_req_t fuse_req = fuse_request->fuse_req;
1422 HashTable_Remove(file_context->request_table,
1423 (
void*)(uintptr_t)file_contents_response->streamId);
1424 HashTable_Unlock(file_context->inode_table);
1426 switch (operation_type)
1428 case FUSE_LL_OPERATION_NONE:
1430 case FUSE_LL_OPERATION_LOOKUP:
1431 fuse_reply_entry(fuse_req, &entry);
1433 case FUSE_LL_OPERATION_GETATTR:
1434 fuse_reply_attr(fuse_req, &entry.attr, entry.attr_timeout);
1436 case FUSE_LL_OPERATION_READ:
1437 fuse_reply_buf(fuse_req, (
const char*)file_contents_response->requestedData,
1438 file_contents_response->cbRequested);
1444 return CHANNEL_RC_OK;
1448#if defined(WITH_CLIPRDR_FILE_APPLE)
1454 CliprdrFileContext* file;
1462static void apple_offer_free(
void* userdata)
1464 AppleOffer* offer = userdata;
1468 for (UINT32 x = 0; offer->paths && (x < offer->count); x++)
1469 free(offer->paths[x]);
1478 WINPR_ASSERT(descriptor);
1479 return ConvertWCharNToUtf8Alloc(descriptor->cFileName, ARRAYSIZE(descriptor->cFileName),
1485 WINPR_ASSERT(descriptor);
1486 return (descriptor->dwFlags & FD_ATTRIBUTES) &&
1487 (descriptor->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
1491static char* apple_local_relative_path(
const char* name)
1495 const size_t len = strlen(name);
1499 char* path = _strdup(name);
1504 for (
size_t x = 0; x <= len; x++)
1506 if ((x < len) && (name[x] ==
'/'))
1508 if ((x < len) && (name[x] !=
'\\'))
1511 const char* component = &name[start];
1512 const size_t clen = x - start;
1513 if ((clen == 0) || ((clen == 1) && (component[0] ==
'.')) ||
1514 ((clen == 2) && (strncmp(component,
"..", 2) == 0)))
1527static BOOL apple_request_file_contents(CliprdrFileContext* file, UINT32 generation,
1528 UINT32 listIndex, UINT32 flags, UINT64 offset, UINT32 size,
1529 BYTE** pdata, UINT32* plen)
1532 WINPR_ASSERT(pdata);
1539 request.common.msgType = CB_FILECONTENTS_REQUEST;
1540 request.listIndex = listIndex;
1541 request.dwFlags = flags;
1542 request.nPositionLow = (UINT32)(offset & 0xFFFFFFFF);
1543 request.nPositionHigh = (UINT32)(offset >> 32);
1544 request.cbRequested = size;
1548 EnterCriticalSection(&file->apple_lock);
1549 CliprdrClientContext* cliprdr = file->apple_cancelled ? nullptr : file->context;
1550 if (!cliprdr || !cliprdr->ClientFileContentsRequest || (generation != file->apple_generation))
1552 LeaveCriticalSection(&file->apple_lock);
1555 request.streamId = ++file->apple_stream_id;
1556 file->apple_contents_success = FALSE;
1557 free(file->apple_contents_data);
1558 file->apple_contents_data =
nullptr;
1559 file->apple_contents_size = 0;
1560 (void)ResetEvent(file->apple_contents_event);
1561 LeaveCriticalSection(&file->apple_lock);
1563 if (cliprdr->ClientFileContentsRequest(cliprdr, &request) != CHANNEL_RC_OK)
1566 rdpContext* context = cliprdr->rdpcontext;
1567 WINPR_ASSERT(context);
1569 HANDLE hdl[2] = { freerdp_abort_event(context), file->apple_contents_event };
1571 if (WaitForMultipleObjects(ARRAYSIZE(hdl), hdl, FALSE, timeout) != WAIT_OBJECT_0 + 1)
1573 WLog_Print(file->log, WLOG_ERROR,
"no file contents reply for stream %" PRIu32,
1578 EnterCriticalSection(&file->apple_lock);
1579 const BOOL rc = file->apple_contents_success && !file->apple_cancelled &&
1580 (generation == file->apple_generation);
1583 *pdata = file->apple_contents_data;
1584 *plen = file->apple_contents_size;
1585 file->apple_contents_data =
nullptr;
1586 file->apple_contents_size = 0;
1588 LeaveCriticalSection(&file->apple_lock);
1593static void apple_cancel_downloads(CliprdrFileContext* file)
1597 EnterCriticalSection(&file->apple_lock);
1598 file->apple_cancelled = TRUE;
1599 file->apple_generation++;
1600 (void)SetEvent(file->apple_contents_event);
1601 LeaveCriticalSection(&file->apple_lock);
1605cliprdr_file_context_apple_contents_response(CliprdrClientContext* cliprdr_context,
1608 WINPR_ASSERT(cliprdr_context);
1609 WINPR_ASSERT(response);
1611 CliprdrFileContext* file = cliprdr_context->custom;
1614 EnterCriticalSection(&file->apple_lock);
1615 if (response->streamId != file->apple_stream_id)
1618 WLog_Print(file->log, WLOG_WARN,
1619 "file contents response for stale stream %" PRIu32
", ignoring",
1620 response->streamId);
1621 LeaveCriticalSection(&file->apple_lock);
1622 return CHANNEL_RC_OK;
1625 BOOL success = (response->common.msgFlags & CB_RESPONSE_OK) &&
1626 !(response->common.msgFlags & CB_RESPONSE_FAIL);
1627 if (success && response->requestedData && (response->cbRequested > 0))
1629 file->apple_contents_data = malloc(response->cbRequested);
1630 if (file->apple_contents_data)
1632 memcpy(file->apple_contents_data, response->requestedData, response->cbRequested);
1633 file->apple_contents_size = response->cbRequested;
1638 file->apple_contents_success = success;
1639 (void)SetEvent(file->apple_contents_event);
1640 LeaveCriticalSection(&file->apple_lock);
1641 return CHANNEL_RC_OK;
1645static BOOL apple_set_write_time(
const FILEDESCRIPTORW* descriptor,
const char* path)
1647 WINPR_ASSERT(descriptor);
1650 if ((descriptor->dwFlags & FD_WRITESTIME) == 0)
1653 const UINT64 filetime = ((1ull * descriptor->ftLastWriteTime.dwHighDateTime) << 32u) |
1654 descriptor->ftLastWriteTime.dwLowDateTime;
1655 UINT64 seconds = filetime / (10ULL * 1000ULL * 1000ULL);
1656 const int32_t usec = (filetime % (10ll * 1000ll * 1000ll)) / 10ll;
1658 if (seconds < WIN32_FILETIME_TO_UNIX_EPOCH)
1661 seconds -= WIN32_FILETIME_TO_UNIX_EPOCH;
1663 struct timeval times[2] = { { .tv_sec = (time_t)seconds, .tv_usec = usec },
1664 { .tv_sec = (time_t)seconds, .tv_usec = usec } };
1665 return utimes(path, times) == 0;
1668static BOOL apple_download_file(CliprdrFileContext* file, UINT32 generation, UINT32 listIndex,
1672 WINPR_ASSERT(descriptor);
1676 if (descriptor->dwFlags & FD_FILESIZE)
1677 size = ((UINT64)descriptor->nFileSizeHigh << 32) | descriptor->nFileSizeLow;
1680 BYTE* data =
nullptr;
1682 const BOOL rc = apple_request_file_contents(file, generation, listIndex, FILECONTENTS_SIZE,
1683 0,
sizeof(UINT64), &data, &len);
1684 if (rc && (len >=
sizeof(UINT64)))
1685 memcpy(&size, data,
sizeof(size));
1687 if (!rc || (len <
sizeof(UINT64)))
1691 FILE* fp = winpr_fopen(path,
"wb");
1696 const UINT32 chunk = 1024 * 1024;
1697 for (UINT64 offset = 0; offset < size;)
1699 const UINT32 requested = (size - offset < chunk) ? (UINT32)(size - offset) : chunk;
1700 BYTE* data =
nullptr;
1702 if (!apple_request_file_contents(file, generation, listIndex, FILECONTENTS_RANGE, offset,
1703 requested, &data, &len) ||
1704 (len == 0) || (len > requested) || (fwrite(data, 1, len, fp) != len))
1714 if (fclose(fp) != 0)
1717 (void)winpr_DeleteFile(path);
1718 else if (!apple_set_write_time(descriptor, path))
1719 WLog_Print(file->log, WLOG_WARN,
"failed to set the modification time of %s", path);
1725static BOOL apple_download_entry(AppleOffer* offer, UINT32 x)
1727 WINPR_ASSERT(offer);
1732 if (!apple_download_file(offer->file, offer->generation, x, &offer->files[x], offer->paths[x]))
1734 WLog_Print(offer->file->log, WLOG_ERROR,
"failed to download %s", offer->paths[x]);
1735 (void)winpr_DeleteFile(offer->paths[x]);
1744static BOOL apple_download(
void* userdata,
size_t index)
1746 AppleOffer* offer = userdata;
1747 WINPR_ASSERT(offer);
1749 if ((index >= offer->count) || !offer->paths[index])
1752 if (!apple_is_directory(&offer->files[index]))
1753 return apple_download_entry(offer, (UINT32)index);
1755 const char* top = offer->paths[index];
1756 const size_t toplen = strlen(top);
1759 for (UINT32 x = 0; x < offer->count; x++)
1761 const char* path = offer->paths[x];
1762 if (!path || apple_is_directory(&offer->files[x]) || (strncmp(path, top, toplen) != 0) ||
1763 (path[toplen] !=
'/'))
1768 rc = apple_download_entry(offer, x);
1769 else if (!offer->done[x])
1770 (void)winpr_DeleteFile(path);
1776static BOOL apple_create_placeholder(
const FILEDESCRIPTORW* descriptor,
char* path)
1778 WINPR_ASSERT(descriptor);
1781 if (apple_is_directory(descriptor))
1782 return winpr_PathMakePath(path,
nullptr);
1784 char* slash = strrchr(path,
'/');
1785 WINPR_ASSERT(slash);
1787 const BOOL parent = winpr_PathMakePath(path,
nullptr);
1792 FILE* fp = winpr_fopen(path,
"wb");
1795 if (fclose(fp) != 0)
1799 (void)apple_set_write_time(descriptor, path);
1804static AppleOffer* apple_offer_new(CliprdrFileContext* file, UINT32 generation,
const void* data,
1805 size_t size,
const char* dir)
1810 if (size > UINT32_MAX)
1813 AppleOffer* offer = calloc(1,
sizeof(AppleOffer));
1817 offer->generation = generation;
1819 if (cliprdr_parse_file_list(data, (UINT32)size, &offer->files, &offer->count) != 0)
1821 if (offer->count == 0)
1824 offer->paths = calloc(offer->count,
sizeof(
char*));
1825 offer->done = calloc(offer->count,
sizeof(BYTE));
1826 if (!offer->paths || !offer->done)
1829 for (UINT32 x = 0; x < offer->count; x++)
1831 char* name = apple_descriptor_name(&offer->files[x]);
1832 char* relative = name ? apple_local_relative_path(name) : nullptr;
1835 WLog_Print(file->log, WLOG_WARN,
"skipping invalid server file name %s",
1836 name ? name :
"(null)");
1843 const int rc = winpr_asprintf(&offer->paths[x], &pathlen,
"%s/%s", dir, relative);
1847 offer->paths[x] =
nullptr;
1850 if (!apple_create_placeholder(&offer->files[x], offer->paths[x]))
1853 WLog_Print(file->log, WLOG_WARN,
"skipping %s, failed to create it", offer->paths[x]);
1854 free(offer->paths[x]);
1855 offer->paths[x] =
nullptr;
1861 apple_offer_free(offer);
1866static BOOL apple_offer_server_files(CliprdrFileContext* file, UINT32 generation,
const void* data,
1867 size_t size,
const char** mime_types,
size_t mime_count,
1868 cliprdr_file_context_data_fkt fkt,
void* userdata)
1872 char* dir = cliprdr_file_apple_new_download_dir();
1878 size_t* top_level =
nullptr;
1879 BOOL* present =
nullptr;
1880 size_t top_level_count = 0;
1881 AppleOffer* offer = apple_offer_new(file, generation, data, size, dir);
1885 top_level = calloc(offer->count,
sizeof(
size_t));
1886 present = calloc(offer->count,
sizeof(BOOL));
1887 if (!top_level || !present)
1890 for (UINT32 x = 0; x < offer->count; x++)
1892 if (!offer->paths[x])
1897 const BOOL top = (strchr(offer->paths[x] + strlen(dir) + 1,
'/') ==
nullptr);
1899 top_level[top_level_count++] = x;
1900 present[x] = top || !apple_is_directory(&offer->files[x]);
1902 if (top_level_count == 0)
1905 WLog_Print(file->log, WLOG_DEBUG,
"offering %" PRIuz
" server files (%" PRIu32
" entries)",
1906 top_level_count, offer->count);
1908 rc = cliprdr_file_apple_offer(dir, (
const char**)offer->paths, present, offer->count, top_level,
1909 top_level_count, apple_download, apple_offer_free, offer,
1910 mime_types, mime_count, fkt, userdata);
1915 (void)winpr_RemoveDirectory_RecursiveA(dir);
1916 apple_offer_free(offer);
1924static UINT cliprdr_file_context_send_file_contents_failure(
1930 WINPR_ASSERT(fileContentsRequest);
1932 const UINT64 offset = (((UINT64)fileContentsRequest->nPositionHigh) << 32) |
1933 ((UINT64)fileContentsRequest->nPositionLow);
1934 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1935 "server file contents request [lockID %" PRIu32
", streamID %" PRIu32
1936 ", index %" PRIu32
"] offset %" PRIu64
", size %" PRIu32
" failed",
1937 fileContentsRequest->clipDataId, fileContentsRequest->streamId,
1938 fileContentsRequest->listIndex, offset, fileContentsRequest->cbRequested);
1940 response.common.msgFlags = CB_RESPONSE_FAIL;
1941 response.streamId = fileContentsRequest->streamId;
1943 WINPR_ASSERT(file->context);
1944 WINPR_ASSERT(file->context->ClientFileContentsResponse);
1945 return file->context->ClientFileContentsResponse(file->context, &response);
1949cliprdr_file_context_send_contents_response(CliprdrFileContext* file,
1951 const void* data,
size_t size)
1953 if (size > UINT32_MAX)
1954 return ERROR_INVALID_PARAMETER;
1957 .requestedData = data,
1958 .cbRequested = (UINT32)size,
1959 .common.msgFlags = CB_RESPONSE_OK };
1961 WINPR_ASSERT(request);
1964 WLog_Print(file->log, WLOG_DEBUG,
"send contents response streamID=%" PRIu32
", size=%" PRIu32,
1965 response.streamId, response.cbRequested);
1966 WINPR_ASSERT(file->context);
1967 WINPR_ASSERT(file->context->ClientFileContentsResponse);
1968 return file->context->ClientFileContentsResponse(file->context, &response);
1971static BOOL dump_streams(
const void* key,
void* value, WINPR_ATTR_UNUSED
void* arg)
1973 const UINT32* ukey = key;
1974 CliprdrLocalStream* cur = value;
1976 writelog(cur->context->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1977 "[key %" PRIu32
"] lockID %" PRIu32
", count %" PRIuz
", locked %d", *ukey,
1978 cur->lockId, cur->count, cur->locked);
1979 for (
size_t x = 0; x < cur->count; x++)
1981 const CliprdrLocalFile* file = &cur->files[x];
1982 writelog(cur->context->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1983 "file [%" PRIuz
"] %s: %" PRId64, x, file->name, file->size);
1988static CliprdrLocalFile* file_info_for_request(CliprdrFileContext* file, UINT32 lockId,
1993 CliprdrLocalStream* cur = HashTable_GetItemValue(file->local_streams, &lockId);
1996 if (listIndex < cur->count)
1998 CliprdrLocalFile* f = &cur->files[listIndex];
2003 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
2004 "invalid entry index for lockID %" PRIu32
", index %" PRIu32
" [count %" PRIuz
2006 lockId, listIndex, cur->count, cur->locked);
2011 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
2012 "missing entry for lockID %" PRIu32
", index %" PRIu32, lockId, listIndex);
2013 if (!HashTable_Foreach(file->local_streams, dump_streams, file))
2014 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
2015 "HashTable_Foreach failed");
2021static CliprdrLocalFile* file_for_request(CliprdrFileContext* file, UINT32 lockId, UINT32 listIndex)
2023 CliprdrLocalFile* f = file_info_for_request(file, lockId, listIndex);
2028 const char* name = f->name;
2029 f->fp = winpr_fopen(name,
"rb");
2033 char ebuffer[256] = WINPR_C_ARRAY_INIT;
2034 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
2035 "[lockID %" PRIu32
", index %" PRIu32
2036 "] failed to open file '%s' [size %" PRId64
"] %s [%d]",
2037 lockId, listIndex, f->name, f->size,
2038 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)), errno);
2046static void cliprdr_local_file_try_close(CliprdrLocalFile* file, UINT res, UINT64 offset,
2053 WINPR_ASSERT(file->context);
2054 WLog_Print(file->context->log, WLOG_DEBUG,
"closing file %s after error %" PRIu32,
2057 else if (((file->size > 0) && (offset + size >= (UINT64)file->size)))
2059 WINPR_ASSERT(file->context);
2060 WLog_Print(file->context->log, WLOG_DEBUG,
"closing file %s after read", file->name);
2068 (void)fclose(file->fp);
2072static UINT cliprdr_file_context_server_file_size_request(
2075 WINPR_ASSERT(fileContentsRequest);
2077 if (fileContentsRequest->cbRequested !=
sizeof(UINT64))
2079 WLog_Print(file->log, WLOG_WARN,
"unexpected FILECONTENTS_SIZE request: %" PRIu32
" bytes",
2080 fileContentsRequest->cbRequested);
2083 HashTable_Lock(file->local_streams);
2085 UINT res = CHANNEL_RC_OK;
2086 CliprdrLocalFile* rfile =
2087 file_for_request(file, fileContentsRequest->clipDataId, fileContentsRequest->listIndex);
2089 res = cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
2092 if (_fseeki64(rfile->fp, 0, SEEK_END) < 0)
2093 res = cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
2096 const INT64 size = _ftelli64(rfile->fp);
2098 cliprdr_local_file_try_close(rfile, res, 0, 0);
2100 res = cliprdr_file_context_send_contents_response(file, fileContentsRequest, &size,
2105 HashTable_Unlock(file->local_streams);
2109static UINT cliprdr_file_context_server_file_range_request(
2112 BYTE* data =
nullptr;
2114 WINPR_ASSERT(fileContentsRequest);
2116 const UINT64 offset = ((1ull * fileContentsRequest->nPositionHigh) << 32) |
2117 (1ull * fileContentsRequest->nPositionLow);
2118 if (offset > INT64_MAX)
2119 return ERROR_INVALID_DATA;
2121 HashTable_Lock(file->local_streams);
2122 CliprdrLocalFile* rfile =
2123 file_for_request(file, fileContentsRequest->clipDataId, fileContentsRequest->listIndex);
2127 if (_fseeki64(rfile->fp, WINPR_ASSERTING_INT_CAST(int64_t, offset), SEEK_SET) < 0)
2130 data = malloc(fileContentsRequest->cbRequested);
2135 const size_t r = fread(data, 1, fileContentsRequest->cbRequested, rfile->fp);
2137 cliprdr_file_context_send_contents_response(file, fileContentsRequest, data, r);
2140 cliprdr_local_file_try_close(rfile, rc, offset, fileContentsRequest->cbRequested);
2141 HashTable_Unlock(file->local_streams);
2146 cliprdr_local_file_try_close(rfile, ERROR_INTERNAL_ERROR, offset,
2147 fileContentsRequest->cbRequested);
2149 HashTable_Unlock(file->local_streams);
2150 return cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
2153static void cliprdr_local_stream_free(
void* obj);
2155static UINT change_lock(CliprdrFileContext* file, UINT32 lockId, BOOL lock)
2157 UINT rc = CHANNEL_RC_OK;
2161 HashTable_Lock(file->local_streams);
2164 CliprdrLocalStream* stream = HashTable_GetItemValue(file->local_streams, &lockId);
2165 if (lock && !stream)
2167 stream = cliprdr_local_stream_new(file, lockId,
nullptr, 0);
2168 if (!HashTable_Insert(file->local_streams, &lockId, stream))
2170 rc = ERROR_INTERNAL_ERROR;
2171 cliprdr_local_stream_free(stream);
2174 file->local_lock_id = lockId;
2178 stream->locked = lock;
2179 stream->lockId = lockId;
2186 if (!HashTable_Foreach(file->local_streams, local_stream_discard, file))
2187 rc = ERROR_INTERNAL_ERROR;
2190 HashTable_Unlock(file->local_streams);
2194static UINT cliprdr_file_context_lock(CliprdrClientContext* context,
2197 WINPR_ASSERT(context);
2198 WINPR_ASSERT(lockClipboardData);
2199 CliprdrFileContext* file = (context->custom);
2200 return change_lock(file, lockClipboardData->clipDataId, TRUE);
2203static UINT cliprdr_file_context_unlock(CliprdrClientContext* context,
2206 WINPR_ASSERT(context);
2207 WINPR_ASSERT(unlockClipboardData);
2208 CliprdrFileContext* file = (context->custom);
2209 return change_lock(file, unlockClipboardData->clipDataId, FALSE);
2212static UINT cliprdr_file_context_server_file_contents_request(
2215 UINT error = NO_ERROR;
2217 WINPR_ASSERT(context);
2218 WINPR_ASSERT(fileContentsRequest);
2220 CliprdrFileContext* file = (context->custom);
2227 if ((fileContentsRequest->dwFlags & (FILECONTENTS_SIZE | FILECONTENTS_RANGE)) ==
2228 (FILECONTENTS_SIZE | FILECONTENTS_RANGE))
2230 WLog_Print(file->log, WLOG_ERROR,
"invalid CLIPRDR_FILECONTENTS_REQUEST.dwFlags");
2231 return cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
2234 if (fileContentsRequest->dwFlags & FILECONTENTS_SIZE)
2235 error = cliprdr_file_context_server_file_size_request(file, fileContentsRequest);
2237 if (fileContentsRequest->dwFlags & FILECONTENTS_RANGE)
2238 error = cliprdr_file_context_server_file_range_request(file, fileContentsRequest);
2242 WLog_Print(file->log, WLOG_ERROR,
"failed to handle CLIPRDR_FILECONTENTS_REQUEST: 0x%08X",
2244 return cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
2247 return CHANNEL_RC_OK;
2250BOOL cliprdr_file_context_init(CliprdrFileContext* file, CliprdrClientContext* cliprdr)
2253 WINPR_ASSERT(cliprdr);
2255 cliprdr->custom = file;
2256 file->context = cliprdr;
2258 cliprdr->ServerLockClipboardData = cliprdr_file_context_lock;
2259 cliprdr->ServerUnlockClipboardData = cliprdr_file_context_unlock;
2260 cliprdr->ServerFileContentsRequest = cliprdr_file_context_server_file_contents_request;
2261#if defined(WITH_FUSE)
2262 cliprdr->ServerFileContentsResponse = cliprdr_file_context_server_file_contents_response;
2264 CliprdrFuseFile* root_dir = fuse_file_new_root(file);
2265 return root_dir !=
nullptr;
2266#elif defined(WITH_CLIPRDR_FILE_APPLE)
2267 cliprdr->ServerFileContentsResponse = cliprdr_file_context_apple_contents_response;
2268 EnterCriticalSection(&file->apple_lock);
2269 file->apple_cancelled = FALSE;
2270 LeaveCriticalSection(&file->apple_lock);
2277#if defined(WITH_FUSE)
2279static bool clear_all_selections(CliprdrFileContext* file_context)
2281 WINPR_ASSERT(file_context);
2282 WINPR_ASSERT(file_context->inode_table);
2284 HashTable_Lock(file_context->inode_table);
2285 const bool rc = clear_selection(file_context, TRUE,
nullptr);
2287 HashTable_Clear(file_context->clip_data_table);
2288 HashTable_Unlock(file_context->inode_table);
2293BOOL cliprdr_file_context_uninit(CliprdrFileContext* file, CliprdrClientContext* cliprdr)
2300#if defined(WITH_FUSE)
2301 if (file->inode_table)
2303 if (!clear_no_cdi_entry(file))
2305 if (!clear_all_selections(file))
2309#if defined(WITH_CLIPRDR_FILE_APPLE)
2311 apple_cancel_downloads(file);
2312 cliprdr_file_apple_detach();
2315 HashTable_Clear(file->local_streams);
2317 file->context =
nullptr;
2322#if defined(WITH_FUSE) || defined(WITH_CLIPRDR_FILE_APPLE)
2323 cliprdr->ServerFileContentsResponse =
nullptr;
2330static BOOL cliprdr_file_content_changed_and_update(
void* ihash,
size_t hsize,
const void* data,
2334 BYTE hash[WINPR_SHA256_DIGEST_LENGTH] = WINPR_C_ARRAY_INIT;
2336 if (hsize <
sizeof(hash))
2339 if (!winpr_Digest(WINPR_MD_SHA256, data, size, hash,
sizeof(hash)))
2342 const BOOL changed = memcmp(hash, ihash,
sizeof(hash)) != 0;
2344 memcpy(ihash, hash,
sizeof(hash));
2348static BOOL cliprdr_file_client_content_changed_and_update(CliprdrFileContext* file,
2349 const void* data,
size_t size)
2352 return cliprdr_file_content_changed_and_update(file->client_data_hash,
2353 sizeof(file->client_data_hash), data, size);
2356#if defined(WITH_FUSE)
2357static fuse_ino_t get_next_free_inode(CliprdrFileContext* file_context)
2361 WINPR_ASSERT(file_context);
2363 ino = file_context->next_ino;
2364 while (ino == 0 || ino == FUSE_ROOT_ID ||
2365 HashTable_GetItemValue(file_context->inode_table, (
void*)(uintptr_t)ino))
2368 file_context->next_ino = ino + 1;
2373static CliprdrFuseFile* clip_data_dir_new(CliprdrFileContext* file_context, BOOL has_clip_data_id,
2374 UINT32 clip_data_id)
2376 WINPR_ASSERT(file_context);
2378 UINT64 data_id = clip_data_id;
2379 if (!has_clip_data_id)
2380 data_id = NO_CLIP_DATA_ID;
2382 CliprdrFuseFile* clip_data_dir = fuse_file_new(
"/%" PRIu64, data_id);
2386 clip_data_dir->ino = get_next_free_inode(file_context);
2387 clip_data_dir->is_directory = TRUE;
2388 clip_data_dir->is_readonly = TRUE;
2389 clip_data_dir->has_clip_data_id = has_clip_data_id;
2390 clip_data_dir->clip_data_id = clip_data_id;
2392 CliprdrFuseFile* root_dir = get_fuse_file_by_ino(file_context, FUSE_ROOT_ID);
2395 WLog_Print(file_context->log, WLOG_ERROR,
"FUSE root directory missing");
2396 fuse_file_free(clip_data_dir);
2399 if (!ArrayList_Append(root_dir->children, clip_data_dir))
2401 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to append FUSE file");
2402 fuse_file_free(clip_data_dir);
2405 clip_data_dir->parent = root_dir;
2407 if (!HashTable_Insert(file_context->inode_table, (
void*)(uintptr_t)clip_data_dir->ino,
2410 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to insert inode into inode table");
2411 ArrayList_Remove(root_dir->children, clip_data_dir);
2412 fuse_file_free(clip_data_dir);
2416 if (!HashTable_Insert(file_context->dir_table, clip_data_dir->filename_with_root,
2419 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to insert inode into dir table");
2420 ArrayList_Remove(root_dir->children, clip_data_dir);
2421 fuse_file_free(clip_data_dir);
2425 return clip_data_dir;
2428static char* get_parent_path(
const char* filepath)
2430 const char* base = strrchr(filepath,
'/');
2433 while ((base > filepath) && (*base ==
'/'))
2436 WINPR_ASSERT(base >= filepath);
2437 const size_t parent_path_length = 1ULL + (size_t)(base - filepath);
2438 char* parent_path = calloc(parent_path_length + 1,
sizeof(
char));
2442 memcpy(parent_path, filepath, parent_path_length);
2447static CliprdrFuseFile* get_parent_directory(CliprdrFileContext* file_context,
const char* path)
2450 WINPR_ASSERT(file_context);
2453 char* parent_path = get_parent_path(path);
2457 CliprdrFuseFile* parent = HashTable_GetItemValue(file_context->dir_table, parent_path);
2463static BOOL selection_handle_file(CliprdrFileContext* file_context,
2464 CliprdrFuseClipDataEntry* clip_data_entry, uint32_t index,
2468 WINPR_ASSERT(file_context);
2469 WINPR_ASSERT(clip_data_entry);
2472 CliprdrFuseFile* clip_data_dir = clip_data_entry->clip_data_dir;
2473 WINPR_ASSERT(clip_data_dir);
2475 char filename[ARRAYSIZE(file->cFileName) * 8] = WINPR_C_ARRAY_INIT;
2476 const SSIZE_T filenamelen = ConvertWCharNToUtf8(file->cFileName, ARRAYSIZE(file->cFileName),
2477 filename, ARRAYSIZE(filename) - 1);
2478 if (filenamelen < 0)
2480 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to convert filename");
2484 for (
size_t j = 0; filename[j]; ++j)
2486 if (filename[j] ==
'\\')
2491 CliprdrFuseFile* fuse_file = fuse_file_new(
2492 "%.*s/%s", WINPR_ASSERTING_INT_CAST(
int, clip_data_dir->filename_with_root_len),
2493 clip_data_dir->filename_with_root, filename);
2496 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to create FUSE file");
2500 fuse_file->parent = get_parent_directory(file_context, fuse_file->filename_with_root);
2501 if (!fuse_file->parent)
2503 WLog_Print(file_context->log, WLOG_ERROR,
"Found no parent for FUSE file");
2507 if (!ArrayList_Append(fuse_file->parent->children, fuse_file))
2509 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to append FUSE file");
2513 fuse_file->list_idx = index;
2514 fuse_file->ino = get_next_free_inode(file_context);
2515 fuse_file->has_clip_data_id = clip_data_entry->has_clip_data_id;
2516 fuse_file->clip_data_id = clip_data_entry->clip_data_id;
2517 if (file->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2518 fuse_file->is_directory = TRUE;
2519 if (file->dwFileAttributes & FILE_ATTRIBUTE_READONLY)
2520 fuse_file->is_readonly = TRUE;
2521 if (file->dwFlags & FD_FILESIZE)
2523 fuse_file->size = (1ull * file->nFileSizeHigh << 32) + 1ull * file->nFileSizeLow;
2524 fuse_file->has_size = TRUE;
2526 if (file->dwFlags & FD_WRITESTIME)
2530 filetime = file->ftLastWriteTime.dwHighDateTime;
2532 filetime += file->ftLastWriteTime.dwLowDateTime;
2534 fuse_file->last_write_time_unix =
2535 1LL * filetime / (10LL * 1000LL * 1000LL) - WIN32_FILETIME_TO_UNIX_EPOCH;
2536 fuse_file->has_last_write_time = TRUE;
2539 if (!HashTable_Insert(file_context->inode_table, (
void*)(uintptr_t)fuse_file->ino, fuse_file))
2541 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to insert inode into inode table");
2545 if (fuse_file->is_directory)
2548 if (!HashTable_Insert(file_context->dir_table, fuse_file->filename_with_root, fuse_file))
2550 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to insert inode into dir table");
2560 fuse_file_free(fuse_file);
2561 if (!clear_entry_selection(clip_data_entry))
2562 WLog_Print(file_context->log, WLOG_WARN,
"failed to clear selection");
2569static BOOL set_selection_for_clip_data_entry(CliprdrFileContext* file_context,
2570 CliprdrFuseClipDataEntry* clip_data_entry,
2573 WINPR_ASSERT(file_context);
2574 WINPR_ASSERT(clip_data_entry);
2575 WINPR_ASSERT(files);
2577 if (clip_data_entry->has_clip_data_id)
2578 WLog_Print(file_context->log, WLOG_DEBUG,
"Setting selection for clipDataId %u",
2579 clip_data_entry->clip_data_id);
2581 WLog_Print(file_context->log, WLOG_DEBUG,
"Setting selection");
2583 for (UINT32 i = 0; i < n_files; ++i)
2586 if (!selection_handle_file(file_context, clip_data_entry, i, file))
2590 if (clip_data_entry->has_clip_data_id)
2591 WLog_Print(file_context->log, WLOG_DEBUG,
"Selection set for clipDataId %u",
2592 clip_data_entry->clip_data_id);
2594 WLog_Print(file_context->log, WLOG_DEBUG,
"Selection set");
2599static BOOL update_exposed_path(CliprdrFileContext* file_context, wClipboard* clip,
2600 CliprdrFuseClipDataEntry* clip_data_entry)
2602 wClipboardDelegate* delegate =
nullptr;
2603 CliprdrFuseFile* clip_data_dir =
nullptr;
2605 WINPR_ASSERT(file_context);
2607 WINPR_ASSERT(clip_data_entry);
2609 delegate = ClipboardGetDelegate(clip);
2610 WINPR_ASSERT(delegate);
2612 clip_data_dir = clip_data_entry->clip_data_dir;
2613 WINPR_ASSERT(clip_data_dir);
2615 free(file_context->exposed_path);
2616 file_context->exposed_path = GetCombinedPath(file_context->path, clip_data_dir->filename);
2617 if (file_context->exposed_path)
2618 WLog_Print(file_context->log, WLOG_DEBUG,
"Updated exposed path to \"%s\"",
2619 file_context->exposed_path);
2621 delegate->basePath = file_context->exposed_path;
2623 return delegate->basePath !=
nullptr;
2627BOOL cliprdr_file_context_update_server_data(CliprdrFileContext* file_context, wClipboard* clip,
2628 const void* data,
size_t size)
2630#if defined(WITH_FUSE)
2631 CliprdrFuseClipDataEntry* clip_data_entry =
nullptr;
2636 WINPR_ASSERT(file_context);
2638 if (size > UINT32_MAX)
2642 if (InterlockedCompareExchange(&file_context->fuse_failed, 0, 0) != 0)
2644 wClipboardDelegate* delegate = ClipboardGetDelegate(clip);
2645 WINPR_ASSERT(delegate);
2646 delegate->basePath =
nullptr;
2650 if (cliprdr_parse_file_list(data, (UINT32)size, &files, &n_files))
2652 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to parse file list");
2656 HashTable_Lock(file_context->inode_table);
2657 HashTable_Lock(file_context->clip_data_table);
2658 if (does_server_support_clipdata_locking(file_context))
2659 clip_data_entry = HashTable_GetItemValue(
2660 file_context->clip_data_table, (
void*)(uintptr_t)file_context->current_clip_data_id);
2662 clip_data_entry = file_context->clip_data_entry_without_id;
2666 if (!clip_data_entry)
2668 WLog_Print(file_context->log, WLOG_WARN,
"No clipDataEntry for the server file list");
2672 if (!clear_entry_selection(clip_data_entry))
2674 WINPR_ASSERT(!clip_data_entry->clip_data_dir);
2676 clip_data_entry->clip_data_dir =
2677 clip_data_dir_new(file_context, does_server_support_clipdata_locking(file_context),
2678 file_context->current_clip_data_id);
2679 if (!clip_data_entry->clip_data_dir)
2681 if (!update_exposed_path(file_context, clip, clip_data_entry))
2683 if (!set_selection_for_clip_data_entry(file_context, clip_data_entry, files, n_files))
2689 HashTable_Unlock(file_context->clip_data_table);
2690 HashTable_Unlock(file_context->inode_table);
2698void* cliprdr_file_context_get_context(CliprdrFileContext* file)
2701 return file->clipboard;
2704void cliprdr_file_session_terminate(CliprdrFileContext* file, BOOL stop_thread)
2709#if defined(WITH_FUSE)
2710 WINPR_ASSERT(file->fuse_stop_sync);
2712 WLog_Print(file->log, WLOG_DEBUG,
"Setting FUSE exit flag");
2713 if (file->fuse_sess)
2714 fuse_session_exit(file->fuse_sess);
2718 WLog_Print(file->log, WLOG_DEBUG,
"Setting FUSE stop event");
2719 (void)SetEvent(file->fuse_stop_sync);
2725#if defined(WITH_FUSE)
2726 WLog_Print(file->log, WLOG_DEBUG,
"Forcing FUSE to check exit flag");
2728 if (winpr_PathFileExists(file->path))
2729 WLog_Print(file->log, WLOG_WARN,
"winpr_PathFileExists(%s) failed", file->path);
2732void cliprdr_file_context_free(CliprdrFileContext* file)
2737#if defined(WITH_FUSE)
2738 if (file->fuse_thread)
2740 WINPR_ASSERT(file->fuse_stop_sync);
2742 WLog_Print(file->log, WLOG_DEBUG,
"Stopping FUSE thread");
2743 cliprdr_file_session_terminate(file, TRUE);
2745 WLog_Print(file->log, WLOG_DEBUG,
"Waiting on FUSE thread");
2746 (void)WaitForSingleObject(file->fuse_thread, INFINITE);
2747 (void)CloseHandle(file->fuse_thread);
2749 if (file->fuse_stop_sync)
2750 (void)CloseHandle(file->fuse_stop_sync);
2751 if (file->fuse_start_sync)
2752 (void)CloseHandle(file->fuse_start_sync);
2754 HashTable_Free(file->request_table);
2755 HashTable_Free(file->clip_data_table);
2756 HashTable_Free(file->dir_table);
2757 HashTable_Free(file->inode_table);
2760#if defined(WITH_CLIPRDR_FILE_APPLE)
2761 if (file->apple_contents_event)
2764 apple_cancel_downloads(file);
2765 cliprdr_file_apple_detach();
2767 free(file->apple_contents_data);
2768 if (file->apple_contents_event)
2770 (void)CloseHandle(file->apple_contents_event);
2771 DeleteCriticalSection(&file->apple_lock);
2774 HashTable_Free(file->local_streams);
2775 winpr_RemoveDirectory(file->path);
2777 free(file->exposed_path);
2781static BOOL create_base_path(CliprdrFileContext* file)
2785 const char* details = freerdp_getApplicationDetailsString();
2786 WINPR_ASSERT(details);
2790 char* base =
nullptr;
2792 winpr_asprintf(&base, &blen,
"%s.%" PRIu32
".%p", details, GetCurrentProcessId(), (
void*)file);
2796 file->path = GetKnownSubPath(KNOWN_PATH_TEMP, base);
2797 winpr_znfree(base, blen);
2801 if (!winpr_PathFileExists(file->path) && !winpr_PathMakePath(file->path,
nullptr))
2803 WLog_Print(file->log, WLOG_ERROR,
"Failed to create directory '%s'", file->path);
2809static void cliprdr_local_file_free(CliprdrLocalFile* file)
2811 const CliprdrLocalFile empty = WINPR_C_ARRAY_INIT;
2816 WLog_Print(file->context->log, WLOG_DEBUG,
"closing file %s, discarding entry", file->name);
2817 (void)fclose(file->fp);
2823static BOOL cliprdr_local_file_new(CliprdrFileContext* context, CliprdrLocalFile* f,
2826 const CliprdrLocalFile empty = WINPR_C_ARRAY_INIT;
2828 WINPR_ASSERT(context);
2832 f->context = context;
2833 f->name = _strdup(path);
2839 cliprdr_local_file_free(f);
2843static void cliprdr_local_files_free(CliprdrLocalStream* stream)
2845 WINPR_ASSERT(stream);
2847 for (
size_t x = 0; x < stream->count; x++)
2848 cliprdr_local_file_free(&stream->files[x]);
2849 free(stream->files);
2851 stream->files =
nullptr;
2855static void cliprdr_local_stream_free(
void* obj)
2857 CliprdrLocalStream* stream = (CliprdrLocalStream*)obj;
2859 cliprdr_local_files_free(stream);
2864static BOOL append_entry(CliprdrLocalStream* stream,
const char* path)
2866 CliprdrLocalFile* tmp = realloc(stream->files,
sizeof(CliprdrLocalFile) * (stream->count + 1));
2869 stream->files = tmp;
2870 CliprdrLocalFile* f = &stream->files[stream->count++];
2872 return cliprdr_local_file_new(stream->context, f, path);
2875static BOOL is_directory(
const char* path)
2877 WCHAR* wpath = ConvertUtf8ToWCharAlloc(path,
nullptr);
2881 HANDLE hFile = CreateFileW(wpath, 0, FILE_SHARE_DELETE,
nullptr, OPEN_EXISTING,
2882 FILE_ATTRIBUTE_NORMAL,
nullptr);
2885 if (hFile == INVALID_HANDLE_VALUE)
2889 const BOOL status = GetFileInformationByHandle(hFile, &fileInformation);
2890 (void)CloseHandle(hFile);
2894 return (fileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
2897static BOOL add_directory(CliprdrLocalStream* stream,
const char* path)
2899 char* wildcardpath = GetCombinedPath(path,
"*");
2902 WCHAR* wpath = ConvertUtf8ToWCharAlloc(wildcardpath,
nullptr);
2908 HANDLE hFind = FindFirstFileW(wpath, &FindFileData);
2911 if (hFind == INVALID_HANDLE_VALUE)
2915 char* next =
nullptr;
2917 WCHAR dotbuffer[6] = WINPR_C_ARRAY_INIT;
2918 WCHAR dotdotbuffer[6] = WINPR_C_ARRAY_INIT;
2919 const WCHAR* dot = InitializeConstWCharFromUtf8(
".", dotbuffer, ARRAYSIZE(dotbuffer));
2920 const WCHAR* dotdot = InitializeConstWCharFromUtf8(
"..", dotdotbuffer, ARRAYSIZE(dotdotbuffer));
2923 if (_wcscmp(FindFileData.cFileName, dot) == 0)
2925 if (_wcscmp(FindFileData.cFileName, dotdot) == 0)
2928 char cFileName[MAX_PATH] = WINPR_C_ARRAY_INIT;
2929 (void)ConvertWCharNToUtf8(FindFileData.cFileName, ARRAYSIZE(FindFileData.cFileName),
2930 cFileName, ARRAYSIZE(cFileName));
2933 next = GetCombinedPath(path, cFileName);
2937 if (!append_entry(stream, next))
2939 if (is_directory(next))
2941 if (!add_directory(stream, next))
2944 }
while (FindNextFileW(hFind, &FindFileData));
2954static BOOL cliprdr_local_stream_update(CliprdrLocalStream* stream,
const char* data,
size_t size)
2957 WINPR_ASSERT(stream);
2961 cliprdr_local_files_free(stream);
2963 stream->files = calloc(size,
sizeof(CliprdrLocalFile));
2967 char* copy = strndup(data, size);
2971 char* dname =
nullptr;
2972 char* saveptr =
nullptr;
2973 char* ptr = strtok_s(copy,
"\r\n", &saveptr);
2976 const char* cname = ptr;
2977 if (strncmp(
"file:///", ptr, 8) == 0)
2979 else if (strncmp(
"file:/", ptr, 6) == 0)
2982 const size_t nlen = strnlen(cname, size);
2984 dname = winpr_str_url_decode(cname, nlen);
2987 if (!append_entry(stream, dname))
2990 if (is_directory(dname))
2992 const BOOL res = add_directory(stream, dname);
2996 ptr = strtok_s(
nullptr,
"\r\n", &saveptr);
3006CliprdrLocalStream* cliprdr_local_stream_new(CliprdrFileContext* context, UINT32 streamID,
3007 const char* data,
size_t size)
3009 WINPR_ASSERT(context);
3010 CliprdrLocalStream* stream = calloc(1,
sizeof(CliprdrLocalStream));
3014 stream->context = context;
3015 if (!cliprdr_local_stream_update(stream, data, size))
3018 stream->lockId = streamID;
3022 cliprdr_local_stream_free(stream);
3026static UINT32 UINTPointerHash(
const void*
id)
3029 return *((
const UINT32*)
id);
3032static BOOL UINTPointerCompare(
const void* pointer1,
const void* pointer2)
3034 if (!pointer1 || !pointer2)
3035 return pointer1 == pointer2;
3037 const UINT32* a = pointer1;
3038 const UINT32* b = pointer2;
3042static void* UINTPointerClone(
const void* other)
3044 const UINT32* src = other;
3048 UINT32* copy = calloc(1,
sizeof(UINT32));
3056#if defined(WITH_FUSE)
3057CliprdrFuseFile* fuse_file_new_root(CliprdrFileContext* file_context)
3059 CliprdrFuseFile* root_dir = fuse_file_new(
"/");
3063 root_dir->ino = FUSE_ROOT_ID;
3064 root_dir->is_directory = TRUE;
3065 root_dir->is_readonly = TRUE;
3067 if (!HashTable_Insert(file_context->inode_table, (
void*)(uintptr_t)root_dir->ino, root_dir))
3069 fuse_file_free(root_dir);
3073 if (!HashTable_Insert(file_context->dir_table, root_dir->filename_with_root, root_dir))
3075 fuse_file_free(root_dir);
3083CliprdrFileContext* cliprdr_file_context_new(
void* context)
3085 CliprdrFileContext* file = calloc(1,
sizeof(CliprdrFileContext));
3089 file->log = WLog_Get(CLIENT_TAG(
"common.cliprdr.file"));
3090 file->clipboard = context;
3092 file->local_streams = HashTable_New(FALSE);
3093 if (!file->local_streams)
3096 if (!HashTable_SetHashFunction(file->local_streams, UINTPointerHash))
3100 wObject* hkobj = HashTable_KeyObject(file->local_streams);
3101 WINPR_ASSERT(hkobj);
3108 wObject* hobj = HashTable_ValueObject(file->local_streams);
3113#if defined(WITH_FUSE)
3114 file->inode_table = HashTable_New(FALSE);
3115 file->dir_table = HashTable_New(FALSE);
3116 file->clip_data_table = HashTable_New(FALSE);
3117 file->request_table = HashTable_New(FALSE);
3118 if (!file->inode_table || !file->dir_table || !file->clip_data_table || !file->request_table)
3122 wObject* ctobj = HashTable_ValueObject(file->request_table);
3123 WINPR_ASSERT(ctobj);
3127 wObject* ctobj = HashTable_ValueObject(file->clip_data_table);
3128 WINPR_ASSERT(ctobj);
3132 if (!HashTable_SetHashFunction(file->dir_table, HashTable_StringHash))
3134 wObject* kobj = HashTable_KeyObject(file->dir_table);
3140 if (!create_base_path(file))
3143#if defined(WITH_CLIPRDR_FILE_APPLE)
3144 if (!InitializeCriticalSectionAndSpinCount(&file->apple_lock, 4000))
3146 if (!(file->apple_contents_event = CreateEvent(
nullptr, TRUE, FALSE,
nullptr)))
3148 DeleteCriticalSection(&file->apple_lock);
3153#if defined(WITH_FUSE)
3154 if (!(file->fuse_start_sync = CreateEvent(
nullptr, TRUE, FALSE,
nullptr)))
3156 if (!(file->fuse_stop_sync = CreateEvent(
nullptr, TRUE, FALSE,
nullptr)))
3158 if (!(file->fuse_thread = CreateThread(
nullptr, 0, cliprdr_file_fuse_thread, file, 0,
nullptr)))
3161 if (WaitForSingleObject(file->fuse_start_sync, INFINITE) == WAIT_FAILED)
3162 WLog_Print(file->log, WLOG_ERROR,
"Failed to wait for start sync");
3167 WINPR_PRAGMA_DIAG_PUSH
3168 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
3169 cliprdr_file_context_free(file);
3170 WINPR_PRAGMA_DIAG_POP
3174BOOL local_stream_discard(
const void* key,
void* value,
void* arg)
3176 CliprdrFileContext* file = arg;
3177 CliprdrLocalStream* stream = value;
3179 WINPR_ASSERT(stream);
3181 if (!stream->locked)
3182 HashTable_Remove(file->local_streams, key);
3186BOOL cliprdr_file_context_clear(CliprdrFileContext* file)
3190 WLog_Print(file->log, WLOG_DEBUG,
"clear file clipboard...");
3192 HashTable_Lock(file->local_streams);
3193 const BOOL res = HashTable_Foreach(file->local_streams, local_stream_discard, file);
3194 HashTable_Unlock(file->local_streams);
3196 memset(file->server_data_hash, 0,
sizeof(file->server_data_hash));
3197 memset(file->client_data_hash, 0,
sizeof(file->client_data_hash));
3201BOOL cliprdr_file_context_update_client_data(CliprdrFileContext* file,
const char* data,
3207 if (!cliprdr_file_client_content_changed_and_update(file, data, size))
3210 if (!cliprdr_file_context_clear(file))
3213 UINT32 lockId = file->local_lock_id;
3215 HashTable_Lock(file->local_streams);
3216 CliprdrLocalStream* stream = HashTable_GetItemValue(file->local_streams, &lockId);
3218 WLog_Print(file->log, WLOG_DEBUG,
"update client file list (stream=%p)...", (
void*)stream);
3220 rc = cliprdr_local_stream_update(stream, data, size);
3223 stream = cliprdr_local_stream_new(file, lockId, data, size);
3224 rc = HashTable_Insert(file->local_streams, &stream->lockId, stream);
3226 cliprdr_local_stream_free(stream);
3230 HashTable_Unlock(file->local_streams);
3234UINT32 cliprdr_file_context_current_flags(CliprdrFileContext* file)
3238 if ((file->file_capability_flags & CB_STREAM_FILECLIP_ENABLED) == 0)
3241 if (!file->file_formats_registered)
3244 return CB_STREAM_FILECLIP_ENABLED | CB_FILECLIP_NO_FILE_PATHS |
3245 CB_HUGE_FILE_SUPPORT_ENABLED;
3248BOOL cliprdr_file_context_set_locally_available(CliprdrFileContext* file, BOOL available)
3251 file->file_formats_registered = available;
3255BOOL cliprdr_file_context_remote_set_flags(CliprdrFileContext* file, UINT32 flags)
3258 file->file_capability_flags = flags;
3262UINT32 cliprdr_file_context_remote_get_flags(CliprdrFileContext* file)
3265 return file->file_capability_flags;
3268BOOL cliprdr_file_context_has_local_support(CliprdrFileContext* file)
3270#if defined(WITH_FUSE)
3272 return InterlockedCompareExchange(&file->fuse_failed, 0, 0) == 0;
3279BOOL cliprdr_file_context_set_local_clipboard_data(CliprdrFileContext* file,
3280 cliprdr_file_context_data_fkt fkt,
3281 void* userdata,
const char** mime_types,
3286 WINPR_ASSERT(mime_types || (count == 0));
3288#if defined(WITH_CLIPRDR_FILE_APPLE)
3293 for (
size_t x = 0; x < count; x++)
3295 if (mime_types[x] && (strcmp(mime_types[x],
"text/uri-list") == 0))
3303 EnterCriticalSection(&file->apple_lock);
3304 const UINT32 generation = file->apple_generation;
3305 LeaveCriticalSection(&file->apple_lock);
3309 const void* data = fkt(userdata,
"FileGroupDescriptorW", &size);
3313 return apple_offer_server_files(file, generation, data, size, mime_types, count, fkt, userdata);
3317 WINPR_UNUSED(userdata);
3318 WINPR_UNUSED(mime_types);
3319 WINPR_UNUSED(count);
3324char* cliprdr_file_context_get_local_uri_list(CliprdrFileContext* file,
size_t* size)
3330#if defined(WITH_CLIPRDR_FILE_APPLE)
3331 return cliprdr_file_apple_get_uri_list(size);
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
This struct contains function pointer to initialize/free objects.
OBJECT_FREE_FN fnObjectFree
WINPR_ATTR_NODISCARD OBJECT_EQUALS_FN fnObjectEquals
WINPR_ATTR_NODISCARD OBJECT_NEW_FN fnObjectNew