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