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;
141 CliprdrFileContext* context;
149 CliprdrLocalFile* files;
150 CliprdrFileContext* context;
153struct cliprdr_file_context
155#if defined(WITH_FUSE)
157 HANDLE fuse_start_sync;
158 HANDLE fuse_stop_sync;
160 struct fuse_session* fuse_sess;
161#if FUSE_USE_VERSION < 30
162 struct fuse_chan* ch;
165 wHashTable* inode_table;
166 wHashTable* dir_table;
167 wHashTable* clip_data_table;
168 wHashTable* request_table;
170 CliprdrFuseClipDataEntry* clip_data_entry_without_id;
171 UINT32 current_clip_data_id;
174 UINT32 next_clip_data_id;
175 UINT32 next_stream_id;
179 BOOL file_formats_registered;
180 UINT32 file_capability_flags;
182 UINT32 local_lock_id;
184 wHashTable* local_streams;
187 CliprdrClientContext* context;
190 BYTE server_data_hash[WINPR_SHA256_DIGEST_LENGTH];
191 BYTE client_data_hash[WINPR_SHA256_DIGEST_LENGTH];
194#if defined(WITH_FUSE)
195static CliprdrFuseFile* get_fuse_file_by_ino(CliprdrFileContext* file_context, fuse_ino_t fuse_ino);
197static void fuse_file_free(
void* data)
199 CliprdrFuseFile* fuse_file = data;
204 ArrayList_Free(fuse_file->children);
205 free(fuse_file->filename_with_root);
210WINPR_ATTR_FORMAT_ARG(1, 2)
211WINPR_ATTR_MALLOC(fuse_file_free, 1)
213static CliprdrFuseFile* fuse_file_new(WINPR_FORMAT_ARG const
char* fmt, ...)
215 CliprdrFuseFile* file = calloc(1,
sizeof(CliprdrFuseFile));
219 file->children = ArrayList_New(FALSE);
226 va_list ap = WINPR_C_ARRAY_INIT;
229 winpr_vasprintf(&file->filename_with_root, &file->filename_with_root_len, fmt, ap);
236 if (file->filename_with_root && (file->filename_with_root_len > 0))
238 file->filename_len = 0;
239 file->filename = strrchr(file->filename_with_root,
'/') + 1;
241 file->filename_len = strnlen(file->filename, file->filename_with_root_len);
245 fuse_file_free(file);
249static void clip_data_entry_free(
void* data)
251 CliprdrFuseClipDataEntry* clip_data_entry = data;
253 if (!clip_data_entry)
256 if (clip_data_entry->has_clip_data_id)
258 CliprdrFileContext* file_context = clip_data_entry->file_context;
261 WINPR_ASSERT(file_context);
263 unlock_clipboard_data.common.msgType = CB_UNLOCK_CLIPDATA;
264 unlock_clipboard_data.clipDataId = clip_data_entry->clip_data_id;
266 const UINT rc = file_context->context->ClientUnlockClipboardData(file_context->context,
267 &unlock_clipboard_data);
268 if (rc != CHANNEL_RC_OK)
269 WLog_Print(file_context->log, WLOG_DEBUG,
270 "ClientUnlockClipboardData failed with %" PRIu32, rc);
272 clip_data_entry->has_clip_data_id = FALSE;
274 WLog_Print(file_context->log, WLOG_DEBUG,
"Destroyed ClipDataEntry with id %u",
275 clip_data_entry->clip_data_id);
278 free(clip_data_entry);
281static BOOL does_server_support_clipdata_locking(CliprdrFileContext* file_context)
283 WINPR_ASSERT(file_context);
285 return (cliprdr_file_context_remote_get_flags(file_context) & CB_CAN_LOCK_CLIPDATA) != 0;
288static UINT32 get_next_free_clip_data_id(CliprdrFileContext* file_context)
290 UINT32 clip_data_id = 0;
292 WINPR_ASSERT(file_context);
294 HashTable_Lock(file_context->inode_table);
295 clip_data_id = file_context->next_clip_data_id;
296 while (clip_data_id == 0 ||
297 HashTable_GetItemValue(file_context->clip_data_table, (
void*)(uintptr_t)clip_data_id))
300 file_context->next_clip_data_id = clip_data_id + 1;
301 HashTable_Unlock(file_context->inode_table);
306static CliprdrFuseClipDataEntry* clip_data_entry_new(CliprdrFileContext* file_context,
307 BOOL needs_clip_data_id)
309 CliprdrFuseClipDataEntry* clip_data_entry =
nullptr;
312 WINPR_ASSERT(file_context);
314 clip_data_entry = calloc(1,
sizeof(CliprdrFuseClipDataEntry));
315 if (!clip_data_entry)
318 clip_data_entry->file_context = file_context;
319 clip_data_entry->clip_data_id = get_next_free_clip_data_id(file_context);
321 if (!needs_clip_data_id)
322 return clip_data_entry;
324 lock_clipboard_data.common.msgType = CB_LOCK_CLIPDATA;
325 lock_clipboard_data.clipDataId = clip_data_entry->clip_data_id;
327 if (file_context->context->ClientLockClipboardData(file_context->context, &lock_clipboard_data))
329 HashTable_Lock(file_context->inode_table);
330 clip_data_entry_free(clip_data_entry);
331 HashTable_Unlock(file_context->inode_table);
334 clip_data_entry->has_clip_data_id = TRUE;
336 WLog_Print(file_context->log, WLOG_DEBUG,
"Created ClipDataEntry with id %u",
337 clip_data_entry->clip_data_id);
339 return clip_data_entry;
342static BOOL should_remove_fuse_file(CliprdrFuseFile* fuse_file, BOOL all_files,
343 BOOL has_clip_data_id, UINT32 clip_data_id)
348 if (fuse_file->ino == FUSE_ROOT_ID)
350 if (!fuse_file->has_clip_data_id && !has_clip_data_id)
352 if (fuse_file->has_clip_data_id && has_clip_data_id &&
353 (fuse_file->clip_data_id == clip_data_id))
359static BOOL maybe_clear_fuse_request(
const void* key,
void* value,
void* arg)
361 CliprdrFuseRequest* fuse_request = value;
362 FuseFileClearContext* clear_context = arg;
363 CliprdrFileContext* file_context = clear_context->file_context;
364 CliprdrFuseFile* fuse_file = fuse_request->fuse_file;
366 WINPR_ASSERT(file_context);
368 if (!should_remove_fuse_file(fuse_file, clear_context->all_files,
369 clear_context->has_clip_data_id, clear_context->clip_data_id))
372 DEBUG_CLIPRDR(file_context->log,
"Clearing FileContentsRequest for file \"%s\"",
373 fuse_file->filename_with_root);
375 fuse_reply_err(fuse_request->fuse_req, EIO);
376 HashTable_Remove(file_context->request_table, key);
381static BOOL maybe_steal_inode(
const void* key,
void* value,
void* arg)
383 CliprdrFuseFile* fuse_file = value;
384 FuseFileClearContext* clear_context = arg;
385 CliprdrFileContext* file_context = clear_context->file_context;
387 WINPR_ASSERT(file_context);
389 if (should_remove_fuse_file(fuse_file, clear_context->all_files,
390 clear_context->has_clip_data_id, clear_context->clip_data_id))
392 if (!ArrayList_Append(clear_context->fuse_files, fuse_file))
393 WLog_Print(file_context->log, WLOG_ERROR,
394 "Failed to append FUSE file to list for deletion");
396 if (!clear_context->all_files && fuse_file->is_directory)
398 HashTable_Remove(file_context->dir_table, fuse_file->filename_with_root);
400 HashTable_Remove(file_context->inode_table, key);
406static BOOL notify_delete_child(
void* data, WINPR_ATTR_UNUSED
size_t index, va_list ap)
408 CliprdrFuseFile* child = data;
412 CliprdrFileContext* file_context = va_arg(ap, CliprdrFileContext*);
413 CliprdrFuseFile* parent = va_arg(ap, CliprdrFuseFile*);
415 WINPR_ASSERT(file_context);
416 WINPR_ASSERT(parent);
418 WINPR_ASSERT(file_context->fuse_sess);
419 fuse_lowlevel_notify_delete(file_context->fuse_sess, parent->ino, child->ino, child->filename,
420 child->filename_len);
425static BOOL invalidate_inode(
void* data, WINPR_ATTR_UNUSED
size_t index, va_list ap)
427 CliprdrFuseFile* fuse_file = data;
429 WINPR_ASSERT(fuse_file);
431 CliprdrFileContext* file_context = va_arg(ap, CliprdrFileContext*);
432 WINPR_ASSERT(file_context);
433 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);
447static bool clear_selection(CliprdrFileContext* file_context, BOOL all_selections,
448 CliprdrFuseClipDataEntry* clip_data_entry)
451 FuseFileClearContext clear_context = WINPR_C_ARRAY_INIT;
452 CliprdrFuseFile* clip_data_dir =
nullptr;
454 WINPR_ASSERT(file_context);
456 clear_context.file_context = file_context;
457 clear_context.fuse_files = ArrayList_New(FALSE);
458 WINPR_ASSERT(clear_context.fuse_files);
460 wObject* aobj = ArrayList_Object(clear_context.fuse_files);
466 clip_data_dir = clip_data_entry->clip_data_dir;
467 clip_data_entry->clip_data_dir =
nullptr;
469 WINPR_ASSERT(clip_data_dir);
471 CliprdrFuseFile* root_dir = get_fuse_file_by_ino(file_context, FUSE_ROOT_ID);
473 ArrayList_Remove(root_dir->children, clip_data_dir);
475 clear_context.has_clip_data_id = clip_data_dir->has_clip_data_id;
476 clear_context.clip_data_id = clip_data_dir->clip_data_id;
478 clear_context.all_files = all_selections;
480 if (clip_data_entry && clip_data_entry->has_clip_data_id)
481 WLog_Print(file_context->log, WLOG_DEBUG,
"Clearing selection for clipDataId %u",
482 clip_data_entry->clip_data_id);
484 WLog_Print(file_context->log, WLOG_DEBUG,
"Clearing selection%s",
485 all_selections ?
"s" :
"");
487 if (!HashTable_Foreach(file_context->request_table, maybe_clear_fuse_request, &clear_context))
491 HashTable_Clear(file_context->dir_table);
493 if (!HashTable_Foreach(file_context->inode_table, maybe_steal_inode, &clear_context))
495 HashTable_Unlock(file_context->inode_table);
497 if (file_context->fuse_sess)
507 if (!ArrayList_ForEach(clear_context.fuse_files, invalidate_inode, file_context))
509 CliprdrFuseFile* root_dir = get_fuse_file_by_ino(file_context, FUSE_ROOT_ID);
510 if (clip_data_dir && root_dir)
512 fuse_lowlevel_notify_delete(file_context->fuse_sess, root_dir->ino, clip_data_dir->ino,
513 clip_data_dir->filename, clip_data_dir->filename_len);
516 ArrayList_Free(clear_context.fuse_files);
518 HashTable_Lock(file_context->inode_table);
519 if (clip_data_entry && clip_data_entry->has_clip_data_id)
520 WLog_Print(file_context->log, WLOG_DEBUG,
"Selection cleared for clipDataId %u",
521 clip_data_entry->clip_data_id);
523 WLog_Print(file_context->log, WLOG_DEBUG,
"Selection%s cleared", all_selections ?
"s" :
"");
528static bool clear_entry_selection(CliprdrFuseClipDataEntry* clip_data_entry)
530 WINPR_ASSERT(clip_data_entry);
532 if (!clip_data_entry->clip_data_dir)
535 return clear_selection(clip_data_entry->file_context, FALSE, clip_data_entry);
539static bool clear_no_cdi_entry(CliprdrFileContext* file_context)
542 WINPR_ASSERT(file_context);
544 WINPR_ASSERT(file_context->inode_table);
545 HashTable_Lock(file_context->inode_table);
546 if (file_context->clip_data_entry_without_id)
548 res = clear_entry_selection(file_context->clip_data_entry_without_id);
550 clip_data_entry_free(file_context->clip_data_entry_without_id);
551 file_context->clip_data_entry_without_id =
nullptr;
553 HashTable_Unlock(file_context->inode_table);
558static BOOL clear_clip_data_entries(WINPR_ATTR_UNUSED
const void* key,
void* value,
559 WINPR_ATTR_UNUSED
void* arg)
561 return clear_entry_selection(value);
565static UINT clear_cdi_entries(CliprdrFileContext* file_context)
567 UINT res = CHANNEL_RC_OK;
568 WINPR_ASSERT(file_context);
570 HashTable_Lock(file_context->inode_table);
571 if (!HashTable_Foreach(file_context->clip_data_table, clear_clip_data_entries,
nullptr))
572 res = ERROR_INTERNAL_ERROR;
574 HashTable_Clear(file_context->clip_data_table);
575 HashTable_Unlock(file_context->inode_table);
580static UINT prepare_clip_data_entry_with_id(CliprdrFileContext* file_context)
582 CliprdrFuseClipDataEntry* clip_data_entry =
nullptr;
584 WINPR_ASSERT(file_context);
586 clip_data_entry = clip_data_entry_new(file_context, TRUE);
587 if (!clip_data_entry)
589 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to create clipDataEntry");
590 return ERROR_INTERNAL_ERROR;
593 HashTable_Lock(file_context->inode_table);
594 if (!HashTable_Insert(file_context->clip_data_table,
595 (
void*)(uintptr_t)clip_data_entry->clip_data_id, clip_data_entry))
597 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to insert clipDataEntry");
598 clip_data_entry_free(clip_data_entry);
599 HashTable_Unlock(file_context->inode_table);
600 return ERROR_INTERNAL_ERROR;
602 HashTable_Unlock(file_context->inode_table);
606 file_context->current_clip_data_id = clip_data_entry->clip_data_id;
608 return CHANNEL_RC_OK;
611static UINT prepare_clip_data_entry_without_id(CliprdrFileContext* file_context)
613 WINPR_ASSERT(file_context);
614 WINPR_ASSERT(!file_context->clip_data_entry_without_id);
616 file_context->clip_data_entry_without_id = clip_data_entry_new(file_context, FALSE);
617 if (!file_context->clip_data_entry_without_id)
619 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to create clipDataEntry");
620 return ERROR_INTERNAL_ERROR;
623 return CHANNEL_RC_OK;
627UINT cliprdr_file_context_notify_new_server_format_list(CliprdrFileContext* file_context)
629 UINT rc = CHANNEL_RC_OK;
630 WINPR_ASSERT(file_context);
631 WINPR_ASSERT(file_context->context);
633#if defined(WITH_FUSE)
634 if (!clear_no_cdi_entry(file_context))
635 return ERROR_INTERNAL_ERROR;
637 rc = clear_cdi_entries(file_context);
638 if (rc != CHANNEL_RC_OK)
641 if (does_server_support_clipdata_locking(file_context))
642 rc = prepare_clip_data_entry_with_id(file_context);
644 rc = prepare_clip_data_entry_without_id(file_context);
649UINT cliprdr_file_context_notify_new_client_format_list(CliprdrFileContext* file_context)
651 WINPR_ASSERT(file_context);
652 WINPR_ASSERT(file_context->context);
654#if defined(WITH_FUSE)
655 if (!clear_no_cdi_entry(file_context))
656 return ERROR_INTERNAL_ERROR;
658 return clear_cdi_entries(file_context);
661 return CHANNEL_RC_OK;
664static CliprdrLocalStream* cliprdr_local_stream_new(CliprdrFileContext* context, UINT32 streamID,
665 const char* data,
size_t size);
666static void cliprdr_file_session_terminate(CliprdrFileContext* file, BOOL stop_thread);
667static BOOL local_stream_discard(
const void* key,
void* value,
void* arg);
669WINPR_ATTR_FORMAT_ARG(6, 7)
670static
void writelog(wLog* log, DWORD level, const
char* fname, const
char* fkt,
size_t line,
671 WINPR_FORMAT_ARG const
char* fmt, ...)
673 if (!WLog_IsLevelActive(log, level))
676 va_list ap = WINPR_C_ARRAY_INIT;
678 WLog_PrintTextMessageVA(log, level, line, fname, fkt, fmt, ap);
682#if defined(WITH_FUSE)
683static CliprdrFuseFile* fuse_file_new_root(CliprdrFileContext* file_context);
684static void cliprdr_file_fuse_lookup(fuse_req_t req, fuse_ino_t parent,
const char* name);
685static void cliprdr_file_fuse_getattr(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info* fi);
686static void cliprdr_file_fuse_readdir(fuse_req_t req, fuse_ino_t ino,
size_t size, off_t off,
687 struct fuse_file_info* fi);
688static void cliprdr_file_fuse_read(fuse_req_t req, fuse_ino_t ino,
size_t size, off_t off,
689 struct fuse_file_info* fi);
690static void cliprdr_file_fuse_open(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info* fi);
691static void cliprdr_file_fuse_opendir(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info* fi);
693static const struct fuse_lowlevel_ops cliprdr_file_fuse_oper = {
694 .lookup = cliprdr_file_fuse_lookup,
695 .getattr = cliprdr_file_fuse_getattr,
696 .readdir = cliprdr_file_fuse_readdir,
697 .open = cliprdr_file_fuse_open,
698 .read = cliprdr_file_fuse_read,
699 .opendir = cliprdr_file_fuse_opendir,
702CliprdrFuseFile* get_fuse_file_by_ino(CliprdrFileContext* file_context, fuse_ino_t fuse_ino)
704 WINPR_ASSERT(file_context);
706 return HashTable_GetItemValue(file_context->inode_table, (
void*)(uintptr_t)fuse_ino);
709static CliprdrFuseFile*
710get_fuse_file_by_name_from_parent(WINPR_ATTR_UNUSED CliprdrFileContext* file_context,
711 CliprdrFuseFile* parent,
const char* name)
713 WINPR_ASSERT(file_context);
714 WINPR_ASSERT(parent);
716 for (
size_t i = 0; i < ArrayList_Count(parent->children); ++i)
718 CliprdrFuseFile* child = ArrayList_GetItem(parent->children, i);
722 if (strncmp(name, child->filename, child->filename_len + 1) == 0)
726 DEBUG_CLIPRDR(file_context->log,
"Requested file \"%s\" in directory \"%s\" does not exist",
727 name, parent->filename);
732static CliprdrFuseRequest* cliprdr_fuse_request_new(CliprdrFileContext* file_context,
733 CliprdrFuseFile* fuse_file, fuse_req_t fuse_req,
734 FuseLowlevelOperationType operation_type)
736 CliprdrFuseRequest* fuse_request =
nullptr;
737 UINT32 stream_id = file_context->next_stream_id;
739 WINPR_ASSERT(file_context);
740 WINPR_ASSERT(fuse_file);
742 fuse_request = calloc(1,
sizeof(CliprdrFuseRequest));
745 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to allocate FUSE request for file \"%s\"",
746 fuse_file->filename_with_root);
750 fuse_request->fuse_file = fuse_file;
751 fuse_request->fuse_req = fuse_req;
752 fuse_request->operation_type = operation_type;
754 while (stream_id == 0 ||
755 HashTable_GetItemValue(file_context->request_table, (
void*)(uintptr_t)stream_id))
757 fuse_request->stream_id = stream_id;
759 file_context->next_stream_id = stream_id + 1;
761 if (!HashTable_Insert(file_context->request_table, (
void*)(uintptr_t)fuse_request->stream_id,
764 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to track FUSE request for file \"%s\"",
765 fuse_file->filename_with_root);
773static BOOL request_file_size_async(CliprdrFileContext* file_context, CliprdrFuseFile* fuse_file,
774 fuse_req_t fuse_req, FuseLowlevelOperationType operation_type)
776 CliprdrFuseRequest* fuse_request =
nullptr;
779 WINPR_ASSERT(file_context);
780 WINPR_ASSERT(fuse_file);
782 fuse_request = cliprdr_fuse_request_new(file_context, fuse_file, fuse_req, operation_type);
786 file_contents_request.common.msgType = CB_FILECONTENTS_REQUEST;
787 file_contents_request.streamId = fuse_request->stream_id;
788 file_contents_request.listIndex = fuse_file->list_idx;
789 file_contents_request.dwFlags = FILECONTENTS_SIZE;
790 file_contents_request.cbRequested = 0x8;
791 file_contents_request.haveClipDataId = fuse_file->has_clip_data_id;
792 file_contents_request.clipDataId = fuse_file->clip_data_id;
794 if (file_context->context->ClientFileContentsRequest(file_context->context,
795 &file_contents_request))
797 WLog_Print(file_context->log, WLOG_ERROR,
798 "Failed to send FileContentsRequest for file \"%s\"",
799 fuse_file->filename_with_root);
800 HashTable_Remove(file_context->request_table, (
void*)(uintptr_t)fuse_request->stream_id);
804 DEBUG_CLIPRDR(file_context->log,
"Requested file size for file \"%s\" with stream id %u",
805 fuse_file->filename, fuse_request->stream_id);
811static void write_file_attributes(CliprdrFuseFile* fuse_file,
struct stat* attr)
813 memset(attr, 0,
sizeof(
struct stat));
818 attr->st_ino = fuse_file->ino;
819 if (fuse_file->is_directory)
821 attr->st_mode = S_IFDIR | (fuse_file->is_readonly ? 0555 : 0755);
826 attr->st_mode = S_IFREG | (fuse_file->is_readonly ? 0444 : 0644);
828 attr->st_size = WINPR_ASSERTING_INT_CAST(off_t, fuse_file->size);
830 attr->st_uid = getuid();
831 attr->st_gid = getgid();
832 attr->st_atime = attr->st_mtime = attr->st_ctime =
833 (fuse_file->has_last_write_time ? fuse_file->last_write_time_unix : time(
nullptr));
836static void cliprdr_file_fuse_lookup(fuse_req_t fuse_req, fuse_ino_t parent_ino,
const char* name)
838 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
839 CliprdrFuseFile* parent =
nullptr;
840 CliprdrFuseFile* fuse_file =
nullptr;
841 struct fuse_entry_param entry = WINPR_C_ARRAY_INIT;
843 WINPR_ASSERT(file_context);
845 HashTable_Lock(file_context->inode_table);
846 if (!(parent = get_fuse_file_by_ino(file_context, parent_ino)) || !parent->is_directory)
848 HashTable_Unlock(file_context->inode_table);
849 fuse_reply_err(fuse_req, ENOENT);
852 if (!(fuse_file = get_fuse_file_by_name_from_parent(file_context, parent, name)))
854 HashTable_Unlock(file_context->inode_table);
855 fuse_reply_err(fuse_req, ENOENT);
859 DEBUG_CLIPRDR(file_context->log,
"lookup() has been called for \"%s\"", name);
860 DEBUG_CLIPRDR(file_context->log,
"Parent is \"%s\", child is \"%s\"",
861 parent->filename_with_root, fuse_file->filename_with_root);
863 if (!fuse_file->is_directory && !fuse_file->has_size)
868 request_file_size_async(file_context, fuse_file, fuse_req, FUSE_LL_OPERATION_LOOKUP);
869 HashTable_Unlock(file_context->inode_table);
872 fuse_reply_err(fuse_req, EIO);
877 entry.ino = fuse_file->ino;
878 write_file_attributes(fuse_file, &entry.attr);
879 entry.attr_timeout = 1.0;
880 entry.entry_timeout = 1.0;
881 HashTable_Unlock(file_context->inode_table);
883 fuse_reply_entry(fuse_req, &entry);
886static void cliprdr_file_fuse_getattr(fuse_req_t fuse_req, fuse_ino_t fuse_ino,
887 WINPR_ATTR_UNUSED
struct fuse_file_info* file_info)
889 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
890 CliprdrFuseFile* fuse_file =
nullptr;
891 struct stat attr = WINPR_C_ARRAY_INIT;
893 WINPR_ASSERT(file_context);
895 HashTable_Lock(file_context->inode_table);
896 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
898 HashTable_Unlock(file_context->inode_table);
899 fuse_reply_err(fuse_req, ENOENT);
903 DEBUG_CLIPRDR(file_context->log,
"getattr() has been called for file \"%s\"",
904 fuse_file->filename_with_root);
906 if (!fuse_file->is_directory && !fuse_file->has_size)
911 request_file_size_async(file_context, fuse_file, fuse_req, FUSE_LL_OPERATION_GETATTR);
912 HashTable_Unlock(file_context->inode_table);
915 fuse_reply_err(fuse_req, EIO);
920 write_file_attributes(fuse_file, &attr);
921 HashTable_Unlock(file_context->inode_table);
923 fuse_reply_attr(fuse_req, &attr, 1.0);
926static void cliprdr_file_fuse_open(fuse_req_t fuse_req, fuse_ino_t fuse_ino,
927 struct fuse_file_info* file_info)
929 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
930 CliprdrFuseFile* fuse_file =
nullptr;
932 WINPR_ASSERT(file_context);
934 HashTable_Lock(file_context->inode_table);
935 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
937 HashTable_Unlock(file_context->inode_table);
938 fuse_reply_err(fuse_req, ENOENT);
941 if (fuse_file->is_directory)
943 HashTable_Unlock(file_context->inode_table);
944 fuse_reply_err(fuse_req, EISDIR);
947 HashTable_Unlock(file_context->inode_table);
949 if ((file_info->flags & O_ACCMODE) != O_RDONLY)
951 fuse_reply_err(fuse_req, EACCES);
956 file_info->direct_io = 1;
958 fuse_reply_open(fuse_req, file_info);
961static BOOL request_file_range_async(CliprdrFileContext* file_context, CliprdrFuseFile* fuse_file,
962 fuse_req_t fuse_req, off_t offset,
size_t requested_size)
966 WINPR_ASSERT(file_context);
967 WINPR_ASSERT(fuse_file);
969 if (requested_size > UINT32_MAX)
972 CliprdrFuseRequest* fuse_request =
973 cliprdr_fuse_request_new(file_context, fuse_file, fuse_req, FUSE_LL_OPERATION_READ);
977 file_contents_request.common.msgType = CB_FILECONTENTS_REQUEST;
978 file_contents_request.streamId = fuse_request->stream_id;
979 file_contents_request.listIndex = fuse_file->list_idx;
980 file_contents_request.dwFlags = FILECONTENTS_RANGE;
981 file_contents_request.nPositionLow = (UINT32)(offset & 0xFFFFFFFF);
982 file_contents_request.nPositionHigh = (UINT32)((offset >> 32) & 0xFFFFFFFF);
983 file_contents_request.cbRequested = (UINT32)requested_size;
984 file_contents_request.haveClipDataId = fuse_file->has_clip_data_id;
985 file_contents_request.clipDataId = fuse_file->clip_data_id;
987 if (file_context->context->ClientFileContentsRequest(file_context->context,
988 &file_contents_request))
990 WLog_Print(file_context->log, WLOG_ERROR,
991 "Failed to send FileContentsRequest for file \"%s\"",
992 fuse_file->filename_with_root);
993 HashTable_Remove(file_context->request_table, (
void*)(uintptr_t)fuse_request->stream_id);
1001 "Requested file range (%zu Bytes at offset %lu) for file \"%s\" with stream id %u",
1002 requested_size, offset, fuse_file->filename, fuse_request->stream_id);
1008static void cliprdr_file_fuse_read(fuse_req_t fuse_req, fuse_ino_t fuse_ino,
size_t size,
1009 off_t offset, WINPR_ATTR_UNUSED
struct fuse_file_info* file_info)
1011 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
1012 CliprdrFuseFile* fuse_file =
nullptr;
1015 WINPR_ASSERT(file_context);
1017 HashTable_Lock(file_context->inode_table);
1018 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
1020 HashTable_Unlock(file_context->inode_table);
1021 fuse_reply_err(fuse_req, ENOENT);
1024 if (fuse_file->is_directory)
1026 HashTable_Unlock(file_context->inode_table);
1027 fuse_reply_err(fuse_req, EISDIR);
1030 if (!fuse_file->has_size || (offset < 0) || ((
size_t)offset > fuse_file->size))
1032 HashTable_Unlock(file_context->inode_table);
1033 fuse_reply_err(fuse_req, EINVAL);
1037 size = MIN(size, 8ULL * 1024ULL * 1024ULL);
1039 result = request_file_range_async(file_context, fuse_file, fuse_req, offset, size);
1040 HashTable_Unlock(file_context->inode_table);
1043 fuse_reply_err(fuse_req, EIO);
1046static void cliprdr_file_fuse_opendir(fuse_req_t fuse_req, fuse_ino_t fuse_ino,
1047 struct fuse_file_info* file_info)
1049 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
1050 CliprdrFuseFile* fuse_file =
nullptr;
1052 WINPR_ASSERT(file_context);
1054 HashTable_Lock(file_context->inode_table);
1055 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
1057 HashTable_Unlock(file_context->inode_table);
1058 fuse_reply_err(fuse_req, ENOENT);
1061 if (!fuse_file->is_directory)
1063 HashTable_Unlock(file_context->inode_table);
1064 fuse_reply_err(fuse_req, ENOTDIR);
1067 HashTable_Unlock(file_context->inode_table);
1069 if ((file_info->flags & O_ACCMODE) != O_RDONLY)
1071 fuse_reply_err(fuse_req, EACCES);
1075 fuse_reply_open(fuse_req, file_info);
1078static void cliprdr_file_fuse_readdir(fuse_req_t fuse_req, fuse_ino_t fuse_ino,
size_t max_size,
1080 WINPR_ATTR_UNUSED
struct fuse_file_info* file_info)
1082 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
1083 CliprdrFuseFile* fuse_file =
nullptr;
1084 CliprdrFuseFile* child =
nullptr;
1085 struct stat attr = WINPR_C_ARRAY_INIT;
1086 size_t written_size = 0;
1087 size_t entry_size = 0;
1088 char* filename =
nullptr;
1089 char* buf =
nullptr;
1091 WINPR_ASSERT(file_context);
1093 HashTable_Lock(file_context->inode_table);
1094 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
1096 HashTable_Unlock(file_context->inode_table);
1097 fuse_reply_err(fuse_req, ENOENT);
1100 if (!fuse_file->is_directory)
1102 HashTable_Unlock(file_context->inode_table);
1103 fuse_reply_err(fuse_req, ENOTDIR);
1107 DEBUG_CLIPRDR(file_context->log,
"Reading directory \"%s\" at offset %lu",
1108 fuse_file->filename_with_root, offset);
1110 if ((offset < 0) || ((
size_t)offset >= ArrayList_Count(fuse_file->children)))
1112 HashTable_Unlock(file_context->inode_table);
1113 fuse_reply_buf(fuse_req,
nullptr, 0);
1117 buf = calloc(max_size,
sizeof(
char));
1120 HashTable_Unlock(file_context->inode_table);
1121 fuse_reply_err(fuse_req, ENOMEM);
1126 for (off_t i = offset; i < 2; ++i)
1130 write_file_attributes(fuse_file, &attr);
1135 write_file_attributes(fuse_file->parent, &attr);
1136 attr.st_ino = fuse_file->parent ? attr.st_ino : FUSE_ROOT_ID;
1137 attr.st_mode = fuse_file->parent ? attr.st_mode : 0555;
1142 WINPR_ASSERT(FALSE);
1149 entry_size = fuse_add_direntry(fuse_req, buf + written_size, max_size - written_size,
1150 filename, &attr, i + 1);
1151 if (entry_size > max_size - written_size)
1154 written_size += entry_size;
1157 for (
size_t j = 0, i = 2; j < ArrayList_Count(fuse_file->children); ++j, ++i)
1159 if (i < (
size_t)offset)
1162 child = ArrayList_GetItem(fuse_file->children, j);
1164 write_file_attributes(child, &attr);
1166 fuse_add_direntry(fuse_req, buf + written_size, max_size - written_size,
1167 child->filename, &attr, WINPR_ASSERTING_INT_CAST(off_t, i + 1));
1168 if (entry_size > max_size - written_size)
1171 written_size += entry_size;
1173 HashTable_Unlock(file_context->inode_table);
1175 fuse_reply_buf(fuse_req, buf, written_size);
1179static void fuse_abort(
int sig,
const char* signame,
void* context)
1181 CliprdrFileContext* file = (CliprdrFileContext*)context;
1185 WLog_Print(file->log, WLOG_INFO,
"signal %s [%d] aborting session", signame, sig);
1186 cliprdr_file_session_terminate(file, FALSE);
1190static DWORD WINAPI cliprdr_file_fuse_thread(LPVOID arg)
1192 CliprdrFileContext* file = (CliprdrFileContext*)arg;
1196 DEBUG_CLIPRDR(file->log,
"Starting fuse with mountpoint '%s'", file->path);
1198 struct fuse_args args = FUSE_ARGS_INIT(0,
nullptr);
1199 fuse_opt_add_arg(&args, file->path);
1200 file->fuse_sess = fuse_session_new(&args, &cliprdr_file_fuse_oper,
1201 sizeof(cliprdr_file_fuse_oper), (
void*)file);
1202 (void)SetEvent(file->fuse_start_sync);
1204 if (file->fuse_sess !=
nullptr)
1206 if (freerdp_add_signal_cleanup_handler(file, fuse_abort))
1208 if (0 == fuse_session_mount(file->fuse_sess, file->path))
1210 fuse_session_loop(file->fuse_sess);
1211 fuse_session_unmount(file->fuse_sess);
1213 freerdp_del_signal_cleanup_handler(file, fuse_abort);
1216 WLog_Print(file->log, WLOG_DEBUG,
"Waiting for FUSE stop sync");
1217 if (WaitForSingleObject(file->fuse_stop_sync, INFINITE) == WAIT_FAILED)
1218 WLog_Print(file->log, WLOG_ERROR,
"Failed to wait for stop sync");
1219 fuse_session_destroy(file->fuse_sess);
1221 fuse_opt_free_args(&args);
1223 DEBUG_CLIPRDR(file->log,
"Quitting fuse with mountpoint '%s'", file->path);
1229static UINT cliprdr_file_context_server_file_contents_response(
1230 CliprdrClientContext* cliprdr_context,
1233 CliprdrFileContext* file_context =
nullptr;
1234 CliprdrFuseRequest* fuse_request =
nullptr;
1235 struct fuse_entry_param entry = WINPR_C_ARRAY_INIT;
1237 WINPR_ASSERT(cliprdr_context);
1238 WINPR_ASSERT(file_contents_response);
1240 file_context = cliprdr_context->custom;
1241 WINPR_ASSERT(file_context);
1243 HashTable_Lock(file_context->inode_table);
1244 fuse_request = HashTable_GetItemValue(file_context->request_table,
1245 (
void*)(uintptr_t)file_contents_response->streamId);
1248 HashTable_Unlock(file_context->inode_table);
1249 return CHANNEL_RC_OK;
1252 if (!(file_contents_response->common.msgFlags & CB_RESPONSE_OK))
1254 WLog_Print(file_context->log, WLOG_WARN,
1255 "FileContentsRequests for file \"%s\" was unsuccessful",
1256 fuse_request->fuse_file->filename);
1258 fuse_reply_err(fuse_request->fuse_req, EIO);
1259 HashTable_Remove(file_context->request_table,
1260 (
void*)(uintptr_t)file_contents_response->streamId);
1261 HashTable_Unlock(file_context->inode_table);
1262 return CHANNEL_RC_OK;
1265 if ((fuse_request->operation_type == FUSE_LL_OPERATION_LOOKUP ||
1266 fuse_request->operation_type == FUSE_LL_OPERATION_GETATTR) &&
1267 file_contents_response->cbRequested !=
sizeof(UINT64))
1269 WLog_Print(file_context->log, WLOG_WARN,
1270 "Received invalid file size for file \"%s\" from the client",
1271 fuse_request->fuse_file->filename);
1272 fuse_reply_err(fuse_request->fuse_req, EIO);
1273 HashTable_Remove(file_context->request_table,
1274 (
void*)(uintptr_t)file_contents_response->streamId);
1275 HashTable_Unlock(file_context->inode_table);
1276 return CHANNEL_RC_OK;
1279 if (fuse_request->operation_type == FUSE_LL_OPERATION_LOOKUP ||
1280 fuse_request->operation_type == FUSE_LL_OPERATION_GETATTR)
1282 DEBUG_CLIPRDR(file_context->log,
"Received file size for file \"%s\" with stream id %u",
1283 fuse_request->fuse_file->filename, file_contents_response->streamId);
1285 fuse_request->fuse_file->size = *((
const UINT64*)file_contents_response->requestedData);
1286 fuse_request->fuse_file->has_size = TRUE;
1288 entry.ino = fuse_request->fuse_file->ino;
1289 write_file_attributes(fuse_request->fuse_file, &entry.attr);
1290 entry.attr_timeout = 1.0;
1291 entry.entry_timeout = 1.0;
1293 else if (fuse_request->operation_type == FUSE_LL_OPERATION_READ)
1295 DEBUG_CLIPRDR(file_context->log,
"Received file range for file \"%s\" with stream id %u",
1296 fuse_request->fuse_file->filename, file_contents_response->streamId);
1298 HashTable_Unlock(file_context->inode_table);
1300 switch (fuse_request->operation_type)
1302 case FUSE_LL_OPERATION_NONE:
1304 case FUSE_LL_OPERATION_LOOKUP:
1305 fuse_reply_entry(fuse_request->fuse_req, &entry);
1307 case FUSE_LL_OPERATION_GETATTR:
1308 fuse_reply_attr(fuse_request->fuse_req, &entry.attr, entry.attr_timeout);
1310 case FUSE_LL_OPERATION_READ:
1311 fuse_reply_buf(fuse_request->fuse_req,
1312 (
const char*)file_contents_response->requestedData,
1313 file_contents_response->cbRequested);
1319 HashTable_Remove(file_context->request_table,
1320 (
void*)(uintptr_t)file_contents_response->streamId);
1322 return CHANNEL_RC_OK;
1326static UINT cliprdr_file_context_send_file_contents_failure(
1332 WINPR_ASSERT(fileContentsRequest);
1334 const UINT64 offset = (((UINT64)fileContentsRequest->nPositionHigh) << 32) |
1335 ((UINT64)fileContentsRequest->nPositionLow);
1336 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1337 "server file contents request [lockID %" PRIu32
", streamID %" PRIu32
1338 ", index %" PRIu32
"] offset %" PRIu64
", size %" PRIu32
" failed",
1339 fileContentsRequest->clipDataId, fileContentsRequest->streamId,
1340 fileContentsRequest->listIndex, offset, fileContentsRequest->cbRequested);
1342 response.common.msgFlags = CB_RESPONSE_FAIL;
1343 response.streamId = fileContentsRequest->streamId;
1345 WINPR_ASSERT(file->context);
1346 WINPR_ASSERT(file->context->ClientFileContentsResponse);
1347 return file->context->ClientFileContentsResponse(file->context, &response);
1351cliprdr_file_context_send_contents_response(CliprdrFileContext* file,
1353 const void* data,
size_t size)
1355 if (size > UINT32_MAX)
1356 return ERROR_INVALID_PARAMETER;
1359 .requestedData = data,
1360 .cbRequested = (UINT32)size,
1361 .common.msgFlags = CB_RESPONSE_OK };
1363 WINPR_ASSERT(request);
1366 WLog_Print(file->log, WLOG_DEBUG,
"send contents response streamID=%" PRIu32
", size=%" PRIu32,
1367 response.streamId, response.cbRequested);
1368 WINPR_ASSERT(file->context);
1369 WINPR_ASSERT(file->context->ClientFileContentsResponse);
1370 return file->context->ClientFileContentsResponse(file->context, &response);
1373static BOOL dump_streams(
const void* key,
void* value, WINPR_ATTR_UNUSED
void* arg)
1375 const UINT32* ukey = key;
1376 CliprdrLocalStream* cur = value;
1378 writelog(cur->context->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1379 "[key %" PRIu32
"] lockID %" PRIu32
", count %" PRIuz
", locked %d", *ukey,
1380 cur->lockId, cur->count, cur->locked);
1381 for (
size_t x = 0; x < cur->count; x++)
1383 const CliprdrLocalFile* file = &cur->files[x];
1384 writelog(cur->context->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1385 "file [%" PRIuz
"] %s: %" PRId64, x, file->name, file->size);
1390static CliprdrLocalFile* file_info_for_request(CliprdrFileContext* file, UINT32 lockId,
1395 CliprdrLocalStream* cur = HashTable_GetItemValue(file->local_streams, &lockId);
1398 if (listIndex < cur->count)
1400 CliprdrLocalFile* f = &cur->files[listIndex];
1405 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1406 "invalid entry index for lockID %" PRIu32
", index %" PRIu32
" [count %" PRIuz
1408 lockId, listIndex, cur->count, cur->locked);
1413 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1414 "missing entry for lockID %" PRIu32
", index %" PRIu32, lockId, listIndex);
1415 if (!HashTable_Foreach(file->local_streams, dump_streams, file))
1416 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1417 "HashTable_Foreach failed");
1423static CliprdrLocalFile* file_for_request(CliprdrFileContext* file, UINT32 lockId, UINT32 listIndex)
1425 CliprdrLocalFile* f = file_info_for_request(file, lockId, listIndex);
1430 const char* name = f->name;
1431 f->fp = winpr_fopen(name,
"rb");
1435 char ebuffer[256] = WINPR_C_ARRAY_INIT;
1436 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1437 "[lockID %" PRIu32
", index %" PRIu32
1438 "] failed to open file '%s' [size %" PRId64
"] %s [%d]",
1439 lockId, listIndex, f->name, f->size,
1440 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)), errno);
1448static void cliprdr_local_file_try_close(CliprdrLocalFile* file, UINT res, UINT64 offset,
1455 WINPR_ASSERT(file->context);
1456 WLog_Print(file->context->log, WLOG_DEBUG,
"closing file %s after error %" PRIu32,
1459 else if (((file->size > 0) && (offset + size >= (UINT64)file->size)))
1461 WINPR_ASSERT(file->context);
1462 WLog_Print(file->context->log, WLOG_DEBUG,
"closing file %s after read", file->name);
1470 (void)fclose(file->fp);
1474static UINT cliprdr_file_context_server_file_size_request(
1477 WINPR_ASSERT(fileContentsRequest);
1479 if (fileContentsRequest->cbRequested !=
sizeof(UINT64))
1481 WLog_Print(file->log, WLOG_WARN,
"unexpected FILECONTENTS_SIZE request: %" PRIu32
" bytes",
1482 fileContentsRequest->cbRequested);
1485 HashTable_Lock(file->local_streams);
1487 UINT res = CHANNEL_RC_OK;
1488 CliprdrLocalFile* rfile =
1489 file_for_request(file, fileContentsRequest->clipDataId, fileContentsRequest->listIndex);
1491 res = cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1494 if (_fseeki64(rfile->fp, 0, SEEK_END) < 0)
1495 res = cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1498 const INT64 size = _ftelli64(rfile->fp);
1500 cliprdr_local_file_try_close(rfile, res, 0, 0);
1502 res = cliprdr_file_context_send_contents_response(file, fileContentsRequest, &size,
1507 HashTable_Unlock(file->local_streams);
1511static UINT cliprdr_file_context_server_file_range_request(
1514 BYTE* data =
nullptr;
1516 WINPR_ASSERT(fileContentsRequest);
1518 HashTable_Lock(file->local_streams);
1519 const UINT64 offset = (((UINT64)fileContentsRequest->nPositionHigh) << 32) |
1520 ((UINT64)fileContentsRequest->nPositionLow);
1522 CliprdrLocalFile* rfile =
1523 file_for_request(file, fileContentsRequest->clipDataId, fileContentsRequest->listIndex);
1527 if (_fseeki64(rfile->fp, WINPR_ASSERTING_INT_CAST(int64_t, offset), SEEK_SET) < 0)
1530 data = malloc(fileContentsRequest->cbRequested);
1535 const size_t r = fread(data, 1, fileContentsRequest->cbRequested, rfile->fp);
1537 cliprdr_file_context_send_contents_response(file, fileContentsRequest, data, r);
1540 cliprdr_local_file_try_close(rfile, rc, offset, fileContentsRequest->cbRequested);
1541 HashTable_Unlock(file->local_streams);
1546 cliprdr_local_file_try_close(rfile, ERROR_INTERNAL_ERROR, offset,
1547 fileContentsRequest->cbRequested);
1549 HashTable_Unlock(file->local_streams);
1550 return cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1553static void cliprdr_local_stream_free(
void* obj);
1555static UINT change_lock(CliprdrFileContext* file, UINT32 lockId, BOOL lock)
1557 UINT rc = CHANNEL_RC_OK;
1561 HashTable_Lock(file->local_streams);
1564 CliprdrLocalStream* stream = HashTable_GetItemValue(file->local_streams, &lockId);
1565 if (lock && !stream)
1567 stream = cliprdr_local_stream_new(file, lockId,
nullptr, 0);
1568 if (!HashTable_Insert(file->local_streams, &lockId, stream))
1570 rc = ERROR_INTERNAL_ERROR;
1571 cliprdr_local_stream_free(stream);
1574 file->local_lock_id = lockId;
1578 stream->locked = lock;
1579 stream->lockId = lockId;
1586 if (!HashTable_Foreach(file->local_streams, local_stream_discard, file))
1587 rc = ERROR_INTERNAL_ERROR;
1590 HashTable_Unlock(file->local_streams);
1594static UINT cliprdr_file_context_lock(CliprdrClientContext* context,
1597 WINPR_ASSERT(context);
1598 WINPR_ASSERT(lockClipboardData);
1599 CliprdrFileContext* file = (context->custom);
1600 return change_lock(file, lockClipboardData->clipDataId, TRUE);
1603static UINT cliprdr_file_context_unlock(CliprdrClientContext* context,
1606 WINPR_ASSERT(context);
1607 WINPR_ASSERT(unlockClipboardData);
1608 CliprdrFileContext* file = (context->custom);
1609 return change_lock(file, unlockClipboardData->clipDataId, FALSE);
1612static UINT cliprdr_file_context_server_file_contents_request(
1615 UINT error = NO_ERROR;
1617 WINPR_ASSERT(context);
1618 WINPR_ASSERT(fileContentsRequest);
1620 CliprdrFileContext* file = (context->custom);
1627 if ((fileContentsRequest->dwFlags & (FILECONTENTS_SIZE | FILECONTENTS_RANGE)) ==
1628 (FILECONTENTS_SIZE | FILECONTENTS_RANGE))
1630 WLog_Print(file->log, WLOG_ERROR,
"invalid CLIPRDR_FILECONTENTS_REQUEST.dwFlags");
1631 return cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1634 if (fileContentsRequest->dwFlags & FILECONTENTS_SIZE)
1635 error = cliprdr_file_context_server_file_size_request(file, fileContentsRequest);
1637 if (fileContentsRequest->dwFlags & FILECONTENTS_RANGE)
1638 error = cliprdr_file_context_server_file_range_request(file, fileContentsRequest);
1642 WLog_Print(file->log, WLOG_ERROR,
"failed to handle CLIPRDR_FILECONTENTS_REQUEST: 0x%08X",
1644 return cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1647 return CHANNEL_RC_OK;
1650BOOL cliprdr_file_context_init(CliprdrFileContext* file, CliprdrClientContext* cliprdr)
1653 WINPR_ASSERT(cliprdr);
1655 cliprdr->custom = file;
1656 file->context = cliprdr;
1658 cliprdr->ServerLockClipboardData = cliprdr_file_context_lock;
1659 cliprdr->ServerUnlockClipboardData = cliprdr_file_context_unlock;
1660 cliprdr->ServerFileContentsRequest = cliprdr_file_context_server_file_contents_request;
1661#if defined(WITH_FUSE)
1662 cliprdr->ServerFileContentsResponse = cliprdr_file_context_server_file_contents_response;
1664 CliprdrFuseFile* root_dir = fuse_file_new_root(file);
1665 return root_dir !=
nullptr;
1671#if defined(WITH_FUSE)
1673static bool clear_all_selections(CliprdrFileContext* file_context)
1675 WINPR_ASSERT(file_context);
1676 WINPR_ASSERT(file_context->inode_table);
1678 HashTable_Lock(file_context->inode_table);
1679 const bool rc = clear_selection(file_context, TRUE,
nullptr);
1681 HashTable_Clear(file_context->clip_data_table);
1682 HashTable_Unlock(file_context->inode_table);
1687BOOL cliprdr_file_context_uninit(CliprdrFileContext* file, CliprdrClientContext* cliprdr)
1694#if defined(WITH_FUSE)
1695 if (file->inode_table)
1697 if (!clear_no_cdi_entry(file))
1699 if (!clear_all_selections(file))
1704 HashTable_Clear(file->local_streams);
1706 file->context =
nullptr;
1711#if defined(WITH_FUSE)
1712 cliprdr->ServerFileContentsResponse =
nullptr;
1719static BOOL cliprdr_file_content_changed_and_update(
void* ihash,
size_t hsize,
const void* data,
1723 BYTE hash[WINPR_SHA256_DIGEST_LENGTH] = WINPR_C_ARRAY_INIT;
1725 if (hsize <
sizeof(hash))
1728 if (!winpr_Digest(WINPR_MD_SHA256, data, size, hash,
sizeof(hash)))
1731 const BOOL changed = memcmp(hash, ihash,
sizeof(hash)) != 0;
1733 memcpy(ihash, hash,
sizeof(hash));
1737static BOOL cliprdr_file_client_content_changed_and_update(CliprdrFileContext* file,
1738 const void* data,
size_t size)
1741 return cliprdr_file_content_changed_and_update(file->client_data_hash,
1742 sizeof(file->client_data_hash), data, size);
1745#if defined(WITH_FUSE)
1746static fuse_ino_t get_next_free_inode(CliprdrFileContext* file_context)
1750 WINPR_ASSERT(file_context);
1752 ino = file_context->next_ino;
1753 while (ino == 0 || ino == FUSE_ROOT_ID ||
1754 HashTable_GetItemValue(file_context->inode_table, (
void*)(uintptr_t)ino))
1757 file_context->next_ino = ino + 1;
1762static CliprdrFuseFile* clip_data_dir_new(CliprdrFileContext* file_context, BOOL has_clip_data_id,
1763 UINT32 clip_data_id)
1765 WINPR_ASSERT(file_context);
1767 UINT64 data_id = clip_data_id;
1768 if (!has_clip_data_id)
1769 data_id = NO_CLIP_DATA_ID;
1771 CliprdrFuseFile* clip_data_dir = fuse_file_new(
"/%" PRIu64, data_id);
1775 clip_data_dir->ino = get_next_free_inode(file_context);
1776 clip_data_dir->is_directory = TRUE;
1777 clip_data_dir->is_readonly = TRUE;
1778 clip_data_dir->has_clip_data_id = has_clip_data_id;
1779 clip_data_dir->clip_data_id = clip_data_id;
1781 CliprdrFuseFile* root_dir = get_fuse_file_by_ino(file_context, FUSE_ROOT_ID);
1784 WLog_Print(file_context->log, WLOG_ERROR,
"FUSE root directory missing");
1785 fuse_file_free(clip_data_dir);
1788 if (!ArrayList_Append(root_dir->children, clip_data_dir))
1790 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to append FUSE file");
1791 fuse_file_free(clip_data_dir);
1794 clip_data_dir->parent = root_dir;
1796 if (!HashTable_Insert(file_context->inode_table, (
void*)(uintptr_t)clip_data_dir->ino,
1799 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to insert inode into inode table");
1800 ArrayList_Remove(root_dir->children, clip_data_dir);
1801 fuse_file_free(clip_data_dir);
1805 if (!HashTable_Insert(file_context->dir_table, clip_data_dir->filename_with_root,
1808 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to insert inode into dir table");
1809 ArrayList_Remove(root_dir->children, clip_data_dir);
1810 fuse_file_free(clip_data_dir);
1814 return clip_data_dir;
1817static char* get_parent_path(
const char* filepath)
1819 const char* base = strrchr(filepath,
'/');
1822 while ((base > filepath) && (*base ==
'/'))
1825 WINPR_ASSERT(base >= filepath);
1826 const size_t parent_path_length = 1ULL + (size_t)(base - filepath);
1827 char* parent_path = calloc(parent_path_length + 1,
sizeof(
char));
1831 memcpy(parent_path, filepath, parent_path_length);
1836static CliprdrFuseFile* get_parent_directory(CliprdrFileContext* file_context,
const char* path)
1839 WINPR_ASSERT(file_context);
1842 char* parent_path = get_parent_path(path);
1846 CliprdrFuseFile* parent = HashTable_GetItemValue(file_context->dir_table, parent_path);
1852static BOOL selection_handle_file(CliprdrFileContext* file_context,
1853 CliprdrFuseClipDataEntry* clip_data_entry, uint32_t index,
1857 WINPR_ASSERT(file_context);
1858 WINPR_ASSERT(clip_data_entry);
1861 CliprdrFuseFile* clip_data_dir = clip_data_entry->clip_data_dir;
1862 WINPR_ASSERT(clip_data_dir);
1864 char filename[ARRAYSIZE(file->cFileName) * 8] = WINPR_C_ARRAY_INIT;
1865 const SSIZE_T filenamelen = ConvertWCharNToUtf8(file->cFileName, ARRAYSIZE(file->cFileName),
1866 filename, ARRAYSIZE(filename) - 1);
1867 if (filenamelen < 0)
1869 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to convert filename");
1873 for (
size_t j = 0; filename[j]; ++j)
1875 if (filename[j] ==
'\\')
1880 CliprdrFuseFile* fuse_file = fuse_file_new(
1881 "%.*s/%s", WINPR_ASSERTING_INT_CAST(
int, clip_data_dir->filename_with_root_len),
1882 clip_data_dir->filename_with_root, filename);
1885 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to create FUSE file");
1889 fuse_file->parent = get_parent_directory(file_context, fuse_file->filename_with_root);
1890 if (!fuse_file->parent)
1892 WLog_Print(file_context->log, WLOG_ERROR,
"Found no parent for FUSE file");
1896 if (!ArrayList_Append(fuse_file->parent->children, fuse_file))
1898 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to append FUSE file");
1902 fuse_file->list_idx = index;
1903 fuse_file->ino = get_next_free_inode(file_context);
1904 fuse_file->has_clip_data_id = clip_data_entry->has_clip_data_id;
1905 fuse_file->clip_data_id = clip_data_entry->clip_data_id;
1906 if (file->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1907 fuse_file->is_directory = TRUE;
1908 if (file->dwFileAttributes & FILE_ATTRIBUTE_READONLY)
1909 fuse_file->is_readonly = TRUE;
1910 if (file->dwFlags & FD_FILESIZE)
1912 fuse_file->size = ((UINT64)file->nFileSizeHigh << 32) + file->nFileSizeLow;
1913 fuse_file->has_size = TRUE;
1915 if (file->dwFlags & FD_WRITESTIME)
1919 filetime = file->ftLastWriteTime.dwHighDateTime;
1921 filetime += file->ftLastWriteTime.dwLowDateTime;
1923 fuse_file->last_write_time_unix =
1924 1LL * filetime / (10LL * 1000LL * 1000LL) - WIN32_FILETIME_TO_UNIX_EPOCH;
1925 fuse_file->has_last_write_time = TRUE;
1928 if (!HashTable_Insert(file_context->inode_table, (
void*)(uintptr_t)fuse_file->ino, fuse_file))
1930 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to insert inode into inode table");
1934 if (fuse_file->is_directory)
1937 if (!HashTable_Insert(file_context->dir_table, fuse_file->filename_with_root, fuse_file))
1939 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to insert inode into dir table");
1949 fuse_file_free(fuse_file);
1950 return clear_entry_selection(clip_data_entry);
1956static BOOL set_selection_for_clip_data_entry(CliprdrFileContext* file_context,
1957 CliprdrFuseClipDataEntry* clip_data_entry,
1960 WINPR_ASSERT(file_context);
1961 WINPR_ASSERT(clip_data_entry);
1962 WINPR_ASSERT(files);
1964 if (clip_data_entry->has_clip_data_id)
1965 WLog_Print(file_context->log, WLOG_DEBUG,
"Setting selection for clipDataId %u",
1966 clip_data_entry->clip_data_id);
1968 WLog_Print(file_context->log, WLOG_DEBUG,
"Setting selection");
1970 for (UINT32 i = 0; i < n_files; ++i)
1973 if (!selection_handle_file(file_context, clip_data_entry, i, file))
1977 if (clip_data_entry->has_clip_data_id)
1978 WLog_Print(file_context->log, WLOG_DEBUG,
"Selection set for clipDataId %u",
1979 clip_data_entry->clip_data_id);
1981 WLog_Print(file_context->log, WLOG_DEBUG,
"Selection set");
1986static BOOL update_exposed_path(CliprdrFileContext* file_context, wClipboard* clip,
1987 CliprdrFuseClipDataEntry* clip_data_entry)
1989 wClipboardDelegate* delegate =
nullptr;
1990 CliprdrFuseFile* clip_data_dir =
nullptr;
1992 WINPR_ASSERT(file_context);
1994 WINPR_ASSERT(clip_data_entry);
1996 delegate = ClipboardGetDelegate(clip);
1997 WINPR_ASSERT(delegate);
1999 clip_data_dir = clip_data_entry->clip_data_dir;
2000 WINPR_ASSERT(clip_data_dir);
2002 free(file_context->exposed_path);
2003 file_context->exposed_path = GetCombinedPath(file_context->path, clip_data_dir->filename);
2004 if (file_context->exposed_path)
2005 WLog_Print(file_context->log, WLOG_DEBUG,
"Updated exposed path to \"%s\"",
2006 file_context->exposed_path);
2008 delegate->basePath = file_context->exposed_path;
2010 return delegate->basePath !=
nullptr;
2014BOOL cliprdr_file_context_update_server_data(CliprdrFileContext* file_context, wClipboard* clip,
2015 const void* data,
size_t size)
2017#if defined(WITH_FUSE)
2018 CliprdrFuseClipDataEntry* clip_data_entry =
nullptr;
2023 WINPR_ASSERT(file_context);
2025 if (size > UINT32_MAX)
2028 if (cliprdr_parse_file_list(data, (UINT32)size, &files, &n_files))
2030 WLog_Print(file_context->log, WLOG_ERROR,
"Failed to parse file list");
2034 HashTable_Lock(file_context->inode_table);
2035 HashTable_Lock(file_context->clip_data_table);
2036 if (does_server_support_clipdata_locking(file_context))
2037 clip_data_entry = HashTable_GetItemValue(
2038 file_context->clip_data_table, (
void*)(uintptr_t)file_context->current_clip_data_id);
2040 clip_data_entry = file_context->clip_data_entry_without_id;
2042 WINPR_ASSERT(clip_data_entry);
2044 if (!clear_entry_selection(clip_data_entry))
2046 WINPR_ASSERT(!clip_data_entry->clip_data_dir);
2048 clip_data_entry->clip_data_dir =
2049 clip_data_dir_new(file_context, does_server_support_clipdata_locking(file_context),
2050 file_context->current_clip_data_id);
2051 if (!clip_data_entry->clip_data_dir)
2053 if (!update_exposed_path(file_context, clip, clip_data_entry))
2055 if (!set_selection_for_clip_data_entry(file_context, clip_data_entry, files, n_files))
2061 HashTable_Unlock(file_context->clip_data_table);
2062 HashTable_Unlock(file_context->inode_table);
2070void* cliprdr_file_context_get_context(CliprdrFileContext* file)
2073 return file->clipboard;
2076void cliprdr_file_session_terminate(CliprdrFileContext* file, BOOL stop_thread)
2081#if defined(WITH_FUSE)
2082 WINPR_ASSERT(file->fuse_stop_sync);
2084 WLog_Print(file->log, WLOG_DEBUG,
"Setting FUSE exit flag");
2085 if (file->fuse_sess)
2086 fuse_session_exit(file->fuse_sess);
2090 WLog_Print(file->log, WLOG_DEBUG,
"Setting FUSE stop event");
2091 (void)SetEvent(file->fuse_stop_sync);
2097#if defined(WITH_FUSE)
2098 WLog_Print(file->log, WLOG_DEBUG,
"Forcing FUSE to check exit flag");
2100 if (winpr_PathFileExists(file->path))
2101 WLog_Print(file->log, WLOG_WARN,
"winpr_PathFileExists(%s) failed", file->path);
2104void cliprdr_file_context_free(CliprdrFileContext* file)
2109#if defined(WITH_FUSE)
2110 if (file->fuse_thread)
2112 WINPR_ASSERT(file->fuse_stop_sync);
2114 WLog_Print(file->log, WLOG_DEBUG,
"Stopping FUSE thread");
2115 cliprdr_file_session_terminate(file, TRUE);
2117 WLog_Print(file->log, WLOG_DEBUG,
"Waiting on FUSE thread");
2118 (void)WaitForSingleObject(file->fuse_thread, INFINITE);
2119 (void)CloseHandle(file->fuse_thread);
2121 if (file->fuse_stop_sync)
2122 (void)CloseHandle(file->fuse_stop_sync);
2123 if (file->fuse_start_sync)
2124 (void)CloseHandle(file->fuse_start_sync);
2126 HashTable_Free(file->request_table);
2127 HashTable_Free(file->clip_data_table);
2128 HashTable_Free(file->dir_table);
2129 HashTable_Free(file->inode_table);
2132 HashTable_Free(file->local_streams);
2133 winpr_RemoveDirectory(file->path);
2135 free(file->exposed_path);
2139static BOOL create_base_path(CliprdrFileContext* file)
2143 char base[64] = WINPR_C_ARRAY_INIT;
2144 (void)_snprintf(base,
sizeof(base),
"com.freerdp.client.cliprdr.%" PRIu32,
2145 GetCurrentProcessId());
2147 file->path = GetKnownSubPath(KNOWN_PATH_TEMP, base);
2151 if (!winpr_PathFileExists(file->path) && !winpr_PathMakePath(file->path,
nullptr))
2153 WLog_Print(file->log, WLOG_ERROR,
"Failed to create directory '%s'", file->path);
2159static void cliprdr_local_file_free(CliprdrLocalFile* file)
2161 const CliprdrLocalFile empty = WINPR_C_ARRAY_INIT;
2166 WLog_Print(file->context->log, WLOG_DEBUG,
"closing file %s, discarding entry", file->name);
2167 (void)fclose(file->fp);
2173static BOOL cliprdr_local_file_new(CliprdrFileContext* context, CliprdrLocalFile* f,
2176 const CliprdrLocalFile empty = WINPR_C_ARRAY_INIT;
2178 WINPR_ASSERT(context);
2182 f->context = context;
2183 f->name = winpr_str_url_decode(path, strlen(path));
2189 cliprdr_local_file_free(f);
2193static void cliprdr_local_files_free(CliprdrLocalStream* stream)
2195 WINPR_ASSERT(stream);
2197 for (
size_t x = 0; x < stream->count; x++)
2198 cliprdr_local_file_free(&stream->files[x]);
2199 free(stream->files);
2201 stream->files =
nullptr;
2205static void cliprdr_local_stream_free(
void* obj)
2207 CliprdrLocalStream* stream = (CliprdrLocalStream*)obj;
2209 cliprdr_local_files_free(stream);
2214static BOOL append_entry(CliprdrLocalStream* stream,
const char* path)
2216 CliprdrLocalFile* tmp = realloc(stream->files,
sizeof(CliprdrLocalFile) * (stream->count + 1));
2219 stream->files = tmp;
2220 CliprdrLocalFile* f = &stream->files[stream->count++];
2222 return cliprdr_local_file_new(stream->context, f, path);
2225static BOOL is_directory(
const char* path)
2227 WCHAR* wpath = ConvertUtf8ToWCharAlloc(path,
nullptr);
2231 HANDLE hFile = CreateFileW(wpath, 0, FILE_SHARE_DELETE,
nullptr, OPEN_EXISTING,
2232 FILE_ATTRIBUTE_NORMAL,
nullptr);
2235 if (hFile == INVALID_HANDLE_VALUE)
2239 const BOOL status = GetFileInformationByHandle(hFile, &fileInformation);
2240 (void)CloseHandle(hFile);
2244 return (fileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
2247static BOOL add_directory(CliprdrLocalStream* stream,
const char* path)
2249 char* wildcardpath = GetCombinedPath(path,
"*");
2252 WCHAR* wpath = ConvertUtf8ToWCharAlloc(wildcardpath,
nullptr);
2258 HANDLE hFind = FindFirstFileW(wpath, &FindFileData);
2261 if (hFind == INVALID_HANDLE_VALUE)
2265 char* next =
nullptr;
2267 WCHAR dotbuffer[6] = WINPR_C_ARRAY_INIT;
2268 WCHAR dotdotbuffer[6] = WINPR_C_ARRAY_INIT;
2269 const WCHAR* dot = InitializeConstWCharFromUtf8(
".", dotbuffer, ARRAYSIZE(dotbuffer));
2270 const WCHAR* dotdot = InitializeConstWCharFromUtf8(
"..", dotdotbuffer, ARRAYSIZE(dotdotbuffer));
2273 if (_wcscmp(FindFileData.cFileName, dot) == 0)
2275 if (_wcscmp(FindFileData.cFileName, dotdot) == 0)
2278 char cFileName[MAX_PATH] = WINPR_C_ARRAY_INIT;
2279 (void)ConvertWCharNToUtf8(FindFileData.cFileName, ARRAYSIZE(FindFileData.cFileName),
2280 cFileName, ARRAYSIZE(cFileName));
2283 next = GetCombinedPath(path, cFileName);
2287 if (!append_entry(stream, next))
2289 if (is_directory(next))
2291 if (!add_directory(stream, next))
2294 }
while (FindNextFileW(hFind, &FindFileData));
2304static BOOL cliprdr_local_stream_update(CliprdrLocalStream* stream,
const char* data,
size_t size)
2307 WINPR_ASSERT(stream);
2311 cliprdr_local_files_free(stream);
2313 stream->files = calloc(size,
sizeof(CliprdrLocalFile));
2317 char* copy = strndup(data, size);
2321 char* saveptr =
nullptr;
2322 char* ptr = strtok_s(copy,
"\r\n", &saveptr);
2325 const char* name = ptr;
2326 if (strncmp(
"file:///", ptr, 8) == 0)
2328 else if (strncmp(
"file:/", ptr, 6) == 0)
2331 if (!append_entry(stream, name))
2334 if (is_directory(name))
2336 const BOOL res = add_directory(stream, name);
2340 ptr = strtok_s(
nullptr,
"\r\n", &saveptr);
2349CliprdrLocalStream* cliprdr_local_stream_new(CliprdrFileContext* context, UINT32 streamID,
2350 const char* data,
size_t size)
2352 WINPR_ASSERT(context);
2353 CliprdrLocalStream* stream = calloc(1,
sizeof(CliprdrLocalStream));
2357 stream->context = context;
2358 if (!cliprdr_local_stream_update(stream, data, size))
2361 stream->lockId = streamID;
2365 cliprdr_local_stream_free(stream);
2369static UINT32 UINTPointerHash(
const void*
id)
2372 return *((
const UINT32*)
id);
2375static BOOL UINTPointerCompare(
const void* pointer1,
const void* pointer2)
2377 if (!pointer1 || !pointer2)
2378 return pointer1 == pointer2;
2380 const UINT32* a = pointer1;
2381 const UINT32* b = pointer2;
2385static void* UINTPointerClone(
const void* other)
2387 const UINT32* src = other;
2391 UINT32* copy = calloc(1,
sizeof(UINT32));
2399#if defined(WITH_FUSE)
2400CliprdrFuseFile* fuse_file_new_root(CliprdrFileContext* file_context)
2402 CliprdrFuseFile* root_dir = fuse_file_new(
"/");
2406 root_dir->ino = FUSE_ROOT_ID;
2407 root_dir->is_directory = TRUE;
2408 root_dir->is_readonly = TRUE;
2410 if (!HashTable_Insert(file_context->inode_table, (
void*)(uintptr_t)root_dir->ino, root_dir))
2412 fuse_file_free(root_dir);
2416 if (!HashTable_Insert(file_context->dir_table, root_dir->filename_with_root, root_dir))
2418 fuse_file_free(root_dir);
2426CliprdrFileContext* cliprdr_file_context_new(
void* context)
2428 CliprdrFileContext* file = calloc(1,
sizeof(CliprdrFileContext));
2432 file->log = WLog_Get(CLIENT_TAG(
"common.cliprdr.file"));
2433 file->clipboard = context;
2435 file->local_streams = HashTable_New(FALSE);
2436 if (!file->local_streams)
2439 if (!HashTable_SetHashFunction(file->local_streams, UINTPointerHash))
2443 wObject* hkobj = HashTable_KeyObject(file->local_streams);
2444 WINPR_ASSERT(hkobj);
2451 wObject* hobj = HashTable_ValueObject(file->local_streams);
2456#if defined(WITH_FUSE)
2457 file->inode_table = HashTable_New(FALSE);
2458 file->dir_table = HashTable_New(FALSE);
2459 file->clip_data_table = HashTable_New(FALSE);
2460 file->request_table = HashTable_New(FALSE);
2461 if (!file->inode_table || !file->dir_table || !file->clip_data_table || !file->request_table)
2465 wObject* ctobj = HashTable_ValueObject(file->request_table);
2466 WINPR_ASSERT(ctobj);
2470 wObject* ctobj = HashTable_ValueObject(file->clip_data_table);
2471 WINPR_ASSERT(ctobj);
2475 if (!HashTable_SetHashFunction(file->dir_table, HashTable_StringHash))
2477 wObject* kobj = HashTable_KeyObject(file->dir_table);
2483 if (!create_base_path(file))
2486#if defined(WITH_FUSE)
2487 if (!(file->fuse_start_sync = CreateEvent(
nullptr, TRUE, FALSE,
nullptr)))
2489 if (!(file->fuse_stop_sync = CreateEvent(
nullptr, TRUE, FALSE,
nullptr)))
2491 if (!(file->fuse_thread = CreateThread(
nullptr, 0, cliprdr_file_fuse_thread, file, 0,
nullptr)))
2494 if (WaitForSingleObject(file->fuse_start_sync, INFINITE) == WAIT_FAILED)
2495 WLog_Print(file->log, WLOG_ERROR,
"Failed to wait for start sync");
2500 WINPR_PRAGMA_DIAG_PUSH
2501 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2502 cliprdr_file_context_free(file);
2503 WINPR_PRAGMA_DIAG_POP
2507BOOL local_stream_discard(
const void* key,
void* value,
void* arg)
2509 CliprdrFileContext* file = arg;
2510 CliprdrLocalStream* stream = value;
2512 WINPR_ASSERT(stream);
2514 if (!stream->locked)
2515 HashTable_Remove(file->local_streams, key);
2519BOOL cliprdr_file_context_clear(CliprdrFileContext* file)
2523 WLog_Print(file->log, WLOG_DEBUG,
"clear file clipboard...");
2525 HashTable_Lock(file->local_streams);
2526 const BOOL res = HashTable_Foreach(file->local_streams, local_stream_discard, file);
2527 HashTable_Unlock(file->local_streams);
2529 memset(file->server_data_hash, 0,
sizeof(file->server_data_hash));
2530 memset(file->client_data_hash, 0,
sizeof(file->client_data_hash));
2534BOOL cliprdr_file_context_update_client_data(CliprdrFileContext* file,
const char* data,
2540 if (!cliprdr_file_client_content_changed_and_update(file, data, size))
2543 if (!cliprdr_file_context_clear(file))
2546 UINT32 lockId = file->local_lock_id;
2548 HashTable_Lock(file->local_streams);
2549 CliprdrLocalStream* stream = HashTable_GetItemValue(file->local_streams, &lockId);
2551 WLog_Print(file->log, WLOG_DEBUG,
"update client file list (stream=%p)...", (
void*)stream);
2553 rc = cliprdr_local_stream_update(stream, data, size);
2556 stream = cliprdr_local_stream_new(file, lockId, data, size);
2557 rc = HashTable_Insert(file->local_streams, &stream->lockId, stream);
2559 cliprdr_local_stream_free(stream);
2563 HashTable_Unlock(file->local_streams);
2567UINT32 cliprdr_file_context_current_flags(CliprdrFileContext* file)
2571 if ((file->file_capability_flags & CB_STREAM_FILECLIP_ENABLED) == 0)
2574 if (!file->file_formats_registered)
2577 return CB_STREAM_FILECLIP_ENABLED | CB_FILECLIP_NO_FILE_PATHS |
2578 CB_HUGE_FILE_SUPPORT_ENABLED;
2581BOOL cliprdr_file_context_set_locally_available(CliprdrFileContext* file, BOOL available)
2584 file->file_formats_registered = available;
2588BOOL cliprdr_file_context_remote_set_flags(CliprdrFileContext* file, UINT32 flags)
2591 file->file_capability_flags = flags;
2595UINT32 cliprdr_file_context_remote_get_flags(CliprdrFileContext* file)
2598 return file->file_capability_flags;
2601BOOL cliprdr_file_context_has_local_support(CliprdrFileContext* file)
2605#if defined(WITH_FUSE)
This struct contains function pointer to initialize/free objects.
OBJECT_FREE_FN fnObjectFree
WINPR_ATTR_NODISCARD OBJECT_EQUALS_FN fnObjectEquals
WINPR_ATTR_NODISCARD OBJECT_NEW_FN fnObjectNew