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);
437 ArrayList_ForEach(fuse_file->children, notify_delete_child, file_context, fuse_file);
439 DEBUG_CLIPRDR(file_context->log,
"Invalidating inode %lu for file \"%s\"", fuse_file->ino,
440 fuse_file->filename);
441 fuse_lowlevel_notify_inval_inode(file_context->fuse_sess, fuse_file->ino, 0, 0);
442 WLog_Print(file_context->log, WLOG_DEBUG,
"Inode %lu invalidated", fuse_file->ino);
448static bool clear_selection(CliprdrFileContext* file_context, BOOL all_selections,
449 CliprdrFuseClipDataEntry* clip_data_entry)
452 FuseFileClearContext clear_context = WINPR_C_ARRAY_INIT;
453 CliprdrFuseFile* clip_data_dir =
nullptr;
455 WINPR_ASSERT(file_context);
457 clear_context.file_context = file_context;
458 clear_context.fuse_files = ArrayList_New(FALSE);
459 WINPR_ASSERT(clear_context.fuse_files);
461 wObject* aobj = ArrayList_Object(clear_context.fuse_files);
467 clip_data_dir = clip_data_entry->clip_data_dir;
468 clip_data_entry->clip_data_dir =
nullptr;
470 WINPR_ASSERT(clip_data_dir);
472 CliprdrFuseFile* root_dir = get_fuse_file_by_ino(file_context, FUSE_ROOT_ID);
474 ArrayList_Remove(root_dir->children, clip_data_dir);
476 clear_context.has_clip_data_id = clip_data_dir->has_clip_data_id;
477 clear_context.clip_data_id = clip_data_dir->clip_data_id;
479 clear_context.all_files = all_selections;
481 if (clip_data_entry && clip_data_entry->has_clip_data_id)
482 WLog_Print(file_context->log, WLOG_DEBUG,
"Clearing selection for clipDataId %u",
483 clip_data_entry->clip_data_id);
485 WLog_Print(file_context->log, WLOG_DEBUG,
"Clearing selection%s",
486 all_selections ?
"s" :
"");
488 if (!HashTable_Foreach(file_context->request_table, maybe_clear_fuse_request, &clear_context))
490 if (!HashTable_Foreach(file_context->inode_table, maybe_steal_inode, &clear_context))
492 HashTable_Unlock(file_context->inode_table);
494 if (file_context->fuse_sess)
504 if (!ArrayList_ForEach(clear_context.fuse_files, invalidate_inode, file_context))
506 CliprdrFuseFile* root_dir = get_fuse_file_by_ino(file_context, FUSE_ROOT_ID);
507 if (clip_data_dir && root_dir)
509 fuse_lowlevel_notify_delete(file_context->fuse_sess, root_dir->ino, clip_data_dir->ino,
510 clip_data_dir->filename, clip_data_dir->filename_len);
513 ArrayList_Free(clear_context.fuse_files);
515 HashTable_Lock(file_context->inode_table);
516 if (clip_data_entry && clip_data_entry->has_clip_data_id)
517 WLog_Print(file_context->log, WLOG_DEBUG,
"Selection cleared for clipDataId %u",
518 clip_data_entry->clip_data_id);
520 WLog_Print(file_context->log, WLOG_DEBUG,
"Selection%s cleared", all_selections ?
"s" :
"");
525static bool clear_entry_selection(CliprdrFuseClipDataEntry* clip_data_entry)
527 WINPR_ASSERT(clip_data_entry);
529 if (!clip_data_entry->clip_data_dir)
532 return clear_selection(clip_data_entry->file_context, FALSE, clip_data_entry);
536static bool clear_no_cdi_entry(CliprdrFileContext* file_context)
539 WINPR_ASSERT(file_context);
541 WINPR_ASSERT(file_context->inode_table);
542 HashTable_Lock(file_context->inode_table);
543 if (file_context->clip_data_entry_without_id)
545 res = clear_entry_selection(file_context->clip_data_entry_without_id);
547 clip_data_entry_free(file_context->clip_data_entry_without_id);
548 file_context->clip_data_entry_without_id =
nullptr;
550 HashTable_Unlock(file_context->inode_table);
555static BOOL clear_clip_data_entries(WINPR_ATTR_UNUSED
const void* key,
void* value,
556 WINPR_ATTR_UNUSED
void* arg)
558 return clear_entry_selection(value);
562static UINT clear_cdi_entries(CliprdrFileContext* file_context)
564 UINT res = CHANNEL_RC_OK;
565 WINPR_ASSERT(file_context);
567 HashTable_Lock(file_context->inode_table);
568 if (!HashTable_Foreach(file_context->clip_data_table, clear_clip_data_entries,
nullptr))
569 res = ERROR_INTERNAL_ERROR;
571 HashTable_Clear(file_context->clip_data_table);
572 HashTable_Unlock(file_context->inode_table);
577static UINT prepare_clip_data_entry_with_id(CliprdrFileContext* file_context)
579 CliprdrFuseClipDataEntry* clip_data_entry =
nullptr;
581 WINPR_ASSERT(file_context);
583 clip_data_entry = clip_data_entry_new(file_context, TRUE);
584 if (!clip_data_entry)
586 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to create clipDataEntry");
587 return ERROR_INTERNAL_ERROR;
590 HashTable_Lock(file_context->inode_table);
591 if (!HashTable_Insert(file_context->clip_data_table,
592 (
void*)(uintptr_t)clip_data_entry->clip_data_id, clip_data_entry))
594 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to insert clipDataEntry");
595 clip_data_entry_free(clip_data_entry);
596 HashTable_Unlock(file_context->inode_table);
597 return ERROR_INTERNAL_ERROR;
599 HashTable_Unlock(file_context->inode_table);
603 file_context->current_clip_data_id = clip_data_entry->clip_data_id;
605 return CHANNEL_RC_OK;
608static UINT prepare_clip_data_entry_without_id(CliprdrFileContext* file_context)
610 WINPR_ASSERT(file_context);
611 WINPR_ASSERT(!file_context->clip_data_entry_without_id);
613 file_context->clip_data_entry_without_id = clip_data_entry_new(file_context, FALSE);
614 if (!file_context->clip_data_entry_without_id)
616 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to create clipDataEntry");
617 return ERROR_INTERNAL_ERROR;
620 return CHANNEL_RC_OK;
624UINT cliprdr_file_context_notify_new_server_format_list(CliprdrFileContext* file_context)
626 UINT rc = CHANNEL_RC_OK;
627 WINPR_ASSERT(file_context);
628 WINPR_ASSERT(file_context->context);
630#if defined(WITH_FUSE)
631 if (!clear_no_cdi_entry(file_context))
632 return ERROR_INTERNAL_ERROR;
634 rc = clear_cdi_entries(file_context);
636 if (does_server_support_clipdata_locking(file_context))
637 rc = prepare_clip_data_entry_with_id(file_context);
639 rc = prepare_clip_data_entry_without_id(file_context);
644UINT cliprdr_file_context_notify_new_client_format_list(CliprdrFileContext* file_context)
646 WINPR_ASSERT(file_context);
647 WINPR_ASSERT(file_context->context);
649#if defined(WITH_FUSE)
650 if (!clear_no_cdi_entry(file_context))
651 return ERROR_INTERNAL_ERROR;
653 return clear_cdi_entries(file_context);
656 return CHANNEL_RC_OK;
659static CliprdrLocalStream* cliprdr_local_stream_new(CliprdrFileContext* context, UINT32 streamID,
660 const char* data,
size_t size);
661static void cliprdr_file_session_terminate(CliprdrFileContext* file, BOOL stop_thread);
662static BOOL local_stream_discard(
const void* key,
void* value,
void* arg);
664WINPR_ATTR_FORMAT_ARG(6, 7)
665static
void writelog(wLog* log, DWORD level, const
char* fname, const
char* fkt,
size_t line,
666 WINPR_FORMAT_ARG const
char* fmt, ...)
668 if (!WLog_IsLevelActive(log, level))
671 va_list ap = WINPR_C_ARRAY_INIT;
673 WLog_PrintTextMessageVA(log, level, line, fname, fkt, fmt, ap);
677#if defined(WITH_FUSE)
678static CliprdrFuseFile* fuse_file_new_root(CliprdrFileContext* file_context);
679static void cliprdr_file_fuse_lookup(fuse_req_t req, fuse_ino_t parent,
const char* name);
680static void cliprdr_file_fuse_getattr(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info* fi);
681static void cliprdr_file_fuse_readdir(fuse_req_t req, fuse_ino_t ino,
size_t size, off_t off,
682 struct fuse_file_info* fi);
683static void cliprdr_file_fuse_read(fuse_req_t req, fuse_ino_t ino,
size_t size, off_t off,
684 struct fuse_file_info* fi);
685static void cliprdr_file_fuse_open(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info* fi);
686static void cliprdr_file_fuse_opendir(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info* fi);
688static const struct fuse_lowlevel_ops cliprdr_file_fuse_oper = {
689 .lookup = cliprdr_file_fuse_lookup,
690 .getattr = cliprdr_file_fuse_getattr,
691 .readdir = cliprdr_file_fuse_readdir,
692 .open = cliprdr_file_fuse_open,
693 .read = cliprdr_file_fuse_read,
694 .opendir = cliprdr_file_fuse_opendir,
697CliprdrFuseFile* get_fuse_file_by_ino(CliprdrFileContext* file_context, fuse_ino_t fuse_ino)
699 WINPR_ASSERT(file_context);
701 return HashTable_GetItemValue(file_context->inode_table, (
void*)(uintptr_t)fuse_ino);
704static CliprdrFuseFile*
705get_fuse_file_by_name_from_parent(WINPR_ATTR_UNUSED CliprdrFileContext* file_context,
706 CliprdrFuseFile* parent,
const char* name)
708 WINPR_ASSERT(file_context);
709 WINPR_ASSERT(parent);
711 for (
size_t i = 0; i < ArrayList_Count(parent->children); ++i)
713 CliprdrFuseFile* child = ArrayList_GetItem(parent->children, i);
717 if (strncmp(name, child->filename, child->filename_len + 1) == 0)
721 DEBUG_CLIPRDR(file_context->log,
"Requested file \"%s\" in directory \"%s\" does not exist",
722 name, parent->filename);
727static CliprdrFuseRequest* cliprdr_fuse_request_new(CliprdrFileContext* file_context,
728 CliprdrFuseFile* fuse_file, fuse_req_t fuse_req,
729 FuseLowlevelOperationType operation_type)
731 CliprdrFuseRequest* fuse_request =
nullptr;
732 UINT32 stream_id = file_context->next_stream_id;
734 WINPR_ASSERT(file_context);
735 WINPR_ASSERT(fuse_file);
737 fuse_request = calloc(1,
sizeof(CliprdrFuseRequest));
740 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to allocate FUSE request for file \"%s\"",
741 fuse_file->filename_with_root);
745 fuse_request->fuse_file = fuse_file;
746 fuse_request->fuse_req = fuse_req;
747 fuse_request->operation_type = operation_type;
749 while (stream_id == 0 ||
750 HashTable_GetItemValue(file_context->request_table, (
void*)(uintptr_t)stream_id))
752 fuse_request->stream_id = stream_id;
754 file_context->next_stream_id = stream_id + 1;
756 if (!HashTable_Insert(file_context->request_table, (
void*)(uintptr_t)fuse_request->stream_id,
759 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to track FUSE request for file \"%s\"",
760 fuse_file->filename_with_root);
768static BOOL request_file_size_async(CliprdrFileContext* file_context, CliprdrFuseFile* fuse_file,
769 fuse_req_t fuse_req, FuseLowlevelOperationType operation_type)
771 CliprdrFuseRequest* fuse_request =
nullptr;
774 WINPR_ASSERT(file_context);
775 WINPR_ASSERT(fuse_file);
777 fuse_request = cliprdr_fuse_request_new(file_context, fuse_file, fuse_req, operation_type);
781 file_contents_request.common.msgType = CB_FILECONTENTS_REQUEST;
782 file_contents_request.streamId = fuse_request->stream_id;
783 file_contents_request.listIndex = fuse_file->list_idx;
784 file_contents_request.dwFlags = FILECONTENTS_SIZE;
785 file_contents_request.cbRequested = 0x8;
786 file_contents_request.haveClipDataId = fuse_file->has_clip_data_id;
787 file_contents_request.clipDataId = fuse_file->clip_data_id;
789 if (file_context->context->ClientFileContentsRequest(file_context->context,
790 &file_contents_request))
792 WLog_Print(file_context->log, WLOG_ERROR,
793 "Failed to send FileContentsRequest for file \"%s\"",
794 fuse_file->filename_with_root);
795 HashTable_Remove(file_context->request_table, (
void*)(uintptr_t)fuse_request->stream_id);
799 DEBUG_CLIPRDR(file_context->log,
"Requested file size for file \"%s\" with stream id %u",
800 fuse_file->filename, fuse_request->stream_id);
806static void write_file_attributes(CliprdrFuseFile* fuse_file,
struct stat* attr)
808 memset(attr, 0,
sizeof(
struct stat));
813 attr->st_ino = fuse_file->ino;
814 if (fuse_file->is_directory)
816 attr->st_mode = S_IFDIR | (fuse_file->is_readonly ? 0555 : 0755);
821 attr->st_mode = S_IFREG | (fuse_file->is_readonly ? 0444 : 0644);
823 attr->st_size = WINPR_ASSERTING_INT_CAST(off_t, fuse_file->size);
825 attr->st_uid = getuid();
826 attr->st_gid = getgid();
827 attr->st_atime = attr->st_mtime = attr->st_ctime =
828 (fuse_file->has_last_write_time ? fuse_file->last_write_time_unix : time(
nullptr));
831static void cliprdr_file_fuse_lookup(fuse_req_t fuse_req, fuse_ino_t parent_ino,
const char* name)
833 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
834 CliprdrFuseFile* parent =
nullptr;
835 CliprdrFuseFile* fuse_file =
nullptr;
836 struct fuse_entry_param entry = WINPR_C_ARRAY_INIT;
838 WINPR_ASSERT(file_context);
840 HashTable_Lock(file_context->inode_table);
841 if (!(parent = get_fuse_file_by_ino(file_context, parent_ino)) || !parent->is_directory)
843 HashTable_Unlock(file_context->inode_table);
844 fuse_reply_err(fuse_req, ENOENT);
847 if (!(fuse_file = get_fuse_file_by_name_from_parent(file_context, parent, name)))
849 HashTable_Unlock(file_context->inode_table);
850 fuse_reply_err(fuse_req, ENOENT);
854 DEBUG_CLIPRDR(file_context->log,
"lookup() has been called for \"%s\"", name);
855 DEBUG_CLIPRDR(file_context->log,
"Parent is \"%s\", child is \"%s\"",
856 parent->filename_with_root, fuse_file->filename_with_root);
858 if (!fuse_file->is_directory && !fuse_file->has_size)
863 request_file_size_async(file_context, fuse_file, fuse_req, FUSE_LL_OPERATION_LOOKUP);
864 HashTable_Unlock(file_context->inode_table);
867 fuse_reply_err(fuse_req, EIO);
872 entry.ino = fuse_file->ino;
873 write_file_attributes(fuse_file, &entry.attr);
874 entry.attr_timeout = 1.0;
875 entry.entry_timeout = 1.0;
876 HashTable_Unlock(file_context->inode_table);
878 fuse_reply_entry(fuse_req, &entry);
881static void cliprdr_file_fuse_getattr(fuse_req_t fuse_req, fuse_ino_t fuse_ino,
882 WINPR_ATTR_UNUSED
struct fuse_file_info* file_info)
884 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
885 CliprdrFuseFile* fuse_file =
nullptr;
886 struct stat attr = WINPR_C_ARRAY_INIT;
888 WINPR_ASSERT(file_context);
890 HashTable_Lock(file_context->inode_table);
891 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
893 HashTable_Unlock(file_context->inode_table);
894 fuse_reply_err(fuse_req, ENOENT);
898 DEBUG_CLIPRDR(file_context->log,
"getattr() has been called for file \"%s\"",
899 fuse_file->filename_with_root);
901 if (!fuse_file->is_directory && !fuse_file->has_size)
906 request_file_size_async(file_context, fuse_file, fuse_req, FUSE_LL_OPERATION_GETATTR);
907 HashTable_Unlock(file_context->inode_table);
910 fuse_reply_err(fuse_req, EIO);
915 write_file_attributes(fuse_file, &attr);
916 HashTable_Unlock(file_context->inode_table);
918 fuse_reply_attr(fuse_req, &attr, 1.0);
921static void cliprdr_file_fuse_open(fuse_req_t fuse_req, fuse_ino_t fuse_ino,
922 struct fuse_file_info* file_info)
924 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
925 CliprdrFuseFile* fuse_file =
nullptr;
927 WINPR_ASSERT(file_context);
929 HashTable_Lock(file_context->inode_table);
930 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
932 HashTable_Unlock(file_context->inode_table);
933 fuse_reply_err(fuse_req, ENOENT);
936 if (fuse_file->is_directory)
938 HashTable_Unlock(file_context->inode_table);
939 fuse_reply_err(fuse_req, EISDIR);
942 HashTable_Unlock(file_context->inode_table);
944 if ((file_info->flags & O_ACCMODE) != O_RDONLY)
946 fuse_reply_err(fuse_req, EACCES);
951 file_info->direct_io = 1;
953 fuse_reply_open(fuse_req, file_info);
956static BOOL request_file_range_async(CliprdrFileContext* file_context, CliprdrFuseFile* fuse_file,
957 fuse_req_t fuse_req, off_t offset,
size_t requested_size)
961 WINPR_ASSERT(file_context);
962 WINPR_ASSERT(fuse_file);
964 if (requested_size > UINT32_MAX)
967 CliprdrFuseRequest* fuse_request =
968 cliprdr_fuse_request_new(file_context, fuse_file, fuse_req, FUSE_LL_OPERATION_READ);
972 file_contents_request.common.msgType = CB_FILECONTENTS_REQUEST;
973 file_contents_request.streamId = fuse_request->stream_id;
974 file_contents_request.listIndex = fuse_file->list_idx;
975 file_contents_request.dwFlags = FILECONTENTS_RANGE;
976 file_contents_request.nPositionLow = (UINT32)(offset & 0xFFFFFFFF);
977 file_contents_request.nPositionHigh = (UINT32)((offset >> 32) & 0xFFFFFFFF);
978 file_contents_request.cbRequested = (UINT32)requested_size;
979 file_contents_request.haveClipDataId = fuse_file->has_clip_data_id;
980 file_contents_request.clipDataId = fuse_file->clip_data_id;
982 if (file_context->context->ClientFileContentsRequest(file_context->context,
983 &file_contents_request))
985 WLog_Print(file_context->log, WLOG_ERROR,
986 "Failed to send FileContentsRequest for file \"%s\"",
987 fuse_file->filename_with_root);
988 HashTable_Remove(file_context->request_table, (
void*)(uintptr_t)fuse_request->stream_id);
996 "Requested file range (%zu Bytes at offset %lu) for file \"%s\" with stream id %u",
997 requested_size, offset, fuse_file->filename, fuse_request->stream_id);
1003static void cliprdr_file_fuse_read(fuse_req_t fuse_req, fuse_ino_t fuse_ino,
size_t size,
1004 off_t offset, WINPR_ATTR_UNUSED
struct fuse_file_info* file_info)
1006 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
1007 CliprdrFuseFile* fuse_file =
nullptr;
1010 WINPR_ASSERT(file_context);
1012 HashTable_Lock(file_context->inode_table);
1013 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
1015 HashTable_Unlock(file_context->inode_table);
1016 fuse_reply_err(fuse_req, ENOENT);
1019 if (fuse_file->is_directory)
1021 HashTable_Unlock(file_context->inode_table);
1022 fuse_reply_err(fuse_req, EISDIR);
1025 if (!fuse_file->has_size || (offset < 0) || ((
size_t)offset > fuse_file->size))
1027 HashTable_Unlock(file_context->inode_table);
1028 fuse_reply_err(fuse_req, EINVAL);
1032 size = MIN(size, 8ULL * 1024ULL * 1024ULL);
1034 result = request_file_range_async(file_context, fuse_file, fuse_req, offset, size);
1035 HashTable_Unlock(file_context->inode_table);
1038 fuse_reply_err(fuse_req, EIO);
1041static void cliprdr_file_fuse_opendir(fuse_req_t fuse_req, fuse_ino_t fuse_ino,
1042 struct fuse_file_info* file_info)
1044 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
1045 CliprdrFuseFile* fuse_file =
nullptr;
1047 WINPR_ASSERT(file_context);
1049 HashTable_Lock(file_context->inode_table);
1050 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
1052 HashTable_Unlock(file_context->inode_table);
1053 fuse_reply_err(fuse_req, ENOENT);
1056 if (!fuse_file->is_directory)
1058 HashTable_Unlock(file_context->inode_table);
1059 fuse_reply_err(fuse_req, ENOTDIR);
1062 HashTable_Unlock(file_context->inode_table);
1064 if ((file_info->flags & O_ACCMODE) != O_RDONLY)
1066 fuse_reply_err(fuse_req, EACCES);
1070 fuse_reply_open(fuse_req, file_info);
1073static void cliprdr_file_fuse_readdir(fuse_req_t fuse_req, fuse_ino_t fuse_ino,
size_t max_size,
1075 WINPR_ATTR_UNUSED
struct fuse_file_info* file_info)
1077 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
1078 CliprdrFuseFile* fuse_file =
nullptr;
1079 CliprdrFuseFile* child =
nullptr;
1080 struct stat attr = WINPR_C_ARRAY_INIT;
1081 size_t written_size = 0;
1082 size_t entry_size = 0;
1083 char* filename =
nullptr;
1084 char* buf =
nullptr;
1086 WINPR_ASSERT(file_context);
1088 HashTable_Lock(file_context->inode_table);
1089 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
1091 HashTable_Unlock(file_context->inode_table);
1092 fuse_reply_err(fuse_req, ENOENT);
1095 if (!fuse_file->is_directory)
1097 HashTable_Unlock(file_context->inode_table);
1098 fuse_reply_err(fuse_req, ENOTDIR);
1102 DEBUG_CLIPRDR(file_context->log,
"Reading directory \"%s\" at offset %lu",
1103 fuse_file->filename_with_root, offset);
1105 if ((offset < 0) || ((
size_t)offset >= ArrayList_Count(fuse_file->children)))
1107 HashTable_Unlock(file_context->inode_table);
1108 fuse_reply_buf(fuse_req,
nullptr, 0);
1112 buf = calloc(max_size,
sizeof(
char));
1115 HashTable_Unlock(file_context->inode_table);
1116 fuse_reply_err(fuse_req, ENOMEM);
1121 for (off_t i = offset; i < 2; ++i)
1125 write_file_attributes(fuse_file, &attr);
1130 write_file_attributes(fuse_file->parent, &attr);
1131 attr.st_ino = fuse_file->parent ? attr.st_ino : FUSE_ROOT_ID;
1132 attr.st_mode = fuse_file->parent ? attr.st_mode : 0555;
1137 WINPR_ASSERT(FALSE);
1144 entry_size = fuse_add_direntry(fuse_req, buf + written_size, max_size - written_size,
1145 filename, &attr, i + 1);
1146 if (entry_size > max_size - written_size)
1149 written_size += entry_size;
1152 for (
size_t j = 0, i = 2; j < ArrayList_Count(fuse_file->children); ++j, ++i)
1154 if (i < (
size_t)offset)
1157 child = ArrayList_GetItem(fuse_file->children, j);
1159 write_file_attributes(child, &attr);
1161 fuse_add_direntry(fuse_req, buf + written_size, max_size - written_size,
1162 child->filename, &attr, WINPR_ASSERTING_INT_CAST(off_t, i + 1));
1163 if (entry_size > max_size - written_size)
1166 written_size += entry_size;
1168 HashTable_Unlock(file_context->inode_table);
1170 fuse_reply_buf(fuse_req, buf, written_size);
1174static void fuse_abort(
int sig,
const char* signame,
void* context)
1176 CliprdrFileContext* file = (CliprdrFileContext*)context;
1180 WLog_Print(file->log, WLOG_INFO,
"signal %s [%d] aborting session", signame, sig);
1181 cliprdr_file_session_terminate(file, FALSE);
1185static DWORD WINAPI cliprdr_file_fuse_thread(LPVOID arg)
1187 CliprdrFileContext* file = (CliprdrFileContext*)arg;
1191 DEBUG_CLIPRDR(file->log,
"Starting fuse with mountpoint '%s'", file->path);
1193 struct fuse_args args = FUSE_ARGS_INIT(0,
nullptr);
1194 fuse_opt_add_arg(&args, file->path);
1195 file->fuse_sess = fuse_session_new(&args, &cliprdr_file_fuse_oper,
1196 sizeof(cliprdr_file_fuse_oper), (
void*)file);
1197 (void)SetEvent(file->fuse_start_sync);
1199 if (file->fuse_sess !=
nullptr)
1201 if (freerdp_add_signal_cleanup_handler(file, fuse_abort))
1203 if (0 == fuse_session_mount(file->fuse_sess, file->path))
1205 fuse_session_loop(file->fuse_sess);
1206 fuse_session_unmount(file->fuse_sess);
1208 freerdp_del_signal_cleanup_handler(file, fuse_abort);
1211 WLog_Print(file->log, WLOG_DEBUG,
"Waiting for FUSE stop sync");
1212 if (WaitForSingleObject(file->fuse_stop_sync, INFINITE) == WAIT_FAILED)
1213 WLog_Print(file->log, WLOG_ERROR,
"Failed to wait for stop sync");
1214 fuse_session_destroy(file->fuse_sess);
1216 fuse_opt_free_args(&args);
1218 DEBUG_CLIPRDR(file->log,
"Quitting fuse with mountpoint '%s'", file->path);
1224static UINT cliprdr_file_context_server_file_contents_response(
1225 CliprdrClientContext* cliprdr_context,
1228 CliprdrFileContext* file_context =
nullptr;
1229 CliprdrFuseRequest* fuse_request =
nullptr;
1230 struct fuse_entry_param entry = WINPR_C_ARRAY_INIT;
1232 WINPR_ASSERT(cliprdr_context);
1233 WINPR_ASSERT(file_contents_response);
1235 file_context = cliprdr_context->custom;
1236 WINPR_ASSERT(file_context);
1238 HashTable_Lock(file_context->inode_table);
1239 fuse_request = HashTable_GetItemValue(file_context->request_table,
1240 (
void*)(uintptr_t)file_contents_response->streamId);
1243 HashTable_Unlock(file_context->inode_table);
1244 return CHANNEL_RC_OK;
1247 if (!(file_contents_response->common.msgFlags & CB_RESPONSE_OK))
1249 WLog_Print(file_context->log, WLOG_WARN,
1250 "FileContentsRequests for file \"%s\" was unsuccessful",
1251 fuse_request->fuse_file->filename);
1253 fuse_reply_err(fuse_request->fuse_req, EIO);
1254 HashTable_Remove(file_context->request_table,
1255 (
void*)(uintptr_t)file_contents_response->streamId);
1256 HashTable_Unlock(file_context->inode_table);
1257 return CHANNEL_RC_OK;
1260 if ((fuse_request->operation_type == FUSE_LL_OPERATION_LOOKUP ||
1261 fuse_request->operation_type == FUSE_LL_OPERATION_GETATTR) &&
1262 file_contents_response->cbRequested !=
sizeof(UINT64))
1264 WLog_Print(file_context->log, WLOG_WARN,
1265 "Received invalid file size for file \"%s\" from the client",
1266 fuse_request->fuse_file->filename);
1267 fuse_reply_err(fuse_request->fuse_req, EIO);
1268 HashTable_Remove(file_context->request_table,
1269 (
void*)(uintptr_t)file_contents_response->streamId);
1270 HashTable_Unlock(file_context->inode_table);
1271 return CHANNEL_RC_OK;
1274 if (fuse_request->operation_type == FUSE_LL_OPERATION_LOOKUP ||
1275 fuse_request->operation_type == FUSE_LL_OPERATION_GETATTR)
1277 DEBUG_CLIPRDR(file_context->log,
"Received file size for file \"%s\" with stream id %u",
1278 fuse_request->fuse_file->filename, file_contents_response->streamId);
1280 fuse_request->fuse_file->size = *((
const UINT64*)file_contents_response->requestedData);
1281 fuse_request->fuse_file->has_size = TRUE;
1283 entry.ino = fuse_request->fuse_file->ino;
1284 write_file_attributes(fuse_request->fuse_file, &entry.attr);
1285 entry.attr_timeout = 1.0;
1286 entry.entry_timeout = 1.0;
1288 else if (fuse_request->operation_type == FUSE_LL_OPERATION_READ)
1290 DEBUG_CLIPRDR(file_context->log,
"Received file range for file \"%s\" with stream id %u",
1291 fuse_request->fuse_file->filename, file_contents_response->streamId);
1293 HashTable_Unlock(file_context->inode_table);
1295 switch (fuse_request->operation_type)
1297 case FUSE_LL_OPERATION_NONE:
1299 case FUSE_LL_OPERATION_LOOKUP:
1300 fuse_reply_entry(fuse_request->fuse_req, &entry);
1302 case FUSE_LL_OPERATION_GETATTR:
1303 fuse_reply_attr(fuse_request->fuse_req, &entry.attr, entry.attr_timeout);
1305 case FUSE_LL_OPERATION_READ:
1306 fuse_reply_buf(fuse_request->fuse_req,
1307 (
const char*)file_contents_response->requestedData,
1308 file_contents_response->cbRequested);
1314 HashTable_Remove(file_context->request_table,
1315 (
void*)(uintptr_t)file_contents_response->streamId);
1317 return CHANNEL_RC_OK;
1321static UINT cliprdr_file_context_send_file_contents_failure(
1327 WINPR_ASSERT(fileContentsRequest);
1329 const UINT64 offset = (((UINT64)fileContentsRequest->nPositionHigh) << 32) |
1330 ((UINT64)fileContentsRequest->nPositionLow);
1331 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1332 "server file contents request [lockID %" PRIu32
", streamID %" PRIu32
1333 ", index %" PRIu32
"] offset %" PRIu64
", size %" PRIu32
" failed",
1334 fileContentsRequest->clipDataId, fileContentsRequest->streamId,
1335 fileContentsRequest->listIndex, offset, fileContentsRequest->cbRequested);
1337 response.common.msgFlags = CB_RESPONSE_FAIL;
1338 response.streamId = fileContentsRequest->streamId;
1340 WINPR_ASSERT(file->context);
1341 WINPR_ASSERT(file->context->ClientFileContentsResponse);
1342 return file->context->ClientFileContentsResponse(file->context, &response);
1346cliprdr_file_context_send_contents_response(CliprdrFileContext* file,
1348 const void* data,
size_t size)
1350 if (size > UINT32_MAX)
1351 return ERROR_INVALID_PARAMETER;
1354 .requestedData = data,
1355 .cbRequested = (UINT32)size,
1356 .common.msgFlags = CB_RESPONSE_OK };
1358 WINPR_ASSERT(request);
1361 WLog_Print(file->log, WLOG_DEBUG,
"send contents response streamID=%" PRIu32
", size=%" PRIu32,
1362 response.streamId, response.cbRequested);
1363 WINPR_ASSERT(file->context);
1364 WINPR_ASSERT(file->context->ClientFileContentsResponse);
1365 return file->context->ClientFileContentsResponse(file->context, &response);
1368static BOOL dump_streams(
const void* key,
void* value, WINPR_ATTR_UNUSED
void* arg)
1370 const UINT32* ukey = key;
1371 CliprdrLocalStream* cur = value;
1373 writelog(cur->context->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1374 "[key %" PRIu32
"] lockID %" PRIu32
", count %" PRIuz
", locked %d", *ukey,
1375 cur->lockId, cur->count, cur->locked);
1376 for (
size_t x = 0; x < cur->count; x++)
1378 const CliprdrLocalFile* file = &cur->files[x];
1379 writelog(cur->context->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1380 "file [%" PRIuz
"] %s: %" PRId64, x, file->name, file->size);
1385static CliprdrLocalFile* file_info_for_request(CliprdrFileContext* file, UINT32 lockId,
1390 CliprdrLocalStream* cur = HashTable_GetItemValue(file->local_streams, &lockId);
1393 if (listIndex < cur->count)
1395 CliprdrLocalFile* f = &cur->files[listIndex];
1400 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1401 "invalid entry index for lockID %" PRIu32
", index %" PRIu32
" [count %" PRIuz
1403 lockId, listIndex, cur->count, cur->locked);
1408 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1409 "missing entry for lockID %" PRIu32
", index %" PRIu32, lockId, listIndex);
1410 (void)HashTable_Foreach(file->local_streams, dump_streams, file);
1416static CliprdrLocalFile* file_for_request(CliprdrFileContext* file, UINT32 lockId, UINT32 listIndex)
1418 CliprdrLocalFile* f = file_info_for_request(file, lockId, listIndex);
1423 const char* name = f->name;
1424 f->fp = winpr_fopen(name,
"rb");
1428 char ebuffer[256] = WINPR_C_ARRAY_INIT;
1429 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1430 "[lockID %" PRIu32
", index %" PRIu32
1431 "] failed to open file '%s' [size %" PRId64
"] %s [%d]",
1432 lockId, listIndex, f->name, f->size,
1433 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)), errno);
1441static void cliprdr_local_file_try_close(CliprdrLocalFile* file, UINT res, UINT64 offset,
1448 WINPR_ASSERT(file->context);
1449 WLog_Print(file->context->log, WLOG_DEBUG,
"closing file %s after error %" PRIu32,
1452 else if (((file->size > 0) && (offset + size >= (UINT64)file->size)))
1454 WINPR_ASSERT(file->context);
1455 WLog_Print(file->context->log, WLOG_DEBUG,
"closing file %s after read", file->name);
1463 (void)fclose(file->fp);
1467static UINT cliprdr_file_context_server_file_size_request(
1470 WINPR_ASSERT(fileContentsRequest);
1472 if (fileContentsRequest->cbRequested !=
sizeof(UINT64))
1474 WLog_Print(file->log, WLOG_WARN,
"unexpected FILECONTENTS_SIZE request: %" PRIu32
" bytes",
1475 fileContentsRequest->cbRequested);
1478 HashTable_Lock(file->local_streams);
1480 UINT res = CHANNEL_RC_OK;
1481 CliprdrLocalFile* rfile =
1482 file_for_request(file, fileContentsRequest->clipDataId, fileContentsRequest->listIndex);
1484 res = cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1487 if (_fseeki64(rfile->fp, 0, SEEK_END) < 0)
1488 res = cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1491 const INT64 size = _ftelli64(rfile->fp);
1493 cliprdr_local_file_try_close(rfile, res, 0, 0);
1495 res = cliprdr_file_context_send_contents_response(file, fileContentsRequest, &size,
1500 HashTable_Unlock(file->local_streams);
1504static UINT cliprdr_file_context_server_file_range_request(
1507 BYTE* data =
nullptr;
1509 WINPR_ASSERT(fileContentsRequest);
1511 HashTable_Lock(file->local_streams);
1512 const UINT64 offset = (((UINT64)fileContentsRequest->nPositionHigh) << 32) |
1513 ((UINT64)fileContentsRequest->nPositionLow);
1515 CliprdrLocalFile* rfile =
1516 file_for_request(file, fileContentsRequest->clipDataId, fileContentsRequest->listIndex);
1520 if (_fseeki64(rfile->fp, WINPR_ASSERTING_INT_CAST(int64_t, offset), SEEK_SET) < 0)
1523 data = malloc(fileContentsRequest->cbRequested);
1528 const size_t r = fread(data, 1, fileContentsRequest->cbRequested, rfile->fp);
1530 cliprdr_file_context_send_contents_response(file, fileContentsRequest, data, r);
1533 cliprdr_local_file_try_close(rfile, rc, offset, fileContentsRequest->cbRequested);
1534 HashTable_Unlock(file->local_streams);
1539 cliprdr_local_file_try_close(rfile, ERROR_INTERNAL_ERROR, offset,
1540 fileContentsRequest->cbRequested);
1542 HashTable_Unlock(file->local_streams);
1543 return cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1546static void cliprdr_local_stream_free(
void* obj);
1548static UINT change_lock(CliprdrFileContext* file, UINT32 lockId, BOOL lock)
1550 UINT rc = CHANNEL_RC_OK;
1554 HashTable_Lock(file->local_streams);
1557 CliprdrLocalStream* stream = HashTable_GetItemValue(file->local_streams, &lockId);
1558 if (lock && !stream)
1560 stream = cliprdr_local_stream_new(file, lockId,
nullptr, 0);
1561 if (!HashTable_Insert(file->local_streams, &lockId, stream))
1563 rc = ERROR_INTERNAL_ERROR;
1564 cliprdr_local_stream_free(stream);
1567 file->local_lock_id = lockId;
1571 stream->locked = lock;
1572 stream->lockId = lockId;
1579 if (!HashTable_Foreach(file->local_streams, local_stream_discard, file))
1580 rc = ERROR_INTERNAL_ERROR;
1583 HashTable_Unlock(file->local_streams);
1587static UINT cliprdr_file_context_lock(CliprdrClientContext* context,
1590 WINPR_ASSERT(context);
1591 WINPR_ASSERT(lockClipboardData);
1592 CliprdrFileContext* file = (context->custom);
1593 return change_lock(file, lockClipboardData->clipDataId, TRUE);
1596static UINT cliprdr_file_context_unlock(CliprdrClientContext* context,
1599 WINPR_ASSERT(context);
1600 WINPR_ASSERT(unlockClipboardData);
1601 CliprdrFileContext* file = (context->custom);
1602 return change_lock(file, unlockClipboardData->clipDataId, FALSE);
1605static UINT cliprdr_file_context_server_file_contents_request(
1608 UINT error = NO_ERROR;
1610 WINPR_ASSERT(context);
1611 WINPR_ASSERT(fileContentsRequest);
1613 CliprdrFileContext* file = (context->custom);
1620 if ((fileContentsRequest->dwFlags & (FILECONTENTS_SIZE | FILECONTENTS_RANGE)) ==
1621 (FILECONTENTS_SIZE | FILECONTENTS_RANGE))
1623 WLog_Print(file->log, WLOG_ERROR,
"invalid CLIPRDR_FILECONTENTS_REQUEST.dwFlags");
1624 return cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1627 if (fileContentsRequest->dwFlags & FILECONTENTS_SIZE)
1628 error = cliprdr_file_context_server_file_size_request(file, fileContentsRequest);
1630 if (fileContentsRequest->dwFlags & FILECONTENTS_RANGE)
1631 error = cliprdr_file_context_server_file_range_request(file, fileContentsRequest);
1635 WLog_Print(file->log, WLOG_ERROR,
"failed to handle CLIPRDR_FILECONTENTS_REQUEST: 0x%08X",
1637 return cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1640 return CHANNEL_RC_OK;
1643BOOL cliprdr_file_context_init(CliprdrFileContext* file, CliprdrClientContext* cliprdr)
1646 WINPR_ASSERT(cliprdr);
1648 cliprdr->custom = file;
1649 file->context = cliprdr;
1651 cliprdr->ServerLockClipboardData = cliprdr_file_context_lock;
1652 cliprdr->ServerUnlockClipboardData = cliprdr_file_context_unlock;
1653 cliprdr->ServerFileContentsRequest = cliprdr_file_context_server_file_contents_request;
1654#if defined(WITH_FUSE)
1655 cliprdr->ServerFileContentsResponse = cliprdr_file_context_server_file_contents_response;
1657 CliprdrFuseFile* root_dir = fuse_file_new_root(file);
1658 return root_dir !=
nullptr;
1664#if defined(WITH_FUSE)
1666static bool clear_all_selections(CliprdrFileContext* file_context)
1668 WINPR_ASSERT(file_context);
1669 WINPR_ASSERT(file_context->inode_table);
1671 HashTable_Lock(file_context->inode_table);
1672 const bool rc = clear_selection(file_context, TRUE,
nullptr);
1674 HashTable_Clear(file_context->clip_data_table);
1675 HashTable_Unlock(file_context->inode_table);
1680BOOL cliprdr_file_context_uninit(CliprdrFileContext* file, CliprdrClientContext* cliprdr)
1683 WINPR_ASSERT(cliprdr);
1687#if defined(WITH_FUSE)
1688 if (file->inode_table)
1690 if (!clear_no_cdi_entry(file))
1692 if (!clear_all_selections(file))
1697 HashTable_Clear(file->local_streams);
1699 file->context =
nullptr;
1701#if defined(WITH_FUSE)
1702 cliprdr->ServerFileContentsResponse =
nullptr;
1708static BOOL cliprdr_file_content_changed_and_update(
void* ihash,
size_t hsize,
const void* data,
1712 BYTE hash[WINPR_SHA256_DIGEST_LENGTH] = WINPR_C_ARRAY_INIT;
1714 if (hsize <
sizeof(hash))
1717 if (!winpr_Digest(WINPR_MD_SHA256, data, size, hash,
sizeof(hash)))
1720 const BOOL changed = memcmp(hash, ihash,
sizeof(hash)) != 0;
1722 memcpy(ihash, hash,
sizeof(hash));
1726static BOOL cliprdr_file_client_content_changed_and_update(CliprdrFileContext* file,
1727 const void* data,
size_t size)
1730 return cliprdr_file_content_changed_and_update(file->client_data_hash,
1731 sizeof(file->client_data_hash), data, size);
1734#if defined(WITH_FUSE)
1735static fuse_ino_t get_next_free_inode(CliprdrFileContext* file_context)
1739 WINPR_ASSERT(file_context);
1741 ino = file_context->next_ino;
1742 while (ino == 0 || ino == FUSE_ROOT_ID ||
1743 HashTable_GetItemValue(file_context->inode_table, (
void*)(uintptr_t)ino))
1746 file_context->next_ino = ino + 1;
1751static CliprdrFuseFile* clip_data_dir_new(CliprdrFileContext* file_context, BOOL has_clip_data_id,
1752 UINT32 clip_data_id)
1754 WINPR_ASSERT(file_context);
1756 UINT64 data_id = clip_data_id;
1757 if (!has_clip_data_id)
1758 data_id = NO_CLIP_DATA_ID;
1760 CliprdrFuseFile* clip_data_dir = fuse_file_new(
"/%" PRIu64, data_id);
1764 clip_data_dir->ino = get_next_free_inode(file_context);
1765 clip_data_dir->is_directory = TRUE;
1766 clip_data_dir->is_readonly = TRUE;
1767 clip_data_dir->has_clip_data_id = has_clip_data_id;
1768 clip_data_dir->clip_data_id = clip_data_id;
1770 CliprdrFuseFile* root_dir = get_fuse_file_by_ino(file_context, FUSE_ROOT_ID);
1773 WLog_Print(file_context->log, WLOG_ERROR,
"FUSE root directory missing");
1774 fuse_file_free(clip_data_dir);
1777 if (!ArrayList_Append(root_dir->children, clip_data_dir))
1779 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to append FUSE file");
1780 fuse_file_free(clip_data_dir);
1783 clip_data_dir->parent = root_dir;
1785 if (!HashTable_Insert(file_context->inode_table, (
void*)(uintptr_t)clip_data_dir->ino,
1788 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to insert inode into inode table");
1789 ArrayList_Remove(root_dir->children, clip_data_dir);
1790 fuse_file_free(clip_data_dir);
1794 return clip_data_dir;
1797static char* get_parent_path(
const char* filepath)
1799 const char* base = strrchr(filepath,
'/');
1802 while ((base > filepath) && (*base ==
'/'))
1805 WINPR_ASSERT(base >= filepath);
1806 const size_t parent_path_length = 1ULL + (size_t)(base - filepath);
1807 char* parent_path = calloc(parent_path_length + 1,
sizeof(
char));
1811 memcpy(parent_path, filepath, parent_path_length);
1816static BOOL is_fuse_file_not_parent(WINPR_ATTR_UNUSED
const void* key,
void* value,
void* arg)
1818 CliprdrFuseFile* fuse_file = value;
1819 CliprdrFuseFindParentContext* find_context = arg;
1821 if (!fuse_file->is_directory)
1824 if (strncmp(find_context->parent_path, fuse_file->filename_with_root,
1825 fuse_file->filename_with_root_len + 1) == 0)
1827 find_context->parent = fuse_file;
1834static CliprdrFuseFile* get_parent_directory(CliprdrFileContext* file_context,
const char* path)
1836 CliprdrFuseFindParentContext find_context = WINPR_C_ARRAY_INIT;
1838 WINPR_ASSERT(file_context);
1841 find_context.parent_path = get_parent_path(path);
1842 if (!find_context.parent_path)
1845 WINPR_ASSERT(!find_context.parent);
1847 if (HashTable_Foreach(file_context->inode_table, is_fuse_file_not_parent, &find_context))
1849 free(find_context.parent_path);
1852 WINPR_ASSERT(find_context.parent);
1854 free(find_context.parent_path);
1856 return find_context.parent;
1860static BOOL selection_handle_file(CliprdrFileContext* file_context,
1861 CliprdrFuseClipDataEntry* clip_data_entry, uint32_t index,
1865 WINPR_ASSERT(file_context);
1866 WINPR_ASSERT(clip_data_entry);
1869 CliprdrFuseFile* clip_data_dir = clip_data_entry->clip_data_dir;
1870 WINPR_ASSERT(clip_data_dir);
1872 char filename[ARRAYSIZE(file->cFileName) * 8] = WINPR_C_ARRAY_INIT;
1873 const SSIZE_T filenamelen = ConvertWCharNToUtf8(file->cFileName, ARRAYSIZE(file->cFileName),
1874 filename, ARRAYSIZE(filename) - 1);
1875 if (filenamelen < 0)
1877 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to convert filename");
1881 for (
size_t j = 0; filename[j]; ++j)
1883 if (filename[j] ==
'\\')
1888 CliprdrFuseFile* fuse_file = fuse_file_new(
1889 "%.*s/%s", WINPR_ASSERTING_INT_CAST(
int, clip_data_dir->filename_with_root_len),
1890 clip_data_dir->filename_with_root, filename);
1893 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to create FUSE file");
1897 fuse_file->parent = get_parent_directory(file_context, fuse_file->filename_with_root);
1898 if (!fuse_file->parent)
1900 WLog_Print(file_context->log, WLOG_ERROR,
"Found no parent for FUSE file");
1904 if (!ArrayList_Append(fuse_file->parent->children, fuse_file))
1906 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to append FUSE file");
1910 fuse_file->list_idx = index;
1911 fuse_file->ino = get_next_free_inode(file_context);
1912 fuse_file->has_clip_data_id = clip_data_entry->has_clip_data_id;
1913 fuse_file->clip_data_id = clip_data_entry->clip_data_id;
1914 if (file->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1915 fuse_file->is_directory = TRUE;
1916 if (file->dwFileAttributes & FILE_ATTRIBUTE_READONLY)
1917 fuse_file->is_readonly = TRUE;
1918 if (file->dwFlags & FD_FILESIZE)
1920 fuse_file->size = ((UINT64)file->nFileSizeHigh << 32) + file->nFileSizeLow;
1921 fuse_file->has_size = TRUE;
1923 if (file->dwFlags & FD_WRITESTIME)
1927 filetime = file->ftLastWriteTime.dwHighDateTime;
1929 filetime += file->ftLastWriteTime.dwLowDateTime;
1931 fuse_file->last_write_time_unix =
1932 1LL * filetime / (10LL * 1000LL * 1000LL) - WIN32_FILETIME_TO_UNIX_EPOCH;
1933 fuse_file->has_last_write_time = TRUE;
1936 if (!HashTable_Insert(file_context->inode_table, (
void*)(uintptr_t)fuse_file->ino, fuse_file))
1938 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to insert inode into inode table");
1947 fuse_file_free(fuse_file);
1948 return clear_entry_selection(clip_data_entry);
1954static BOOL set_selection_for_clip_data_entry(CliprdrFileContext* file_context,
1955 CliprdrFuseClipDataEntry* clip_data_entry,
1958 WINPR_ASSERT(file_context);
1959 WINPR_ASSERT(clip_data_entry);
1960 WINPR_ASSERT(files);
1962 if (clip_data_entry->has_clip_data_id)
1963 WLog_Print(file_context->log, WLOG_DEBUG,
"Setting selection for clipDataId %u",
1964 clip_data_entry->clip_data_id);
1966 WLog_Print(file_context->log, WLOG_DEBUG,
"Setting selection");
1968 for (UINT32 i = 0; i < n_files; ++i)
1971 if (!selection_handle_file(file_context, clip_data_entry, i, file))
1975 if (clip_data_entry->has_clip_data_id)
1976 WLog_Print(file_context->log, WLOG_DEBUG,
"Selection set for clipDataId %u",
1977 clip_data_entry->clip_data_id);
1979 WLog_Print(file_context->log, WLOG_DEBUG,
"Selection set");
1984static BOOL update_exposed_path(CliprdrFileContext* file_context, wClipboard* clip,
1985 CliprdrFuseClipDataEntry* clip_data_entry)
1987 wClipboardDelegate* delegate =
nullptr;
1988 CliprdrFuseFile* clip_data_dir =
nullptr;
1990 WINPR_ASSERT(file_context);
1992 WINPR_ASSERT(clip_data_entry);
1994 delegate = ClipboardGetDelegate(clip);
1995 WINPR_ASSERT(delegate);
1997 clip_data_dir = clip_data_entry->clip_data_dir;
1998 WINPR_ASSERT(clip_data_dir);
2000 free(file_context->exposed_path);
2001 file_context->exposed_path = GetCombinedPath(file_context->path, clip_data_dir->filename);
2002 if (file_context->exposed_path)
2003 WLog_Print(file_context->log, WLOG_DEBUG,
"Updated exposed path to \"%s\"",
2004 file_context->exposed_path);
2006 delegate->basePath = file_context->exposed_path;
2008 return delegate->basePath !=
nullptr;
2012BOOL cliprdr_file_context_update_server_data(CliprdrFileContext* file_context, wClipboard* clip,
2013 const void* data,
size_t size)
2015#if defined(WITH_FUSE)
2016 CliprdrFuseClipDataEntry* clip_data_entry =
nullptr;
2021 WINPR_ASSERT(file_context);
2023 if (size > UINT32_MAX)
2026 if (cliprdr_parse_file_list(data, (UINT32)size, &files, &n_files))
2028 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to parse file list");
2032 HashTable_Lock(file_context->inode_table);
2033 HashTable_Lock(file_context->clip_data_table);
2034 if (does_server_support_clipdata_locking(file_context))
2035 clip_data_entry = HashTable_GetItemValue(
2036 file_context->clip_data_table, (
void*)(uintptr_t)file_context->current_clip_data_id);
2038 clip_data_entry = file_context->clip_data_entry_without_id;
2040 WINPR_ASSERT(clip_data_entry);
2042 if (!clear_entry_selection(clip_data_entry))
2044 WINPR_ASSERT(!clip_data_entry->clip_data_dir);
2046 clip_data_entry->clip_data_dir =
2047 clip_data_dir_new(file_context, does_server_support_clipdata_locking(file_context),
2048 file_context->current_clip_data_id);
2049 if (!clip_data_entry->clip_data_dir)
2051 if (!update_exposed_path(file_context, clip, clip_data_entry))
2053 if (!set_selection_for_clip_data_entry(file_context, clip_data_entry, files, n_files))
2059 HashTable_Unlock(file_context->clip_data_table);
2060 HashTable_Unlock(file_context->inode_table);
2068void* cliprdr_file_context_get_context(CliprdrFileContext* file)
2071 return file->clipboard;
2074void cliprdr_file_session_terminate(CliprdrFileContext* file, BOOL stop_thread)
2079#if defined(WITH_FUSE)
2080 WINPR_ASSERT(file->fuse_stop_sync);
2082 WLog_Print(file->log, WLOG_DEBUG,
"Setting FUSE exit flag");
2083 if (file->fuse_sess)
2084 fuse_session_exit(file->fuse_sess);
2088 WLog_Print(file->log, WLOG_DEBUG,
"Setting FUSE stop event");
2089 (void)SetEvent(file->fuse_stop_sync);
2095#if defined(WITH_FUSE)
2096 WLog_Print(file->log, WLOG_DEBUG,
"Forcing FUSE to check exit flag");
2098 (void)winpr_PathFileExists(file->path);
2101void cliprdr_file_context_free(CliprdrFileContext* file)
2106#if defined(WITH_FUSE)
2107 if (file->fuse_thread)
2109 WINPR_ASSERT(file->fuse_stop_sync);
2111 WLog_Print(file->log, WLOG_DEBUG,
"Stopping FUSE thread");
2112 cliprdr_file_session_terminate(file, TRUE);
2114 WLog_Print(file->log, WLOG_DEBUG,
"Waiting on FUSE thread");
2115 (void)WaitForSingleObject(file->fuse_thread, INFINITE);
2116 (void)CloseHandle(file->fuse_thread);
2118 if (file->fuse_stop_sync)
2119 (void)CloseHandle(file->fuse_stop_sync);
2120 if (file->fuse_start_sync)
2121 (void)CloseHandle(file->fuse_start_sync);
2123 HashTable_Free(file->request_table);
2124 HashTable_Free(file->clip_data_table);
2125 HashTable_Free(file->inode_table);
2128 HashTable_Free(file->local_streams);
2129 winpr_RemoveDirectory(file->path);
2131 free(file->exposed_path);
2135static BOOL create_base_path(CliprdrFileContext* file)
2139 char base[64] = WINPR_C_ARRAY_INIT;
2140 (void)_snprintf(base,
sizeof(base),
"com.freerdp.client.cliprdr.%" PRIu32,
2141 GetCurrentProcessId());
2143 file->path = GetKnownSubPath(KNOWN_PATH_TEMP, base);
2147 if (!winpr_PathFileExists(file->path) && !winpr_PathMakePath(file->path,
nullptr))
2149 WLog_Print(file->log, WLOG_ERROR,
"Failed to create directory '%s'", file->path);
2155static void cliprdr_local_file_free(CliprdrLocalFile* file)
2157 const CliprdrLocalFile empty = WINPR_C_ARRAY_INIT;
2162 WLog_Print(file->context->log, WLOG_DEBUG,
"closing file %s, discarding entry", file->name);
2163 (void)fclose(file->fp);
2169static BOOL cliprdr_local_file_new(CliprdrFileContext* context, CliprdrLocalFile* f,
2172 const CliprdrLocalFile empty = WINPR_C_ARRAY_INIT;
2174 WINPR_ASSERT(context);
2178 f->context = context;
2179 f->name = winpr_str_url_decode(path, strlen(path));
2185 cliprdr_local_file_free(f);
2189static void cliprdr_local_files_free(CliprdrLocalStream* stream)
2191 WINPR_ASSERT(stream);
2193 for (
size_t x = 0; x < stream->count; x++)
2194 cliprdr_local_file_free(&stream->files[x]);
2195 free(stream->files);
2197 stream->files =
nullptr;
2201static void cliprdr_local_stream_free(
void* obj)
2203 CliprdrLocalStream* stream = (CliprdrLocalStream*)obj;
2205 cliprdr_local_files_free(stream);
2210static BOOL append_entry(CliprdrLocalStream* stream,
const char* path)
2212 CliprdrLocalFile* tmp = realloc(stream->files,
sizeof(CliprdrLocalFile) * (stream->count + 1));
2215 stream->files = tmp;
2216 CliprdrLocalFile* f = &stream->files[stream->count++];
2218 return cliprdr_local_file_new(stream->context, f, path);
2221static BOOL is_directory(
const char* path)
2223 WCHAR* wpath = ConvertUtf8ToWCharAlloc(path,
nullptr);
2227 HANDLE hFile = CreateFileW(wpath, 0, FILE_SHARE_DELETE,
nullptr, OPEN_EXISTING,
2228 FILE_ATTRIBUTE_NORMAL,
nullptr);
2231 if (hFile == INVALID_HANDLE_VALUE)
2235 const BOOL status = GetFileInformationByHandle(hFile, &fileInformation);
2236 (void)CloseHandle(hFile);
2240 return (fileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
2243static BOOL add_directory(CliprdrLocalStream* stream,
const char* path)
2245 char* wildcardpath = GetCombinedPath(path,
"*");
2248 WCHAR* wpath = ConvertUtf8ToWCharAlloc(wildcardpath,
nullptr);
2254 HANDLE hFind = FindFirstFileW(wpath, &FindFileData);
2257 if (hFind == INVALID_HANDLE_VALUE)
2261 char* next =
nullptr;
2263 WCHAR dotbuffer[6] = WINPR_C_ARRAY_INIT;
2264 WCHAR dotdotbuffer[6] = WINPR_C_ARRAY_INIT;
2265 const WCHAR* dot = InitializeConstWCharFromUtf8(
".", dotbuffer, ARRAYSIZE(dotbuffer));
2266 const WCHAR* dotdot = InitializeConstWCharFromUtf8(
"..", dotdotbuffer, ARRAYSIZE(dotdotbuffer));
2269 if (_wcscmp(FindFileData.cFileName, dot) == 0)
2271 if (_wcscmp(FindFileData.cFileName, dotdot) == 0)
2274 char cFileName[MAX_PATH] = WINPR_C_ARRAY_INIT;
2275 (void)ConvertWCharNToUtf8(FindFileData.cFileName, ARRAYSIZE(FindFileData.cFileName),
2276 cFileName, ARRAYSIZE(cFileName));
2279 next = GetCombinedPath(path, cFileName);
2283 if (!append_entry(stream, next))
2285 if (is_directory(next))
2287 if (!add_directory(stream, next))
2290 }
while (FindNextFileW(hFind, &FindFileData));
2300static BOOL cliprdr_local_stream_update(CliprdrLocalStream* stream,
const char* data,
size_t size)
2303 WINPR_ASSERT(stream);
2307 cliprdr_local_files_free(stream);
2309 stream->files = calloc(size,
sizeof(CliprdrLocalFile));
2313 char* copy = strndup(data, size);
2317 char* saveptr =
nullptr;
2318 char* ptr = strtok_s(copy,
"\r\n", &saveptr);
2321 const char* name = ptr;
2322 if (strncmp(
"file:///", ptr, 8) == 0)
2324 else if (strncmp(
"file:/", ptr, 6) == 0)
2327 if (!append_entry(stream, name))
2330 if (is_directory(name))
2332 const BOOL res = add_directory(stream, name);
2336 ptr = strtok_s(
nullptr,
"\r\n", &saveptr);
2345CliprdrLocalStream* cliprdr_local_stream_new(CliprdrFileContext* context, UINT32 streamID,
2346 const char* data,
size_t size)
2348 WINPR_ASSERT(context);
2349 CliprdrLocalStream* stream = calloc(1,
sizeof(CliprdrLocalStream));
2353 stream->context = context;
2354 if (!cliprdr_local_stream_update(stream, data, size))
2357 stream->lockId = streamID;
2361 cliprdr_local_stream_free(stream);
2365static UINT32 UINTPointerHash(
const void*
id)
2368 return *((
const UINT32*)
id);
2371static BOOL UINTPointerCompare(
const void* pointer1,
const void* pointer2)
2373 if (!pointer1 || !pointer2)
2374 return pointer1 == pointer2;
2376 const UINT32* a = pointer1;
2377 const UINT32* b = pointer2;
2381static void* UINTPointerClone(
const void* other)
2383 const UINT32* src = other;
2387 UINT32* copy = calloc(1,
sizeof(UINT32));
2395#if defined(WITH_FUSE)
2396CliprdrFuseFile* fuse_file_new_root(CliprdrFileContext* file_context)
2398 CliprdrFuseFile* root_dir = fuse_file_new(
"/");
2402 root_dir->ino = FUSE_ROOT_ID;
2403 root_dir->is_directory = TRUE;
2404 root_dir->is_readonly = TRUE;
2406 if (!HashTable_Insert(file_context->inode_table, (
void*)(uintptr_t)root_dir->ino, root_dir))
2408 fuse_file_free(root_dir);
2416CliprdrFileContext* cliprdr_file_context_new(
void* context)
2418 CliprdrFileContext* file = calloc(1,
sizeof(CliprdrFileContext));
2422 file->log = WLog_Get(CLIENT_TAG(
"common.cliprdr.file"));
2423 file->clipboard = context;
2425 file->local_streams = HashTable_New(FALSE);
2426 if (!file->local_streams)
2429 if (!HashTable_SetHashFunction(file->local_streams, UINTPointerHash))
2433 wObject* hkobj = HashTable_KeyObject(file->local_streams);
2434 WINPR_ASSERT(hkobj);
2441 wObject* hobj = HashTable_ValueObject(file->local_streams);
2446#if defined(WITH_FUSE)
2447 file->inode_table = HashTable_New(FALSE);
2448 file->clip_data_table = HashTable_New(FALSE);
2449 file->request_table = HashTable_New(FALSE);
2450 if (!file->inode_table || !file->clip_data_table || !file->request_table)
2454 wObject* ctobj = HashTable_ValueObject(file->request_table);
2455 WINPR_ASSERT(ctobj);
2459 wObject* ctobj = HashTable_ValueObject(file->clip_data_table);
2460 WINPR_ASSERT(ctobj);
2465 if (!create_base_path(file))
2468#if defined(WITH_FUSE)
2469 if (!(file->fuse_start_sync = CreateEvent(
nullptr, TRUE, FALSE,
nullptr)))
2471 if (!(file->fuse_stop_sync = CreateEvent(
nullptr, TRUE, FALSE,
nullptr)))
2473 if (!(file->fuse_thread = CreateThread(
nullptr, 0, cliprdr_file_fuse_thread, file, 0,
nullptr)))
2476 if (WaitForSingleObject(file->fuse_start_sync, INFINITE) == WAIT_FAILED)
2477 WLog_Print(file->log, WLOG_ERROR,
"Failed to wait for start sync");
2482 WINPR_PRAGMA_DIAG_PUSH
2483 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2484 cliprdr_file_context_free(file);
2485 WINPR_PRAGMA_DIAG_POP
2489BOOL local_stream_discard(
const void* key,
void* value,
void* arg)
2491 CliprdrFileContext* file = arg;
2492 CliprdrLocalStream* stream = value;
2494 WINPR_ASSERT(stream);
2496 if (!stream->locked)
2497 HashTable_Remove(file->local_streams, key);
2501BOOL cliprdr_file_context_clear(CliprdrFileContext* file)
2505 WLog_Print(file->log, WLOG_DEBUG,
"clear file clipboard...");
2507 HashTable_Lock(file->local_streams);
2508 const BOOL res = HashTable_Foreach(file->local_streams, local_stream_discard, file);
2509 HashTable_Unlock(file->local_streams);
2511 memset(file->server_data_hash, 0,
sizeof(file->server_data_hash));
2512 memset(file->client_data_hash, 0,
sizeof(file->client_data_hash));
2516BOOL cliprdr_file_context_update_client_data(CliprdrFileContext* file,
const char* data,
2522 if (!cliprdr_file_client_content_changed_and_update(file, data, size))
2525 if (!cliprdr_file_context_clear(file))
2528 UINT32 lockId = file->local_lock_id;
2530 HashTable_Lock(file->local_streams);
2531 CliprdrLocalStream* stream = HashTable_GetItemValue(file->local_streams, &lockId);
2533 WLog_Print(file->log, WLOG_DEBUG,
"update client file list (stream=%p)...", (
void*)stream);
2535 rc = cliprdr_local_stream_update(stream, data, size);
2538 stream = cliprdr_local_stream_new(file, lockId, data, size);
2539 rc = HashTable_Insert(file->local_streams, &stream->lockId, stream);
2541 cliprdr_local_stream_free(stream);
2545 HashTable_Unlock(file->local_streams);
2549UINT32 cliprdr_file_context_current_flags(CliprdrFileContext* file)
2553 if ((file->file_capability_flags & CB_STREAM_FILECLIP_ENABLED) == 0)
2556 if (!file->file_formats_registered)
2559 return CB_STREAM_FILECLIP_ENABLED | CB_FILECLIP_NO_FILE_PATHS |
2560 CB_HUGE_FILE_SUPPORT_ENABLED;
2563BOOL cliprdr_file_context_set_locally_available(CliprdrFileContext* file, BOOL available)
2566 file->file_formats_registered = available;
2570BOOL cliprdr_file_context_remote_set_flags(CliprdrFileContext* file, UINT32 flags)
2573 file->file_capability_flags = flags;
2577UINT32 cliprdr_file_context_remote_get_flags(CliprdrFileContext* file)
2580 return file->file_capability_flags;
2583BOOL cliprdr_file_context_has_local_support(CliprdrFileContext* file)
2587#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