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/image.h>
45#include <winpr/stream.h>
46#include <winpr/clipboard.h>
47#include <winpr/path.h>
49#include <freerdp/utils/signal.h>
50#include <freerdp/log.h>
51#include <freerdp/client/cliprdr.h>
52#include <freerdp/channels/channels.h>
53#include <freerdp/channels/cliprdr.h>
55#include <freerdp/client/client_cliprdr_file.h>
57#define NO_CLIP_DATA_ID (UINT64_C(1) << 32)
58#define WIN32_FILETIME_TO_UNIX_EPOCH INT64_C(11644473600)
60#ifdef WITH_DEBUG_CLIPRDR
61#define DEBUG_CLIPRDR(log, ...) WLog_Print(log, WLOG_DEBUG, __VA_ARGS__)
63#define DEBUG_CLIPRDR(log, ...) \
70typedef enum eFuseLowlevelOperationType
72 FUSE_LL_OPERATION_NONE,
73 FUSE_LL_OPERATION_LOOKUP,
74 FUSE_LL_OPERATION_GETATTR,
75 FUSE_LL_OPERATION_READ,
76} FuseLowlevelOperationType;
78typedef struct sCliprdrFuseFile CliprdrFuseFile;
80struct sCliprdrFuseFile
82 CliprdrFuseFile* parent;
88 size_t filename_with_root_len;
89 char* filename_with_root;
99 BOOL has_last_write_time;
100 INT64 last_write_time_unix;
102 BOOL has_clip_data_id;
108 CliprdrFileContext* file_context;
110 CliprdrFuseFile* clip_data_dir;
112 BOOL has_clip_data_id;
114} CliprdrFuseClipDataEntry;
118 CliprdrFileContext* file_context;
120 wArrayList* fuse_files;
123 BOOL has_clip_data_id;
125} FuseFileClearContext;
129 FuseLowlevelOperationType operation_type;
130 CliprdrFuseFile* fuse_file;
137 CliprdrFuseFile* parent;
139} CliprdrFuseFindParentContext;
147 CliprdrFileContext* context;
155 CliprdrLocalFile* files;
156 CliprdrFileContext* context;
159struct cliprdr_file_context
161#if defined(WITH_FUSE)
163 HANDLE fuse_start_sync;
164 HANDLE fuse_stop_sync;
166 struct fuse_session* fuse_sess;
167#if FUSE_USE_VERSION < 30
168 struct fuse_chan* ch;
171 wHashTable* inode_table;
172 wHashTable* clip_data_table;
173 wHashTable* request_table;
175 CliprdrFuseClipDataEntry* clip_data_entry_without_id;
176 UINT32 current_clip_data_id;
179 UINT32 next_clip_data_id;
180 UINT32 next_stream_id;
184 BOOL file_formats_registered;
185 UINT32 file_capability_flags;
187 UINT32 local_lock_id;
189 wHashTable* local_streams;
192 CliprdrClientContext* context;
195 BYTE server_data_hash[WINPR_SHA256_DIGEST_LENGTH];
196 BYTE client_data_hash[WINPR_SHA256_DIGEST_LENGTH];
199#if defined(WITH_FUSE)
200static CliprdrFuseFile* get_fuse_file_by_ino(CliprdrFileContext* file_context, fuse_ino_t fuse_ino);
202static void fuse_file_free(
void* data)
204 CliprdrFuseFile* fuse_file = data;
209 ArrayList_Free(fuse_file->children);
210 free(fuse_file->filename_with_root);
215WINPR_ATTR_FORMAT_ARG(1, 2)
216WINPR_ATTR_MALLOC(fuse_file_free, 1)
218static CliprdrFuseFile* fuse_file_new(WINPR_FORMAT_ARG const
char* fmt, ...)
220 CliprdrFuseFile* file = calloc(1,
sizeof(CliprdrFuseFile));
224 file->children = ArrayList_New(FALSE);
231 va_list ap = WINPR_C_ARRAY_INIT;
234 winpr_vasprintf(&file->filename_with_root, &file->filename_with_root_len, fmt, ap);
241 if (file->filename_with_root && (file->filename_with_root_len > 0))
243 file->filename_len = 0;
244 file->filename = strrchr(file->filename_with_root,
'/') + 1;
246 file->filename_len = strnlen(file->filename, file->filename_with_root_len);
250 fuse_file_free(file);
254static void clip_data_entry_free(
void* data)
256 CliprdrFuseClipDataEntry* clip_data_entry = data;
258 if (!clip_data_entry)
261 if (clip_data_entry->has_clip_data_id)
263 CliprdrFileContext* file_context = clip_data_entry->file_context;
266 WINPR_ASSERT(file_context);
268 unlock_clipboard_data.common.msgType = CB_UNLOCK_CLIPDATA;
269 unlock_clipboard_data.clipDataId = clip_data_entry->clip_data_id;
271 const UINT rc = file_context->context->ClientUnlockClipboardData(file_context->context,
272 &unlock_clipboard_data);
273 if (rc != CHANNEL_RC_OK)
274 WLog_Print(file_context->log, WLOG_DEBUG,
275 "ClientUnlockClipboardData failed with %" PRIu32, rc);
277 clip_data_entry->has_clip_data_id = FALSE;
279 WLog_Print(file_context->log, WLOG_DEBUG,
"Destroyed ClipDataEntry with id %u",
280 clip_data_entry->clip_data_id);
283 free(clip_data_entry);
286static BOOL does_server_support_clipdata_locking(CliprdrFileContext* file_context)
288 WINPR_ASSERT(file_context);
290 return (cliprdr_file_context_remote_get_flags(file_context) & CB_CAN_LOCK_CLIPDATA) != 0;
293static UINT32 get_next_free_clip_data_id(CliprdrFileContext* file_context)
295 UINT32 clip_data_id = 0;
297 WINPR_ASSERT(file_context);
299 HashTable_Lock(file_context->inode_table);
300 clip_data_id = file_context->next_clip_data_id;
301 while (clip_data_id == 0 ||
302 HashTable_GetItemValue(file_context->clip_data_table, (
void*)(uintptr_t)clip_data_id))
305 file_context->next_clip_data_id = clip_data_id + 1;
306 HashTable_Unlock(file_context->inode_table);
311static CliprdrFuseClipDataEntry* clip_data_entry_new(CliprdrFileContext* file_context,
312 BOOL needs_clip_data_id)
314 CliprdrFuseClipDataEntry* clip_data_entry =
nullptr;
317 WINPR_ASSERT(file_context);
319 clip_data_entry = calloc(1,
sizeof(CliprdrFuseClipDataEntry));
320 if (!clip_data_entry)
323 clip_data_entry->file_context = file_context;
324 clip_data_entry->clip_data_id = get_next_free_clip_data_id(file_context);
326 if (!needs_clip_data_id)
327 return clip_data_entry;
329 lock_clipboard_data.common.msgType = CB_LOCK_CLIPDATA;
330 lock_clipboard_data.clipDataId = clip_data_entry->clip_data_id;
332 if (file_context->context->ClientLockClipboardData(file_context->context, &lock_clipboard_data))
334 HashTable_Lock(file_context->inode_table);
335 clip_data_entry_free(clip_data_entry);
336 HashTable_Unlock(file_context->inode_table);
339 clip_data_entry->has_clip_data_id = TRUE;
341 WLog_Print(file_context->log, WLOG_DEBUG,
"Created ClipDataEntry with id %u",
342 clip_data_entry->clip_data_id);
344 return clip_data_entry;
347static BOOL should_remove_fuse_file(CliprdrFuseFile* fuse_file, BOOL all_files,
348 BOOL has_clip_data_id, UINT32 clip_data_id)
353 if (fuse_file->ino == FUSE_ROOT_ID)
355 if (!fuse_file->has_clip_data_id && !has_clip_data_id)
357 if (fuse_file->has_clip_data_id && has_clip_data_id &&
358 (fuse_file->clip_data_id == clip_data_id))
364static BOOL maybe_clear_fuse_request(
const void* key,
void* value,
void* arg)
366 CliprdrFuseRequest* fuse_request = value;
367 FuseFileClearContext* clear_context = arg;
368 CliprdrFileContext* file_context = clear_context->file_context;
369 CliprdrFuseFile* fuse_file = fuse_request->fuse_file;
371 WINPR_ASSERT(file_context);
373 if (!should_remove_fuse_file(fuse_file, clear_context->all_files,
374 clear_context->has_clip_data_id, clear_context->clip_data_id))
377 DEBUG_CLIPRDR(file_context->log,
"Clearing FileContentsRequest for file \"%s\"",
378 fuse_file->filename_with_root);
380 fuse_reply_err(fuse_request->fuse_req, EIO);
381 HashTable_Remove(file_context->request_table, key);
386static BOOL maybe_steal_inode(
const void* key,
void* value,
void* arg)
388 CliprdrFuseFile* fuse_file = value;
389 FuseFileClearContext* clear_context = arg;
390 CliprdrFileContext* file_context = clear_context->file_context;
392 WINPR_ASSERT(file_context);
394 if (should_remove_fuse_file(fuse_file, clear_context->all_files,
395 clear_context->has_clip_data_id, clear_context->clip_data_id))
397 if (!ArrayList_Append(clear_context->fuse_files, fuse_file))
398 WLog_Print(file_context->log, WLOG_ERROR,
399 "Failed to append FUSE file to list for deletion");
401 HashTable_Remove(file_context->inode_table, key);
407static BOOL notify_delete_child(
void* data, WINPR_ATTR_UNUSED
size_t index, va_list ap)
409 CliprdrFuseFile* child = data;
413 CliprdrFileContext* file_context = va_arg(ap, CliprdrFileContext*);
414 CliprdrFuseFile* parent = va_arg(ap, CliprdrFuseFile*);
416 WINPR_ASSERT(file_context);
417 WINPR_ASSERT(parent);
419 WINPR_ASSERT(file_context->fuse_sess);
420 fuse_lowlevel_notify_delete(file_context->fuse_sess, parent->ino, child->ino, child->filename,
421 child->filename_len);
426static BOOL invalidate_inode(
void* data, WINPR_ATTR_UNUSED
size_t index, va_list ap)
428 CliprdrFuseFile* fuse_file = data;
430 WINPR_ASSERT(fuse_file);
432 CliprdrFileContext* file_context = va_arg(ap, CliprdrFileContext*);
433 WINPR_ASSERT(file_context);
434 WINPR_ASSERT(file_context->fuse_sess);
436 ArrayList_ForEach(fuse_file->children, notify_delete_child, file_context, fuse_file);
438 DEBUG_CLIPRDR(file_context->log,
"Invalidating inode %lu for file \"%s\"", fuse_file->ino,
439 fuse_file->filename);
440 fuse_lowlevel_notify_inval_inode(file_context->fuse_sess, fuse_file->ino, 0, 0);
441 WLog_Print(file_context->log, WLOG_DEBUG,
"Inode %lu invalidated", fuse_file->ino);
446static void clear_selection(CliprdrFileContext* file_context, BOOL all_selections,
447 CliprdrFuseClipDataEntry* clip_data_entry)
449 FuseFileClearContext clear_context = WINPR_C_ARRAY_INIT;
450 CliprdrFuseFile* clip_data_dir =
nullptr;
452 WINPR_ASSERT(file_context);
454 clear_context.file_context = file_context;
455 clear_context.fuse_files = ArrayList_New(FALSE);
456 WINPR_ASSERT(clear_context.fuse_files);
458 wObject* aobj = ArrayList_Object(clear_context.fuse_files);
464 clip_data_dir = clip_data_entry->clip_data_dir;
465 clip_data_entry->clip_data_dir =
nullptr;
467 WINPR_ASSERT(clip_data_dir);
469 CliprdrFuseFile* root_dir = get_fuse_file_by_ino(file_context, FUSE_ROOT_ID);
471 ArrayList_Remove(root_dir->children, clip_data_dir);
473 clear_context.has_clip_data_id = clip_data_dir->has_clip_data_id;
474 clear_context.clip_data_id = clip_data_dir->clip_data_id;
476 clear_context.all_files = all_selections;
478 if (clip_data_entry && clip_data_entry->has_clip_data_id)
479 WLog_Print(file_context->log, WLOG_DEBUG,
"Clearing selection for clipDataId %u",
480 clip_data_entry->clip_data_id);
482 WLog_Print(file_context->log, WLOG_DEBUG,
"Clearing selection%s",
483 all_selections ?
"s" :
"");
485 HashTable_Foreach(file_context->request_table, maybe_clear_fuse_request, &clear_context);
486 HashTable_Foreach(file_context->inode_table, maybe_steal_inode, &clear_context);
487 HashTable_Unlock(file_context->inode_table);
489 if (file_context->fuse_sess)
499 ArrayList_ForEach(clear_context.fuse_files, invalidate_inode, file_context);
500 CliprdrFuseFile* root_dir = get_fuse_file_by_ino(file_context, FUSE_ROOT_ID);
501 if (clip_data_dir && root_dir)
503 fuse_lowlevel_notify_delete(file_context->fuse_sess, root_dir->ino, clip_data_dir->ino,
504 clip_data_dir->filename, clip_data_dir->filename_len);
507 ArrayList_Free(clear_context.fuse_files);
509 HashTable_Lock(file_context->inode_table);
510 if (clip_data_entry && clip_data_entry->has_clip_data_id)
511 WLog_Print(file_context->log, WLOG_DEBUG,
"Selection cleared for clipDataId %u",
512 clip_data_entry->clip_data_id);
514 WLog_Print(file_context->log, WLOG_DEBUG,
"Selection%s cleared", all_selections ?
"s" :
"");
517static void clear_entry_selection(CliprdrFuseClipDataEntry* clip_data_entry)
519 WINPR_ASSERT(clip_data_entry);
521 if (!clip_data_entry->clip_data_dir)
524 clear_selection(clip_data_entry->file_context, FALSE, clip_data_entry);
527static void clear_no_cdi_entry(CliprdrFileContext* file_context)
529 WINPR_ASSERT(file_context);
531 WINPR_ASSERT(file_context->inode_table);
532 HashTable_Lock(file_context->inode_table);
533 if (file_context->clip_data_entry_without_id)
535 clear_entry_selection(file_context->clip_data_entry_without_id);
537 clip_data_entry_free(file_context->clip_data_entry_without_id);
538 file_context->clip_data_entry_without_id =
nullptr;
540 HashTable_Unlock(file_context->inode_table);
543static BOOL clear_clip_data_entries(WINPR_ATTR_UNUSED
const void* key,
void* value,
544 WINPR_ATTR_UNUSED
void* arg)
546 clear_entry_selection(value);
551static void clear_cdi_entries(CliprdrFileContext* file_context)
553 WINPR_ASSERT(file_context);
555 HashTable_Lock(file_context->inode_table);
556 HashTable_Foreach(file_context->clip_data_table, clear_clip_data_entries,
nullptr);
558 HashTable_Clear(file_context->clip_data_table);
559 HashTable_Unlock(file_context->inode_table);
562static UINT prepare_clip_data_entry_with_id(CliprdrFileContext* file_context)
564 CliprdrFuseClipDataEntry* clip_data_entry =
nullptr;
566 WINPR_ASSERT(file_context);
568 clip_data_entry = clip_data_entry_new(file_context, TRUE);
569 if (!clip_data_entry)
571 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to create clipDataEntry");
572 return ERROR_INTERNAL_ERROR;
575 HashTable_Lock(file_context->inode_table);
576 if (!HashTable_Insert(file_context->clip_data_table,
577 (
void*)(uintptr_t)clip_data_entry->clip_data_id, clip_data_entry))
579 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to insert clipDataEntry");
580 clip_data_entry_free(clip_data_entry);
581 HashTable_Unlock(file_context->inode_table);
582 return ERROR_INTERNAL_ERROR;
584 HashTable_Unlock(file_context->inode_table);
588 file_context->current_clip_data_id = clip_data_entry->clip_data_id;
590 return CHANNEL_RC_OK;
593static UINT prepare_clip_data_entry_without_id(CliprdrFileContext* file_context)
595 WINPR_ASSERT(file_context);
596 WINPR_ASSERT(!file_context->clip_data_entry_without_id);
598 file_context->clip_data_entry_without_id = clip_data_entry_new(file_context, FALSE);
599 if (!file_context->clip_data_entry_without_id)
601 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to create clipDataEntry");
602 return ERROR_INTERNAL_ERROR;
605 return CHANNEL_RC_OK;
609UINT cliprdr_file_context_notify_new_server_format_list(CliprdrFileContext* file_context)
611 UINT rc = CHANNEL_RC_OK;
612 WINPR_ASSERT(file_context);
613 WINPR_ASSERT(file_context->context);
615#if defined(WITH_FUSE)
616 clear_no_cdi_entry(file_context);
618 clear_cdi_entries(file_context);
620 if (does_server_support_clipdata_locking(file_context))
621 rc = prepare_clip_data_entry_with_id(file_context);
623 rc = prepare_clip_data_entry_without_id(file_context);
628UINT cliprdr_file_context_notify_new_client_format_list(CliprdrFileContext* file_context)
630 WINPR_ASSERT(file_context);
631 WINPR_ASSERT(file_context->context);
633#if defined(WITH_FUSE)
634 clear_no_cdi_entry(file_context);
636 clear_cdi_entries(file_context);
639 return CHANNEL_RC_OK;
642static CliprdrLocalStream* cliprdr_local_stream_new(CliprdrFileContext* context, UINT32 streamID,
643 const char* data,
size_t size);
644static void cliprdr_file_session_terminate(CliprdrFileContext* file, BOOL stop_thread);
645static BOOL local_stream_discard(
const void* key,
void* value,
void* arg);
647WINPR_ATTR_FORMAT_ARG(6, 7)
648static
void writelog(wLog* log, DWORD level, const
char* fname, const
char* fkt,
size_t line,
649 WINPR_FORMAT_ARG const
char* fmt, ...)
651 if (!WLog_IsLevelActive(log, level))
654 va_list ap = WINPR_C_ARRAY_INIT;
656 WLog_PrintTextMessageVA(log, level, line, fname, fkt, fmt, ap);
660#if defined(WITH_FUSE)
661static CliprdrFuseFile* fuse_file_new_root(CliprdrFileContext* file_context);
662static void cliprdr_file_fuse_lookup(fuse_req_t req, fuse_ino_t parent,
const char* name);
663static void cliprdr_file_fuse_getattr(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info* fi);
664static void cliprdr_file_fuse_readdir(fuse_req_t req, fuse_ino_t ino,
size_t size, off_t off,
665 struct fuse_file_info* fi);
666static void cliprdr_file_fuse_read(fuse_req_t req, fuse_ino_t ino,
size_t size, off_t off,
667 struct fuse_file_info* fi);
668static void cliprdr_file_fuse_open(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info* fi);
669static void cliprdr_file_fuse_opendir(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info* fi);
671static const struct fuse_lowlevel_ops cliprdr_file_fuse_oper = {
672 .lookup = cliprdr_file_fuse_lookup,
673 .getattr = cliprdr_file_fuse_getattr,
674 .readdir = cliprdr_file_fuse_readdir,
675 .open = cliprdr_file_fuse_open,
676 .read = cliprdr_file_fuse_read,
677 .opendir = cliprdr_file_fuse_opendir,
680CliprdrFuseFile* get_fuse_file_by_ino(CliprdrFileContext* file_context, fuse_ino_t fuse_ino)
682 WINPR_ASSERT(file_context);
684 return HashTable_GetItemValue(file_context->inode_table, (
void*)(uintptr_t)fuse_ino);
687static CliprdrFuseFile*
688get_fuse_file_by_name_from_parent(WINPR_ATTR_UNUSED CliprdrFileContext* file_context,
689 CliprdrFuseFile* parent,
const char* name)
691 WINPR_ASSERT(file_context);
692 WINPR_ASSERT(parent);
694 for (
size_t i = 0; i < ArrayList_Count(parent->children); ++i)
696 CliprdrFuseFile* child = ArrayList_GetItem(parent->children, i);
700 if (strncmp(name, child->filename, child->filename_len + 1) == 0)
704 DEBUG_CLIPRDR(file_context->log,
"Requested file \"%s\" in directory \"%s\" does not exist",
705 name, parent->filename);
710static CliprdrFuseRequest* cliprdr_fuse_request_new(CliprdrFileContext* file_context,
711 CliprdrFuseFile* fuse_file, fuse_req_t fuse_req,
712 FuseLowlevelOperationType operation_type)
714 CliprdrFuseRequest* fuse_request =
nullptr;
715 UINT32 stream_id = file_context->next_stream_id;
717 WINPR_ASSERT(file_context);
718 WINPR_ASSERT(fuse_file);
720 fuse_request = calloc(1,
sizeof(CliprdrFuseRequest));
723 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to allocate FUSE request for file \"%s\"",
724 fuse_file->filename_with_root);
728 fuse_request->fuse_file = fuse_file;
729 fuse_request->fuse_req = fuse_req;
730 fuse_request->operation_type = operation_type;
732 while (stream_id == 0 ||
733 HashTable_GetItemValue(file_context->request_table, (
void*)(uintptr_t)stream_id))
735 fuse_request->stream_id = stream_id;
737 file_context->next_stream_id = stream_id + 1;
739 if (!HashTable_Insert(file_context->request_table, (
void*)(uintptr_t)fuse_request->stream_id,
742 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to track FUSE request for file \"%s\"",
743 fuse_file->filename_with_root);
751static BOOL request_file_size_async(CliprdrFileContext* file_context, CliprdrFuseFile* fuse_file,
752 fuse_req_t fuse_req, FuseLowlevelOperationType operation_type)
754 CliprdrFuseRequest* fuse_request =
nullptr;
757 WINPR_ASSERT(file_context);
758 WINPR_ASSERT(fuse_file);
760 fuse_request = cliprdr_fuse_request_new(file_context, fuse_file, fuse_req, operation_type);
764 file_contents_request.common.msgType = CB_FILECONTENTS_REQUEST;
765 file_contents_request.streamId = fuse_request->stream_id;
766 file_contents_request.listIndex = fuse_file->list_idx;
767 file_contents_request.dwFlags = FILECONTENTS_SIZE;
768 file_contents_request.cbRequested = 0x8;
769 file_contents_request.haveClipDataId = fuse_file->has_clip_data_id;
770 file_contents_request.clipDataId = fuse_file->clip_data_id;
772 if (file_context->context->ClientFileContentsRequest(file_context->context,
773 &file_contents_request))
775 WLog_Print(file_context->log, WLOG_ERROR,
776 "Failed to send FileContentsRequest for file \"%s\"",
777 fuse_file->filename_with_root);
778 HashTable_Remove(file_context->request_table, (
void*)(uintptr_t)fuse_request->stream_id);
782 DEBUG_CLIPRDR(file_context->log,
"Requested file size for file \"%s\" with stream id %u",
783 fuse_file->filename, fuse_request->stream_id);
789static void write_file_attributes(CliprdrFuseFile* fuse_file,
struct stat* attr)
791 memset(attr, 0,
sizeof(
struct stat));
796 attr->st_ino = fuse_file->ino;
797 if (fuse_file->is_directory)
799 attr->st_mode = S_IFDIR | (fuse_file->is_readonly ? 0555 : 0755);
804 attr->st_mode = S_IFREG | (fuse_file->is_readonly ? 0444 : 0644);
806 attr->st_size = WINPR_ASSERTING_INT_CAST(off_t, fuse_file->size);
808 attr->st_uid = getuid();
809 attr->st_gid = getgid();
810 attr->st_atime = attr->st_mtime = attr->st_ctime =
811 (fuse_file->has_last_write_time ? fuse_file->last_write_time_unix : time(
nullptr));
814static void cliprdr_file_fuse_lookup(fuse_req_t fuse_req, fuse_ino_t parent_ino,
const char* name)
816 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
817 CliprdrFuseFile* parent =
nullptr;
818 CliprdrFuseFile* fuse_file =
nullptr;
819 struct fuse_entry_param entry = WINPR_C_ARRAY_INIT;
821 WINPR_ASSERT(file_context);
823 HashTable_Lock(file_context->inode_table);
824 if (!(parent = get_fuse_file_by_ino(file_context, parent_ino)) || !parent->is_directory)
826 HashTable_Unlock(file_context->inode_table);
827 fuse_reply_err(fuse_req, ENOENT);
830 if (!(fuse_file = get_fuse_file_by_name_from_parent(file_context, parent, name)))
832 HashTable_Unlock(file_context->inode_table);
833 fuse_reply_err(fuse_req, ENOENT);
837 DEBUG_CLIPRDR(file_context->log,
"lookup() has been called for \"%s\"", name);
838 DEBUG_CLIPRDR(file_context->log,
"Parent is \"%s\", child is \"%s\"",
839 parent->filename_with_root, fuse_file->filename_with_root);
841 if (!fuse_file->is_directory && !fuse_file->has_size)
846 request_file_size_async(file_context, fuse_file, fuse_req, FUSE_LL_OPERATION_LOOKUP);
847 HashTable_Unlock(file_context->inode_table);
850 fuse_reply_err(fuse_req, EIO);
855 entry.ino = fuse_file->ino;
856 write_file_attributes(fuse_file, &entry.attr);
857 entry.attr_timeout = 1.0;
858 entry.entry_timeout = 1.0;
859 HashTable_Unlock(file_context->inode_table);
861 fuse_reply_entry(fuse_req, &entry);
864static void cliprdr_file_fuse_getattr(fuse_req_t fuse_req, fuse_ino_t fuse_ino,
865 WINPR_ATTR_UNUSED
struct fuse_file_info* file_info)
867 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
868 CliprdrFuseFile* fuse_file =
nullptr;
869 struct stat attr = WINPR_C_ARRAY_INIT;
871 WINPR_ASSERT(file_context);
873 HashTable_Lock(file_context->inode_table);
874 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
876 HashTable_Unlock(file_context->inode_table);
877 fuse_reply_err(fuse_req, ENOENT);
881 DEBUG_CLIPRDR(file_context->log,
"getattr() has been called for file \"%s\"",
882 fuse_file->filename_with_root);
884 if (!fuse_file->is_directory && !fuse_file->has_size)
889 request_file_size_async(file_context, fuse_file, fuse_req, FUSE_LL_OPERATION_GETATTR);
890 HashTable_Unlock(file_context->inode_table);
893 fuse_reply_err(fuse_req, EIO);
898 write_file_attributes(fuse_file, &attr);
899 HashTable_Unlock(file_context->inode_table);
901 fuse_reply_attr(fuse_req, &attr, 1.0);
904static void cliprdr_file_fuse_open(fuse_req_t fuse_req, fuse_ino_t fuse_ino,
905 struct fuse_file_info* file_info)
907 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
908 CliprdrFuseFile* fuse_file =
nullptr;
910 WINPR_ASSERT(file_context);
912 HashTable_Lock(file_context->inode_table);
913 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
915 HashTable_Unlock(file_context->inode_table);
916 fuse_reply_err(fuse_req, ENOENT);
919 if (fuse_file->is_directory)
921 HashTable_Unlock(file_context->inode_table);
922 fuse_reply_err(fuse_req, EISDIR);
925 HashTable_Unlock(file_context->inode_table);
927 if ((file_info->flags & O_ACCMODE) != O_RDONLY)
929 fuse_reply_err(fuse_req, EACCES);
934 file_info->direct_io = 1;
936 fuse_reply_open(fuse_req, file_info);
939static BOOL request_file_range_async(CliprdrFileContext* file_context, CliprdrFuseFile* fuse_file,
940 fuse_req_t fuse_req, off_t offset,
size_t requested_size)
944 WINPR_ASSERT(file_context);
945 WINPR_ASSERT(fuse_file);
947 if (requested_size > UINT32_MAX)
950 CliprdrFuseRequest* fuse_request =
951 cliprdr_fuse_request_new(file_context, fuse_file, fuse_req, FUSE_LL_OPERATION_READ);
955 file_contents_request.common.msgType = CB_FILECONTENTS_REQUEST;
956 file_contents_request.streamId = fuse_request->stream_id;
957 file_contents_request.listIndex = fuse_file->list_idx;
958 file_contents_request.dwFlags = FILECONTENTS_RANGE;
959 file_contents_request.nPositionLow = (UINT32)(offset & 0xFFFFFFFF);
960 file_contents_request.nPositionHigh = (UINT32)((offset >> 32) & 0xFFFFFFFF);
961 file_contents_request.cbRequested = (UINT32)requested_size;
962 file_contents_request.haveClipDataId = fuse_file->has_clip_data_id;
963 file_contents_request.clipDataId = fuse_file->clip_data_id;
965 if (file_context->context->ClientFileContentsRequest(file_context->context,
966 &file_contents_request))
968 WLog_Print(file_context->log, WLOG_ERROR,
969 "Failed to send FileContentsRequest for file \"%s\"",
970 fuse_file->filename_with_root);
971 HashTable_Remove(file_context->request_table, (
void*)(uintptr_t)fuse_request->stream_id);
979 "Requested file range (%zu Bytes at offset %lu) for file \"%s\" with stream id %u",
980 requested_size, offset, fuse_file->filename, fuse_request->stream_id);
986static void cliprdr_file_fuse_read(fuse_req_t fuse_req, fuse_ino_t fuse_ino,
size_t size,
987 off_t offset, WINPR_ATTR_UNUSED
struct fuse_file_info* file_info)
989 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
990 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 if (!fuse_file->has_size || (offset < 0) || ((
size_t)offset > fuse_file->size))
1010 HashTable_Unlock(file_context->inode_table);
1011 fuse_reply_err(fuse_req, EINVAL);
1015 size = MIN(size, 8ULL * 1024ULL * 1024ULL);
1017 result = request_file_range_async(file_context, fuse_file, fuse_req, offset, size);
1018 HashTable_Unlock(file_context->inode_table);
1021 fuse_reply_err(fuse_req, EIO);
1024static void cliprdr_file_fuse_opendir(fuse_req_t fuse_req, fuse_ino_t fuse_ino,
1025 struct fuse_file_info* file_info)
1027 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
1028 CliprdrFuseFile* fuse_file =
nullptr;
1030 WINPR_ASSERT(file_context);
1032 HashTable_Lock(file_context->inode_table);
1033 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
1035 HashTable_Unlock(file_context->inode_table);
1036 fuse_reply_err(fuse_req, ENOENT);
1039 if (!fuse_file->is_directory)
1041 HashTable_Unlock(file_context->inode_table);
1042 fuse_reply_err(fuse_req, ENOTDIR);
1045 HashTable_Unlock(file_context->inode_table);
1047 if ((file_info->flags & O_ACCMODE) != O_RDONLY)
1049 fuse_reply_err(fuse_req, EACCES);
1053 fuse_reply_open(fuse_req, file_info);
1056static void cliprdr_file_fuse_readdir(fuse_req_t fuse_req, fuse_ino_t fuse_ino,
size_t max_size,
1058 WINPR_ATTR_UNUSED
struct fuse_file_info* file_info)
1060 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
1061 CliprdrFuseFile* fuse_file =
nullptr;
1062 CliprdrFuseFile* child =
nullptr;
1063 struct stat attr = WINPR_C_ARRAY_INIT;
1064 size_t written_size = 0;
1065 size_t entry_size = 0;
1066 char* filename =
nullptr;
1067 char* buf =
nullptr;
1069 WINPR_ASSERT(file_context);
1071 HashTable_Lock(file_context->inode_table);
1072 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
1074 HashTable_Unlock(file_context->inode_table);
1075 fuse_reply_err(fuse_req, ENOENT);
1078 if (!fuse_file->is_directory)
1080 HashTable_Unlock(file_context->inode_table);
1081 fuse_reply_err(fuse_req, ENOTDIR);
1085 DEBUG_CLIPRDR(file_context->log,
"Reading directory \"%s\" at offset %lu",
1086 fuse_file->filename_with_root, offset);
1088 if ((offset < 0) || ((
size_t)offset >= ArrayList_Count(fuse_file->children)))
1090 HashTable_Unlock(file_context->inode_table);
1091 fuse_reply_buf(fuse_req,
nullptr, 0);
1095 buf = calloc(max_size,
sizeof(
char));
1098 HashTable_Unlock(file_context->inode_table);
1099 fuse_reply_err(fuse_req, ENOMEM);
1104 for (off_t i = offset; i < 2; ++i)
1108 write_file_attributes(fuse_file, &attr);
1113 write_file_attributes(fuse_file->parent, &attr);
1114 attr.st_ino = fuse_file->parent ? attr.st_ino : FUSE_ROOT_ID;
1115 attr.st_mode = fuse_file->parent ? attr.st_mode : 0555;
1120 WINPR_ASSERT(FALSE);
1127 entry_size = fuse_add_direntry(fuse_req, buf + written_size, max_size - written_size,
1128 filename, &attr, i + 1);
1129 if (entry_size > max_size - written_size)
1132 written_size += entry_size;
1135 for (
size_t j = 0, i = 2; j < ArrayList_Count(fuse_file->children); ++j, ++i)
1137 if (i < (
size_t)offset)
1140 child = ArrayList_GetItem(fuse_file->children, j);
1142 write_file_attributes(child, &attr);
1144 fuse_add_direntry(fuse_req, buf + written_size, max_size - written_size,
1145 child->filename, &attr, WINPR_ASSERTING_INT_CAST(off_t, i + 1));
1146 if (entry_size > max_size - written_size)
1149 written_size += entry_size;
1151 HashTable_Unlock(file_context->inode_table);
1153 fuse_reply_buf(fuse_req, buf, written_size);
1157static void fuse_abort(
int sig,
const char* signame,
void* context)
1159 CliprdrFileContext* file = (CliprdrFileContext*)context;
1163 WLog_Print(file->log, WLOG_INFO,
"signal %s [%d] aborting session", signame, sig);
1164 cliprdr_file_session_terminate(file, FALSE);
1168static DWORD WINAPI cliprdr_file_fuse_thread(LPVOID arg)
1170 CliprdrFileContext* file = (CliprdrFileContext*)arg;
1174 DEBUG_CLIPRDR(file->log,
"Starting fuse with mountpoint '%s'", file->path);
1176 struct fuse_args args = FUSE_ARGS_INIT(0,
nullptr);
1177 fuse_opt_add_arg(&args, file->path);
1178 file->fuse_sess = fuse_session_new(&args, &cliprdr_file_fuse_oper,
1179 sizeof(cliprdr_file_fuse_oper), (
void*)file);
1180 (void)SetEvent(file->fuse_start_sync);
1182 if (file->fuse_sess !=
nullptr)
1184 if (freerdp_add_signal_cleanup_handler(file, fuse_abort))
1186 if (0 == fuse_session_mount(file->fuse_sess, file->path))
1188 fuse_session_loop(file->fuse_sess);
1189 fuse_session_unmount(file->fuse_sess);
1191 freerdp_del_signal_cleanup_handler(file, fuse_abort);
1194 WLog_Print(file->log, WLOG_DEBUG,
"Waiting for FUSE stop sync");
1195 if (WaitForSingleObject(file->fuse_stop_sync, INFINITE) == WAIT_FAILED)
1196 WLog_Print(file->log, WLOG_ERROR,
"Failed to wait for stop sync");
1197 fuse_session_destroy(file->fuse_sess);
1199 fuse_opt_free_args(&args);
1201 DEBUG_CLIPRDR(file->log,
"Quitting fuse with mountpoint '%s'", file->path);
1207static UINT cliprdr_file_context_server_file_contents_response(
1208 CliprdrClientContext* cliprdr_context,
1211 CliprdrFileContext* file_context =
nullptr;
1212 CliprdrFuseRequest* fuse_request =
nullptr;
1213 struct fuse_entry_param entry = WINPR_C_ARRAY_INIT;
1215 WINPR_ASSERT(cliprdr_context);
1216 WINPR_ASSERT(file_contents_response);
1218 file_context = cliprdr_context->custom;
1219 WINPR_ASSERT(file_context);
1221 HashTable_Lock(file_context->inode_table);
1222 fuse_request = HashTable_GetItemValue(file_context->request_table,
1223 (
void*)(uintptr_t)file_contents_response->streamId);
1226 HashTable_Unlock(file_context->inode_table);
1227 return CHANNEL_RC_OK;
1230 if (!(file_contents_response->common.msgFlags & CB_RESPONSE_OK))
1232 WLog_Print(file_context->log, WLOG_WARN,
1233 "FileContentsRequests for file \"%s\" was unsuccessful",
1234 fuse_request->fuse_file->filename);
1236 fuse_reply_err(fuse_request->fuse_req, EIO);
1237 HashTable_Remove(file_context->request_table,
1238 (
void*)(uintptr_t)file_contents_response->streamId);
1239 HashTable_Unlock(file_context->inode_table);
1240 return CHANNEL_RC_OK;
1243 if ((fuse_request->operation_type == FUSE_LL_OPERATION_LOOKUP ||
1244 fuse_request->operation_type == FUSE_LL_OPERATION_GETATTR) &&
1245 file_contents_response->cbRequested !=
sizeof(UINT64))
1247 WLog_Print(file_context->log, WLOG_WARN,
1248 "Received invalid file size for file \"%s\" from the client",
1249 fuse_request->fuse_file->filename);
1250 fuse_reply_err(fuse_request->fuse_req, EIO);
1251 HashTable_Remove(file_context->request_table,
1252 (
void*)(uintptr_t)file_contents_response->streamId);
1253 HashTable_Unlock(file_context->inode_table);
1254 return CHANNEL_RC_OK;
1257 if (fuse_request->operation_type == FUSE_LL_OPERATION_LOOKUP ||
1258 fuse_request->operation_type == FUSE_LL_OPERATION_GETATTR)
1260 DEBUG_CLIPRDR(file_context->log,
"Received file size for file \"%s\" with stream id %u",
1261 fuse_request->fuse_file->filename, file_contents_response->streamId);
1263 fuse_request->fuse_file->size = *((
const UINT64*)file_contents_response->requestedData);
1264 fuse_request->fuse_file->has_size = TRUE;
1266 entry.ino = fuse_request->fuse_file->ino;
1267 write_file_attributes(fuse_request->fuse_file, &entry.attr);
1268 entry.attr_timeout = 1.0;
1269 entry.entry_timeout = 1.0;
1271 else if (fuse_request->operation_type == FUSE_LL_OPERATION_READ)
1273 DEBUG_CLIPRDR(file_context->log,
"Received file range for file \"%s\" with stream id %u",
1274 fuse_request->fuse_file->filename, file_contents_response->streamId);
1276 HashTable_Unlock(file_context->inode_table);
1278 switch (fuse_request->operation_type)
1280 case FUSE_LL_OPERATION_NONE:
1282 case FUSE_LL_OPERATION_LOOKUP:
1283 fuse_reply_entry(fuse_request->fuse_req, &entry);
1285 case FUSE_LL_OPERATION_GETATTR:
1286 fuse_reply_attr(fuse_request->fuse_req, &entry.attr, entry.attr_timeout);
1288 case FUSE_LL_OPERATION_READ:
1289 fuse_reply_buf(fuse_request->fuse_req,
1290 (
const char*)file_contents_response->requestedData,
1291 file_contents_response->cbRequested);
1297 HashTable_Remove(file_context->request_table,
1298 (
void*)(uintptr_t)file_contents_response->streamId);
1300 return CHANNEL_RC_OK;
1304static UINT cliprdr_file_context_send_file_contents_failure(
1310 WINPR_ASSERT(fileContentsRequest);
1312 const UINT64 offset = (((UINT64)fileContentsRequest->nPositionHigh) << 32) |
1313 ((UINT64)fileContentsRequest->nPositionLow);
1314 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1315 "server file contents request [lockID %" PRIu32
", streamID %" PRIu32
1316 ", index %" PRIu32
"] offset %" PRIu64
", size %" PRIu32
" failed",
1317 fileContentsRequest->clipDataId, fileContentsRequest->streamId,
1318 fileContentsRequest->listIndex, offset, fileContentsRequest->cbRequested);
1320 response.common.msgFlags = CB_RESPONSE_FAIL;
1321 response.streamId = fileContentsRequest->streamId;
1323 WINPR_ASSERT(file->context);
1324 WINPR_ASSERT(file->context->ClientFileContentsResponse);
1325 return file->context->ClientFileContentsResponse(file->context, &response);
1329cliprdr_file_context_send_contents_response(CliprdrFileContext* file,
1331 const void* data,
size_t size)
1333 if (size > UINT32_MAX)
1334 return ERROR_INVALID_PARAMETER;
1337 .requestedData = data,
1338 .cbRequested = (UINT32)size,
1339 .common.msgFlags = CB_RESPONSE_OK };
1341 WINPR_ASSERT(request);
1344 WLog_Print(file->log, WLOG_DEBUG,
"send contents response streamID=%" PRIu32
", size=%" PRIu32,
1345 response.streamId, response.cbRequested);
1346 WINPR_ASSERT(file->context);
1347 WINPR_ASSERT(file->context->ClientFileContentsResponse);
1348 return file->context->ClientFileContentsResponse(file->context, &response);
1351static BOOL dump_streams(
const void* key,
void* value, WINPR_ATTR_UNUSED
void* arg)
1353 const UINT32* ukey = key;
1354 CliprdrLocalStream* cur = value;
1356 writelog(cur->context->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1357 "[key %" PRIu32
"] lockID %" PRIu32
", count %" PRIuz
", locked %d", *ukey,
1358 cur->lockId, cur->count, cur->locked);
1359 for (
size_t x = 0; x < cur->count; x++)
1361 const CliprdrLocalFile* file = &cur->files[x];
1362 writelog(cur->context->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1363 "file [%" PRIuz
"] %s: %" PRId64, x, file->name, file->size);
1368static CliprdrLocalFile* file_info_for_request(CliprdrFileContext* file, UINT32 lockId,
1373 CliprdrLocalStream* cur = HashTable_GetItemValue(file->local_streams, &lockId);
1376 if (listIndex < cur->count)
1378 CliprdrLocalFile* f = &cur->files[listIndex];
1383 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1384 "invalid entry index for lockID %" PRIu32
", index %" PRIu32
" [count %" PRIuz
1386 lockId, listIndex, cur->count, cur->locked);
1391 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1392 "missing entry for lockID %" PRIu32
", index %" PRIu32, lockId, listIndex);
1393 HashTable_Foreach(file->local_streams, dump_streams, file);
1399static CliprdrLocalFile* file_for_request(CliprdrFileContext* file, UINT32 lockId, UINT32 listIndex)
1401 CliprdrLocalFile* f = file_info_for_request(file, lockId, listIndex);
1406 const char* name = f->name;
1407 f->fp = winpr_fopen(name,
"rb");
1411 char ebuffer[256] = WINPR_C_ARRAY_INIT;
1412 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1413 "[lockID %" PRIu32
", index %" PRIu32
1414 "] failed to open file '%s' [size %" PRId64
"] %s [%d]",
1415 lockId, listIndex, f->name, f->size,
1416 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)), errno);
1424static void cliprdr_local_file_try_close(CliprdrLocalFile* file, UINT res, UINT64 offset,
1431 WINPR_ASSERT(file->context);
1432 WLog_Print(file->context->log, WLOG_DEBUG,
"closing file %s after error %" PRIu32,
1435 else if (((file->size > 0) && (offset + size >= (UINT64)file->size)))
1437 WINPR_ASSERT(file->context);
1438 WLog_Print(file->context->log, WLOG_DEBUG,
"closing file %s after read", file->name);
1446 (void)fclose(file->fp);
1450static UINT cliprdr_file_context_server_file_size_request(
1453 WINPR_ASSERT(fileContentsRequest);
1455 if (fileContentsRequest->cbRequested !=
sizeof(UINT64))
1457 WLog_Print(file->log, WLOG_WARN,
"unexpected FILECONTENTS_SIZE request: %" PRIu32
" bytes",
1458 fileContentsRequest->cbRequested);
1461 HashTable_Lock(file->local_streams);
1463 UINT res = CHANNEL_RC_OK;
1464 CliprdrLocalFile* rfile =
1465 file_for_request(file, fileContentsRequest->clipDataId, fileContentsRequest->listIndex);
1467 res = cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1470 if (_fseeki64(rfile->fp, 0, SEEK_END) < 0)
1471 res = cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1474 const INT64 size = _ftelli64(rfile->fp);
1476 cliprdr_local_file_try_close(rfile, res, 0, 0);
1478 res = cliprdr_file_context_send_contents_response(file, fileContentsRequest, &size,
1483 HashTable_Unlock(file->local_streams);
1487static UINT cliprdr_file_context_server_file_range_request(
1490 BYTE* data =
nullptr;
1492 WINPR_ASSERT(fileContentsRequest);
1494 HashTable_Lock(file->local_streams);
1495 const UINT64 offset = (((UINT64)fileContentsRequest->nPositionHigh) << 32) |
1496 ((UINT64)fileContentsRequest->nPositionLow);
1498 CliprdrLocalFile* rfile =
1499 file_for_request(file, fileContentsRequest->clipDataId, fileContentsRequest->listIndex);
1503 if (_fseeki64(rfile->fp, WINPR_ASSERTING_INT_CAST(int64_t, offset), SEEK_SET) < 0)
1506 data = malloc(fileContentsRequest->cbRequested);
1511 const size_t r = fread(data, 1, fileContentsRequest->cbRequested, rfile->fp);
1513 cliprdr_file_context_send_contents_response(file, fileContentsRequest, data, r);
1516 cliprdr_local_file_try_close(rfile, rc, offset, fileContentsRequest->cbRequested);
1517 HashTable_Unlock(file->local_streams);
1522 cliprdr_local_file_try_close(rfile, ERROR_INTERNAL_ERROR, offset,
1523 fileContentsRequest->cbRequested);
1525 HashTable_Unlock(file->local_streams);
1526 return cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1529static void cliprdr_local_stream_free(
void* obj);
1531static UINT change_lock(CliprdrFileContext* file, UINT32 lockId, BOOL lock)
1533 UINT rc = CHANNEL_RC_OK;
1537 HashTable_Lock(file->local_streams);
1540 CliprdrLocalStream* stream = HashTable_GetItemValue(file->local_streams, &lockId);
1541 if (lock && !stream)
1543 stream = cliprdr_local_stream_new(file, lockId,
nullptr, 0);
1544 if (!HashTable_Insert(file->local_streams, &lockId, stream))
1546 rc = ERROR_INTERNAL_ERROR;
1547 cliprdr_local_stream_free(stream);
1550 file->local_lock_id = lockId;
1554 stream->locked = lock;
1555 stream->lockId = lockId;
1562 if (!HashTable_Foreach(file->local_streams, local_stream_discard, file))
1563 rc = ERROR_INTERNAL_ERROR;
1566 HashTable_Unlock(file->local_streams);
1570static UINT cliprdr_file_context_lock(CliprdrClientContext* context,
1573 WINPR_ASSERT(context);
1574 WINPR_ASSERT(lockClipboardData);
1575 CliprdrFileContext* file = (context->custom);
1576 return change_lock(file, lockClipboardData->clipDataId, TRUE);
1579static UINT cliprdr_file_context_unlock(CliprdrClientContext* context,
1582 WINPR_ASSERT(context);
1583 WINPR_ASSERT(unlockClipboardData);
1584 CliprdrFileContext* file = (context->custom);
1585 return change_lock(file, unlockClipboardData->clipDataId, FALSE);
1588static UINT cliprdr_file_context_server_file_contents_request(
1591 UINT error = NO_ERROR;
1593 WINPR_ASSERT(context);
1594 WINPR_ASSERT(fileContentsRequest);
1596 CliprdrFileContext* file = (context->custom);
1603 if ((fileContentsRequest->dwFlags & (FILECONTENTS_SIZE | FILECONTENTS_RANGE)) ==
1604 (FILECONTENTS_SIZE | FILECONTENTS_RANGE))
1606 WLog_Print(file->log, WLOG_ERROR,
"invalid CLIPRDR_FILECONTENTS_REQUEST.dwFlags");
1607 return cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1610 if (fileContentsRequest->dwFlags & FILECONTENTS_SIZE)
1611 error = cliprdr_file_context_server_file_size_request(file, fileContentsRequest);
1613 if (fileContentsRequest->dwFlags & FILECONTENTS_RANGE)
1614 error = cliprdr_file_context_server_file_range_request(file, fileContentsRequest);
1618 WLog_Print(file->log, WLOG_ERROR,
"failed to handle CLIPRDR_FILECONTENTS_REQUEST: 0x%08X",
1620 return cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1623 return CHANNEL_RC_OK;
1626BOOL cliprdr_file_context_init(CliprdrFileContext* file, CliprdrClientContext* cliprdr)
1629 WINPR_ASSERT(cliprdr);
1631 cliprdr->custom = file;
1632 file->context = cliprdr;
1634 cliprdr->ServerLockClipboardData = cliprdr_file_context_lock;
1635 cliprdr->ServerUnlockClipboardData = cliprdr_file_context_unlock;
1636 cliprdr->ServerFileContentsRequest = cliprdr_file_context_server_file_contents_request;
1637#if defined(WITH_FUSE)
1638 cliprdr->ServerFileContentsResponse = cliprdr_file_context_server_file_contents_response;
1640 CliprdrFuseFile* root_dir = fuse_file_new_root(file);
1641 return root_dir !=
nullptr;
1647#if defined(WITH_FUSE)
1648static void clear_all_selections(CliprdrFileContext* file_context)
1650 WINPR_ASSERT(file_context);
1651 WINPR_ASSERT(file_context->inode_table);
1653 HashTable_Lock(file_context->inode_table);
1654 clear_selection(file_context, TRUE,
nullptr);
1656 HashTable_Clear(file_context->clip_data_table);
1657 HashTable_Unlock(file_context->inode_table);
1661BOOL cliprdr_file_context_uninit(CliprdrFileContext* file, CliprdrClientContext* cliprdr)
1664 WINPR_ASSERT(cliprdr);
1668#if defined(WITH_FUSE)
1669 if (file->inode_table)
1671 clear_no_cdi_entry(file);
1672 clear_all_selections(file);
1676 HashTable_Clear(file->local_streams);
1678 file->context =
nullptr;
1680#if defined(WITH_FUSE)
1681 cliprdr->ServerFileContentsResponse =
nullptr;
1687static BOOL cliprdr_file_content_changed_and_update(
void* ihash,
size_t hsize,
const void* data,
1691 BYTE hash[WINPR_SHA256_DIGEST_LENGTH] = WINPR_C_ARRAY_INIT;
1693 if (hsize <
sizeof(hash))
1696 if (!winpr_Digest(WINPR_MD_SHA256, data, size, hash,
sizeof(hash)))
1699 const BOOL changed = memcmp(hash, ihash,
sizeof(hash)) != 0;
1701 memcpy(ihash, hash,
sizeof(hash));
1705static BOOL cliprdr_file_client_content_changed_and_update(CliprdrFileContext* file,
1706 const void* data,
size_t size)
1709 return cliprdr_file_content_changed_and_update(file->client_data_hash,
1710 sizeof(file->client_data_hash), data, size);
1713#if defined(WITH_FUSE)
1714static fuse_ino_t get_next_free_inode(CliprdrFileContext* file_context)
1718 WINPR_ASSERT(file_context);
1720 ino = file_context->next_ino;
1721 while (ino == 0 || ino == FUSE_ROOT_ID ||
1722 HashTable_GetItemValue(file_context->inode_table, (
void*)(uintptr_t)ino))
1725 file_context->next_ino = ino + 1;
1730static CliprdrFuseFile* clip_data_dir_new(CliprdrFileContext* file_context, BOOL has_clip_data_id,
1731 UINT32 clip_data_id)
1733 WINPR_ASSERT(file_context);
1735 UINT64 data_id = clip_data_id;
1736 if (!has_clip_data_id)
1737 data_id = NO_CLIP_DATA_ID;
1739 CliprdrFuseFile* clip_data_dir = fuse_file_new(
"/%" PRIu64, data_id);
1743 clip_data_dir->ino = get_next_free_inode(file_context);
1744 clip_data_dir->is_directory = TRUE;
1745 clip_data_dir->is_readonly = TRUE;
1746 clip_data_dir->has_clip_data_id = has_clip_data_id;
1747 clip_data_dir->clip_data_id = clip_data_id;
1749 CliprdrFuseFile* root_dir = get_fuse_file_by_ino(file_context, FUSE_ROOT_ID);
1752 WLog_Print(file_context->log, WLOG_ERROR,
"FUSE root directory missing");
1753 fuse_file_free(clip_data_dir);
1756 if (!ArrayList_Append(root_dir->children, clip_data_dir))
1758 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to append FUSE file");
1759 fuse_file_free(clip_data_dir);
1762 clip_data_dir->parent = root_dir;
1764 if (!HashTable_Insert(file_context->inode_table, (
void*)(uintptr_t)clip_data_dir->ino,
1767 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to insert inode into inode table");
1768 ArrayList_Remove(root_dir->children, clip_data_dir);
1769 fuse_file_free(clip_data_dir);
1773 return clip_data_dir;
1776static char* get_parent_path(
const char* filepath)
1778 const char* base = strrchr(filepath,
'/');
1781 while ((base > filepath) && (*base ==
'/'))
1784 WINPR_ASSERT(base >= filepath);
1785 const size_t parent_path_length = 1ULL + (size_t)(base - filepath);
1786 char* parent_path = calloc(parent_path_length + 1,
sizeof(
char));
1790 memcpy(parent_path, filepath, parent_path_length);
1795static BOOL is_fuse_file_not_parent(WINPR_ATTR_UNUSED
const void* key,
void* value,
void* arg)
1797 CliprdrFuseFile* fuse_file = value;
1798 CliprdrFuseFindParentContext* find_context = arg;
1800 if (!fuse_file->is_directory)
1803 if (strncmp(find_context->parent_path, fuse_file->filename_with_root,
1804 fuse_file->filename_with_root_len + 1) == 0)
1806 find_context->parent = fuse_file;
1813static CliprdrFuseFile* get_parent_directory(CliprdrFileContext* file_context,
const char* path)
1815 CliprdrFuseFindParentContext find_context = WINPR_C_ARRAY_INIT;
1817 WINPR_ASSERT(file_context);
1820 find_context.parent_path = get_parent_path(path);
1821 if (!find_context.parent_path)
1824 WINPR_ASSERT(!find_context.parent);
1826 if (HashTable_Foreach(file_context->inode_table, is_fuse_file_not_parent, &find_context))
1828 free(find_context.parent_path);
1831 WINPR_ASSERT(find_context.parent);
1833 free(find_context.parent_path);
1835 return find_context.parent;
1839static BOOL selection_handle_file(CliprdrFileContext* file_context,
1840 CliprdrFuseClipDataEntry* clip_data_entry, uint32_t index,
1844 WINPR_ASSERT(file_context);
1845 WINPR_ASSERT(clip_data_entry);
1848 CliprdrFuseFile* clip_data_dir = clip_data_entry->clip_data_dir;
1849 WINPR_ASSERT(clip_data_dir);
1851 char filename[ARRAYSIZE(file->cFileName) * 8] = WINPR_C_ARRAY_INIT;
1852 const SSIZE_T filenamelen = ConvertWCharNToUtf8(file->cFileName, ARRAYSIZE(file->cFileName),
1853 filename, ARRAYSIZE(filename) - 1);
1854 if (filenamelen < 0)
1856 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to convert filename");
1860 for (
size_t j = 0; filename[j]; ++j)
1862 if (filename[j] ==
'\\')
1867 CliprdrFuseFile* fuse_file = fuse_file_new(
1868 "%.*s/%s", WINPR_ASSERTING_INT_CAST(
int, clip_data_dir->filename_with_root_len),
1869 clip_data_dir->filename_with_root, filename);
1872 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to create FUSE file");
1876 fuse_file->parent = get_parent_directory(file_context, fuse_file->filename_with_root);
1877 if (!fuse_file->parent)
1879 WLog_Print(file_context->log, WLOG_ERROR,
"Found no parent for FUSE file");
1883 if (!ArrayList_Append(fuse_file->parent->children, fuse_file))
1885 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to append FUSE file");
1889 fuse_file->list_idx = index;
1890 fuse_file->ino = get_next_free_inode(file_context);
1891 fuse_file->has_clip_data_id = clip_data_entry->has_clip_data_id;
1892 fuse_file->clip_data_id = clip_data_entry->clip_data_id;
1893 if (file->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1894 fuse_file->is_directory = TRUE;
1895 if (file->dwFileAttributes & FILE_ATTRIBUTE_READONLY)
1896 fuse_file->is_readonly = TRUE;
1897 if (file->dwFlags & FD_FILESIZE)
1899 fuse_file->size = ((UINT64)file->nFileSizeHigh << 32) + file->nFileSizeLow;
1900 fuse_file->has_size = TRUE;
1902 if (file->dwFlags & FD_WRITESTIME)
1906 filetime = file->ftLastWriteTime.dwHighDateTime;
1908 filetime += file->ftLastWriteTime.dwLowDateTime;
1910 fuse_file->last_write_time_unix =
1911 1LL * filetime / (10LL * 1000LL * 1000LL) - WIN32_FILETIME_TO_UNIX_EPOCH;
1912 fuse_file->has_last_write_time = TRUE;
1915 if (!HashTable_Insert(file_context->inode_table, (
void*)(uintptr_t)fuse_file->ino, fuse_file))
1917 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to insert inode into inode table");
1926 fuse_file_free(fuse_file);
1927 clear_entry_selection(clip_data_entry);
1934static BOOL set_selection_for_clip_data_entry(CliprdrFileContext* file_context,
1935 CliprdrFuseClipDataEntry* clip_data_entry,
1938 WINPR_ASSERT(file_context);
1939 WINPR_ASSERT(clip_data_entry);
1940 WINPR_ASSERT(files);
1942 if (clip_data_entry->has_clip_data_id)
1943 WLog_Print(file_context->log, WLOG_DEBUG,
"Setting selection for clipDataId %u",
1944 clip_data_entry->clip_data_id);
1946 WLog_Print(file_context->log, WLOG_DEBUG,
"Setting selection");
1948 for (UINT32 i = 0; i < n_files; ++i)
1951 if (!selection_handle_file(file_context, clip_data_entry, i, file))
1955 if (clip_data_entry->has_clip_data_id)
1956 WLog_Print(file_context->log, WLOG_DEBUG,
"Selection set for clipDataId %u",
1957 clip_data_entry->clip_data_id);
1959 WLog_Print(file_context->log, WLOG_DEBUG,
"Selection set");
1964static BOOL update_exposed_path(CliprdrFileContext* file_context, wClipboard* clip,
1965 CliprdrFuseClipDataEntry* clip_data_entry)
1967 wClipboardDelegate* delegate =
nullptr;
1968 CliprdrFuseFile* clip_data_dir =
nullptr;
1970 WINPR_ASSERT(file_context);
1972 WINPR_ASSERT(clip_data_entry);
1974 delegate = ClipboardGetDelegate(clip);
1975 WINPR_ASSERT(delegate);
1977 clip_data_dir = clip_data_entry->clip_data_dir;
1978 WINPR_ASSERT(clip_data_dir);
1980 free(file_context->exposed_path);
1981 file_context->exposed_path = GetCombinedPath(file_context->path, clip_data_dir->filename);
1982 if (file_context->exposed_path)
1983 WLog_Print(file_context->log, WLOG_DEBUG,
"Updated exposed path to \"%s\"",
1984 file_context->exposed_path);
1986 delegate->basePath = file_context->exposed_path;
1988 return delegate->basePath !=
nullptr;
1992BOOL cliprdr_file_context_update_server_data(CliprdrFileContext* file_context, wClipboard* clip,
1993 const void* data,
size_t size)
1995#if defined(WITH_FUSE)
1996 CliprdrFuseClipDataEntry* clip_data_entry =
nullptr;
2001 WINPR_ASSERT(file_context);
2003 if (size > UINT32_MAX)
2006 if (cliprdr_parse_file_list(data, (UINT32)size, &files, &n_files))
2008 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to parse file list");
2012 HashTable_Lock(file_context->inode_table);
2013 HashTable_Lock(file_context->clip_data_table);
2014 if (does_server_support_clipdata_locking(file_context))
2015 clip_data_entry = HashTable_GetItemValue(
2016 file_context->clip_data_table, (
void*)(uintptr_t)file_context->current_clip_data_id);
2018 clip_data_entry = file_context->clip_data_entry_without_id;
2020 WINPR_ASSERT(clip_data_entry);
2022 clear_entry_selection(clip_data_entry);
2023 WINPR_ASSERT(!clip_data_entry->clip_data_dir);
2025 clip_data_entry->clip_data_dir =
2026 clip_data_dir_new(file_context, does_server_support_clipdata_locking(file_context),
2027 file_context->current_clip_data_id);
2028 if (!clip_data_entry->clip_data_dir)
2030 if (!update_exposed_path(file_context, clip, clip_data_entry))
2032 if (!set_selection_for_clip_data_entry(file_context, clip_data_entry, files, n_files))
2038 HashTable_Unlock(file_context->clip_data_table);
2039 HashTable_Unlock(file_context->inode_table);
2047void* cliprdr_file_context_get_context(CliprdrFileContext* file)
2050 return file->clipboard;
2053void cliprdr_file_session_terminate(CliprdrFileContext* file, BOOL stop_thread)
2058#if defined(WITH_FUSE)
2059 WINPR_ASSERT(file->fuse_stop_sync);
2061 WLog_Print(file->log, WLOG_DEBUG,
"Setting FUSE exit flag");
2062 if (file->fuse_sess)
2063 fuse_session_exit(file->fuse_sess);
2067 WLog_Print(file->log, WLOG_DEBUG,
"Setting FUSE stop event");
2068 (void)SetEvent(file->fuse_stop_sync);
2074#if defined(WITH_FUSE)
2075 WLog_Print(file->log, WLOG_DEBUG,
"Forcing FUSE to check exit flag");
2077 (void)winpr_PathFileExists(file->path);
2080void cliprdr_file_context_free(CliprdrFileContext* file)
2085#if defined(WITH_FUSE)
2086 if (file->fuse_thread)
2088 WINPR_ASSERT(file->fuse_stop_sync);
2090 WLog_Print(file->log, WLOG_DEBUG,
"Stopping FUSE thread");
2091 cliprdr_file_session_terminate(file, TRUE);
2093 WLog_Print(file->log, WLOG_DEBUG,
"Waiting on FUSE thread");
2094 (void)WaitForSingleObject(file->fuse_thread, INFINITE);
2095 (void)CloseHandle(file->fuse_thread);
2097 if (file->fuse_stop_sync)
2098 (void)CloseHandle(file->fuse_stop_sync);
2099 if (file->fuse_start_sync)
2100 (void)CloseHandle(file->fuse_start_sync);
2102 HashTable_Free(file->request_table);
2103 HashTable_Free(file->clip_data_table);
2104 HashTable_Free(file->inode_table);
2107 HashTable_Free(file->local_streams);
2108 winpr_RemoveDirectory(file->path);
2110 free(file->exposed_path);
2114static BOOL create_base_path(CliprdrFileContext* file)
2118 char base[64] = WINPR_C_ARRAY_INIT;
2119 (void)_snprintf(base,
sizeof(base),
"com.freerdp.client.cliprdr.%" PRIu32,
2120 GetCurrentProcessId());
2122 file->path = GetKnownSubPath(KNOWN_PATH_TEMP, base);
2126 if (!winpr_PathFileExists(file->path) && !winpr_PathMakePath(file->path,
nullptr))
2128 WLog_Print(file->log, WLOG_ERROR,
"Failed to create directory '%s'", file->path);
2134static void cliprdr_local_file_free(CliprdrLocalFile* file)
2136 const CliprdrLocalFile empty = WINPR_C_ARRAY_INIT;
2141 WLog_Print(file->context->log, WLOG_DEBUG,
"closing file %s, discarding entry", file->name);
2142 (void)fclose(file->fp);
2148static BOOL cliprdr_local_file_new(CliprdrFileContext* context, CliprdrLocalFile* f,
2151 const CliprdrLocalFile empty = WINPR_C_ARRAY_INIT;
2153 WINPR_ASSERT(context);
2157 f->context = context;
2158 f->name = winpr_str_url_decode(path, strlen(path));
2164 cliprdr_local_file_free(f);
2168static void cliprdr_local_files_free(CliprdrLocalStream* stream)
2170 WINPR_ASSERT(stream);
2172 for (
size_t x = 0; x < stream->count; x++)
2173 cliprdr_local_file_free(&stream->files[x]);
2174 free(stream->files);
2176 stream->files =
nullptr;
2180static void cliprdr_local_stream_free(
void* obj)
2182 CliprdrLocalStream* stream = (CliprdrLocalStream*)obj;
2184 cliprdr_local_files_free(stream);
2189static BOOL append_entry(CliprdrLocalStream* stream,
const char* path)
2191 CliprdrLocalFile* tmp = realloc(stream->files,
sizeof(CliprdrLocalFile) * (stream->count + 1));
2194 stream->files = tmp;
2195 CliprdrLocalFile* f = &stream->files[stream->count++];
2197 return cliprdr_local_file_new(stream->context, f, path);
2200static BOOL is_directory(
const char* path)
2202 WCHAR* wpath = ConvertUtf8ToWCharAlloc(path,
nullptr);
2206 HANDLE hFile = CreateFileW(wpath, 0, FILE_SHARE_DELETE,
nullptr, OPEN_EXISTING,
2207 FILE_ATTRIBUTE_NORMAL,
nullptr);
2210 if (hFile == INVALID_HANDLE_VALUE)
2214 const BOOL status = GetFileInformationByHandle(hFile, &fileInformation);
2215 (void)CloseHandle(hFile);
2219 return (fileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
2222static BOOL add_directory(CliprdrLocalStream* stream,
const char* path)
2224 char* wildcardpath = GetCombinedPath(path,
"*");
2227 WCHAR* wpath = ConvertUtf8ToWCharAlloc(wildcardpath,
nullptr);
2233 HANDLE hFind = FindFirstFileW(wpath, &FindFileData);
2236 if (hFind == INVALID_HANDLE_VALUE)
2240 char* next =
nullptr;
2242 WCHAR dotbuffer[6] = WINPR_C_ARRAY_INIT;
2243 WCHAR dotdotbuffer[6] = WINPR_C_ARRAY_INIT;
2244 const WCHAR* dot = InitializeConstWCharFromUtf8(
".", dotbuffer, ARRAYSIZE(dotbuffer));
2245 const WCHAR* dotdot = InitializeConstWCharFromUtf8(
"..", dotdotbuffer, ARRAYSIZE(dotdotbuffer));
2248 if (_wcscmp(FindFileData.cFileName, dot) == 0)
2250 if (_wcscmp(FindFileData.cFileName, dotdot) == 0)
2253 char cFileName[MAX_PATH] = WINPR_C_ARRAY_INIT;
2254 (void)ConvertWCharNToUtf8(FindFileData.cFileName, ARRAYSIZE(FindFileData.cFileName),
2255 cFileName, ARRAYSIZE(cFileName));
2258 next = GetCombinedPath(path, cFileName);
2262 if (!append_entry(stream, next))
2264 if (is_directory(next))
2266 if (!add_directory(stream, next))
2269 }
while (FindNextFileW(hFind, &FindFileData));
2279static BOOL cliprdr_local_stream_update(CliprdrLocalStream* stream,
const char* data,
size_t size)
2282 WINPR_ASSERT(stream);
2286 cliprdr_local_files_free(stream);
2288 stream->files = calloc(size,
sizeof(CliprdrLocalFile));
2292 char* copy = strndup(data, size);
2296 char* saveptr =
nullptr;
2297 char* ptr = strtok_s(copy,
"\r\n", &saveptr);
2300 const char* name = ptr;
2301 if (strncmp(
"file:///", ptr, 8) == 0)
2303 else if (strncmp(
"file:/", ptr, 6) == 0)
2306 if (!append_entry(stream, name))
2309 if (is_directory(name))
2311 const BOOL res = add_directory(stream, name);
2315 ptr = strtok_s(
nullptr,
"\r\n", &saveptr);
2324CliprdrLocalStream* cliprdr_local_stream_new(CliprdrFileContext* context, UINT32 streamID,
2325 const char* data,
size_t size)
2327 WINPR_ASSERT(context);
2328 CliprdrLocalStream* stream = calloc(1,
sizeof(CliprdrLocalStream));
2332 stream->context = context;
2333 if (!cliprdr_local_stream_update(stream, data, size))
2336 stream->lockId = streamID;
2340 cliprdr_local_stream_free(stream);
2344static UINT32 UINTPointerHash(
const void*
id)
2347 return *((
const UINT32*)
id);
2350static BOOL UINTPointerCompare(
const void* pointer1,
const void* pointer2)
2352 if (!pointer1 || !pointer2)
2353 return pointer1 == pointer2;
2355 const UINT32* a = pointer1;
2356 const UINT32* b = pointer2;
2360static void* UINTPointerClone(
const void* other)
2362 const UINT32* src = other;
2366 UINT32* copy = calloc(1,
sizeof(UINT32));
2374#if defined(WITH_FUSE)
2375CliprdrFuseFile* fuse_file_new_root(CliprdrFileContext* file_context)
2377 CliprdrFuseFile* root_dir = fuse_file_new(
"/");
2381 root_dir->ino = FUSE_ROOT_ID;
2382 root_dir->is_directory = TRUE;
2383 root_dir->is_readonly = TRUE;
2385 if (!HashTable_Insert(file_context->inode_table, (
void*)(uintptr_t)root_dir->ino, root_dir))
2387 fuse_file_free(root_dir);
2395CliprdrFileContext* cliprdr_file_context_new(
void* context)
2397 CliprdrFileContext* file = calloc(1,
sizeof(CliprdrFileContext));
2401 file->log = WLog_Get(CLIENT_TAG(
"common.cliprdr.file"));
2402 file->clipboard = context;
2404 file->local_streams = HashTable_New(FALSE);
2405 if (!file->local_streams)
2408 if (!HashTable_SetHashFunction(file->local_streams, UINTPointerHash))
2412 wObject* hkobj = HashTable_KeyObject(file->local_streams);
2413 WINPR_ASSERT(hkobj);
2420 wObject* hobj = HashTable_ValueObject(file->local_streams);
2425#if defined(WITH_FUSE)
2426 file->inode_table = HashTable_New(FALSE);
2427 file->clip_data_table = HashTable_New(FALSE);
2428 file->request_table = HashTable_New(FALSE);
2429 if (!file->inode_table || !file->clip_data_table || !file->request_table)
2433 wObject* ctobj = HashTable_ValueObject(file->request_table);
2434 WINPR_ASSERT(ctobj);
2438 wObject* ctobj = HashTable_ValueObject(file->clip_data_table);
2439 WINPR_ASSERT(ctobj);
2444 if (!create_base_path(file))
2447#if defined(WITH_FUSE)
2448 if (!(file->fuse_start_sync = CreateEvent(
nullptr, TRUE, FALSE,
nullptr)))
2450 if (!(file->fuse_stop_sync = CreateEvent(
nullptr, TRUE, FALSE,
nullptr)))
2452 if (!(file->fuse_thread = CreateThread(
nullptr, 0, cliprdr_file_fuse_thread, file, 0,
nullptr)))
2455 if (WaitForSingleObject(file->fuse_start_sync, INFINITE) == WAIT_FAILED)
2456 WLog_Print(file->log, WLOG_ERROR,
"Failed to wait for start sync");
2461 WINPR_PRAGMA_DIAG_PUSH
2462 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2463 cliprdr_file_context_free(file);
2464 WINPR_PRAGMA_DIAG_POP
2468BOOL local_stream_discard(
const void* key,
void* value,
void* arg)
2470 CliprdrFileContext* file = arg;
2471 CliprdrLocalStream* stream = value;
2473 WINPR_ASSERT(stream);
2475 if (!stream->locked)
2476 HashTable_Remove(file->local_streams, key);
2480BOOL cliprdr_file_context_clear(CliprdrFileContext* file)
2484 WLog_Print(file->log, WLOG_DEBUG,
"clear file clipboard...");
2486 HashTable_Lock(file->local_streams);
2487 HashTable_Foreach(file->local_streams, local_stream_discard, file);
2488 HashTable_Unlock(file->local_streams);
2490 memset(file->server_data_hash, 0,
sizeof(file->server_data_hash));
2491 memset(file->client_data_hash, 0,
sizeof(file->client_data_hash));
2495BOOL cliprdr_file_context_update_client_data(CliprdrFileContext* file,
const char* data,
2501 if (!cliprdr_file_client_content_changed_and_update(file, data, size))
2504 if (!cliprdr_file_context_clear(file))
2507 UINT32 lockId = file->local_lock_id;
2509 HashTable_Lock(file->local_streams);
2510 CliprdrLocalStream* stream = HashTable_GetItemValue(file->local_streams, &lockId);
2512 WLog_Print(file->log, WLOG_DEBUG,
"update client file list (stream=%p)...", (
void*)stream);
2514 rc = cliprdr_local_stream_update(stream, data, size);
2517 stream = cliprdr_local_stream_new(file, lockId, data, size);
2518 rc = HashTable_Insert(file->local_streams, &stream->lockId, stream);
2520 cliprdr_local_stream_free(stream);
2524 HashTable_Unlock(file->local_streams);
2528UINT32 cliprdr_file_context_current_flags(CliprdrFileContext* file)
2532 if ((file->file_capability_flags & CB_STREAM_FILECLIP_ENABLED) == 0)
2535 if (!file->file_formats_registered)
2538 return CB_STREAM_FILECLIP_ENABLED | CB_FILECLIP_NO_FILE_PATHS |
2539 CB_HUGE_FILE_SUPPORT_ENABLED;
2542BOOL cliprdr_file_context_set_locally_available(CliprdrFileContext* file, BOOL available)
2545 file->file_formats_registered = available;
2549BOOL cliprdr_file_context_remote_set_flags(CliprdrFileContext* file, UINT32 flags)
2552 file->file_capability_flags = flags;
2556UINT32 cliprdr_file_context_remote_get_flags(CliprdrFileContext* file)
2559 return file->file_capability_flags;
2562BOOL cliprdr_file_context_has_local_support(CliprdrFileContext* file)
2566#if defined(WITH_FUSE)
This struct contains function pointer to initialize/free objects.
OBJECT_FREE_FN fnObjectFree
OBJECT_EQUALS_FN fnObjectEquals
OBJECT_NEW_FN fnObjectNew