FreeRDP
Loading...
Searching...
No Matches
client_cliprdr_file.c
1
24#include <freerdp/config.h>
25
26#include <stdlib.h>
27#include <errno.h>
28
29#ifdef WITH_FUSE
30#define FUSE_USE_VERSION 30
31#include <fuse_lowlevel.h>
32#endif
33
34#if defined(WITH_FUSE)
35#include <sys/mount.h>
36#include <sys/stat.h>
37#include <errno.h>
38#include <time.h>
39#endif
40
41#include <winpr/crt.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>
48
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>
54
55#include <freerdp/client/client_cliprdr_file.h>
56
57#define NO_CLIP_DATA_ID (UINT64_C(1) << 32)
58#define WIN32_FILETIME_TO_UNIX_EPOCH INT64_C(11644473600)
59
60#ifdef WITH_DEBUG_CLIPRDR
61#define DEBUG_CLIPRDR(log, ...) WLog_Print(log, WLOG_DEBUG, __VA_ARGS__)
62#else
63#define DEBUG_CLIPRDR(log, ...) \
64 do \
65 { \
66 } while (0)
67#endif
68
69#if defined(WITH_FUSE)
70typedef enum eFuseLowlevelOperationType
71{
72 FUSE_LL_OPERATION_NONE,
73 FUSE_LL_OPERATION_LOOKUP,
74 FUSE_LL_OPERATION_GETATTR,
75 FUSE_LL_OPERATION_READ,
76} FuseLowlevelOperationType;
77
78typedef struct sCliprdrFuseFile CliprdrFuseFile;
79
80struct sCliprdrFuseFile
81{
82 CliprdrFuseFile* parent;
83 wArrayList* children;
84
85 size_t filename_len;
86 const char* filename;
87
88 size_t filename_with_root_len;
89 char* filename_with_root;
90 UINT32 list_idx;
91 fuse_ino_t ino;
92
93 BOOL is_directory;
94 BOOL is_readonly;
95
96 BOOL has_size;
97 UINT64 size;
98
99 BOOL has_last_write_time;
100 INT64 last_write_time_unix;
101
102 BOOL has_clip_data_id;
103 UINT32 clip_data_id;
104};
105
106typedef struct
107{
108 CliprdrFileContext* file_context;
109
110 CliprdrFuseFile* clip_data_dir;
111
112 BOOL has_clip_data_id;
113 UINT32 clip_data_id;
114} CliprdrFuseClipDataEntry;
115
116typedef struct
117{
118 CliprdrFileContext* file_context;
119
120 wArrayList* fuse_files;
121
122 BOOL all_files;
123 BOOL has_clip_data_id;
124 UINT32 clip_data_id;
125} FuseFileClearContext;
126
127typedef struct
128{
129 FuseLowlevelOperationType operation_type;
130 CliprdrFuseFile* fuse_file;
131 fuse_req_t fuse_req;
132 UINT32 stream_id;
133} CliprdrFuseRequest;
134#endif
135
136typedef struct
137{
138 char* name;
139 FILE* fp;
140 INT64 size;
141 CliprdrFileContext* context;
142} CliprdrLocalFile;
143
144typedef struct
145{
146 UINT32 lockId;
147 BOOL locked;
148 size_t count;
149 CliprdrLocalFile* files;
150 CliprdrFileContext* context;
151} CliprdrLocalStream;
152
153struct cliprdr_file_context
154{
155#if defined(WITH_FUSE)
156 /* FUSE related**/
157 HANDLE fuse_start_sync;
158 HANDLE fuse_stop_sync;
159 HANDLE fuse_thread;
160 struct fuse_session* fuse_sess;
161#if FUSE_USE_VERSION < 30
162 struct fuse_chan* ch;
163#endif
164
165 wHashTable* inode_table;
166 wHashTable* dir_table;
167 wHashTable* clip_data_table;
168 wHashTable* request_table;
169
170 CliprdrFuseClipDataEntry* clip_data_entry_without_id;
171 UINT32 current_clip_data_id;
172
173 fuse_ino_t next_ino;
174 UINT32 next_clip_data_id;
175 UINT32 next_stream_id;
176#endif
177
178 /* File clipping */
179 BOOL file_formats_registered;
180 UINT32 file_capability_flags;
181
182 UINT32 local_lock_id;
183
184 wHashTable* local_streams;
185 wLog* log;
186 void* clipboard;
187 CliprdrClientContext* context;
188 char* path;
189 char* exposed_path;
190 BYTE server_data_hash[WINPR_SHA256_DIGEST_LENGTH];
191 BYTE client_data_hash[WINPR_SHA256_DIGEST_LENGTH];
192};
193
194#if defined(WITH_FUSE)
195static CliprdrFuseFile* get_fuse_file_by_ino(CliprdrFileContext* file_context, fuse_ino_t fuse_ino);
196
197static void fuse_file_free(void* data)
198{
199 CliprdrFuseFile* fuse_file = data;
200
201 if (!fuse_file)
202 return;
203
204 ArrayList_Free(fuse_file->children);
205 free(fuse_file->filename_with_root);
206
207 free(fuse_file);
208}
209
210WINPR_ATTR_FORMAT_ARG(1, 2)
211WINPR_ATTR_MALLOC(fuse_file_free, 1)
212WINPR_ATTR_NODISCARD
213static CliprdrFuseFile* fuse_file_new(WINPR_FORMAT_ARG const char* fmt, ...)
214{
215 CliprdrFuseFile* file = calloc(1, sizeof(CliprdrFuseFile));
216 if (!file)
217 return nullptr;
218
219 file->children = ArrayList_New(FALSE);
220 if (!file->children)
221 goto fail;
222
223 WINPR_ASSERT(fmt);
224
225 {
226 va_list ap = WINPR_C_ARRAY_INIT;
227 va_start(ap, fmt);
228 const int rc =
229 winpr_vasprintf(&file->filename_with_root, &file->filename_with_root_len, fmt, ap);
230 va_end(ap);
231
232 if (rc < 0)
233 goto fail;
234 }
235
236 if (file->filename_with_root && (file->filename_with_root_len > 0))
237 {
238 file->filename_len = 0;
239 file->filename = strrchr(file->filename_with_root, '/') + 1;
240 if (file->filename)
241 file->filename_len = strnlen(file->filename, file->filename_with_root_len);
242 }
243 return file;
244fail:
245 fuse_file_free(file);
246 return nullptr;
247}
248
249static void clip_data_entry_free(void* data)
250{
251 CliprdrFuseClipDataEntry* clip_data_entry = data;
252
253 if (!clip_data_entry)
254 return;
255
256 if (clip_data_entry->has_clip_data_id)
257 {
258 CliprdrFileContext* file_context = clip_data_entry->file_context;
259 CLIPRDR_UNLOCK_CLIPBOARD_DATA unlock_clipboard_data = WINPR_C_ARRAY_INIT;
260
261 WINPR_ASSERT(file_context);
262
263 unlock_clipboard_data.common.msgType = CB_UNLOCK_CLIPDATA;
264 unlock_clipboard_data.clipDataId = clip_data_entry->clip_data_id;
265
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);
271
272 clip_data_entry->has_clip_data_id = FALSE;
273
274 WLog_Print(file_context->log, WLOG_DEBUG, "Destroyed ClipDataEntry with id %u",
275 clip_data_entry->clip_data_id);
276 }
277
278 free(clip_data_entry);
279}
280
281static BOOL does_server_support_clipdata_locking(CliprdrFileContext* file_context)
282{
283 WINPR_ASSERT(file_context);
284
285 return (cliprdr_file_context_remote_get_flags(file_context) & CB_CAN_LOCK_CLIPDATA) != 0;
286}
287
288static UINT32 get_next_free_clip_data_id(CliprdrFileContext* file_context)
289{
290 UINT32 clip_data_id = 0;
291
292 WINPR_ASSERT(file_context);
293
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))
298 ++clip_data_id;
299
300 file_context->next_clip_data_id = clip_data_id + 1;
301 HashTable_Unlock(file_context->inode_table);
302
303 return clip_data_id;
304}
305
306static CliprdrFuseClipDataEntry* clip_data_entry_new(CliprdrFileContext* file_context,
307 BOOL needs_clip_data_id)
308{
309 CliprdrFuseClipDataEntry* clip_data_entry = nullptr;
310 CLIPRDR_LOCK_CLIPBOARD_DATA lock_clipboard_data = WINPR_C_ARRAY_INIT;
311
312 WINPR_ASSERT(file_context);
313
314 clip_data_entry = calloc(1, sizeof(CliprdrFuseClipDataEntry));
315 if (!clip_data_entry)
316 return nullptr;
317
318 clip_data_entry->file_context = file_context;
319 clip_data_entry->clip_data_id = get_next_free_clip_data_id(file_context);
320
321 if (!needs_clip_data_id)
322 return clip_data_entry;
323
324 lock_clipboard_data.common.msgType = CB_LOCK_CLIPDATA;
325 lock_clipboard_data.clipDataId = clip_data_entry->clip_data_id;
326
327 if (file_context->context->ClientLockClipboardData(file_context->context, &lock_clipboard_data))
328 {
329 HashTable_Lock(file_context->inode_table);
330 clip_data_entry_free(clip_data_entry);
331 HashTable_Unlock(file_context->inode_table);
332 return nullptr;
333 }
334 clip_data_entry->has_clip_data_id = TRUE;
335
336 WLog_Print(file_context->log, WLOG_DEBUG, "Created ClipDataEntry with id %u",
337 clip_data_entry->clip_data_id);
338
339 return clip_data_entry;
340}
341
342static BOOL should_remove_fuse_file(CliprdrFuseFile* fuse_file, BOOL all_files,
343 BOOL has_clip_data_id, UINT32 clip_data_id)
344{
345 if (all_files)
346 return TRUE;
347
348 if (fuse_file->ino == FUSE_ROOT_ID)
349 return FALSE;
350 if (!fuse_file->has_clip_data_id && !has_clip_data_id)
351 return TRUE;
352 if (fuse_file->has_clip_data_id && has_clip_data_id &&
353 (fuse_file->clip_data_id == clip_data_id))
354 return TRUE;
355
356 return FALSE;
357}
358
359static BOOL maybe_clear_fuse_request(const void* key, void* value, void* arg)
360{
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;
365
366 WINPR_ASSERT(file_context);
367
368 if (!should_remove_fuse_file(fuse_file, clear_context->all_files,
369 clear_context->has_clip_data_id, clear_context->clip_data_id))
370 return TRUE;
371
372 DEBUG_CLIPRDR(file_context->log, "Clearing FileContentsRequest for file \"%s\"",
373 fuse_file->filename_with_root);
374
375 fuse_reply_err(fuse_request->fuse_req, EIO);
376 HashTable_Remove(file_context->request_table, key);
377
378 return TRUE;
379}
380
381static BOOL maybe_steal_inode(const void* key, void* value, void* arg)
382{
383 CliprdrFuseFile* fuse_file = value;
384 FuseFileClearContext* clear_context = arg;
385 CliprdrFileContext* file_context = clear_context->file_context;
386
387 WINPR_ASSERT(file_context);
388
389 if (should_remove_fuse_file(fuse_file, clear_context->all_files,
390 clear_context->has_clip_data_id, clear_context->clip_data_id))
391 {
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");
395
396 if (!clear_context->all_files && fuse_file->is_directory)
397 {
398 HashTable_Remove(file_context->dir_table, fuse_file->filename_with_root);
399 }
400 HashTable_Remove(file_context->inode_table, key);
401 }
402
403 return TRUE;
404}
405
406static BOOL notify_delete_child(void* data, WINPR_ATTR_UNUSED size_t index, va_list ap)
407{
408 CliprdrFuseFile* child = data;
409
410 WINPR_ASSERT(child);
411
412 CliprdrFileContext* file_context = va_arg(ap, CliprdrFileContext*);
413 CliprdrFuseFile* parent = va_arg(ap, CliprdrFuseFile*);
414
415 WINPR_ASSERT(file_context);
416 WINPR_ASSERT(parent);
417
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);
421
422 return TRUE;
423}
424
425static BOOL invalidate_inode(void* data, WINPR_ATTR_UNUSED size_t index, va_list ap)
426{
427 CliprdrFuseFile* fuse_file = data;
428
429 WINPR_ASSERT(fuse_file);
430
431 CliprdrFileContext* file_context = va_arg(ap, CliprdrFileContext*);
432 WINPR_ASSERT(file_context);
433 WINPR_ASSERT(file_context->fuse_sess);
434
435 const BOOL res =
436 ArrayList_ForEach(fuse_file->children, notify_delete_child, file_context, fuse_file);
437
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);
442
443 return res;
444}
445
446WINPR_ATTR_NODISCARD
447static bool clear_selection(CliprdrFileContext* file_context, BOOL all_selections,
448 CliprdrFuseClipDataEntry* clip_data_entry)
449{
450 bool res = true;
451 FuseFileClearContext clear_context = WINPR_C_ARRAY_INIT;
452 CliprdrFuseFile* clip_data_dir = nullptr;
453
454 WINPR_ASSERT(file_context);
455
456 clear_context.file_context = file_context;
457 clear_context.fuse_files = ArrayList_New(FALSE);
458 WINPR_ASSERT(clear_context.fuse_files);
459
460 wObject* aobj = ArrayList_Object(clear_context.fuse_files);
461 WINPR_ASSERT(aobj);
462 aobj->fnObjectFree = fuse_file_free;
463
464 if (clip_data_entry)
465 {
466 clip_data_dir = clip_data_entry->clip_data_dir;
467 clip_data_entry->clip_data_dir = nullptr;
468
469 WINPR_ASSERT(clip_data_dir);
470
471 CliprdrFuseFile* root_dir = get_fuse_file_by_ino(file_context, FUSE_ROOT_ID);
472 if (root_dir)
473 ArrayList_Remove(root_dir->children, clip_data_dir);
474
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;
477 }
478 clear_context.all_files = all_selections;
479
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);
483 else
484 WLog_Print(file_context->log, WLOG_DEBUG, "Clearing selection%s",
485 all_selections ? "s" : "");
486
487 if (!HashTable_Foreach(file_context->request_table, maybe_clear_fuse_request, &clear_context))
488 res = false;
489 if (all_selections)
490 {
491 HashTable_Clear(file_context->dir_table);
492 }
493 if (!HashTable_Foreach(file_context->inode_table, maybe_steal_inode, &clear_context))
494 res = false;
495 HashTable_Unlock(file_context->inode_table);
496
497 if (file_context->fuse_sess)
498 {
499 /*
500 * fuse_lowlevel_notify_inval_inode() is a blocking operation. If we receive a
501 * FUSE request (e.g. read()), then FUSE would block in read(), since the
502 * mutex of the inode_table would still be locked, if we wouldn't unlock it
503 * here.
504 * So, to avoid a deadlock here, unlock the mutex and reply all incoming
505 * operations with -ENOENT until the invalidation process is complete.
506 */
507 if (!ArrayList_ForEach(clear_context.fuse_files, invalidate_inode, file_context))
508 res = false;
509 CliprdrFuseFile* root_dir = get_fuse_file_by_ino(file_context, FUSE_ROOT_ID);
510 if (clip_data_dir && root_dir)
511 {
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);
514 }
515 }
516 ArrayList_Free(clear_context.fuse_files);
517
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);
522 else
523 WLog_Print(file_context->log, WLOG_DEBUG, "Selection%s cleared", all_selections ? "s" : "");
524 return res;
525}
526
527WINPR_ATTR_NODISCARD
528static bool clear_entry_selection(CliprdrFuseClipDataEntry* clip_data_entry)
529{
530 WINPR_ASSERT(clip_data_entry);
531
532 if (!clip_data_entry->clip_data_dir)
533 return true;
534
535 return clear_selection(clip_data_entry->file_context, FALSE, clip_data_entry);
536}
537
538WINPR_ATTR_NODISCARD
539static bool clear_no_cdi_entry(CliprdrFileContext* file_context)
540{
541 BOOL res = true;
542 WINPR_ASSERT(file_context);
543
544 WINPR_ASSERT(file_context->inode_table);
545 HashTable_Lock(file_context->inode_table);
546 if (file_context->clip_data_entry_without_id)
547 {
548 res = clear_entry_selection(file_context->clip_data_entry_without_id);
549
550 clip_data_entry_free(file_context->clip_data_entry_without_id);
551 file_context->clip_data_entry_without_id = nullptr;
552 }
553 HashTable_Unlock(file_context->inode_table);
554 return res;
555}
556
557WINPR_ATTR_NODISCARD
558static BOOL clear_clip_data_entries(WINPR_ATTR_UNUSED const void* key, void* value,
559 WINPR_ATTR_UNUSED void* arg)
560{
561 return clear_entry_selection(value);
562}
563
564WINPR_ATTR_NODISCARD
565static UINT clear_cdi_entries(CliprdrFileContext* file_context)
566{
567 UINT res = CHANNEL_RC_OK;
568 WINPR_ASSERT(file_context);
569
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;
573
574 HashTable_Clear(file_context->clip_data_table);
575 HashTable_Unlock(file_context->inode_table);
576
577 return res;
578}
579
580static UINT prepare_clip_data_entry_with_id(CliprdrFileContext* file_context)
581{
582 CliprdrFuseClipDataEntry* clip_data_entry = nullptr;
583
584 WINPR_ASSERT(file_context);
585
586 clip_data_entry = clip_data_entry_new(file_context, TRUE);
587 if (!clip_data_entry)
588 {
589 WLog_Print(file_context->log, WLOG_ERROR, "Failed to create clipDataEntry");
590 return ERROR_INTERNAL_ERROR;
591 }
592
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))
596 {
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;
601 }
602 HashTable_Unlock(file_context->inode_table);
603
604 // HashTable_Insert owns clip_data_entry
605 // NOLINTNEXTLINE(clang-analyzer-unix.Malloc)
606 file_context->current_clip_data_id = clip_data_entry->clip_data_id;
607
608 return CHANNEL_RC_OK;
609}
610
611static UINT prepare_clip_data_entry_without_id(CliprdrFileContext* file_context)
612{
613 WINPR_ASSERT(file_context);
614 WINPR_ASSERT(!file_context->clip_data_entry_without_id);
615
616 file_context->clip_data_entry_without_id = clip_data_entry_new(file_context, FALSE);
617 if (!file_context->clip_data_entry_without_id)
618 {
619 WLog_Print(file_context->log, WLOG_ERROR, "Failed to create clipDataEntry");
620 return ERROR_INTERNAL_ERROR;
621 }
622
623 return CHANNEL_RC_OK;
624}
625#endif
626
627UINT cliprdr_file_context_notify_new_server_format_list(CliprdrFileContext* file_context)
628{
629 UINT rc = CHANNEL_RC_OK;
630 WINPR_ASSERT(file_context);
631 WINPR_ASSERT(file_context->context);
632
633#if defined(WITH_FUSE)
634 if (!clear_no_cdi_entry(file_context))
635 return ERROR_INTERNAL_ERROR;
636 /* TODO: assign timeouts to old locks instead */
637 rc = clear_cdi_entries(file_context);
638 if (rc != CHANNEL_RC_OK)
639 return rc;
640
641 if (does_server_support_clipdata_locking(file_context))
642 rc = prepare_clip_data_entry_with_id(file_context);
643 else
644 rc = prepare_clip_data_entry_without_id(file_context);
645#endif
646 return rc;
647}
648
649UINT cliprdr_file_context_notify_new_client_format_list(CliprdrFileContext* file_context)
650{
651 WINPR_ASSERT(file_context);
652 WINPR_ASSERT(file_context->context);
653
654#if defined(WITH_FUSE)
655 if (!clear_no_cdi_entry(file_context))
656 return ERROR_INTERNAL_ERROR;
657 /* TODO: assign timeouts to old locks instead */
658 return clear_cdi_entries(file_context);
659#endif
660
661 return CHANNEL_RC_OK;
662}
663
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);
668
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, ...)
672{
673 if (!WLog_IsLevelActive(log, level))
674 return;
675
676 va_list ap = WINPR_C_ARRAY_INIT;
677 va_start(ap, fmt);
678 WLog_PrintTextMessageVA(log, level, line, fname, fkt, fmt, ap);
679 va_end(ap);
680}
681
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);
692
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,
700};
701
702CliprdrFuseFile* get_fuse_file_by_ino(CliprdrFileContext* file_context, fuse_ino_t fuse_ino)
703{
704 WINPR_ASSERT(file_context);
705
706 return HashTable_GetItemValue(file_context->inode_table, (void*)(uintptr_t)fuse_ino);
707}
708
709static CliprdrFuseFile*
710get_fuse_file_by_name_from_parent(WINPR_ATTR_UNUSED CliprdrFileContext* file_context,
711 CliprdrFuseFile* parent, const char* name)
712{
713 WINPR_ASSERT(file_context);
714 WINPR_ASSERT(parent);
715
716 for (size_t i = 0; i < ArrayList_Count(parent->children); ++i)
717 {
718 CliprdrFuseFile* child = ArrayList_GetItem(parent->children, i);
719
720 WINPR_ASSERT(child);
721
722 if (strncmp(name, child->filename, child->filename_len + 1) == 0)
723 return child;
724 }
725
726 DEBUG_CLIPRDR(file_context->log, "Requested file \"%s\" in directory \"%s\" does not exist",
727 name, parent->filename);
728
729 return nullptr;
730}
731
732static CliprdrFuseRequest* cliprdr_fuse_request_new(CliprdrFileContext* file_context,
733 CliprdrFuseFile* fuse_file, fuse_req_t fuse_req,
734 FuseLowlevelOperationType operation_type)
735{
736 CliprdrFuseRequest* fuse_request = nullptr;
737 UINT32 stream_id = file_context->next_stream_id;
738
739 WINPR_ASSERT(file_context);
740 WINPR_ASSERT(fuse_file);
741
742 fuse_request = calloc(1, sizeof(CliprdrFuseRequest));
743 if (!fuse_request)
744 {
745 WLog_Print(file_context->log, WLOG_ERROR, "Failed to allocate FUSE request for file \"%s\"",
746 fuse_file->filename_with_root);
747 return nullptr;
748 }
749
750 fuse_request->fuse_file = fuse_file;
751 fuse_request->fuse_req = fuse_req;
752 fuse_request->operation_type = operation_type;
753
754 while (stream_id == 0 ||
755 HashTable_GetItemValue(file_context->request_table, (void*)(uintptr_t)stream_id))
756 ++stream_id;
757 fuse_request->stream_id = stream_id;
758
759 file_context->next_stream_id = stream_id + 1;
760
761 if (!HashTable_Insert(file_context->request_table, (void*)(uintptr_t)fuse_request->stream_id,
762 fuse_request))
763 {
764 WLog_Print(file_context->log, WLOG_ERROR, "Failed to track FUSE request for file \"%s\"",
765 fuse_file->filename_with_root);
766 free(fuse_request);
767 return nullptr;
768 }
769
770 return fuse_request;
771}
772
773static BOOL request_file_size_async(CliprdrFileContext* file_context, CliprdrFuseFile* fuse_file,
774 fuse_req_t fuse_req, FuseLowlevelOperationType operation_type)
775{
776 CliprdrFuseRequest* fuse_request = nullptr;
777 CLIPRDR_FILE_CONTENTS_REQUEST file_contents_request = WINPR_C_ARRAY_INIT;
778
779 WINPR_ASSERT(file_context);
780 WINPR_ASSERT(fuse_file);
781
782 fuse_request = cliprdr_fuse_request_new(file_context, fuse_file, fuse_req, operation_type);
783 if (!fuse_request)
784 return FALSE;
785
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;
793
794 if (file_context->context->ClientFileContentsRequest(file_context->context,
795 &file_contents_request))
796 {
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);
801 return FALSE;
802 }
803 // NOLINTNEXTLINE(clang-analyzer-unix.Malloc)
804 DEBUG_CLIPRDR(file_context->log, "Requested file size for file \"%s\" with stream id %u",
805 fuse_file->filename, fuse_request->stream_id);
806
807 // NOLINTNEXTLINE(clang-analyzer-unix.Malloc)
808 return TRUE;
809}
810
811static void write_file_attributes(CliprdrFuseFile* fuse_file, struct stat* attr)
812{
813 memset(attr, 0, sizeof(struct stat));
814
815 if (!fuse_file)
816 return;
817
818 attr->st_ino = fuse_file->ino;
819 if (fuse_file->is_directory)
820 {
821 attr->st_mode = S_IFDIR | (fuse_file->is_readonly ? 0555 : 0755);
822 attr->st_nlink = 2;
823 }
824 else
825 {
826 attr->st_mode = S_IFREG | (fuse_file->is_readonly ? 0444 : 0644);
827 attr->st_nlink = 1;
828 attr->st_size = WINPR_ASSERTING_INT_CAST(off_t, fuse_file->size);
829 }
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));
834}
835
836static void cliprdr_file_fuse_lookup(fuse_req_t fuse_req, fuse_ino_t parent_ino, const char* name)
837{
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;
842
843 WINPR_ASSERT(file_context);
844
845 HashTable_Lock(file_context->inode_table);
846 if (!(parent = get_fuse_file_by_ino(file_context, parent_ino)) || !parent->is_directory)
847 {
848 HashTable_Unlock(file_context->inode_table);
849 fuse_reply_err(fuse_req, ENOENT);
850 return;
851 }
852 if (!(fuse_file = get_fuse_file_by_name_from_parent(file_context, parent, name)))
853 {
854 HashTable_Unlock(file_context->inode_table);
855 fuse_reply_err(fuse_req, ENOENT);
856 return;
857 }
858
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);
862
863 if (!fuse_file->is_directory && !fuse_file->has_size)
864 {
865 BOOL result = 0;
866
867 result =
868 request_file_size_async(file_context, fuse_file, fuse_req, FUSE_LL_OPERATION_LOOKUP);
869 HashTable_Unlock(file_context->inode_table);
870
871 if (!result)
872 fuse_reply_err(fuse_req, EIO);
873
874 return;
875 }
876
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);
882
883 fuse_reply_entry(fuse_req, &entry);
884}
885
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)
888{
889 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
890 CliprdrFuseFile* fuse_file = nullptr;
891 struct stat attr = WINPR_C_ARRAY_INIT;
892
893 WINPR_ASSERT(file_context);
894
895 HashTable_Lock(file_context->inode_table);
896 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
897 {
898 HashTable_Unlock(file_context->inode_table);
899 fuse_reply_err(fuse_req, ENOENT);
900 return;
901 }
902
903 DEBUG_CLIPRDR(file_context->log, "getattr() has been called for file \"%s\"",
904 fuse_file->filename_with_root);
905
906 if (!fuse_file->is_directory && !fuse_file->has_size)
907 {
908 BOOL result = 0;
909
910 result =
911 request_file_size_async(file_context, fuse_file, fuse_req, FUSE_LL_OPERATION_GETATTR);
912 HashTable_Unlock(file_context->inode_table);
913
914 if (!result)
915 fuse_reply_err(fuse_req, EIO);
916
917 return;
918 }
919
920 write_file_attributes(fuse_file, &attr);
921 HashTable_Unlock(file_context->inode_table);
922
923 fuse_reply_attr(fuse_req, &attr, 1.0);
924}
925
926static void cliprdr_file_fuse_open(fuse_req_t fuse_req, fuse_ino_t fuse_ino,
927 struct fuse_file_info* file_info)
928{
929 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
930 CliprdrFuseFile* fuse_file = nullptr;
931
932 WINPR_ASSERT(file_context);
933
934 HashTable_Lock(file_context->inode_table);
935 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
936 {
937 HashTable_Unlock(file_context->inode_table);
938 fuse_reply_err(fuse_req, ENOENT);
939 return;
940 }
941 if (fuse_file->is_directory)
942 {
943 HashTable_Unlock(file_context->inode_table);
944 fuse_reply_err(fuse_req, EISDIR);
945 return;
946 }
947 HashTable_Unlock(file_context->inode_table);
948
949 if ((file_info->flags & O_ACCMODE) != O_RDONLY)
950 {
951 fuse_reply_err(fuse_req, EACCES);
952 return;
953 }
954
955 /* Important for KDE to get file correctly */
956 file_info->direct_io = 1;
957
958 fuse_reply_open(fuse_req, file_info);
959}
960
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)
963{
964 CLIPRDR_FILE_CONTENTS_REQUEST file_contents_request = WINPR_C_ARRAY_INIT;
965
966 WINPR_ASSERT(file_context);
967 WINPR_ASSERT(fuse_file);
968
969 if (requested_size > UINT32_MAX)
970 return FALSE;
971
972 CliprdrFuseRequest* fuse_request =
973 cliprdr_fuse_request_new(file_context, fuse_file, fuse_req, FUSE_LL_OPERATION_READ);
974 if (!fuse_request)
975 return FALSE;
976
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;
986
987 if (file_context->context->ClientFileContentsRequest(file_context->context,
988 &file_contents_request))
989 {
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);
994 return FALSE;
995 }
996
997 // file_context->request_table owns fuse_request
998 // NOLINTBEGIN(clang-analyzer-unix.Malloc)
999 DEBUG_CLIPRDR(
1000 file_context->log,
1001 "Requested file range (%zu Bytes at offset %ld) for file \"%s\" with stream id %u",
1002 requested_size, offset, fuse_file->filename, fuse_request->stream_id);
1003
1004 return TRUE;
1005 // NOLINTEND(clang-analyzer-unix.Malloc)
1006}
1007
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)
1010{
1011 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
1012 CliprdrFuseFile* fuse_file = nullptr;
1013 BOOL result = 0;
1014
1015 WINPR_ASSERT(file_context);
1016
1017 HashTable_Lock(file_context->inode_table);
1018 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
1019 {
1020 HashTable_Unlock(file_context->inode_table);
1021 fuse_reply_err(fuse_req, ENOENT);
1022 return;
1023 }
1024 if (fuse_file->is_directory)
1025 {
1026 HashTable_Unlock(file_context->inode_table);
1027 fuse_reply_err(fuse_req, EISDIR);
1028 return;
1029 }
1030 if (!fuse_file->has_size || (offset < 0) || ((size_t)offset > fuse_file->size))
1031 {
1032 HashTable_Unlock(file_context->inode_table);
1033 fuse_reply_err(fuse_req, EINVAL);
1034 return;
1035 }
1036
1037 size = MIN(size, 8ULL * 1024ULL * 1024ULL);
1038
1039 result = request_file_range_async(file_context, fuse_file, fuse_req, offset, size);
1040 HashTable_Unlock(file_context->inode_table);
1041
1042 if (!result)
1043 fuse_reply_err(fuse_req, EIO);
1044}
1045
1046static void cliprdr_file_fuse_opendir(fuse_req_t fuse_req, fuse_ino_t fuse_ino,
1047 struct fuse_file_info* file_info)
1048{
1049 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
1050 CliprdrFuseFile* fuse_file = nullptr;
1051
1052 WINPR_ASSERT(file_context);
1053
1054 HashTable_Lock(file_context->inode_table);
1055 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
1056 {
1057 HashTable_Unlock(file_context->inode_table);
1058 fuse_reply_err(fuse_req, ENOENT);
1059 return;
1060 }
1061 if (!fuse_file->is_directory)
1062 {
1063 HashTable_Unlock(file_context->inode_table);
1064 fuse_reply_err(fuse_req, ENOTDIR);
1065 return;
1066 }
1067 HashTable_Unlock(file_context->inode_table);
1068
1069 if ((file_info->flags & O_ACCMODE) != O_RDONLY)
1070 {
1071 fuse_reply_err(fuse_req, EACCES);
1072 return;
1073 }
1074
1075 fuse_reply_open(fuse_req, file_info);
1076}
1077
1078static void cliprdr_file_fuse_readdir(fuse_req_t fuse_req, fuse_ino_t fuse_ino, size_t max_size,
1079 off_t offset,
1080 WINPR_ATTR_UNUSED struct fuse_file_info* file_info)
1081{
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;
1090
1091 WINPR_ASSERT(file_context);
1092
1093 HashTable_Lock(file_context->inode_table);
1094 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
1095 {
1096 HashTable_Unlock(file_context->inode_table);
1097 fuse_reply_err(fuse_req, ENOENT);
1098 return;
1099 }
1100 if (!fuse_file->is_directory)
1101 {
1102 HashTable_Unlock(file_context->inode_table);
1103 fuse_reply_err(fuse_req, ENOTDIR);
1104 return;
1105 }
1106
1107 DEBUG_CLIPRDR(file_context->log, "Reading directory \"%s\" at offset %ld",
1108 fuse_file->filename_with_root, offset);
1109
1110 if ((offset < 0) || ((size_t)offset >= ArrayList_Count(fuse_file->children)))
1111 {
1112 HashTable_Unlock(file_context->inode_table);
1113 fuse_reply_buf(fuse_req, nullptr, 0);
1114 return;
1115 }
1116
1117 buf = calloc(max_size, sizeof(char));
1118 if (!buf)
1119 {
1120 HashTable_Unlock(file_context->inode_table);
1121 fuse_reply_err(fuse_req, ENOMEM);
1122 return;
1123 }
1124 written_size = 0;
1125
1126 for (off_t i = offset; i < 2; ++i)
1127 {
1128 if (i == 0)
1129 {
1130 write_file_attributes(fuse_file, &attr);
1131 filename = ".";
1132 }
1133 else if (i == 1)
1134 {
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;
1138 filename = "..";
1139 }
1140 else
1141 {
1142 WINPR_ASSERT(FALSE);
1143 }
1144
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)
1152 break;
1153
1154 written_size += entry_size;
1155 }
1156
1157 for (size_t j = 0, i = 2; j < ArrayList_Count(fuse_file->children); ++j, ++i)
1158 {
1159 if (i < (size_t)offset)
1160 continue;
1161
1162 child = ArrayList_GetItem(fuse_file->children, j);
1163
1164 write_file_attributes(child, &attr);
1165 entry_size =
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)
1169 break;
1170
1171 written_size += entry_size;
1172 }
1173 HashTable_Unlock(file_context->inode_table);
1174
1175 fuse_reply_buf(fuse_req, buf, written_size);
1176 free(buf);
1177}
1178
1179static void fuse_abort(int sig, const char* signame, void* context)
1180{
1181 CliprdrFileContext* file = (CliprdrFileContext*)context;
1182
1183 if (file)
1184 {
1185 WLog_Print(file->log, WLOG_INFO, "signal %s [%d] aborting session", signame, sig);
1186 cliprdr_file_session_terminate(file, FALSE);
1187 }
1188}
1189
1190static DWORD WINAPI cliprdr_file_fuse_thread(LPVOID arg)
1191{
1192 CliprdrFileContext* file = (CliprdrFileContext*)arg;
1193
1194 WINPR_ASSERT(file);
1195
1196 DEBUG_CLIPRDR(file->log, "Starting fuse with mountpoint '%s'", file->path);
1197
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);
1203
1204 if (file->fuse_sess != nullptr)
1205 {
1206 if (freerdp_add_signal_cleanup_handler(file, fuse_abort))
1207 {
1208 if (0 == fuse_session_mount(file->fuse_sess, file->path))
1209 {
1210 fuse_session_loop(file->fuse_sess);
1211 fuse_session_unmount(file->fuse_sess);
1212 }
1213 freerdp_del_signal_cleanup_handler(file, fuse_abort);
1214 }
1215
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);
1220 }
1221 fuse_opt_free_args(&args);
1222
1223 DEBUG_CLIPRDR(file->log, "Quitting fuse with mountpoint '%s'", file->path);
1224
1225 ExitThread(0);
1226 return 0;
1227}
1228
1229static UINT cliprdr_file_context_server_file_contents_response(
1230 CliprdrClientContext* cliprdr_context,
1231 const CLIPRDR_FILE_CONTENTS_RESPONSE* file_contents_response)
1232{
1233 CliprdrFileContext* file_context = nullptr;
1234 CliprdrFuseRequest* fuse_request = nullptr;
1235 struct fuse_entry_param entry = WINPR_C_ARRAY_INIT;
1236
1237 WINPR_ASSERT(cliprdr_context);
1238 WINPR_ASSERT(file_contents_response);
1239
1240 file_context = cliprdr_context->custom;
1241 WINPR_ASSERT(file_context);
1242
1243 HashTable_Lock(file_context->inode_table);
1244 fuse_request = HashTable_GetItemValue(file_context->request_table,
1245 (void*)(uintptr_t)file_contents_response->streamId);
1246 if (!fuse_request)
1247 {
1248 HashTable_Unlock(file_context->inode_table);
1249 return CHANNEL_RC_OK;
1250 }
1251
1252 if (!(file_contents_response->common.msgFlags & CB_RESPONSE_OK))
1253 {
1254 WLog_Print(file_context->log, WLOG_WARN,
1255 "FileContentsRequests for file \"%s\" was unsuccessful",
1256 fuse_request->fuse_file->filename);
1257
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;
1263 }
1264
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))
1268 {
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;
1277 }
1278
1279 if (fuse_request->operation_type == FUSE_LL_OPERATION_LOOKUP ||
1280 fuse_request->operation_type == FUSE_LL_OPERATION_GETATTR)
1281 {
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);
1284
1285 fuse_request->fuse_file->size =
1286 *(WINPR_PACKED_ALIGN_CAST(const UINT64*, file_contents_response->requestedData));
1287 fuse_request->fuse_file->has_size = TRUE;
1288
1289 entry.ino = fuse_request->fuse_file->ino;
1290 write_file_attributes(fuse_request->fuse_file, &entry.attr);
1291 entry.attr_timeout = 1.0;
1292 entry.entry_timeout = 1.0;
1293 }
1294 else if (fuse_request->operation_type == FUSE_LL_OPERATION_READ)
1295 {
1296 DEBUG_CLIPRDR(file_context->log, "Received file range for file \"%s\" with stream id %u",
1297 fuse_request->fuse_file->filename, file_contents_response->streamId);
1298 }
1299 HashTable_Unlock(file_context->inode_table);
1300
1301 switch (fuse_request->operation_type)
1302 {
1303 case FUSE_LL_OPERATION_NONE:
1304 break;
1305 case FUSE_LL_OPERATION_LOOKUP:
1306 fuse_reply_entry(fuse_request->fuse_req, &entry);
1307 break;
1308 case FUSE_LL_OPERATION_GETATTR:
1309 fuse_reply_attr(fuse_request->fuse_req, &entry.attr, entry.attr_timeout);
1310 break;
1311 case FUSE_LL_OPERATION_READ:
1312 fuse_reply_buf(fuse_request->fuse_req,
1313 (const char*)file_contents_response->requestedData,
1314 file_contents_response->cbRequested);
1315 break;
1316 default:
1317 break;
1318 }
1319
1320 HashTable_Remove(file_context->request_table,
1321 (void*)(uintptr_t)file_contents_response->streamId);
1322
1323 return CHANNEL_RC_OK;
1324}
1325#endif
1326
1327static UINT cliprdr_file_context_send_file_contents_failure(
1328 CliprdrFileContext* file, const CLIPRDR_FILE_CONTENTS_REQUEST* fileContentsRequest)
1329{
1330 CLIPRDR_FILE_CONTENTS_RESPONSE response = WINPR_C_ARRAY_INIT;
1331
1332 WINPR_ASSERT(file);
1333 WINPR_ASSERT(fileContentsRequest);
1334
1335 const UINT64 offset = (((UINT64)fileContentsRequest->nPositionHigh) << 32) |
1336 ((UINT64)fileContentsRequest->nPositionLow);
1337 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1338 "server file contents request [lockID %" PRIu32 ", streamID %" PRIu32
1339 ", index %" PRIu32 "] offset %" PRIu64 ", size %" PRIu32 " failed",
1340 fileContentsRequest->clipDataId, fileContentsRequest->streamId,
1341 fileContentsRequest->listIndex, offset, fileContentsRequest->cbRequested);
1342
1343 response.common.msgFlags = CB_RESPONSE_FAIL;
1344 response.streamId = fileContentsRequest->streamId;
1345
1346 WINPR_ASSERT(file->context);
1347 WINPR_ASSERT(file->context->ClientFileContentsResponse);
1348 return file->context->ClientFileContentsResponse(file->context, &response);
1349}
1350
1351static UINT
1352cliprdr_file_context_send_contents_response(CliprdrFileContext* file,
1353 const CLIPRDR_FILE_CONTENTS_REQUEST* request,
1354 const void* data, size_t size)
1355{
1356 if (size > UINT32_MAX)
1357 return ERROR_INVALID_PARAMETER;
1358
1359 CLIPRDR_FILE_CONTENTS_RESPONSE response = { .streamId = request->streamId,
1360 .requestedData = data,
1361 .cbRequested = (UINT32)size,
1362 .common.msgFlags = CB_RESPONSE_OK };
1363
1364 WINPR_ASSERT(request);
1365 WINPR_ASSERT(file);
1366
1367 WLog_Print(file->log, WLOG_DEBUG, "send contents response streamID=%" PRIu32 ", size=%" PRIu32,
1368 response.streamId, response.cbRequested);
1369 WINPR_ASSERT(file->context);
1370 WINPR_ASSERT(file->context->ClientFileContentsResponse);
1371 return file->context->ClientFileContentsResponse(file->context, &response);
1372}
1373
1374static BOOL dump_streams(const void* key, void* value, WINPR_ATTR_UNUSED void* arg)
1375{
1376 const UINT32* ukey = key;
1377 CliprdrLocalStream* cur = value;
1378
1379 writelog(cur->context->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1380 "[key %" PRIu32 "] lockID %" PRIu32 ", count %" PRIuz ", locked %d", *ukey,
1381 cur->lockId, cur->count, cur->locked);
1382 for (size_t x = 0; x < cur->count; x++)
1383 {
1384 const CliprdrLocalFile* file = &cur->files[x];
1385 writelog(cur->context->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1386 "file [%" PRIuz "] %s: %" PRId64, x, file->name, file->size);
1387 }
1388 return TRUE;
1389}
1390
1391static CliprdrLocalFile* file_info_for_request(CliprdrFileContext* file, UINT32 lockId,
1392 UINT32 listIndex)
1393{
1394 WINPR_ASSERT(file);
1395
1396 CliprdrLocalStream* cur = HashTable_GetItemValue(file->local_streams, &lockId);
1397 if (cur)
1398 {
1399 if (listIndex < cur->count)
1400 {
1401 CliprdrLocalFile* f = &cur->files[listIndex];
1402 return f;
1403 }
1404 else
1405 {
1406 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1407 "invalid entry index for lockID %" PRIu32 ", index %" PRIu32 " [count %" PRIuz
1408 "] [locked %d]",
1409 lockId, listIndex, cur->count, cur->locked);
1410 }
1411 }
1412 else
1413 {
1414 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1415 "missing entry for lockID %" PRIu32 ", index %" PRIu32, lockId, listIndex);
1416 if (!HashTable_Foreach(file->local_streams, dump_streams, file))
1417 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1418 "HashTable_Foreach failed");
1419 }
1420
1421 return nullptr;
1422}
1423
1424static CliprdrLocalFile* file_for_request(CliprdrFileContext* file, UINT32 lockId, UINT32 listIndex)
1425{
1426 CliprdrLocalFile* f = file_info_for_request(file, lockId, listIndex);
1427 if (f)
1428 {
1429 if (!f->fp)
1430 {
1431 const char* name = f->name;
1432 f->fp = winpr_fopen(name, "rb");
1433 }
1434 if (!f->fp)
1435 {
1436 char ebuffer[256] = WINPR_C_ARRAY_INIT;
1437 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1438 "[lockID %" PRIu32 ", index %" PRIu32
1439 "] failed to open file '%s' [size %" PRId64 "] %s [%d]",
1440 lockId, listIndex, f->name, f->size,
1441 winpr_strerror(errno, ebuffer, sizeof(ebuffer)), errno);
1442 return nullptr;
1443 }
1444 }
1445
1446 return f;
1447}
1448
1449static void cliprdr_local_file_try_close(CliprdrLocalFile* file, UINT res, UINT64 offset,
1450 UINT64 size)
1451{
1452 WINPR_ASSERT(file);
1453
1454 if (res != 0)
1455 {
1456 WINPR_ASSERT(file->context);
1457 WLog_Print(file->context->log, WLOG_DEBUG, "closing file %s after error %" PRIu32,
1458 file->name, res);
1459 }
1460 else if (((file->size > 0) && (offset + size >= (UINT64)file->size)))
1461 {
1462 WINPR_ASSERT(file->context);
1463 WLog_Print(file->context->log, WLOG_DEBUG, "closing file %s after read", file->name);
1464 }
1465 else
1466 {
1467 // TODO: we need to keep track of open files to avoid running out of file descriptors
1468 // TODO: for the time being just close again.
1469 }
1470 if (file->fp)
1471 (void)fclose(file->fp);
1472 file->fp = nullptr;
1473}
1474
1475static UINT cliprdr_file_context_server_file_size_request(
1476 CliprdrFileContext* file, const CLIPRDR_FILE_CONTENTS_REQUEST* fileContentsRequest)
1477{
1478 WINPR_ASSERT(fileContentsRequest);
1479
1480 if (fileContentsRequest->cbRequested != sizeof(UINT64))
1481 {
1482 WLog_Print(file->log, WLOG_WARN, "unexpected FILECONTENTS_SIZE request: %" PRIu32 " bytes",
1483 fileContentsRequest->cbRequested);
1484 }
1485
1486 HashTable_Lock(file->local_streams);
1487
1488 UINT res = CHANNEL_RC_OK;
1489 CliprdrLocalFile* rfile =
1490 file_for_request(file, fileContentsRequest->clipDataId, fileContentsRequest->listIndex);
1491 if (!rfile)
1492 res = cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1493 else
1494 {
1495 if (_fseeki64(rfile->fp, 0, SEEK_END) < 0)
1496 res = cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1497 else
1498 {
1499 const INT64 size = _ftelli64(rfile->fp);
1500 rfile->size = size;
1501 cliprdr_local_file_try_close(rfile, res, 0, 0);
1502
1503 res = cliprdr_file_context_send_contents_response(file, fileContentsRequest, &size,
1504 sizeof(size));
1505 }
1506 }
1507
1508 HashTable_Unlock(file->local_streams);
1509 return res;
1510}
1511
1512static UINT cliprdr_file_context_server_file_range_request(
1513 CliprdrFileContext* file, const CLIPRDR_FILE_CONTENTS_REQUEST* fileContentsRequest)
1514{
1515 BYTE* data = nullptr;
1516
1517 WINPR_ASSERT(fileContentsRequest);
1518
1519 HashTable_Lock(file->local_streams);
1520 const UINT64 offset = (((UINT64)fileContentsRequest->nPositionHigh) << 32) |
1521 ((UINT64)fileContentsRequest->nPositionLow);
1522
1523 CliprdrLocalFile* rfile =
1524 file_for_request(file, fileContentsRequest->clipDataId, fileContentsRequest->listIndex);
1525 if (!rfile)
1526 goto fail;
1527
1528 if (_fseeki64(rfile->fp, WINPR_ASSERTING_INT_CAST(int64_t, offset), SEEK_SET) < 0)
1529 goto fail;
1530
1531 data = malloc(fileContentsRequest->cbRequested);
1532 if (!data)
1533 goto fail;
1534
1535 {
1536 const size_t r = fread(data, 1, fileContentsRequest->cbRequested, rfile->fp);
1537 const UINT rc =
1538 cliprdr_file_context_send_contents_response(file, fileContentsRequest, data, r);
1539 free(data);
1540
1541 cliprdr_local_file_try_close(rfile, rc, offset, fileContentsRequest->cbRequested);
1542 HashTable_Unlock(file->local_streams);
1543 return rc;
1544 }
1545fail:
1546 if (rfile)
1547 cliprdr_local_file_try_close(rfile, ERROR_INTERNAL_ERROR, offset,
1548 fileContentsRequest->cbRequested);
1549 free(data);
1550 HashTable_Unlock(file->local_streams);
1551 return cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1552}
1553
1554static void cliprdr_local_stream_free(void* obj);
1555
1556static UINT change_lock(CliprdrFileContext* file, UINT32 lockId, BOOL lock)
1557{
1558 UINT rc = CHANNEL_RC_OK;
1559
1560 WINPR_ASSERT(file);
1561
1562 HashTable_Lock(file->local_streams);
1563
1564 {
1565 CliprdrLocalStream* stream = HashTable_GetItemValue(file->local_streams, &lockId);
1566 if (lock && !stream)
1567 {
1568 stream = cliprdr_local_stream_new(file, lockId, nullptr, 0);
1569 if (!HashTable_Insert(file->local_streams, &lockId, stream))
1570 {
1571 rc = ERROR_INTERNAL_ERROR;
1572 cliprdr_local_stream_free(stream);
1573 stream = nullptr;
1574 }
1575 file->local_lock_id = lockId;
1576 }
1577 if (stream)
1578 {
1579 stream->locked = lock;
1580 stream->lockId = lockId;
1581 }
1582 }
1583
1584 // NOLINTNEXTLINE(clang-analyzer-unix.Malloc): HashTable_Insert ownership stream
1585 if (!lock)
1586 {
1587 if (!HashTable_Foreach(file->local_streams, local_stream_discard, file))
1588 rc = ERROR_INTERNAL_ERROR;
1589 }
1590
1591 HashTable_Unlock(file->local_streams);
1592 return rc;
1593}
1594
1595static UINT cliprdr_file_context_lock(CliprdrClientContext* context,
1596 const CLIPRDR_LOCK_CLIPBOARD_DATA* lockClipboardData)
1597{
1598 WINPR_ASSERT(context);
1599 WINPR_ASSERT(lockClipboardData);
1600 CliprdrFileContext* file = (context->custom);
1601 return change_lock(file, lockClipboardData->clipDataId, TRUE);
1602}
1603
1604static UINT cliprdr_file_context_unlock(CliprdrClientContext* context,
1605 const CLIPRDR_UNLOCK_CLIPBOARD_DATA* unlockClipboardData)
1606{
1607 WINPR_ASSERT(context);
1608 WINPR_ASSERT(unlockClipboardData);
1609 CliprdrFileContext* file = (context->custom);
1610 return change_lock(file, unlockClipboardData->clipDataId, FALSE);
1611}
1612
1613static UINT cliprdr_file_context_server_file_contents_request(
1614 CliprdrClientContext* context, const CLIPRDR_FILE_CONTENTS_REQUEST* fileContentsRequest)
1615{
1616 UINT error = NO_ERROR;
1617
1618 WINPR_ASSERT(context);
1619 WINPR_ASSERT(fileContentsRequest);
1620
1621 CliprdrFileContext* file = (context->custom);
1622 WINPR_ASSERT(file);
1623
1624 /*
1625 * MS-RDPECLIP 2.2.5.3 File Contents Request PDU (CLIPRDR_FILECONTENTS_REQUEST):
1626 * The FILECONTENTS_SIZE and FILECONTENTS_RANGE flags MUST NOT be set at the same time.
1627 */
1628 if ((fileContentsRequest->dwFlags & (FILECONTENTS_SIZE | FILECONTENTS_RANGE)) ==
1629 (FILECONTENTS_SIZE | FILECONTENTS_RANGE))
1630 {
1631 WLog_Print(file->log, WLOG_ERROR, "invalid CLIPRDR_FILECONTENTS_REQUEST.dwFlags");
1632 return cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1633 }
1634
1635 if (fileContentsRequest->dwFlags & FILECONTENTS_SIZE)
1636 error = cliprdr_file_context_server_file_size_request(file, fileContentsRequest);
1637
1638 if (fileContentsRequest->dwFlags & FILECONTENTS_RANGE)
1639 error = cliprdr_file_context_server_file_range_request(file, fileContentsRequest);
1640
1641 if (error)
1642 {
1643 WLog_Print(file->log, WLOG_ERROR, "failed to handle CLIPRDR_FILECONTENTS_REQUEST: 0x%08X",
1644 error);
1645 return cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1646 }
1647
1648 return CHANNEL_RC_OK;
1649}
1650
1651BOOL cliprdr_file_context_init(CliprdrFileContext* file, CliprdrClientContext* cliprdr)
1652{
1653 WINPR_ASSERT(file);
1654 WINPR_ASSERT(cliprdr);
1655
1656 cliprdr->custom = file;
1657 file->context = cliprdr;
1658
1659 cliprdr->ServerLockClipboardData = cliprdr_file_context_lock;
1660 cliprdr->ServerUnlockClipboardData = cliprdr_file_context_unlock;
1661 cliprdr->ServerFileContentsRequest = cliprdr_file_context_server_file_contents_request;
1662#if defined(WITH_FUSE)
1663 cliprdr->ServerFileContentsResponse = cliprdr_file_context_server_file_contents_response;
1664
1665 CliprdrFuseFile* root_dir = fuse_file_new_root(file);
1666 return root_dir != nullptr;
1667#else
1668 return TRUE;
1669#endif
1670}
1671
1672#if defined(WITH_FUSE)
1673WINPR_ATTR_NODISCARD
1674static bool clear_all_selections(CliprdrFileContext* file_context)
1675{
1676 WINPR_ASSERT(file_context);
1677 WINPR_ASSERT(file_context->inode_table);
1678
1679 HashTable_Lock(file_context->inode_table);
1680 const bool rc = clear_selection(file_context, TRUE, nullptr);
1681
1682 HashTable_Clear(file_context->clip_data_table);
1683 HashTable_Unlock(file_context->inode_table);
1684 return rc;
1685}
1686#endif
1687
1688BOOL cliprdr_file_context_uninit(CliprdrFileContext* file, CliprdrClientContext* cliprdr)
1689{
1690
1691 // Clear all data before the channel is closed
1692 // the cleanup handlers are dependent on a working channel.
1693 if (file)
1694 {
1695#if defined(WITH_FUSE)
1696 if (file->inode_table)
1697 {
1698 if (!clear_no_cdi_entry(file))
1699 return FALSE;
1700 if (!clear_all_selections(file))
1701 return FALSE;
1702 }
1703#endif
1704
1705 HashTable_Clear(file->local_streams);
1706
1707 file->context = nullptr;
1708 }
1709
1710 if (cliprdr)
1711 {
1712#if defined(WITH_FUSE)
1713 cliprdr->ServerFileContentsResponse = nullptr;
1714#endif
1715 }
1716
1717 return TRUE;
1718}
1719
1720static BOOL cliprdr_file_content_changed_and_update(void* ihash, size_t hsize, const void* data,
1721 size_t size)
1722{
1723
1724 BYTE hash[WINPR_SHA256_DIGEST_LENGTH] = WINPR_C_ARRAY_INIT;
1725
1726 if (hsize < sizeof(hash))
1727 return FALSE;
1728
1729 if (!winpr_Digest(WINPR_MD_SHA256, data, size, hash, sizeof(hash)))
1730 return FALSE;
1731
1732 const BOOL changed = memcmp(hash, ihash, sizeof(hash)) != 0;
1733 if (changed)
1734 memcpy(ihash, hash, sizeof(hash));
1735 return changed;
1736}
1737
1738static BOOL cliprdr_file_client_content_changed_and_update(CliprdrFileContext* file,
1739 const void* data, size_t size)
1740{
1741 WINPR_ASSERT(file);
1742 return cliprdr_file_content_changed_and_update(file->client_data_hash,
1743 sizeof(file->client_data_hash), data, size);
1744}
1745
1746#if defined(WITH_FUSE)
1747static fuse_ino_t get_next_free_inode(CliprdrFileContext* file_context)
1748{
1749 fuse_ino_t ino = 0;
1750
1751 WINPR_ASSERT(file_context);
1752
1753 ino = file_context->next_ino;
1754 while (ino == 0 || ino == FUSE_ROOT_ID ||
1755 HashTable_GetItemValue(file_context->inode_table, (void*)(uintptr_t)ino))
1756 ++ino;
1757
1758 file_context->next_ino = ino + 1;
1759
1760 return ino;
1761}
1762
1763static CliprdrFuseFile* clip_data_dir_new(CliprdrFileContext* file_context, BOOL has_clip_data_id,
1764 UINT32 clip_data_id)
1765{
1766 WINPR_ASSERT(file_context);
1767
1768 UINT64 data_id = clip_data_id;
1769 if (!has_clip_data_id)
1770 data_id = NO_CLIP_DATA_ID;
1771
1772 CliprdrFuseFile* clip_data_dir = fuse_file_new("/%" PRIu64, data_id);
1773 if (!clip_data_dir)
1774 return nullptr;
1775
1776 clip_data_dir->ino = get_next_free_inode(file_context);
1777 clip_data_dir->is_directory = TRUE;
1778 clip_data_dir->is_readonly = TRUE;
1779 clip_data_dir->has_clip_data_id = has_clip_data_id;
1780 clip_data_dir->clip_data_id = clip_data_id;
1781
1782 CliprdrFuseFile* root_dir = get_fuse_file_by_ino(file_context, FUSE_ROOT_ID);
1783 if (!root_dir)
1784 {
1785 WLog_Print(file_context->log, WLOG_ERROR, "FUSE root directory missing");
1786 fuse_file_free(clip_data_dir);
1787 return nullptr;
1788 }
1789 if (!ArrayList_Append(root_dir->children, clip_data_dir))
1790 {
1791 WLog_Print(file_context->log, WLOG_ERROR, "Failed to append FUSE file");
1792 fuse_file_free(clip_data_dir);
1793 return nullptr;
1794 }
1795 clip_data_dir->parent = root_dir;
1796
1797 if (!HashTable_Insert(file_context->inode_table, (void*)(uintptr_t)clip_data_dir->ino,
1798 clip_data_dir))
1799 {
1800 WLog_Print(file_context->log, WLOG_ERROR, "Failed to insert inode into inode table");
1801 ArrayList_Remove(root_dir->children, clip_data_dir);
1802 fuse_file_free(clip_data_dir);
1803 return nullptr;
1804 }
1805
1806 if (!HashTable_Insert(file_context->dir_table, clip_data_dir->filename_with_root,
1807 clip_data_dir))
1808 {
1809 WLog_Print(file_context->log, WLOG_ERROR, "Failed to insert inode into dir table");
1810 ArrayList_Remove(root_dir->children, clip_data_dir);
1811 fuse_file_free(clip_data_dir);
1812 return nullptr;
1813 }
1814
1815 return clip_data_dir;
1816}
1817
1818static char* get_parent_path(const char* filepath)
1819{
1820 const char* base = strrchr(filepath, '/');
1821 WINPR_ASSERT(base);
1822
1823 while ((base > filepath) && (*base == '/'))
1824 --base;
1825
1826 WINPR_ASSERT(base >= filepath);
1827 const size_t parent_path_length = 1ULL + (size_t)(base - filepath);
1828 char* parent_path = calloc(parent_path_length + 1, sizeof(char));
1829 if (!parent_path)
1830 return nullptr;
1831
1832 memcpy(parent_path, filepath, parent_path_length);
1833
1834 return parent_path;
1835}
1836
1837static CliprdrFuseFile* get_parent_directory(CliprdrFileContext* file_context, const char* path)
1838{
1839
1840 WINPR_ASSERT(file_context);
1841 WINPR_ASSERT(path);
1842
1843 char* parent_path = get_parent_path(path);
1844 if (!parent_path)
1845 return nullptr;
1846
1847 CliprdrFuseFile* parent = HashTable_GetItemValue(file_context->dir_table, parent_path);
1848 free(parent_path);
1849 return parent;
1850}
1851
1852// NOLINTBEGIN(clang-analyzer-unix.Malloc) HashTable_Insert owns fuse_file
1853static BOOL selection_handle_file(CliprdrFileContext* file_context,
1854 CliprdrFuseClipDataEntry* clip_data_entry, uint32_t index,
1855 const FILEDESCRIPTORW* file)
1856{
1857
1858 WINPR_ASSERT(file_context);
1859 WINPR_ASSERT(clip_data_entry);
1860 WINPR_ASSERT(file);
1861
1862 CliprdrFuseFile* clip_data_dir = clip_data_entry->clip_data_dir;
1863 WINPR_ASSERT(clip_data_dir);
1864
1865 char filename[ARRAYSIZE(file->cFileName) * 8] = WINPR_C_ARRAY_INIT;
1866 const SSIZE_T filenamelen = ConvertWCharNToUtf8(file->cFileName, ARRAYSIZE(file->cFileName),
1867 filename, ARRAYSIZE(filename) - 1);
1868 if (filenamelen < 0)
1869 {
1870 WLog_Print(file_context->log, WLOG_ERROR, "Failed to convert filename");
1871 return FALSE;
1872 }
1873
1874 for (size_t j = 0; filename[j]; ++j)
1875 {
1876 if (filename[j] == '\\')
1877 filename[j] = '/';
1878 }
1879
1880 BOOL crc = FALSE;
1881 CliprdrFuseFile* fuse_file = fuse_file_new(
1882 "%.*s/%s", WINPR_ASSERTING_INT_CAST(int, clip_data_dir->filename_with_root_len),
1883 clip_data_dir->filename_with_root, filename);
1884 if (!fuse_file)
1885 {
1886 WLog_Print(file_context->log, WLOG_ERROR, "Failed to create FUSE file");
1887 goto end;
1888 }
1889
1890 fuse_file->parent = get_parent_directory(file_context, fuse_file->filename_with_root);
1891 if (!fuse_file->parent)
1892 {
1893 WLog_Print(file_context->log, WLOG_ERROR, "Found no parent for FUSE file");
1894 goto end;
1895 }
1896
1897 if (!ArrayList_Append(fuse_file->parent->children, fuse_file))
1898 {
1899 WLog_Print(file_context->log, WLOG_ERROR, "Failed to append FUSE file");
1900 goto end;
1901 }
1902
1903 fuse_file->list_idx = index;
1904 fuse_file->ino = get_next_free_inode(file_context);
1905 fuse_file->has_clip_data_id = clip_data_entry->has_clip_data_id;
1906 fuse_file->clip_data_id = clip_data_entry->clip_data_id;
1907 if (file->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1908 fuse_file->is_directory = TRUE;
1909 if (file->dwFileAttributes & FILE_ATTRIBUTE_READONLY)
1910 fuse_file->is_readonly = TRUE;
1911 if (file->dwFlags & FD_FILESIZE)
1912 {
1913 fuse_file->size = ((UINT64)file->nFileSizeHigh << 32) + file->nFileSizeLow;
1914 fuse_file->has_size = TRUE;
1915 }
1916 if (file->dwFlags & FD_WRITESTIME)
1917 {
1918 INT64 filetime = 0;
1919
1920 filetime = file->ftLastWriteTime.dwHighDateTime;
1921 filetime <<= 32;
1922 filetime += file->ftLastWriteTime.dwLowDateTime;
1923
1924 fuse_file->last_write_time_unix =
1925 1LL * filetime / (10LL * 1000LL * 1000LL) - WIN32_FILETIME_TO_UNIX_EPOCH;
1926 fuse_file->has_last_write_time = TRUE;
1927 }
1928
1929 if (!HashTable_Insert(file_context->inode_table, (void*)(uintptr_t)fuse_file->ino, fuse_file))
1930 {
1931 WLog_Print(file_context->log, WLOG_ERROR, "Failed to insert inode into inode table");
1932 goto end;
1933 }
1934
1935 if (fuse_file->is_directory)
1936 {
1937
1938 if (!HashTable_Insert(file_context->dir_table, fuse_file->filename_with_root, fuse_file))
1939 {
1940 WLog_Print(file_context->log, WLOG_ERROR, "Failed to insert inode into dir table");
1941 goto end;
1942 }
1943 }
1944
1945 crc = TRUE;
1946
1947end:
1948 if (!crc)
1949 {
1950 fuse_file_free(fuse_file);
1951 return clear_entry_selection(clip_data_entry);
1952 }
1953 return TRUE;
1954}
1955// NOLINTEND(clang-analyzer-unix.Malloc) HashTable_Insert owns fuse_file
1956
1957static BOOL set_selection_for_clip_data_entry(CliprdrFileContext* file_context,
1958 CliprdrFuseClipDataEntry* clip_data_entry,
1959 const FILEDESCRIPTORW* files, UINT32 n_files)
1960{
1961 WINPR_ASSERT(file_context);
1962 WINPR_ASSERT(clip_data_entry);
1963 WINPR_ASSERT(files);
1964
1965 if (clip_data_entry->has_clip_data_id)
1966 WLog_Print(file_context->log, WLOG_DEBUG, "Setting selection for clipDataId %u",
1967 clip_data_entry->clip_data_id);
1968 else
1969 WLog_Print(file_context->log, WLOG_DEBUG, "Setting selection");
1970
1971 for (UINT32 i = 0; i < n_files; ++i)
1972 {
1973 const FILEDESCRIPTORW* file = &files[i];
1974 if (!selection_handle_file(file_context, clip_data_entry, i, file))
1975 return FALSE;
1976 }
1977
1978 if (clip_data_entry->has_clip_data_id)
1979 WLog_Print(file_context->log, WLOG_DEBUG, "Selection set for clipDataId %u",
1980 clip_data_entry->clip_data_id);
1981 else
1982 WLog_Print(file_context->log, WLOG_DEBUG, "Selection set");
1983
1984 return TRUE;
1985}
1986
1987static BOOL update_exposed_path(CliprdrFileContext* file_context, wClipboard* clip,
1988 CliprdrFuseClipDataEntry* clip_data_entry)
1989{
1990 wClipboardDelegate* delegate = nullptr;
1991 CliprdrFuseFile* clip_data_dir = nullptr;
1992
1993 WINPR_ASSERT(file_context);
1994 WINPR_ASSERT(clip);
1995 WINPR_ASSERT(clip_data_entry);
1996
1997 delegate = ClipboardGetDelegate(clip);
1998 WINPR_ASSERT(delegate);
1999
2000 clip_data_dir = clip_data_entry->clip_data_dir;
2001 WINPR_ASSERT(clip_data_dir);
2002
2003 free(file_context->exposed_path);
2004 file_context->exposed_path = GetCombinedPath(file_context->path, clip_data_dir->filename);
2005 if (file_context->exposed_path)
2006 WLog_Print(file_context->log, WLOG_DEBUG, "Updated exposed path to \"%s\"",
2007 file_context->exposed_path);
2008
2009 delegate->basePath = file_context->exposed_path;
2010
2011 return delegate->basePath != nullptr;
2012}
2013#endif
2014
2015BOOL cliprdr_file_context_update_server_data(CliprdrFileContext* file_context, wClipboard* clip,
2016 const void* data, size_t size)
2017{
2018#if defined(WITH_FUSE)
2019 CliprdrFuseClipDataEntry* clip_data_entry = nullptr;
2020 FILEDESCRIPTORW* files = nullptr;
2021 UINT32 n_files = 0;
2022 BOOL rc = FALSE;
2023
2024 WINPR_ASSERT(file_context);
2025 WINPR_ASSERT(clip);
2026 if (size > UINT32_MAX)
2027 return FALSE;
2028
2029 if (cliprdr_parse_file_list(data, (UINT32)size, &files, &n_files))
2030 {
2031 WLog_Print(file_context->log, WLOG_ERROR, "Failed to parse file list");
2032 return FALSE;
2033 }
2034
2035 HashTable_Lock(file_context->inode_table);
2036 HashTable_Lock(file_context->clip_data_table);
2037 if (does_server_support_clipdata_locking(file_context))
2038 clip_data_entry = HashTable_GetItemValue(
2039 file_context->clip_data_table, (void*)(uintptr_t)file_context->current_clip_data_id);
2040 else
2041 clip_data_entry = file_context->clip_data_entry_without_id;
2042
2043 WINPR_ASSERT(clip_data_entry);
2044
2045 if (!clear_entry_selection(clip_data_entry))
2046 goto fail;
2047 WINPR_ASSERT(!clip_data_entry->clip_data_dir);
2048
2049 clip_data_entry->clip_data_dir =
2050 clip_data_dir_new(file_context, does_server_support_clipdata_locking(file_context),
2051 file_context->current_clip_data_id);
2052 if (!clip_data_entry->clip_data_dir)
2053 goto fail;
2054 if (!update_exposed_path(file_context, clip, clip_data_entry))
2055 goto fail;
2056 if (!set_selection_for_clip_data_entry(file_context, clip_data_entry, files, n_files))
2057 goto fail;
2058
2059 rc = TRUE;
2060
2061fail:
2062 HashTable_Unlock(file_context->clip_data_table);
2063 HashTable_Unlock(file_context->inode_table);
2064 free(files);
2065 return rc;
2066#else
2067 return FALSE;
2068#endif
2069}
2070
2071void* cliprdr_file_context_get_context(CliprdrFileContext* file)
2072{
2073 WINPR_ASSERT(file);
2074 return file->clipboard;
2075}
2076
2077void cliprdr_file_session_terminate(CliprdrFileContext* file, BOOL stop_thread)
2078{
2079 if (!file)
2080 return;
2081
2082#if defined(WITH_FUSE)
2083 WINPR_ASSERT(file->fuse_stop_sync);
2084
2085 WLog_Print(file->log, WLOG_DEBUG, "Setting FUSE exit flag");
2086 if (file->fuse_sess)
2087 fuse_session_exit(file->fuse_sess);
2088
2089 if (stop_thread)
2090 {
2091 WLog_Print(file->log, WLOG_DEBUG, "Setting FUSE stop event");
2092 (void)SetEvent(file->fuse_stop_sync);
2093 }
2094#endif
2095 /* not elegant but works for umounting FUSE
2096 fuse_chan must receive an oper buf to unblock fuse_session_receive_buf function.
2097 */
2098#if defined(WITH_FUSE)
2099 WLog_Print(file->log, WLOG_DEBUG, "Forcing FUSE to check exit flag");
2100#endif
2101 if (winpr_PathFileExists(file->path))
2102 WLog_Print(file->log, WLOG_WARN, "winpr_PathFileExists(%s) failed", file->path);
2103}
2104
2105void cliprdr_file_context_free(CliprdrFileContext* file)
2106{
2107 if (!file)
2108 return;
2109
2110#if defined(WITH_FUSE)
2111 if (file->fuse_thread)
2112 {
2113 WINPR_ASSERT(file->fuse_stop_sync);
2114
2115 WLog_Print(file->log, WLOG_DEBUG, "Stopping FUSE thread");
2116 cliprdr_file_session_terminate(file, TRUE);
2117
2118 WLog_Print(file->log, WLOG_DEBUG, "Waiting on FUSE thread");
2119 (void)WaitForSingleObject(file->fuse_thread, INFINITE);
2120 (void)CloseHandle(file->fuse_thread);
2121 }
2122 if (file->fuse_stop_sync)
2123 (void)CloseHandle(file->fuse_stop_sync);
2124 if (file->fuse_start_sync)
2125 (void)CloseHandle(file->fuse_start_sync);
2126
2127 HashTable_Free(file->request_table);
2128 HashTable_Free(file->clip_data_table);
2129 HashTable_Free(file->dir_table);
2130 HashTable_Free(file->inode_table);
2131
2132#endif
2133 HashTable_Free(file->local_streams);
2134 winpr_RemoveDirectory(file->path);
2135 free(file->path);
2136 free(file->exposed_path);
2137 free(file);
2138}
2139
2140static BOOL create_base_path(CliprdrFileContext* file)
2141{
2142 WINPR_ASSERT(file);
2143
2144 char base[64] = WINPR_C_ARRAY_INIT;
2145 (void)_snprintf(base, sizeof(base), "com.freerdp.client.cliprdr.%" PRIu32,
2146 GetCurrentProcessId());
2147
2148 file->path = GetKnownSubPath(KNOWN_PATH_TEMP, base);
2149 if (!file->path)
2150 return FALSE;
2151
2152 if (!winpr_PathFileExists(file->path) && !winpr_PathMakePath(file->path, nullptr))
2153 {
2154 WLog_Print(file->log, WLOG_ERROR, "Failed to create directory '%s'", file->path);
2155 return FALSE;
2156 }
2157 return TRUE;
2158}
2159
2160static void cliprdr_local_file_free(CliprdrLocalFile* file)
2161{
2162 const CliprdrLocalFile empty = WINPR_C_ARRAY_INIT;
2163 if (!file)
2164 return;
2165 if (file->fp)
2166 {
2167 WLog_Print(file->context->log, WLOG_DEBUG, "closing file %s, discarding entry", file->name);
2168 (void)fclose(file->fp);
2169 }
2170 free(file->name);
2171 *file = empty;
2172}
2173
2174static BOOL cliprdr_local_file_new(CliprdrFileContext* context, CliprdrLocalFile* f,
2175 const char* path)
2176{
2177 const CliprdrLocalFile empty = WINPR_C_ARRAY_INIT;
2178 WINPR_ASSERT(f);
2179 WINPR_ASSERT(context);
2180 WINPR_ASSERT(path);
2181
2182 *f = empty;
2183 f->context = context;
2184 f->name = winpr_str_url_decode(path, strlen(path));
2185 if (!f->name)
2186 goto fail;
2187
2188 return TRUE;
2189fail:
2190 cliprdr_local_file_free(f);
2191 return FALSE;
2192}
2193
2194static void cliprdr_local_files_free(CliprdrLocalStream* stream)
2195{
2196 WINPR_ASSERT(stream);
2197
2198 for (size_t x = 0; x < stream->count; x++)
2199 cliprdr_local_file_free(&stream->files[x]);
2200 free(stream->files);
2201
2202 stream->files = nullptr;
2203 stream->count = 0;
2204}
2205
2206static void cliprdr_local_stream_free(void* obj)
2207{
2208 CliprdrLocalStream* stream = (CliprdrLocalStream*)obj;
2209 if (stream)
2210 cliprdr_local_files_free(stream);
2211
2212 free(stream);
2213}
2214
2215static BOOL append_entry(CliprdrLocalStream* stream, const char* path)
2216{
2217 CliprdrLocalFile* tmp = realloc(stream->files, sizeof(CliprdrLocalFile) * (stream->count + 1));
2218 if (!tmp)
2219 return FALSE;
2220 stream->files = tmp;
2221 CliprdrLocalFile* f = &stream->files[stream->count++];
2222
2223 return cliprdr_local_file_new(stream->context, f, path);
2224}
2225
2226static BOOL is_directory(const char* path)
2227{
2228 WCHAR* wpath = ConvertUtf8ToWCharAlloc(path, nullptr);
2229 if (!wpath)
2230 return FALSE;
2231
2232 HANDLE hFile = CreateFileW(wpath, 0, FILE_SHARE_DELETE, nullptr, OPEN_EXISTING,
2233 FILE_ATTRIBUTE_NORMAL, nullptr);
2234 free(wpath);
2235
2236 if (hFile == INVALID_HANDLE_VALUE)
2237 return FALSE;
2238
2239 BY_HANDLE_FILE_INFORMATION fileInformation = WINPR_C_ARRAY_INIT;
2240 const BOOL status = GetFileInformationByHandle(hFile, &fileInformation);
2241 (void)CloseHandle(hFile);
2242 if (!status)
2243 return FALSE;
2244
2245 return (fileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
2246}
2247
2248static BOOL add_directory(CliprdrLocalStream* stream, const char* path)
2249{
2250 char* wildcardpath = GetCombinedPath(path, "*");
2251 if (!wildcardpath)
2252 return FALSE;
2253 WCHAR* wpath = ConvertUtf8ToWCharAlloc(wildcardpath, nullptr);
2254 free(wildcardpath);
2255 if (!wpath)
2256 return FALSE;
2257
2258 WIN32_FIND_DATAW FindFileData = WINPR_C_ARRAY_INIT;
2259 HANDLE hFind = FindFirstFileW(wpath, &FindFileData);
2260 free(wpath);
2261
2262 if (hFind == INVALID_HANDLE_VALUE)
2263 return FALSE;
2264
2265 BOOL rc = FALSE;
2266 char* next = nullptr;
2267
2268 WCHAR dotbuffer[6] = WINPR_C_ARRAY_INIT;
2269 WCHAR dotdotbuffer[6] = WINPR_C_ARRAY_INIT;
2270 const WCHAR* dot = InitializeConstWCharFromUtf8(".", dotbuffer, ARRAYSIZE(dotbuffer));
2271 const WCHAR* dotdot = InitializeConstWCharFromUtf8("..", dotdotbuffer, ARRAYSIZE(dotdotbuffer));
2272 do
2273 {
2274 if (_wcscmp(FindFileData.cFileName, dot) == 0)
2275 continue;
2276 if (_wcscmp(FindFileData.cFileName, dotdot) == 0)
2277 continue;
2278
2279 char cFileName[MAX_PATH] = WINPR_C_ARRAY_INIT;
2280 (void)ConvertWCharNToUtf8(FindFileData.cFileName, ARRAYSIZE(FindFileData.cFileName),
2281 cFileName, ARRAYSIZE(cFileName));
2282
2283 free(next);
2284 next = GetCombinedPath(path, cFileName);
2285 if (!next)
2286 goto fail;
2287
2288 if (!append_entry(stream, next))
2289 goto fail;
2290 if (is_directory(next))
2291 {
2292 if (!add_directory(stream, next))
2293 goto fail;
2294 }
2295 } while (FindNextFileW(hFind, &FindFileData));
2296
2297 rc = TRUE;
2298fail:
2299 free(next);
2300 FindClose(hFind);
2301
2302 return rc;
2303}
2304
2305static BOOL cliprdr_local_stream_update(CliprdrLocalStream* stream, const char* data, size_t size)
2306{
2307 BOOL rc = FALSE;
2308 WINPR_ASSERT(stream);
2309 if (size == 0)
2310 return TRUE;
2311
2312 cliprdr_local_files_free(stream);
2313
2314 stream->files = calloc(size, sizeof(CliprdrLocalFile));
2315 if (!stream->files)
2316 return FALSE;
2317
2318 char* copy = strndup(data, size);
2319 if (!copy)
2320 return FALSE;
2321
2322 char* dname = nullptr;
2323 char* saveptr = nullptr;
2324 char* ptr = strtok_s(copy, "\r\n", &saveptr);
2325 while (ptr)
2326 {
2327 const char* cname = ptr;
2328 if (strncmp("file:///", ptr, 8) == 0)
2329 cname = &ptr[7];
2330 else if (strncmp("file:/", ptr, 6) == 0)
2331 cname = &ptr[5];
2332
2333 const size_t nlen = strnlen(cname, size);
2334 free(dname);
2335 dname = winpr_str_url_decode(cname, nlen);
2336 if (!dname)
2337 goto fail;
2338 if (!append_entry(stream, dname))
2339 goto fail;
2340
2341 if (is_directory(dname))
2342 {
2343 const BOOL res = add_directory(stream, dname);
2344 if (!res)
2345 goto fail;
2346 }
2347 ptr = strtok_s(nullptr, "\r\n", &saveptr);
2348 }
2349
2350 rc = TRUE;
2351fail:
2352 free(dname);
2353 free(copy);
2354 return rc;
2355}
2356
2357CliprdrLocalStream* cliprdr_local_stream_new(CliprdrFileContext* context, UINT32 streamID,
2358 const char* data, size_t size)
2359{
2360 WINPR_ASSERT(context);
2361 CliprdrLocalStream* stream = calloc(1, sizeof(CliprdrLocalStream));
2362 if (!stream)
2363 return nullptr;
2364
2365 stream->context = context;
2366 if (!cliprdr_local_stream_update(stream, data, size))
2367 goto fail;
2368
2369 stream->lockId = streamID;
2370 return stream;
2371
2372fail:
2373 cliprdr_local_stream_free(stream);
2374 return nullptr;
2375}
2376
2377static UINT32 UINTPointerHash(const void* id)
2378{
2379 WINPR_ASSERT(id);
2380 return *((const UINT32*)id);
2381}
2382
2383static BOOL UINTPointerCompare(const void* pointer1, const void* pointer2)
2384{
2385 if (!pointer1 || !pointer2)
2386 return pointer1 == pointer2;
2387
2388 const UINT32* a = pointer1;
2389 const UINT32* b = pointer2;
2390 return *a == *b;
2391}
2392
2393static void* UINTPointerClone(const void* other)
2394{
2395 const UINT32* src = other;
2396 if (!src)
2397 return nullptr;
2398
2399 UINT32* copy = calloc(1, sizeof(UINT32));
2400 if (!copy)
2401 return nullptr;
2402
2403 *copy = *src;
2404 return copy;
2405}
2406
2407#if defined(WITH_FUSE)
2408CliprdrFuseFile* fuse_file_new_root(CliprdrFileContext* file_context)
2409{
2410 CliprdrFuseFile* root_dir = fuse_file_new("/");
2411 if (!root_dir)
2412 return nullptr;
2413
2414 root_dir->ino = FUSE_ROOT_ID;
2415 root_dir->is_directory = TRUE;
2416 root_dir->is_readonly = TRUE;
2417
2418 if (!HashTable_Insert(file_context->inode_table, (void*)(uintptr_t)root_dir->ino, root_dir))
2419 {
2420 fuse_file_free(root_dir);
2421 return nullptr;
2422 }
2423
2424 if (!HashTable_Insert(file_context->dir_table, root_dir->filename_with_root, root_dir))
2425 {
2426 fuse_file_free(root_dir);
2427 return nullptr;
2428 }
2429
2430 return root_dir;
2431}
2432#endif
2433
2434CliprdrFileContext* cliprdr_file_context_new(void* context)
2435{
2436 CliprdrFileContext* file = calloc(1, sizeof(CliprdrFileContext));
2437 if (!file)
2438 return nullptr;
2439
2440 file->log = WLog_Get(CLIENT_TAG("common.cliprdr.file"));
2441 file->clipboard = context;
2442
2443 file->local_streams = HashTable_New(FALSE);
2444 if (!file->local_streams)
2445 goto fail;
2446
2447 if (!HashTable_SetHashFunction(file->local_streams, UINTPointerHash))
2448 goto fail;
2449
2450 {
2451 wObject* hkobj = HashTable_KeyObject(file->local_streams);
2452 WINPR_ASSERT(hkobj);
2453 hkobj->fnObjectEquals = UINTPointerCompare;
2454 hkobj->fnObjectFree = free;
2455 hkobj->fnObjectNew = UINTPointerClone;
2456 }
2457
2458 {
2459 wObject* hobj = HashTable_ValueObject(file->local_streams);
2460 WINPR_ASSERT(hobj);
2461 hobj->fnObjectFree = cliprdr_local_stream_free;
2462 }
2463
2464#if defined(WITH_FUSE)
2465 file->inode_table = HashTable_New(FALSE);
2466 file->dir_table = HashTable_New(FALSE);
2467 file->clip_data_table = HashTable_New(FALSE);
2468 file->request_table = HashTable_New(FALSE);
2469 if (!file->inode_table || !file->dir_table || !file->clip_data_table || !file->request_table)
2470 goto fail;
2471
2472 {
2473 wObject* ctobj = HashTable_ValueObject(file->request_table);
2474 WINPR_ASSERT(ctobj);
2475 ctobj->fnObjectFree = free;
2476 }
2477 {
2478 wObject* ctobj = HashTable_ValueObject(file->clip_data_table);
2479 WINPR_ASSERT(ctobj);
2480 ctobj->fnObjectFree = clip_data_entry_free;
2481 }
2482 {
2483 if (!HashTable_SetHashFunction(file->dir_table, HashTable_StringHash))
2484 goto fail;
2485 wObject* kobj = HashTable_KeyObject(file->dir_table);
2486 WINPR_ASSERT(kobj);
2487 kobj->fnObjectEquals = HashTable_StringCompare;
2488 }
2489#endif
2490
2491 if (!create_base_path(file))
2492 goto fail;
2493
2494#if defined(WITH_FUSE)
2495 if (!(file->fuse_start_sync = CreateEvent(nullptr, TRUE, FALSE, nullptr)))
2496 goto fail;
2497 if (!(file->fuse_stop_sync = CreateEvent(nullptr, TRUE, FALSE, nullptr)))
2498 goto fail;
2499 if (!(file->fuse_thread = CreateThread(nullptr, 0, cliprdr_file_fuse_thread, file, 0, nullptr)))
2500 goto fail;
2501
2502 if (WaitForSingleObject(file->fuse_start_sync, INFINITE) == WAIT_FAILED)
2503 WLog_Print(file->log, WLOG_ERROR, "Failed to wait for start sync");
2504#endif
2505 return file;
2506
2507fail:
2508 WINPR_PRAGMA_DIAG_PUSH
2509 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2510 cliprdr_file_context_free(file);
2511 WINPR_PRAGMA_DIAG_POP
2512 return nullptr;
2513}
2514
2515BOOL local_stream_discard(const void* key, void* value, void* arg)
2516{
2517 CliprdrFileContext* file = arg;
2518 CliprdrLocalStream* stream = value;
2519 WINPR_ASSERT(file);
2520 WINPR_ASSERT(stream);
2521
2522 if (!stream->locked)
2523 HashTable_Remove(file->local_streams, key);
2524 return TRUE;
2525}
2526
2527BOOL cliprdr_file_context_clear(CliprdrFileContext* file)
2528{
2529 WINPR_ASSERT(file);
2530
2531 WLog_Print(file->log, WLOG_DEBUG, "clear file clipboard...");
2532
2533 HashTable_Lock(file->local_streams);
2534 const BOOL res = HashTable_Foreach(file->local_streams, local_stream_discard, file);
2535 HashTable_Unlock(file->local_streams);
2536
2537 memset(file->server_data_hash, 0, sizeof(file->server_data_hash));
2538 memset(file->client_data_hash, 0, sizeof(file->client_data_hash));
2539 return res;
2540}
2541
2542BOOL cliprdr_file_context_update_client_data(CliprdrFileContext* file, const char* data,
2543 size_t size)
2544{
2545 BOOL rc = FALSE;
2546
2547 WINPR_ASSERT(file);
2548 if (!cliprdr_file_client_content_changed_and_update(file, data, size))
2549 return TRUE;
2550
2551 if (!cliprdr_file_context_clear(file))
2552 return FALSE;
2553
2554 UINT32 lockId = file->local_lock_id;
2555
2556 HashTable_Lock(file->local_streams);
2557 CliprdrLocalStream* stream = HashTable_GetItemValue(file->local_streams, &lockId);
2558
2559 WLog_Print(file->log, WLOG_DEBUG, "update client file list (stream=%p)...", (void*)stream);
2560 if (stream)
2561 rc = cliprdr_local_stream_update(stream, data, size);
2562 else
2563 {
2564 stream = cliprdr_local_stream_new(file, lockId, data, size);
2565 rc = HashTable_Insert(file->local_streams, &stream->lockId, stream);
2566 if (!rc)
2567 cliprdr_local_stream_free(stream);
2568 }
2569 // HashTable_Insert owns stream
2570 // NOLINTNEXTLINE(clang-analyzer-unix.Malloc)
2571 HashTable_Unlock(file->local_streams);
2572 return rc;
2573}
2574
2575UINT32 cliprdr_file_context_current_flags(CliprdrFileContext* file)
2576{
2577 WINPR_ASSERT(file);
2578
2579 if ((file->file_capability_flags & CB_STREAM_FILECLIP_ENABLED) == 0)
2580 return 0;
2581
2582 if (!file->file_formats_registered)
2583 return 0;
2584
2585 return CB_STREAM_FILECLIP_ENABLED | CB_FILECLIP_NO_FILE_PATHS |
2586 CB_HUGE_FILE_SUPPORT_ENABLED; // | CB_CAN_LOCK_CLIPDATA;
2587}
2588
2589BOOL cliprdr_file_context_set_locally_available(CliprdrFileContext* file, BOOL available)
2590{
2591 WINPR_ASSERT(file);
2592 file->file_formats_registered = available;
2593 return TRUE;
2594}
2595
2596BOOL cliprdr_file_context_remote_set_flags(CliprdrFileContext* file, UINT32 flags)
2597{
2598 WINPR_ASSERT(file);
2599 file->file_capability_flags = flags;
2600 return TRUE;
2601}
2602
2603UINT32 cliprdr_file_context_remote_get_flags(CliprdrFileContext* file)
2604{
2605 WINPR_ASSERT(file);
2606 return file->file_capability_flags;
2607}
2608
2609BOOL cliprdr_file_context_has_local_support(CliprdrFileContext* file)
2610{
2611 WINPR_UNUSED(file);
2612
2613#if defined(WITH_FUSE)
2614 return TRUE;
2615#else
2616 return FALSE;
2617#endif
2618}
This struct contains function pointer to initialize/free objects.
Definition collections.h:52
OBJECT_FREE_FN fnObjectFree
Definition collections.h:59
WINPR_ATTR_NODISCARD OBJECT_EQUALS_FN fnObjectEquals
Definition collections.h:61
WINPR_ATTR_NODISCARD OBJECT_NEW_FN fnObjectNew
Definition collections.h:54