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 %lu) 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 %lu",
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 = *((const UINT64*)file_contents_response->requestedData);
1286 fuse_request->fuse_file->has_size = TRUE;
1287
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;
1292 }
1293 else if (fuse_request->operation_type == FUSE_LL_OPERATION_READ)
1294 {
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);
1297 }
1298 HashTable_Unlock(file_context->inode_table);
1299
1300 switch (fuse_request->operation_type)
1301 {
1302 case FUSE_LL_OPERATION_NONE:
1303 break;
1304 case FUSE_LL_OPERATION_LOOKUP:
1305 fuse_reply_entry(fuse_request->fuse_req, &entry);
1306 break;
1307 case FUSE_LL_OPERATION_GETATTR:
1308 fuse_reply_attr(fuse_request->fuse_req, &entry.attr, entry.attr_timeout);
1309 break;
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);
1314 break;
1315 default:
1316 break;
1317 }
1318
1319 HashTable_Remove(file_context->request_table,
1320 (void*)(uintptr_t)file_contents_response->streamId);
1321
1322 return CHANNEL_RC_OK;
1323}
1324#endif
1325
1326static UINT cliprdr_file_context_send_file_contents_failure(
1327 CliprdrFileContext* file, const CLIPRDR_FILE_CONTENTS_REQUEST* fileContentsRequest)
1328{
1329 CLIPRDR_FILE_CONTENTS_RESPONSE response = WINPR_C_ARRAY_INIT;
1330
1331 WINPR_ASSERT(file);
1332 WINPR_ASSERT(fileContentsRequest);
1333
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);
1341
1342 response.common.msgFlags = CB_RESPONSE_FAIL;
1343 response.streamId = fileContentsRequest->streamId;
1344
1345 WINPR_ASSERT(file->context);
1346 WINPR_ASSERT(file->context->ClientFileContentsResponse);
1347 return file->context->ClientFileContentsResponse(file->context, &response);
1348}
1349
1350static UINT
1351cliprdr_file_context_send_contents_response(CliprdrFileContext* file,
1352 const CLIPRDR_FILE_CONTENTS_REQUEST* request,
1353 const void* data, size_t size)
1354{
1355 if (size > UINT32_MAX)
1356 return ERROR_INVALID_PARAMETER;
1357
1358 CLIPRDR_FILE_CONTENTS_RESPONSE response = { .streamId = request->streamId,
1359 .requestedData = data,
1360 .cbRequested = (UINT32)size,
1361 .common.msgFlags = CB_RESPONSE_OK };
1362
1363 WINPR_ASSERT(request);
1364 WINPR_ASSERT(file);
1365
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);
1371}
1372
1373static BOOL dump_streams(const void* key, void* value, WINPR_ATTR_UNUSED void* arg)
1374{
1375 const UINT32* ukey = key;
1376 CliprdrLocalStream* cur = value;
1377
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++)
1382 {
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);
1386 }
1387 return TRUE;
1388}
1389
1390static CliprdrLocalFile* file_info_for_request(CliprdrFileContext* file, UINT32 lockId,
1391 UINT32 listIndex)
1392{
1393 WINPR_ASSERT(file);
1394
1395 CliprdrLocalStream* cur = HashTable_GetItemValue(file->local_streams, &lockId);
1396 if (cur)
1397 {
1398 if (listIndex < cur->count)
1399 {
1400 CliprdrLocalFile* f = &cur->files[listIndex];
1401 return f;
1402 }
1403 else
1404 {
1405 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1406 "invalid entry index for lockID %" PRIu32 ", index %" PRIu32 " [count %" PRIuz
1407 "] [locked %d]",
1408 lockId, listIndex, cur->count, cur->locked);
1409 }
1410 }
1411 else
1412 {
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");
1418 }
1419
1420 return nullptr;
1421}
1422
1423static CliprdrLocalFile* file_for_request(CliprdrFileContext* file, UINT32 lockId, UINT32 listIndex)
1424{
1425 CliprdrLocalFile* f = file_info_for_request(file, lockId, listIndex);
1426 if (f)
1427 {
1428 if (!f->fp)
1429 {
1430 const char* name = f->name;
1431 f->fp = winpr_fopen(name, "rb");
1432 }
1433 if (!f->fp)
1434 {
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);
1441 return nullptr;
1442 }
1443 }
1444
1445 return f;
1446}
1447
1448static void cliprdr_local_file_try_close(CliprdrLocalFile* file, UINT res, UINT64 offset,
1449 UINT64 size)
1450{
1451 WINPR_ASSERT(file);
1452
1453 if (res != 0)
1454 {
1455 WINPR_ASSERT(file->context);
1456 WLog_Print(file->context->log, WLOG_DEBUG, "closing file %s after error %" PRIu32,
1457 file->name, res);
1458 }
1459 else if (((file->size > 0) && (offset + size >= (UINT64)file->size)))
1460 {
1461 WINPR_ASSERT(file->context);
1462 WLog_Print(file->context->log, WLOG_DEBUG, "closing file %s after read", file->name);
1463 }
1464 else
1465 {
1466 // TODO: we need to keep track of open files to avoid running out of file descriptors
1467 // TODO: for the time being just close again.
1468 }
1469 if (file->fp)
1470 (void)fclose(file->fp);
1471 file->fp = nullptr;
1472}
1473
1474static UINT cliprdr_file_context_server_file_size_request(
1475 CliprdrFileContext* file, const CLIPRDR_FILE_CONTENTS_REQUEST* fileContentsRequest)
1476{
1477 WINPR_ASSERT(fileContentsRequest);
1478
1479 if (fileContentsRequest->cbRequested != sizeof(UINT64))
1480 {
1481 WLog_Print(file->log, WLOG_WARN, "unexpected FILECONTENTS_SIZE request: %" PRIu32 " bytes",
1482 fileContentsRequest->cbRequested);
1483 }
1484
1485 HashTable_Lock(file->local_streams);
1486
1487 UINT res = CHANNEL_RC_OK;
1488 CliprdrLocalFile* rfile =
1489 file_for_request(file, fileContentsRequest->clipDataId, fileContentsRequest->listIndex);
1490 if (!rfile)
1491 res = cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1492 else
1493 {
1494 if (_fseeki64(rfile->fp, 0, SEEK_END) < 0)
1495 res = cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1496 else
1497 {
1498 const INT64 size = _ftelli64(rfile->fp);
1499 rfile->size = size;
1500 cliprdr_local_file_try_close(rfile, res, 0, 0);
1501
1502 res = cliprdr_file_context_send_contents_response(file, fileContentsRequest, &size,
1503 sizeof(size));
1504 }
1505 }
1506
1507 HashTable_Unlock(file->local_streams);
1508 return res;
1509}
1510
1511static UINT cliprdr_file_context_server_file_range_request(
1512 CliprdrFileContext* file, const CLIPRDR_FILE_CONTENTS_REQUEST* fileContentsRequest)
1513{
1514 BYTE* data = nullptr;
1515
1516 WINPR_ASSERT(fileContentsRequest);
1517
1518 HashTable_Lock(file->local_streams);
1519 const UINT64 offset = (((UINT64)fileContentsRequest->nPositionHigh) << 32) |
1520 ((UINT64)fileContentsRequest->nPositionLow);
1521
1522 CliprdrLocalFile* rfile =
1523 file_for_request(file, fileContentsRequest->clipDataId, fileContentsRequest->listIndex);
1524 if (!rfile)
1525 goto fail;
1526
1527 if (_fseeki64(rfile->fp, WINPR_ASSERTING_INT_CAST(int64_t, offset), SEEK_SET) < 0)
1528 goto fail;
1529
1530 data = malloc(fileContentsRequest->cbRequested);
1531 if (!data)
1532 goto fail;
1533
1534 {
1535 const size_t r = fread(data, 1, fileContentsRequest->cbRequested, rfile->fp);
1536 const UINT rc =
1537 cliprdr_file_context_send_contents_response(file, fileContentsRequest, data, r);
1538 free(data);
1539
1540 cliprdr_local_file_try_close(rfile, rc, offset, fileContentsRequest->cbRequested);
1541 HashTable_Unlock(file->local_streams);
1542 return rc;
1543 }
1544fail:
1545 if (rfile)
1546 cliprdr_local_file_try_close(rfile, ERROR_INTERNAL_ERROR, offset,
1547 fileContentsRequest->cbRequested);
1548 free(data);
1549 HashTable_Unlock(file->local_streams);
1550 return cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1551}
1552
1553static void cliprdr_local_stream_free(void* obj);
1554
1555static UINT change_lock(CliprdrFileContext* file, UINT32 lockId, BOOL lock)
1556{
1557 UINT rc = CHANNEL_RC_OK;
1558
1559 WINPR_ASSERT(file);
1560
1561 HashTable_Lock(file->local_streams);
1562
1563 {
1564 CliprdrLocalStream* stream = HashTable_GetItemValue(file->local_streams, &lockId);
1565 if (lock && !stream)
1566 {
1567 stream = cliprdr_local_stream_new(file, lockId, nullptr, 0);
1568 if (!HashTable_Insert(file->local_streams, &lockId, stream))
1569 {
1570 rc = ERROR_INTERNAL_ERROR;
1571 cliprdr_local_stream_free(stream);
1572 stream = nullptr;
1573 }
1574 file->local_lock_id = lockId;
1575 }
1576 if (stream)
1577 {
1578 stream->locked = lock;
1579 stream->lockId = lockId;
1580 }
1581 }
1582
1583 // NOLINTNEXTLINE(clang-analyzer-unix.Malloc): HashTable_Insert ownership stream
1584 if (!lock)
1585 {
1586 if (!HashTable_Foreach(file->local_streams, local_stream_discard, file))
1587 rc = ERROR_INTERNAL_ERROR;
1588 }
1589
1590 HashTable_Unlock(file->local_streams);
1591 return rc;
1592}
1593
1594static UINT cliprdr_file_context_lock(CliprdrClientContext* context,
1595 const CLIPRDR_LOCK_CLIPBOARD_DATA* lockClipboardData)
1596{
1597 WINPR_ASSERT(context);
1598 WINPR_ASSERT(lockClipboardData);
1599 CliprdrFileContext* file = (context->custom);
1600 return change_lock(file, lockClipboardData->clipDataId, TRUE);
1601}
1602
1603static UINT cliprdr_file_context_unlock(CliprdrClientContext* context,
1604 const CLIPRDR_UNLOCK_CLIPBOARD_DATA* unlockClipboardData)
1605{
1606 WINPR_ASSERT(context);
1607 WINPR_ASSERT(unlockClipboardData);
1608 CliprdrFileContext* file = (context->custom);
1609 return change_lock(file, unlockClipboardData->clipDataId, FALSE);
1610}
1611
1612static UINT cliprdr_file_context_server_file_contents_request(
1613 CliprdrClientContext* context, const CLIPRDR_FILE_CONTENTS_REQUEST* fileContentsRequest)
1614{
1615 UINT error = NO_ERROR;
1616
1617 WINPR_ASSERT(context);
1618 WINPR_ASSERT(fileContentsRequest);
1619
1620 CliprdrFileContext* file = (context->custom);
1621 WINPR_ASSERT(file);
1622
1623 /*
1624 * MS-RDPECLIP 2.2.5.3 File Contents Request PDU (CLIPRDR_FILECONTENTS_REQUEST):
1625 * The FILECONTENTS_SIZE and FILECONTENTS_RANGE flags MUST NOT be set at the same time.
1626 */
1627 if ((fileContentsRequest->dwFlags & (FILECONTENTS_SIZE | FILECONTENTS_RANGE)) ==
1628 (FILECONTENTS_SIZE | FILECONTENTS_RANGE))
1629 {
1630 WLog_Print(file->log, WLOG_ERROR, "invalid CLIPRDR_FILECONTENTS_REQUEST.dwFlags");
1631 return cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1632 }
1633
1634 if (fileContentsRequest->dwFlags & FILECONTENTS_SIZE)
1635 error = cliprdr_file_context_server_file_size_request(file, fileContentsRequest);
1636
1637 if (fileContentsRequest->dwFlags & FILECONTENTS_RANGE)
1638 error = cliprdr_file_context_server_file_range_request(file, fileContentsRequest);
1639
1640 if (error)
1641 {
1642 WLog_Print(file->log, WLOG_ERROR, "failed to handle CLIPRDR_FILECONTENTS_REQUEST: 0x%08X",
1643 error);
1644 return cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1645 }
1646
1647 return CHANNEL_RC_OK;
1648}
1649
1650BOOL cliprdr_file_context_init(CliprdrFileContext* file, CliprdrClientContext* cliprdr)
1651{
1652 WINPR_ASSERT(file);
1653 WINPR_ASSERT(cliprdr);
1654
1655 cliprdr->custom = file;
1656 file->context = cliprdr;
1657
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;
1663
1664 CliprdrFuseFile* root_dir = fuse_file_new_root(file);
1665 return root_dir != nullptr;
1666#else
1667 return TRUE;
1668#endif
1669}
1670
1671#if defined(WITH_FUSE)
1672WINPR_ATTR_NODISCARD
1673static bool clear_all_selections(CliprdrFileContext* file_context)
1674{
1675 WINPR_ASSERT(file_context);
1676 WINPR_ASSERT(file_context->inode_table);
1677
1678 HashTable_Lock(file_context->inode_table);
1679 const bool rc = clear_selection(file_context, TRUE, nullptr);
1680
1681 HashTable_Clear(file_context->clip_data_table);
1682 HashTable_Unlock(file_context->inode_table);
1683 return rc;
1684}
1685#endif
1686
1687BOOL cliprdr_file_context_uninit(CliprdrFileContext* file, CliprdrClientContext* cliprdr)
1688{
1689
1690 // Clear all data before the channel is closed
1691 // the cleanup handlers are dependent on a working channel.
1692 if (file)
1693 {
1694#if defined(WITH_FUSE)
1695 if (file->inode_table)
1696 {
1697 if (!clear_no_cdi_entry(file))
1698 return FALSE;
1699 if (!clear_all_selections(file))
1700 return FALSE;
1701 }
1702#endif
1703
1704 HashTable_Clear(file->local_streams);
1705
1706 file->context = nullptr;
1707 }
1708
1709 if (cliprdr)
1710 {
1711#if defined(WITH_FUSE)
1712 cliprdr->ServerFileContentsResponse = nullptr;
1713#endif
1714 }
1715
1716 return TRUE;
1717}
1718
1719static BOOL cliprdr_file_content_changed_and_update(void* ihash, size_t hsize, const void* data,
1720 size_t size)
1721{
1722
1723 BYTE hash[WINPR_SHA256_DIGEST_LENGTH] = WINPR_C_ARRAY_INIT;
1724
1725 if (hsize < sizeof(hash))
1726 return FALSE;
1727
1728 if (!winpr_Digest(WINPR_MD_SHA256, data, size, hash, sizeof(hash)))
1729 return FALSE;
1730
1731 const BOOL changed = memcmp(hash, ihash, sizeof(hash)) != 0;
1732 if (changed)
1733 memcpy(ihash, hash, sizeof(hash));
1734 return changed;
1735}
1736
1737static BOOL cliprdr_file_client_content_changed_and_update(CliprdrFileContext* file,
1738 const void* data, size_t size)
1739{
1740 WINPR_ASSERT(file);
1741 return cliprdr_file_content_changed_and_update(file->client_data_hash,
1742 sizeof(file->client_data_hash), data, size);
1743}
1744
1745#if defined(WITH_FUSE)
1746static fuse_ino_t get_next_free_inode(CliprdrFileContext* file_context)
1747{
1748 fuse_ino_t ino = 0;
1749
1750 WINPR_ASSERT(file_context);
1751
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))
1755 ++ino;
1756
1757 file_context->next_ino = ino + 1;
1758
1759 return ino;
1760}
1761
1762static CliprdrFuseFile* clip_data_dir_new(CliprdrFileContext* file_context, BOOL has_clip_data_id,
1763 UINT32 clip_data_id)
1764{
1765 WINPR_ASSERT(file_context);
1766
1767 UINT64 data_id = clip_data_id;
1768 if (!has_clip_data_id)
1769 data_id = NO_CLIP_DATA_ID;
1770
1771 CliprdrFuseFile* clip_data_dir = fuse_file_new("/%" PRIu64, data_id);
1772 if (!clip_data_dir)
1773 return nullptr;
1774
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;
1780
1781 CliprdrFuseFile* root_dir = get_fuse_file_by_ino(file_context, FUSE_ROOT_ID);
1782 if (!root_dir)
1783 {
1784 WLog_Print(file_context->log, WLOG_ERROR, "FUSE root directory missing");
1785 fuse_file_free(clip_data_dir);
1786 return nullptr;
1787 }
1788 if (!ArrayList_Append(root_dir->children, clip_data_dir))
1789 {
1790 WLog_Print(file_context->log, WLOG_ERROR, "Failed to append FUSE file");
1791 fuse_file_free(clip_data_dir);
1792 return nullptr;
1793 }
1794 clip_data_dir->parent = root_dir;
1795
1796 if (!HashTable_Insert(file_context->inode_table, (void*)(uintptr_t)clip_data_dir->ino,
1797 clip_data_dir))
1798 {
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);
1802 return nullptr;
1803 }
1804
1805 if (!HashTable_Insert(file_context->dir_table, clip_data_dir->filename_with_root,
1806 clip_data_dir))
1807 {
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);
1811 return nullptr;
1812 }
1813
1814 return clip_data_dir;
1815}
1816
1817static char* get_parent_path(const char* filepath)
1818{
1819 const char* base = strrchr(filepath, '/');
1820 WINPR_ASSERT(base);
1821
1822 while ((base > filepath) && (*base == '/'))
1823 --base;
1824
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));
1828 if (!parent_path)
1829 return nullptr;
1830
1831 memcpy(parent_path, filepath, parent_path_length);
1832
1833 return parent_path;
1834}
1835
1836static CliprdrFuseFile* get_parent_directory(CliprdrFileContext* file_context, const char* path)
1837{
1838
1839 WINPR_ASSERT(file_context);
1840 WINPR_ASSERT(path);
1841
1842 char* parent_path = get_parent_path(path);
1843 if (!parent_path)
1844 return nullptr;
1845
1846 CliprdrFuseFile* parent = HashTable_GetItemValue(file_context->dir_table, parent_path);
1847 free(parent_path);
1848 return parent;
1849}
1850
1851// NOLINTBEGIN(clang-analyzer-unix.Malloc) HashTable_Insert owns fuse_file
1852static BOOL selection_handle_file(CliprdrFileContext* file_context,
1853 CliprdrFuseClipDataEntry* clip_data_entry, uint32_t index,
1854 const FILEDESCRIPTORW* file)
1855{
1856
1857 WINPR_ASSERT(file_context);
1858 WINPR_ASSERT(clip_data_entry);
1859 WINPR_ASSERT(file);
1860
1861 CliprdrFuseFile* clip_data_dir = clip_data_entry->clip_data_dir;
1862 WINPR_ASSERT(clip_data_dir);
1863
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)
1868 {
1869 WLog_Print(file_context->log, WLOG_ERROR, "Failed to convert filename");
1870 return FALSE;
1871 }
1872
1873 for (size_t j = 0; filename[j]; ++j)
1874 {
1875 if (filename[j] == '\\')
1876 filename[j] = '/';
1877 }
1878
1879 BOOL crc = FALSE;
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);
1883 if (!fuse_file)
1884 {
1885 WLog_Print(file_context->log, WLOG_ERROR, "Failed to create FUSE file");
1886 goto end;
1887 }
1888
1889 fuse_file->parent = get_parent_directory(file_context, fuse_file->filename_with_root);
1890 if (!fuse_file->parent)
1891 {
1892 WLog_Print(file_context->log, WLOG_ERROR, "Found no parent for FUSE file");
1893 goto end;
1894 }
1895
1896 if (!ArrayList_Append(fuse_file->parent->children, fuse_file))
1897 {
1898 WLog_Print(file_context->log, WLOG_ERROR, "Failed to append FUSE file");
1899 goto end;
1900 }
1901
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)
1911 {
1912 fuse_file->size = ((UINT64)file->nFileSizeHigh << 32) + file->nFileSizeLow;
1913 fuse_file->has_size = TRUE;
1914 }
1915 if (file->dwFlags & FD_WRITESTIME)
1916 {
1917 INT64 filetime = 0;
1918
1919 filetime = file->ftLastWriteTime.dwHighDateTime;
1920 filetime <<= 32;
1921 filetime += file->ftLastWriteTime.dwLowDateTime;
1922
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;
1926 }
1927
1928 if (!HashTable_Insert(file_context->inode_table, (void*)(uintptr_t)fuse_file->ino, fuse_file))
1929 {
1930 WLog_Print(file_context->log, WLOG_ERROR, "Failed to insert inode into inode table");
1931 goto end;
1932 }
1933
1934 if (fuse_file->is_directory)
1935 {
1936
1937 if (!HashTable_Insert(file_context->dir_table, fuse_file->filename_with_root, fuse_file))
1938 {
1939 WLog_Print(file_context->log, WLOG_ERROR, "Failed to insert inode into dir table");
1940 goto end;
1941 }
1942 }
1943
1944 crc = TRUE;
1945
1946end:
1947 if (!crc)
1948 {
1949 fuse_file_free(fuse_file);
1950 return clear_entry_selection(clip_data_entry);
1951 }
1952 return TRUE;
1953}
1954// NOLINTEND(clang-analyzer-unix.Malloc) HashTable_Insert owns fuse_file
1955
1956static BOOL set_selection_for_clip_data_entry(CliprdrFileContext* file_context,
1957 CliprdrFuseClipDataEntry* clip_data_entry,
1958 const FILEDESCRIPTORW* files, UINT32 n_files)
1959{
1960 WINPR_ASSERT(file_context);
1961 WINPR_ASSERT(clip_data_entry);
1962 WINPR_ASSERT(files);
1963
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);
1967 else
1968 WLog_Print(file_context->log, WLOG_DEBUG, "Setting selection");
1969
1970 for (UINT32 i = 0; i < n_files; ++i)
1971 {
1972 const FILEDESCRIPTORW* file = &files[i];
1973 if (!selection_handle_file(file_context, clip_data_entry, i, file))
1974 return FALSE;
1975 }
1976
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);
1980 else
1981 WLog_Print(file_context->log, WLOG_DEBUG, "Selection set");
1982
1983 return TRUE;
1984}
1985
1986static BOOL update_exposed_path(CliprdrFileContext* file_context, wClipboard* clip,
1987 CliprdrFuseClipDataEntry* clip_data_entry)
1988{
1989 wClipboardDelegate* delegate = nullptr;
1990 CliprdrFuseFile* clip_data_dir = nullptr;
1991
1992 WINPR_ASSERT(file_context);
1993 WINPR_ASSERT(clip);
1994 WINPR_ASSERT(clip_data_entry);
1995
1996 delegate = ClipboardGetDelegate(clip);
1997 WINPR_ASSERT(delegate);
1998
1999 clip_data_dir = clip_data_entry->clip_data_dir;
2000 WINPR_ASSERT(clip_data_dir);
2001
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);
2007
2008 delegate->basePath = file_context->exposed_path;
2009
2010 return delegate->basePath != nullptr;
2011}
2012#endif
2013
2014BOOL cliprdr_file_context_update_server_data(CliprdrFileContext* file_context, wClipboard* clip,
2015 const void* data, size_t size)
2016{
2017#if defined(WITH_FUSE)
2018 CliprdrFuseClipDataEntry* clip_data_entry = nullptr;
2019 FILEDESCRIPTORW* files = nullptr;
2020 UINT32 n_files = 0;
2021 BOOL rc = FALSE;
2022
2023 WINPR_ASSERT(file_context);
2024 WINPR_ASSERT(clip);
2025 if (size > UINT32_MAX)
2026 return FALSE;
2027
2028 if (cliprdr_parse_file_list(data, (UINT32)size, &files, &n_files))
2029 {
2030 WLog_Print(file_context->log, WLOG_ERROR, "Failed to parse file list");
2031 return FALSE;
2032 }
2033
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);
2039 else
2040 clip_data_entry = file_context->clip_data_entry_without_id;
2041
2042 WINPR_ASSERT(clip_data_entry);
2043
2044 if (!clear_entry_selection(clip_data_entry))
2045 goto fail;
2046 WINPR_ASSERT(!clip_data_entry->clip_data_dir);
2047
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)
2052 goto fail;
2053 if (!update_exposed_path(file_context, clip, clip_data_entry))
2054 goto fail;
2055 if (!set_selection_for_clip_data_entry(file_context, clip_data_entry, files, n_files))
2056 goto fail;
2057
2058 rc = TRUE;
2059
2060fail:
2061 HashTable_Unlock(file_context->clip_data_table);
2062 HashTable_Unlock(file_context->inode_table);
2063 free(files);
2064 return rc;
2065#else
2066 return FALSE;
2067#endif
2068}
2069
2070void* cliprdr_file_context_get_context(CliprdrFileContext* file)
2071{
2072 WINPR_ASSERT(file);
2073 return file->clipboard;
2074}
2075
2076void cliprdr_file_session_terminate(CliprdrFileContext* file, BOOL stop_thread)
2077{
2078 if (!file)
2079 return;
2080
2081#if defined(WITH_FUSE)
2082 WINPR_ASSERT(file->fuse_stop_sync);
2083
2084 WLog_Print(file->log, WLOG_DEBUG, "Setting FUSE exit flag");
2085 if (file->fuse_sess)
2086 fuse_session_exit(file->fuse_sess);
2087
2088 if (stop_thread)
2089 {
2090 WLog_Print(file->log, WLOG_DEBUG, "Setting FUSE stop event");
2091 (void)SetEvent(file->fuse_stop_sync);
2092 }
2093#endif
2094 /* not elegant but works for umounting FUSE
2095 fuse_chan must receive an oper buf to unblock fuse_session_receive_buf function.
2096 */
2097#if defined(WITH_FUSE)
2098 WLog_Print(file->log, WLOG_DEBUG, "Forcing FUSE to check exit flag");
2099#endif
2100 if (winpr_PathFileExists(file->path))
2101 WLog_Print(file->log, WLOG_WARN, "winpr_PathFileExists(%s) failed", file->path);
2102}
2103
2104void cliprdr_file_context_free(CliprdrFileContext* file)
2105{
2106 if (!file)
2107 return;
2108
2109#if defined(WITH_FUSE)
2110 if (file->fuse_thread)
2111 {
2112 WINPR_ASSERT(file->fuse_stop_sync);
2113
2114 WLog_Print(file->log, WLOG_DEBUG, "Stopping FUSE thread");
2115 cliprdr_file_session_terminate(file, TRUE);
2116
2117 WLog_Print(file->log, WLOG_DEBUG, "Waiting on FUSE thread");
2118 (void)WaitForSingleObject(file->fuse_thread, INFINITE);
2119 (void)CloseHandle(file->fuse_thread);
2120 }
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);
2125
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);
2130
2131#endif
2132 HashTable_Free(file->local_streams);
2133 winpr_RemoveDirectory(file->path);
2134 free(file->path);
2135 free(file->exposed_path);
2136 free(file);
2137}
2138
2139static BOOL create_base_path(CliprdrFileContext* file)
2140{
2141 WINPR_ASSERT(file);
2142
2143 char base[64] = WINPR_C_ARRAY_INIT;
2144 (void)_snprintf(base, sizeof(base), "com.freerdp.client.cliprdr.%" PRIu32,
2145 GetCurrentProcessId());
2146
2147 file->path = GetKnownSubPath(KNOWN_PATH_TEMP, base);
2148 if (!file->path)
2149 return FALSE;
2150
2151 if (!winpr_PathFileExists(file->path) && !winpr_PathMakePath(file->path, nullptr))
2152 {
2153 WLog_Print(file->log, WLOG_ERROR, "Failed to create directory '%s'", file->path);
2154 return FALSE;
2155 }
2156 return TRUE;
2157}
2158
2159static void cliprdr_local_file_free(CliprdrLocalFile* file)
2160{
2161 const CliprdrLocalFile empty = WINPR_C_ARRAY_INIT;
2162 if (!file)
2163 return;
2164 if (file->fp)
2165 {
2166 WLog_Print(file->context->log, WLOG_DEBUG, "closing file %s, discarding entry", file->name);
2167 (void)fclose(file->fp);
2168 }
2169 free(file->name);
2170 *file = empty;
2171}
2172
2173static BOOL cliprdr_local_file_new(CliprdrFileContext* context, CliprdrLocalFile* f,
2174 const char* path)
2175{
2176 const CliprdrLocalFile empty = WINPR_C_ARRAY_INIT;
2177 WINPR_ASSERT(f);
2178 WINPR_ASSERT(context);
2179 WINPR_ASSERT(path);
2180
2181 *f = empty;
2182 f->context = context;
2183 f->name = winpr_str_url_decode(path, strlen(path));
2184 if (!f->name)
2185 goto fail;
2186
2187 return TRUE;
2188fail:
2189 cliprdr_local_file_free(f);
2190 return FALSE;
2191}
2192
2193static void cliprdr_local_files_free(CliprdrLocalStream* stream)
2194{
2195 WINPR_ASSERT(stream);
2196
2197 for (size_t x = 0; x < stream->count; x++)
2198 cliprdr_local_file_free(&stream->files[x]);
2199 free(stream->files);
2200
2201 stream->files = nullptr;
2202 stream->count = 0;
2203}
2204
2205static void cliprdr_local_stream_free(void* obj)
2206{
2207 CliprdrLocalStream* stream = (CliprdrLocalStream*)obj;
2208 if (stream)
2209 cliprdr_local_files_free(stream);
2210
2211 free(stream);
2212}
2213
2214static BOOL append_entry(CliprdrLocalStream* stream, const char* path)
2215{
2216 CliprdrLocalFile* tmp = realloc(stream->files, sizeof(CliprdrLocalFile) * (stream->count + 1));
2217 if (!tmp)
2218 return FALSE;
2219 stream->files = tmp;
2220 CliprdrLocalFile* f = &stream->files[stream->count++];
2221
2222 return cliprdr_local_file_new(stream->context, f, path);
2223}
2224
2225static BOOL is_directory(const char* path)
2226{
2227 WCHAR* wpath = ConvertUtf8ToWCharAlloc(path, nullptr);
2228 if (!wpath)
2229 return FALSE;
2230
2231 HANDLE hFile = CreateFileW(wpath, 0, FILE_SHARE_DELETE, nullptr, OPEN_EXISTING,
2232 FILE_ATTRIBUTE_NORMAL, nullptr);
2233 free(wpath);
2234
2235 if (hFile == INVALID_HANDLE_VALUE)
2236 return FALSE;
2237
2238 BY_HANDLE_FILE_INFORMATION fileInformation = WINPR_C_ARRAY_INIT;
2239 const BOOL status = GetFileInformationByHandle(hFile, &fileInformation);
2240 (void)CloseHandle(hFile);
2241 if (!status)
2242 return FALSE;
2243
2244 return (fileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
2245}
2246
2247static BOOL add_directory(CliprdrLocalStream* stream, const char* path)
2248{
2249 char* wildcardpath = GetCombinedPath(path, "*");
2250 if (!wildcardpath)
2251 return FALSE;
2252 WCHAR* wpath = ConvertUtf8ToWCharAlloc(wildcardpath, nullptr);
2253 free(wildcardpath);
2254 if (!wpath)
2255 return FALSE;
2256
2257 WIN32_FIND_DATAW FindFileData = WINPR_C_ARRAY_INIT;
2258 HANDLE hFind = FindFirstFileW(wpath, &FindFileData);
2259 free(wpath);
2260
2261 if (hFind == INVALID_HANDLE_VALUE)
2262 return FALSE;
2263
2264 BOOL rc = FALSE;
2265 char* next = nullptr;
2266
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));
2271 do
2272 {
2273 if (_wcscmp(FindFileData.cFileName, dot) == 0)
2274 continue;
2275 if (_wcscmp(FindFileData.cFileName, dotdot) == 0)
2276 continue;
2277
2278 char cFileName[MAX_PATH] = WINPR_C_ARRAY_INIT;
2279 (void)ConvertWCharNToUtf8(FindFileData.cFileName, ARRAYSIZE(FindFileData.cFileName),
2280 cFileName, ARRAYSIZE(cFileName));
2281
2282 free(next);
2283 next = GetCombinedPath(path, cFileName);
2284 if (!next)
2285 goto fail;
2286
2287 if (!append_entry(stream, next))
2288 goto fail;
2289 if (is_directory(next))
2290 {
2291 if (!add_directory(stream, next))
2292 goto fail;
2293 }
2294 } while (FindNextFileW(hFind, &FindFileData));
2295
2296 rc = TRUE;
2297fail:
2298 free(next);
2299 FindClose(hFind);
2300
2301 return rc;
2302}
2303
2304static BOOL cliprdr_local_stream_update(CliprdrLocalStream* stream, const char* data, size_t size)
2305{
2306 BOOL rc = FALSE;
2307 WINPR_ASSERT(stream);
2308 if (size == 0)
2309 return TRUE;
2310
2311 cliprdr_local_files_free(stream);
2312
2313 stream->files = calloc(size, sizeof(CliprdrLocalFile));
2314 if (!stream->files)
2315 return FALSE;
2316
2317 char* copy = strndup(data, size);
2318 if (!copy)
2319 return FALSE;
2320
2321 char* saveptr = nullptr;
2322 char* ptr = strtok_s(copy, "\r\n", &saveptr);
2323 while (ptr)
2324 {
2325 const char* name = ptr;
2326 if (strncmp("file:///", ptr, 8) == 0)
2327 name = &ptr[7];
2328 else if (strncmp("file:/", ptr, 6) == 0)
2329 name = &ptr[5];
2330
2331 if (!append_entry(stream, name))
2332 goto fail;
2333
2334 if (is_directory(name))
2335 {
2336 const BOOL res = add_directory(stream, name);
2337 if (!res)
2338 goto fail;
2339 }
2340 ptr = strtok_s(nullptr, "\r\n", &saveptr);
2341 }
2342
2343 rc = TRUE;
2344fail:
2345 free(copy);
2346 return rc;
2347}
2348
2349CliprdrLocalStream* cliprdr_local_stream_new(CliprdrFileContext* context, UINT32 streamID,
2350 const char* data, size_t size)
2351{
2352 WINPR_ASSERT(context);
2353 CliprdrLocalStream* stream = calloc(1, sizeof(CliprdrLocalStream));
2354 if (!stream)
2355 return nullptr;
2356
2357 stream->context = context;
2358 if (!cliprdr_local_stream_update(stream, data, size))
2359 goto fail;
2360
2361 stream->lockId = streamID;
2362 return stream;
2363
2364fail:
2365 cliprdr_local_stream_free(stream);
2366 return nullptr;
2367}
2368
2369static UINT32 UINTPointerHash(const void* id)
2370{
2371 WINPR_ASSERT(id);
2372 return *((const UINT32*)id);
2373}
2374
2375static BOOL UINTPointerCompare(const void* pointer1, const void* pointer2)
2376{
2377 if (!pointer1 || !pointer2)
2378 return pointer1 == pointer2;
2379
2380 const UINT32* a = pointer1;
2381 const UINT32* b = pointer2;
2382 return *a == *b;
2383}
2384
2385static void* UINTPointerClone(const void* other)
2386{
2387 const UINT32* src = other;
2388 if (!src)
2389 return nullptr;
2390
2391 UINT32* copy = calloc(1, sizeof(UINT32));
2392 if (!copy)
2393 return nullptr;
2394
2395 *copy = *src;
2396 return copy;
2397}
2398
2399#if defined(WITH_FUSE)
2400CliprdrFuseFile* fuse_file_new_root(CliprdrFileContext* file_context)
2401{
2402 CliprdrFuseFile* root_dir = fuse_file_new("/");
2403 if (!root_dir)
2404 return nullptr;
2405
2406 root_dir->ino = FUSE_ROOT_ID;
2407 root_dir->is_directory = TRUE;
2408 root_dir->is_readonly = TRUE;
2409
2410 if (!HashTable_Insert(file_context->inode_table, (void*)(uintptr_t)root_dir->ino, root_dir))
2411 {
2412 fuse_file_free(root_dir);
2413 return nullptr;
2414 }
2415
2416 if (!HashTable_Insert(file_context->dir_table, root_dir->filename_with_root, root_dir))
2417 {
2418 fuse_file_free(root_dir);
2419 return nullptr;
2420 }
2421
2422 return root_dir;
2423}
2424#endif
2425
2426CliprdrFileContext* cliprdr_file_context_new(void* context)
2427{
2428 CliprdrFileContext* file = calloc(1, sizeof(CliprdrFileContext));
2429 if (!file)
2430 return nullptr;
2431
2432 file->log = WLog_Get(CLIENT_TAG("common.cliprdr.file"));
2433 file->clipboard = context;
2434
2435 file->local_streams = HashTable_New(FALSE);
2436 if (!file->local_streams)
2437 goto fail;
2438
2439 if (!HashTable_SetHashFunction(file->local_streams, UINTPointerHash))
2440 goto fail;
2441
2442 {
2443 wObject* hkobj = HashTable_KeyObject(file->local_streams);
2444 WINPR_ASSERT(hkobj);
2445 hkobj->fnObjectEquals = UINTPointerCompare;
2446 hkobj->fnObjectFree = free;
2447 hkobj->fnObjectNew = UINTPointerClone;
2448 }
2449
2450 {
2451 wObject* hobj = HashTable_ValueObject(file->local_streams);
2452 WINPR_ASSERT(hobj);
2453 hobj->fnObjectFree = cliprdr_local_stream_free;
2454 }
2455
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)
2462 goto fail;
2463
2464 {
2465 wObject* ctobj = HashTable_ValueObject(file->request_table);
2466 WINPR_ASSERT(ctobj);
2467 ctobj->fnObjectFree = free;
2468 }
2469 {
2470 wObject* ctobj = HashTable_ValueObject(file->clip_data_table);
2471 WINPR_ASSERT(ctobj);
2472 ctobj->fnObjectFree = clip_data_entry_free;
2473 }
2474 {
2475 if (!HashTable_SetHashFunction(file->dir_table, HashTable_StringHash))
2476 goto fail;
2477 wObject* kobj = HashTable_KeyObject(file->dir_table);
2478 WINPR_ASSERT(kobj);
2479 kobj->fnObjectEquals = HashTable_StringCompare;
2480 }
2481#endif
2482
2483 if (!create_base_path(file))
2484 goto fail;
2485
2486#if defined(WITH_FUSE)
2487 if (!(file->fuse_start_sync = CreateEvent(nullptr, TRUE, FALSE, nullptr)))
2488 goto fail;
2489 if (!(file->fuse_stop_sync = CreateEvent(nullptr, TRUE, FALSE, nullptr)))
2490 goto fail;
2491 if (!(file->fuse_thread = CreateThread(nullptr, 0, cliprdr_file_fuse_thread, file, 0, nullptr)))
2492 goto fail;
2493
2494 if (WaitForSingleObject(file->fuse_start_sync, INFINITE) == WAIT_FAILED)
2495 WLog_Print(file->log, WLOG_ERROR, "Failed to wait for start sync");
2496#endif
2497 return file;
2498
2499fail:
2500 WINPR_PRAGMA_DIAG_PUSH
2501 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2502 cliprdr_file_context_free(file);
2503 WINPR_PRAGMA_DIAG_POP
2504 return nullptr;
2505}
2506
2507BOOL local_stream_discard(const void* key, void* value, void* arg)
2508{
2509 CliprdrFileContext* file = arg;
2510 CliprdrLocalStream* stream = value;
2511 WINPR_ASSERT(file);
2512 WINPR_ASSERT(stream);
2513
2514 if (!stream->locked)
2515 HashTable_Remove(file->local_streams, key);
2516 return TRUE;
2517}
2518
2519BOOL cliprdr_file_context_clear(CliprdrFileContext* file)
2520{
2521 WINPR_ASSERT(file);
2522
2523 WLog_Print(file->log, WLOG_DEBUG, "clear file clipboard...");
2524
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);
2528
2529 memset(file->server_data_hash, 0, sizeof(file->server_data_hash));
2530 memset(file->client_data_hash, 0, sizeof(file->client_data_hash));
2531 return res;
2532}
2533
2534BOOL cliprdr_file_context_update_client_data(CliprdrFileContext* file, const char* data,
2535 size_t size)
2536{
2537 BOOL rc = FALSE;
2538
2539 WINPR_ASSERT(file);
2540 if (!cliprdr_file_client_content_changed_and_update(file, data, size))
2541 return TRUE;
2542
2543 if (!cliprdr_file_context_clear(file))
2544 return FALSE;
2545
2546 UINT32 lockId = file->local_lock_id;
2547
2548 HashTable_Lock(file->local_streams);
2549 CliprdrLocalStream* stream = HashTable_GetItemValue(file->local_streams, &lockId);
2550
2551 WLog_Print(file->log, WLOG_DEBUG, "update client file list (stream=%p)...", (void*)stream);
2552 if (stream)
2553 rc = cliprdr_local_stream_update(stream, data, size);
2554 else
2555 {
2556 stream = cliprdr_local_stream_new(file, lockId, data, size);
2557 rc = HashTable_Insert(file->local_streams, &stream->lockId, stream);
2558 if (!rc)
2559 cliprdr_local_stream_free(stream);
2560 }
2561 // HashTable_Insert owns stream
2562 // NOLINTNEXTLINE(clang-analyzer-unix.Malloc)
2563 HashTable_Unlock(file->local_streams);
2564 return rc;
2565}
2566
2567UINT32 cliprdr_file_context_current_flags(CliprdrFileContext* file)
2568{
2569 WINPR_ASSERT(file);
2570
2571 if ((file->file_capability_flags & CB_STREAM_FILECLIP_ENABLED) == 0)
2572 return 0;
2573
2574 if (!file->file_formats_registered)
2575 return 0;
2576
2577 return CB_STREAM_FILECLIP_ENABLED | CB_FILECLIP_NO_FILE_PATHS |
2578 CB_HUGE_FILE_SUPPORT_ENABLED; // | CB_CAN_LOCK_CLIPDATA;
2579}
2580
2581BOOL cliprdr_file_context_set_locally_available(CliprdrFileContext* file, BOOL available)
2582{
2583 WINPR_ASSERT(file);
2584 file->file_formats_registered = available;
2585 return TRUE;
2586}
2587
2588BOOL cliprdr_file_context_remote_set_flags(CliprdrFileContext* file, UINT32 flags)
2589{
2590 WINPR_ASSERT(file);
2591 file->file_capability_flags = flags;
2592 return TRUE;
2593}
2594
2595UINT32 cliprdr_file_context_remote_get_flags(CliprdrFileContext* file)
2596{
2597 WINPR_ASSERT(file);
2598 return file->file_capability_flags;
2599}
2600
2601BOOL cliprdr_file_context_has_local_support(CliprdrFileContext* file)
2602{
2603 WINPR_UNUSED(file);
2604
2605#if defined(WITH_FUSE)
2606 return TRUE;
2607#else
2608 return FALSE;
2609#endif
2610}
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