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
636 if (does_server_support_clipdata_locking(file_context))
637 rc = prepare_clip_data_entry_with_id(file_context);
638 else
639 rc = prepare_clip_data_entry_without_id(file_context);
640#endif
641 return rc;
642}
643
644UINT cliprdr_file_context_notify_new_client_format_list(CliprdrFileContext* file_context)
645{
646 WINPR_ASSERT(file_context);
647 WINPR_ASSERT(file_context->context);
648
649#if defined(WITH_FUSE)
650 if (!clear_no_cdi_entry(file_context))
651 return ERROR_INTERNAL_ERROR;
652 /* TODO: assign timeouts to old locks instead */
653 return clear_cdi_entries(file_context);
654#endif
655
656 return CHANNEL_RC_OK;
657}
658
659static CliprdrLocalStream* cliprdr_local_stream_new(CliprdrFileContext* context, UINT32 streamID,
660 const char* data, size_t size);
661static void cliprdr_file_session_terminate(CliprdrFileContext* file, BOOL stop_thread);
662static BOOL local_stream_discard(const void* key, void* value, void* arg);
663
664WINPR_ATTR_FORMAT_ARG(6, 7)
665static void writelog(wLog* log, DWORD level, const char* fname, const char* fkt, size_t line,
666 WINPR_FORMAT_ARG const char* fmt, ...)
667{
668 if (!WLog_IsLevelActive(log, level))
669 return;
670
671 va_list ap = WINPR_C_ARRAY_INIT;
672 va_start(ap, fmt);
673 WLog_PrintTextMessageVA(log, level, line, fname, fkt, fmt, ap);
674 va_end(ap);
675}
676
677#if defined(WITH_FUSE)
678static CliprdrFuseFile* fuse_file_new_root(CliprdrFileContext* file_context);
679static void cliprdr_file_fuse_lookup(fuse_req_t req, fuse_ino_t parent, const char* name);
680static void cliprdr_file_fuse_getattr(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info* fi);
681static void cliprdr_file_fuse_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off,
682 struct fuse_file_info* fi);
683static void cliprdr_file_fuse_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off,
684 struct fuse_file_info* fi);
685static void cliprdr_file_fuse_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info* fi);
686static void cliprdr_file_fuse_opendir(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info* fi);
687
688static const struct fuse_lowlevel_ops cliprdr_file_fuse_oper = {
689 .lookup = cliprdr_file_fuse_lookup,
690 .getattr = cliprdr_file_fuse_getattr,
691 .readdir = cliprdr_file_fuse_readdir,
692 .open = cliprdr_file_fuse_open,
693 .read = cliprdr_file_fuse_read,
694 .opendir = cliprdr_file_fuse_opendir,
695};
696
697CliprdrFuseFile* get_fuse_file_by_ino(CliprdrFileContext* file_context, fuse_ino_t fuse_ino)
698{
699 WINPR_ASSERT(file_context);
700
701 return HashTable_GetItemValue(file_context->inode_table, (void*)(uintptr_t)fuse_ino);
702}
703
704static CliprdrFuseFile*
705get_fuse_file_by_name_from_parent(WINPR_ATTR_UNUSED CliprdrFileContext* file_context,
706 CliprdrFuseFile* parent, const char* name)
707{
708 WINPR_ASSERT(file_context);
709 WINPR_ASSERT(parent);
710
711 for (size_t i = 0; i < ArrayList_Count(parent->children); ++i)
712 {
713 CliprdrFuseFile* child = ArrayList_GetItem(parent->children, i);
714
715 WINPR_ASSERT(child);
716
717 if (strncmp(name, child->filename, child->filename_len + 1) == 0)
718 return child;
719 }
720
721 DEBUG_CLIPRDR(file_context->log, "Requested file \"%s\" in directory \"%s\" does not exist",
722 name, parent->filename);
723
724 return nullptr;
725}
726
727static CliprdrFuseRequest* cliprdr_fuse_request_new(CliprdrFileContext* file_context,
728 CliprdrFuseFile* fuse_file, fuse_req_t fuse_req,
729 FuseLowlevelOperationType operation_type)
730{
731 CliprdrFuseRequest* fuse_request = nullptr;
732 UINT32 stream_id = file_context->next_stream_id;
733
734 WINPR_ASSERT(file_context);
735 WINPR_ASSERT(fuse_file);
736
737 fuse_request = calloc(1, sizeof(CliprdrFuseRequest));
738 if (!fuse_request)
739 {
740 WLog_Print(file_context->log, WLOG_ERROR, "Failed to allocate FUSE request for file \"%s\"",
741 fuse_file->filename_with_root);
742 return nullptr;
743 }
744
745 fuse_request->fuse_file = fuse_file;
746 fuse_request->fuse_req = fuse_req;
747 fuse_request->operation_type = operation_type;
748
749 while (stream_id == 0 ||
750 HashTable_GetItemValue(file_context->request_table, (void*)(uintptr_t)stream_id))
751 ++stream_id;
752 fuse_request->stream_id = stream_id;
753
754 file_context->next_stream_id = stream_id + 1;
755
756 if (!HashTable_Insert(file_context->request_table, (void*)(uintptr_t)fuse_request->stream_id,
757 fuse_request))
758 {
759 WLog_Print(file_context->log, WLOG_ERROR, "Failed to track FUSE request for file \"%s\"",
760 fuse_file->filename_with_root);
761 free(fuse_request);
762 return nullptr;
763 }
764
765 return fuse_request;
766}
767
768static BOOL request_file_size_async(CliprdrFileContext* file_context, CliprdrFuseFile* fuse_file,
769 fuse_req_t fuse_req, FuseLowlevelOperationType operation_type)
770{
771 CliprdrFuseRequest* fuse_request = nullptr;
772 CLIPRDR_FILE_CONTENTS_REQUEST file_contents_request = WINPR_C_ARRAY_INIT;
773
774 WINPR_ASSERT(file_context);
775 WINPR_ASSERT(fuse_file);
776
777 fuse_request = cliprdr_fuse_request_new(file_context, fuse_file, fuse_req, operation_type);
778 if (!fuse_request)
779 return FALSE;
780
781 file_contents_request.common.msgType = CB_FILECONTENTS_REQUEST;
782 file_contents_request.streamId = fuse_request->stream_id;
783 file_contents_request.listIndex = fuse_file->list_idx;
784 file_contents_request.dwFlags = FILECONTENTS_SIZE;
785 file_contents_request.cbRequested = 0x8;
786 file_contents_request.haveClipDataId = fuse_file->has_clip_data_id;
787 file_contents_request.clipDataId = fuse_file->clip_data_id;
788
789 if (file_context->context->ClientFileContentsRequest(file_context->context,
790 &file_contents_request))
791 {
792 WLog_Print(file_context->log, WLOG_ERROR,
793 "Failed to send FileContentsRequest for file \"%s\"",
794 fuse_file->filename_with_root);
795 HashTable_Remove(file_context->request_table, (void*)(uintptr_t)fuse_request->stream_id);
796 return FALSE;
797 }
798 // NOLINTNEXTLINE(clang-analyzer-unix.Malloc)
799 DEBUG_CLIPRDR(file_context->log, "Requested file size for file \"%s\" with stream id %u",
800 fuse_file->filename, fuse_request->stream_id);
801
802 // NOLINTNEXTLINE(clang-analyzer-unix.Malloc)
803 return TRUE;
804}
805
806static void write_file_attributes(CliprdrFuseFile* fuse_file, struct stat* attr)
807{
808 memset(attr, 0, sizeof(struct stat));
809
810 if (!fuse_file)
811 return;
812
813 attr->st_ino = fuse_file->ino;
814 if (fuse_file->is_directory)
815 {
816 attr->st_mode = S_IFDIR | (fuse_file->is_readonly ? 0555 : 0755);
817 attr->st_nlink = 2;
818 }
819 else
820 {
821 attr->st_mode = S_IFREG | (fuse_file->is_readonly ? 0444 : 0644);
822 attr->st_nlink = 1;
823 attr->st_size = WINPR_ASSERTING_INT_CAST(off_t, fuse_file->size);
824 }
825 attr->st_uid = getuid();
826 attr->st_gid = getgid();
827 attr->st_atime = attr->st_mtime = attr->st_ctime =
828 (fuse_file->has_last_write_time ? fuse_file->last_write_time_unix : time(nullptr));
829}
830
831static void cliprdr_file_fuse_lookup(fuse_req_t fuse_req, fuse_ino_t parent_ino, const char* name)
832{
833 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
834 CliprdrFuseFile* parent = nullptr;
835 CliprdrFuseFile* fuse_file = nullptr;
836 struct fuse_entry_param entry = WINPR_C_ARRAY_INIT;
837
838 WINPR_ASSERT(file_context);
839
840 HashTable_Lock(file_context->inode_table);
841 if (!(parent = get_fuse_file_by_ino(file_context, parent_ino)) || !parent->is_directory)
842 {
843 HashTable_Unlock(file_context->inode_table);
844 fuse_reply_err(fuse_req, ENOENT);
845 return;
846 }
847 if (!(fuse_file = get_fuse_file_by_name_from_parent(file_context, parent, name)))
848 {
849 HashTable_Unlock(file_context->inode_table);
850 fuse_reply_err(fuse_req, ENOENT);
851 return;
852 }
853
854 DEBUG_CLIPRDR(file_context->log, "lookup() has been called for \"%s\"", name);
855 DEBUG_CLIPRDR(file_context->log, "Parent is \"%s\", child is \"%s\"",
856 parent->filename_with_root, fuse_file->filename_with_root);
857
858 if (!fuse_file->is_directory && !fuse_file->has_size)
859 {
860 BOOL result = 0;
861
862 result =
863 request_file_size_async(file_context, fuse_file, fuse_req, FUSE_LL_OPERATION_LOOKUP);
864 HashTable_Unlock(file_context->inode_table);
865
866 if (!result)
867 fuse_reply_err(fuse_req, EIO);
868
869 return;
870 }
871
872 entry.ino = fuse_file->ino;
873 write_file_attributes(fuse_file, &entry.attr);
874 entry.attr_timeout = 1.0;
875 entry.entry_timeout = 1.0;
876 HashTable_Unlock(file_context->inode_table);
877
878 fuse_reply_entry(fuse_req, &entry);
879}
880
881static void cliprdr_file_fuse_getattr(fuse_req_t fuse_req, fuse_ino_t fuse_ino,
882 WINPR_ATTR_UNUSED struct fuse_file_info* file_info)
883{
884 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
885 CliprdrFuseFile* fuse_file = nullptr;
886 struct stat attr = WINPR_C_ARRAY_INIT;
887
888 WINPR_ASSERT(file_context);
889
890 HashTable_Lock(file_context->inode_table);
891 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
892 {
893 HashTable_Unlock(file_context->inode_table);
894 fuse_reply_err(fuse_req, ENOENT);
895 return;
896 }
897
898 DEBUG_CLIPRDR(file_context->log, "getattr() has been called for file \"%s\"",
899 fuse_file->filename_with_root);
900
901 if (!fuse_file->is_directory && !fuse_file->has_size)
902 {
903 BOOL result = 0;
904
905 result =
906 request_file_size_async(file_context, fuse_file, fuse_req, FUSE_LL_OPERATION_GETATTR);
907 HashTable_Unlock(file_context->inode_table);
908
909 if (!result)
910 fuse_reply_err(fuse_req, EIO);
911
912 return;
913 }
914
915 write_file_attributes(fuse_file, &attr);
916 HashTable_Unlock(file_context->inode_table);
917
918 fuse_reply_attr(fuse_req, &attr, 1.0);
919}
920
921static void cliprdr_file_fuse_open(fuse_req_t fuse_req, fuse_ino_t fuse_ino,
922 struct fuse_file_info* file_info)
923{
924 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
925 CliprdrFuseFile* fuse_file = nullptr;
926
927 WINPR_ASSERT(file_context);
928
929 HashTable_Lock(file_context->inode_table);
930 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
931 {
932 HashTable_Unlock(file_context->inode_table);
933 fuse_reply_err(fuse_req, ENOENT);
934 return;
935 }
936 if (fuse_file->is_directory)
937 {
938 HashTable_Unlock(file_context->inode_table);
939 fuse_reply_err(fuse_req, EISDIR);
940 return;
941 }
942 HashTable_Unlock(file_context->inode_table);
943
944 if ((file_info->flags & O_ACCMODE) != O_RDONLY)
945 {
946 fuse_reply_err(fuse_req, EACCES);
947 return;
948 }
949
950 /* Important for KDE to get file correctly */
951 file_info->direct_io = 1;
952
953 fuse_reply_open(fuse_req, file_info);
954}
955
956static BOOL request_file_range_async(CliprdrFileContext* file_context, CliprdrFuseFile* fuse_file,
957 fuse_req_t fuse_req, off_t offset, size_t requested_size)
958{
959 CLIPRDR_FILE_CONTENTS_REQUEST file_contents_request = WINPR_C_ARRAY_INIT;
960
961 WINPR_ASSERT(file_context);
962 WINPR_ASSERT(fuse_file);
963
964 if (requested_size > UINT32_MAX)
965 return FALSE;
966
967 CliprdrFuseRequest* fuse_request =
968 cliprdr_fuse_request_new(file_context, fuse_file, fuse_req, FUSE_LL_OPERATION_READ);
969 if (!fuse_request)
970 return FALSE;
971
972 file_contents_request.common.msgType = CB_FILECONTENTS_REQUEST;
973 file_contents_request.streamId = fuse_request->stream_id;
974 file_contents_request.listIndex = fuse_file->list_idx;
975 file_contents_request.dwFlags = FILECONTENTS_RANGE;
976 file_contents_request.nPositionLow = (UINT32)(offset & 0xFFFFFFFF);
977 file_contents_request.nPositionHigh = (UINT32)((offset >> 32) & 0xFFFFFFFF);
978 file_contents_request.cbRequested = (UINT32)requested_size;
979 file_contents_request.haveClipDataId = fuse_file->has_clip_data_id;
980 file_contents_request.clipDataId = fuse_file->clip_data_id;
981
982 if (file_context->context->ClientFileContentsRequest(file_context->context,
983 &file_contents_request))
984 {
985 WLog_Print(file_context->log, WLOG_ERROR,
986 "Failed to send FileContentsRequest for file \"%s\"",
987 fuse_file->filename_with_root);
988 HashTable_Remove(file_context->request_table, (void*)(uintptr_t)fuse_request->stream_id);
989 return FALSE;
990 }
991
992 // file_context->request_table owns fuse_request
993 // NOLINTBEGIN(clang-analyzer-unix.Malloc)
994 DEBUG_CLIPRDR(
995 file_context->log,
996 "Requested file range (%zu Bytes at offset %lu) for file \"%s\" with stream id %u",
997 requested_size, offset, fuse_file->filename, fuse_request->stream_id);
998
999 return TRUE;
1000 // NOLINTEND(clang-analyzer-unix.Malloc)
1001}
1002
1003static void cliprdr_file_fuse_read(fuse_req_t fuse_req, fuse_ino_t fuse_ino, size_t size,
1004 off_t offset, WINPR_ATTR_UNUSED struct fuse_file_info* file_info)
1005{
1006 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
1007 CliprdrFuseFile* fuse_file = nullptr;
1008 BOOL result = 0;
1009
1010 WINPR_ASSERT(file_context);
1011
1012 HashTable_Lock(file_context->inode_table);
1013 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
1014 {
1015 HashTable_Unlock(file_context->inode_table);
1016 fuse_reply_err(fuse_req, ENOENT);
1017 return;
1018 }
1019 if (fuse_file->is_directory)
1020 {
1021 HashTable_Unlock(file_context->inode_table);
1022 fuse_reply_err(fuse_req, EISDIR);
1023 return;
1024 }
1025 if (!fuse_file->has_size || (offset < 0) || ((size_t)offset > fuse_file->size))
1026 {
1027 HashTable_Unlock(file_context->inode_table);
1028 fuse_reply_err(fuse_req, EINVAL);
1029 return;
1030 }
1031
1032 size = MIN(size, 8ULL * 1024ULL * 1024ULL);
1033
1034 result = request_file_range_async(file_context, fuse_file, fuse_req, offset, size);
1035 HashTable_Unlock(file_context->inode_table);
1036
1037 if (!result)
1038 fuse_reply_err(fuse_req, EIO);
1039}
1040
1041static void cliprdr_file_fuse_opendir(fuse_req_t fuse_req, fuse_ino_t fuse_ino,
1042 struct fuse_file_info* file_info)
1043{
1044 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
1045 CliprdrFuseFile* fuse_file = nullptr;
1046
1047 WINPR_ASSERT(file_context);
1048
1049 HashTable_Lock(file_context->inode_table);
1050 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
1051 {
1052 HashTable_Unlock(file_context->inode_table);
1053 fuse_reply_err(fuse_req, ENOENT);
1054 return;
1055 }
1056 if (!fuse_file->is_directory)
1057 {
1058 HashTable_Unlock(file_context->inode_table);
1059 fuse_reply_err(fuse_req, ENOTDIR);
1060 return;
1061 }
1062 HashTable_Unlock(file_context->inode_table);
1063
1064 if ((file_info->flags & O_ACCMODE) != O_RDONLY)
1065 {
1066 fuse_reply_err(fuse_req, EACCES);
1067 return;
1068 }
1069
1070 fuse_reply_open(fuse_req, file_info);
1071}
1072
1073static void cliprdr_file_fuse_readdir(fuse_req_t fuse_req, fuse_ino_t fuse_ino, size_t max_size,
1074 off_t offset,
1075 WINPR_ATTR_UNUSED struct fuse_file_info* file_info)
1076{
1077 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
1078 CliprdrFuseFile* fuse_file = nullptr;
1079 CliprdrFuseFile* child = nullptr;
1080 struct stat attr = WINPR_C_ARRAY_INIT;
1081 size_t written_size = 0;
1082 size_t entry_size = 0;
1083 char* filename = nullptr;
1084 char* buf = nullptr;
1085
1086 WINPR_ASSERT(file_context);
1087
1088 HashTable_Lock(file_context->inode_table);
1089 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
1090 {
1091 HashTable_Unlock(file_context->inode_table);
1092 fuse_reply_err(fuse_req, ENOENT);
1093 return;
1094 }
1095 if (!fuse_file->is_directory)
1096 {
1097 HashTable_Unlock(file_context->inode_table);
1098 fuse_reply_err(fuse_req, ENOTDIR);
1099 return;
1100 }
1101
1102 DEBUG_CLIPRDR(file_context->log, "Reading directory \"%s\" at offset %lu",
1103 fuse_file->filename_with_root, offset);
1104
1105 if ((offset < 0) || ((size_t)offset >= ArrayList_Count(fuse_file->children)))
1106 {
1107 HashTable_Unlock(file_context->inode_table);
1108 fuse_reply_buf(fuse_req, nullptr, 0);
1109 return;
1110 }
1111
1112 buf = calloc(max_size, sizeof(char));
1113 if (!buf)
1114 {
1115 HashTable_Unlock(file_context->inode_table);
1116 fuse_reply_err(fuse_req, ENOMEM);
1117 return;
1118 }
1119 written_size = 0;
1120
1121 for (off_t i = offset; i < 2; ++i)
1122 {
1123 if (i == 0)
1124 {
1125 write_file_attributes(fuse_file, &attr);
1126 filename = ".";
1127 }
1128 else if (i == 1)
1129 {
1130 write_file_attributes(fuse_file->parent, &attr);
1131 attr.st_ino = fuse_file->parent ? attr.st_ino : FUSE_ROOT_ID;
1132 attr.st_mode = fuse_file->parent ? attr.st_mode : 0555;
1133 filename = "..";
1134 }
1135 else
1136 {
1137 WINPR_ASSERT(FALSE);
1138 }
1139
1144 entry_size = fuse_add_direntry(fuse_req, buf + written_size, max_size - written_size,
1145 filename, &attr, i + 1);
1146 if (entry_size > max_size - written_size)
1147 break;
1148
1149 written_size += entry_size;
1150 }
1151
1152 for (size_t j = 0, i = 2; j < ArrayList_Count(fuse_file->children); ++j, ++i)
1153 {
1154 if (i < (size_t)offset)
1155 continue;
1156
1157 child = ArrayList_GetItem(fuse_file->children, j);
1158
1159 write_file_attributes(child, &attr);
1160 entry_size =
1161 fuse_add_direntry(fuse_req, buf + written_size, max_size - written_size,
1162 child->filename, &attr, WINPR_ASSERTING_INT_CAST(off_t, i + 1));
1163 if (entry_size > max_size - written_size)
1164 break;
1165
1166 written_size += entry_size;
1167 }
1168 HashTable_Unlock(file_context->inode_table);
1169
1170 fuse_reply_buf(fuse_req, buf, written_size);
1171 free(buf);
1172}
1173
1174static void fuse_abort(int sig, const char* signame, void* context)
1175{
1176 CliprdrFileContext* file = (CliprdrFileContext*)context;
1177
1178 if (file)
1179 {
1180 WLog_Print(file->log, WLOG_INFO, "signal %s [%d] aborting session", signame, sig);
1181 cliprdr_file_session_terminate(file, FALSE);
1182 }
1183}
1184
1185static DWORD WINAPI cliprdr_file_fuse_thread(LPVOID arg)
1186{
1187 CliprdrFileContext* file = (CliprdrFileContext*)arg;
1188
1189 WINPR_ASSERT(file);
1190
1191 DEBUG_CLIPRDR(file->log, "Starting fuse with mountpoint '%s'", file->path);
1192
1193 struct fuse_args args = FUSE_ARGS_INIT(0, nullptr);
1194 fuse_opt_add_arg(&args, file->path);
1195 file->fuse_sess = fuse_session_new(&args, &cliprdr_file_fuse_oper,
1196 sizeof(cliprdr_file_fuse_oper), (void*)file);
1197 (void)SetEvent(file->fuse_start_sync);
1198
1199 if (file->fuse_sess != nullptr)
1200 {
1201 if (freerdp_add_signal_cleanup_handler(file, fuse_abort))
1202 {
1203 if (0 == fuse_session_mount(file->fuse_sess, file->path))
1204 {
1205 fuse_session_loop(file->fuse_sess);
1206 fuse_session_unmount(file->fuse_sess);
1207 }
1208 freerdp_del_signal_cleanup_handler(file, fuse_abort);
1209 }
1210
1211 WLog_Print(file->log, WLOG_DEBUG, "Waiting for FUSE stop sync");
1212 if (WaitForSingleObject(file->fuse_stop_sync, INFINITE) == WAIT_FAILED)
1213 WLog_Print(file->log, WLOG_ERROR, "Failed to wait for stop sync");
1214 fuse_session_destroy(file->fuse_sess);
1215 }
1216 fuse_opt_free_args(&args);
1217
1218 DEBUG_CLIPRDR(file->log, "Quitting fuse with mountpoint '%s'", file->path);
1219
1220 ExitThread(0);
1221 return 0;
1222}
1223
1224static UINT cliprdr_file_context_server_file_contents_response(
1225 CliprdrClientContext* cliprdr_context,
1226 const CLIPRDR_FILE_CONTENTS_RESPONSE* file_contents_response)
1227{
1228 CliprdrFileContext* file_context = nullptr;
1229 CliprdrFuseRequest* fuse_request = nullptr;
1230 struct fuse_entry_param entry = WINPR_C_ARRAY_INIT;
1231
1232 WINPR_ASSERT(cliprdr_context);
1233 WINPR_ASSERT(file_contents_response);
1234
1235 file_context = cliprdr_context->custom;
1236 WINPR_ASSERT(file_context);
1237
1238 HashTable_Lock(file_context->inode_table);
1239 fuse_request = HashTable_GetItemValue(file_context->request_table,
1240 (void*)(uintptr_t)file_contents_response->streamId);
1241 if (!fuse_request)
1242 {
1243 HashTable_Unlock(file_context->inode_table);
1244 return CHANNEL_RC_OK;
1245 }
1246
1247 if (!(file_contents_response->common.msgFlags & CB_RESPONSE_OK))
1248 {
1249 WLog_Print(file_context->log, WLOG_WARN,
1250 "FileContentsRequests for file \"%s\" was unsuccessful",
1251 fuse_request->fuse_file->filename);
1252
1253 fuse_reply_err(fuse_request->fuse_req, EIO);
1254 HashTable_Remove(file_context->request_table,
1255 (void*)(uintptr_t)file_contents_response->streamId);
1256 HashTable_Unlock(file_context->inode_table);
1257 return CHANNEL_RC_OK;
1258 }
1259
1260 if ((fuse_request->operation_type == FUSE_LL_OPERATION_LOOKUP ||
1261 fuse_request->operation_type == FUSE_LL_OPERATION_GETATTR) &&
1262 file_contents_response->cbRequested != sizeof(UINT64))
1263 {
1264 WLog_Print(file_context->log, WLOG_WARN,
1265 "Received invalid file size for file \"%s\" from the client",
1266 fuse_request->fuse_file->filename);
1267 fuse_reply_err(fuse_request->fuse_req, EIO);
1268 HashTable_Remove(file_context->request_table,
1269 (void*)(uintptr_t)file_contents_response->streamId);
1270 HashTable_Unlock(file_context->inode_table);
1271 return CHANNEL_RC_OK;
1272 }
1273
1274 if (fuse_request->operation_type == FUSE_LL_OPERATION_LOOKUP ||
1275 fuse_request->operation_type == FUSE_LL_OPERATION_GETATTR)
1276 {
1277 DEBUG_CLIPRDR(file_context->log, "Received file size for file \"%s\" with stream id %u",
1278 fuse_request->fuse_file->filename, file_contents_response->streamId);
1279
1280 fuse_request->fuse_file->size = *((const UINT64*)file_contents_response->requestedData);
1281 fuse_request->fuse_file->has_size = TRUE;
1282
1283 entry.ino = fuse_request->fuse_file->ino;
1284 write_file_attributes(fuse_request->fuse_file, &entry.attr);
1285 entry.attr_timeout = 1.0;
1286 entry.entry_timeout = 1.0;
1287 }
1288 else if (fuse_request->operation_type == FUSE_LL_OPERATION_READ)
1289 {
1290 DEBUG_CLIPRDR(file_context->log, "Received file range for file \"%s\" with stream id %u",
1291 fuse_request->fuse_file->filename, file_contents_response->streamId);
1292 }
1293 HashTable_Unlock(file_context->inode_table);
1294
1295 switch (fuse_request->operation_type)
1296 {
1297 case FUSE_LL_OPERATION_NONE:
1298 break;
1299 case FUSE_LL_OPERATION_LOOKUP:
1300 fuse_reply_entry(fuse_request->fuse_req, &entry);
1301 break;
1302 case FUSE_LL_OPERATION_GETATTR:
1303 fuse_reply_attr(fuse_request->fuse_req, &entry.attr, entry.attr_timeout);
1304 break;
1305 case FUSE_LL_OPERATION_READ:
1306 fuse_reply_buf(fuse_request->fuse_req,
1307 (const char*)file_contents_response->requestedData,
1308 file_contents_response->cbRequested);
1309 break;
1310 default:
1311 break;
1312 }
1313
1314 HashTable_Remove(file_context->request_table,
1315 (void*)(uintptr_t)file_contents_response->streamId);
1316
1317 return CHANNEL_RC_OK;
1318}
1319#endif
1320
1321static UINT cliprdr_file_context_send_file_contents_failure(
1322 CliprdrFileContext* file, const CLIPRDR_FILE_CONTENTS_REQUEST* fileContentsRequest)
1323{
1324 CLIPRDR_FILE_CONTENTS_RESPONSE response = WINPR_C_ARRAY_INIT;
1325
1326 WINPR_ASSERT(file);
1327 WINPR_ASSERT(fileContentsRequest);
1328
1329 const UINT64 offset = (((UINT64)fileContentsRequest->nPositionHigh) << 32) |
1330 ((UINT64)fileContentsRequest->nPositionLow);
1331 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1332 "server file contents request [lockID %" PRIu32 ", streamID %" PRIu32
1333 ", index %" PRIu32 "] offset %" PRIu64 ", size %" PRIu32 " failed",
1334 fileContentsRequest->clipDataId, fileContentsRequest->streamId,
1335 fileContentsRequest->listIndex, offset, fileContentsRequest->cbRequested);
1336
1337 response.common.msgFlags = CB_RESPONSE_FAIL;
1338 response.streamId = fileContentsRequest->streamId;
1339
1340 WINPR_ASSERT(file->context);
1341 WINPR_ASSERT(file->context->ClientFileContentsResponse);
1342 return file->context->ClientFileContentsResponse(file->context, &response);
1343}
1344
1345static UINT
1346cliprdr_file_context_send_contents_response(CliprdrFileContext* file,
1347 const CLIPRDR_FILE_CONTENTS_REQUEST* request,
1348 const void* data, size_t size)
1349{
1350 if (size > UINT32_MAX)
1351 return ERROR_INVALID_PARAMETER;
1352
1353 CLIPRDR_FILE_CONTENTS_RESPONSE response = { .streamId = request->streamId,
1354 .requestedData = data,
1355 .cbRequested = (UINT32)size,
1356 .common.msgFlags = CB_RESPONSE_OK };
1357
1358 WINPR_ASSERT(request);
1359 WINPR_ASSERT(file);
1360
1361 WLog_Print(file->log, WLOG_DEBUG, "send contents response streamID=%" PRIu32 ", size=%" PRIu32,
1362 response.streamId, response.cbRequested);
1363 WINPR_ASSERT(file->context);
1364 WINPR_ASSERT(file->context->ClientFileContentsResponse);
1365 return file->context->ClientFileContentsResponse(file->context, &response);
1366}
1367
1368static BOOL dump_streams(const void* key, void* value, WINPR_ATTR_UNUSED void* arg)
1369{
1370 const UINT32* ukey = key;
1371 CliprdrLocalStream* cur = value;
1372
1373 writelog(cur->context->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1374 "[key %" PRIu32 "] lockID %" PRIu32 ", count %" PRIuz ", locked %d", *ukey,
1375 cur->lockId, cur->count, cur->locked);
1376 for (size_t x = 0; x < cur->count; x++)
1377 {
1378 const CliprdrLocalFile* file = &cur->files[x];
1379 writelog(cur->context->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1380 "file [%" PRIuz "] %s: %" PRId64, x, file->name, file->size);
1381 }
1382 return TRUE;
1383}
1384
1385static CliprdrLocalFile* file_info_for_request(CliprdrFileContext* file, UINT32 lockId,
1386 UINT32 listIndex)
1387{
1388 WINPR_ASSERT(file);
1389
1390 CliprdrLocalStream* cur = HashTable_GetItemValue(file->local_streams, &lockId);
1391 if (cur)
1392 {
1393 if (listIndex < cur->count)
1394 {
1395 CliprdrLocalFile* f = &cur->files[listIndex];
1396 return f;
1397 }
1398 else
1399 {
1400 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1401 "invalid entry index for lockID %" PRIu32 ", index %" PRIu32 " [count %" PRIuz
1402 "] [locked %d]",
1403 lockId, listIndex, cur->count, cur->locked);
1404 }
1405 }
1406 else
1407 {
1408 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1409 "missing entry for lockID %" PRIu32 ", index %" PRIu32, lockId, listIndex);
1410 (void)HashTable_Foreach(file->local_streams, dump_streams, file);
1411 }
1412
1413 return nullptr;
1414}
1415
1416static CliprdrLocalFile* file_for_request(CliprdrFileContext* file, UINT32 lockId, UINT32 listIndex)
1417{
1418 CliprdrLocalFile* f = file_info_for_request(file, lockId, listIndex);
1419 if (f)
1420 {
1421 if (!f->fp)
1422 {
1423 const char* name = f->name;
1424 f->fp = winpr_fopen(name, "rb");
1425 }
1426 if (!f->fp)
1427 {
1428 char ebuffer[256] = WINPR_C_ARRAY_INIT;
1429 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1430 "[lockID %" PRIu32 ", index %" PRIu32
1431 "] failed to open file '%s' [size %" PRId64 "] %s [%d]",
1432 lockId, listIndex, f->name, f->size,
1433 winpr_strerror(errno, ebuffer, sizeof(ebuffer)), errno);
1434 return nullptr;
1435 }
1436 }
1437
1438 return f;
1439}
1440
1441static void cliprdr_local_file_try_close(CliprdrLocalFile* file, UINT res, UINT64 offset,
1442 UINT64 size)
1443{
1444 WINPR_ASSERT(file);
1445
1446 if (res != 0)
1447 {
1448 WINPR_ASSERT(file->context);
1449 WLog_Print(file->context->log, WLOG_DEBUG, "closing file %s after error %" PRIu32,
1450 file->name, res);
1451 }
1452 else if (((file->size > 0) && (offset + size >= (UINT64)file->size)))
1453 {
1454 WINPR_ASSERT(file->context);
1455 WLog_Print(file->context->log, WLOG_DEBUG, "closing file %s after read", file->name);
1456 }
1457 else
1458 {
1459 // TODO: we need to keep track of open files to avoid running out of file descriptors
1460 // TODO: for the time being just close again.
1461 }
1462 if (file->fp)
1463 (void)fclose(file->fp);
1464 file->fp = nullptr;
1465}
1466
1467static UINT cliprdr_file_context_server_file_size_request(
1468 CliprdrFileContext* file, const CLIPRDR_FILE_CONTENTS_REQUEST* fileContentsRequest)
1469{
1470 WINPR_ASSERT(fileContentsRequest);
1471
1472 if (fileContentsRequest->cbRequested != sizeof(UINT64))
1473 {
1474 WLog_Print(file->log, WLOG_WARN, "unexpected FILECONTENTS_SIZE request: %" PRIu32 " bytes",
1475 fileContentsRequest->cbRequested);
1476 }
1477
1478 HashTable_Lock(file->local_streams);
1479
1480 UINT res = CHANNEL_RC_OK;
1481 CliprdrLocalFile* rfile =
1482 file_for_request(file, fileContentsRequest->clipDataId, fileContentsRequest->listIndex);
1483 if (!rfile)
1484 res = cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1485 else
1486 {
1487 if (_fseeki64(rfile->fp, 0, SEEK_END) < 0)
1488 res = cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1489 else
1490 {
1491 const INT64 size = _ftelli64(rfile->fp);
1492 rfile->size = size;
1493 cliprdr_local_file_try_close(rfile, res, 0, 0);
1494
1495 res = cliprdr_file_context_send_contents_response(file, fileContentsRequest, &size,
1496 sizeof(size));
1497 }
1498 }
1499
1500 HashTable_Unlock(file->local_streams);
1501 return res;
1502}
1503
1504static UINT cliprdr_file_context_server_file_range_request(
1505 CliprdrFileContext* file, const CLIPRDR_FILE_CONTENTS_REQUEST* fileContentsRequest)
1506{
1507 BYTE* data = nullptr;
1508
1509 WINPR_ASSERT(fileContentsRequest);
1510
1511 HashTable_Lock(file->local_streams);
1512 const UINT64 offset = (((UINT64)fileContentsRequest->nPositionHigh) << 32) |
1513 ((UINT64)fileContentsRequest->nPositionLow);
1514
1515 CliprdrLocalFile* rfile =
1516 file_for_request(file, fileContentsRequest->clipDataId, fileContentsRequest->listIndex);
1517 if (!rfile)
1518 goto fail;
1519
1520 if (_fseeki64(rfile->fp, WINPR_ASSERTING_INT_CAST(int64_t, offset), SEEK_SET) < 0)
1521 goto fail;
1522
1523 data = malloc(fileContentsRequest->cbRequested);
1524 if (!data)
1525 goto fail;
1526
1527 {
1528 const size_t r = fread(data, 1, fileContentsRequest->cbRequested, rfile->fp);
1529 const UINT rc =
1530 cliprdr_file_context_send_contents_response(file, fileContentsRequest, data, r);
1531 free(data);
1532
1533 cliprdr_local_file_try_close(rfile, rc, offset, fileContentsRequest->cbRequested);
1534 HashTable_Unlock(file->local_streams);
1535 return rc;
1536 }
1537fail:
1538 if (rfile)
1539 cliprdr_local_file_try_close(rfile, ERROR_INTERNAL_ERROR, offset,
1540 fileContentsRequest->cbRequested);
1541 free(data);
1542 HashTable_Unlock(file->local_streams);
1543 return cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1544}
1545
1546static void cliprdr_local_stream_free(void* obj);
1547
1548static UINT change_lock(CliprdrFileContext* file, UINT32 lockId, BOOL lock)
1549{
1550 UINT rc = CHANNEL_RC_OK;
1551
1552 WINPR_ASSERT(file);
1553
1554 HashTable_Lock(file->local_streams);
1555
1556 {
1557 CliprdrLocalStream* stream = HashTable_GetItemValue(file->local_streams, &lockId);
1558 if (lock && !stream)
1559 {
1560 stream = cliprdr_local_stream_new(file, lockId, nullptr, 0);
1561 if (!HashTable_Insert(file->local_streams, &lockId, stream))
1562 {
1563 rc = ERROR_INTERNAL_ERROR;
1564 cliprdr_local_stream_free(stream);
1565 stream = nullptr;
1566 }
1567 file->local_lock_id = lockId;
1568 }
1569 if (stream)
1570 {
1571 stream->locked = lock;
1572 stream->lockId = lockId;
1573 }
1574 }
1575
1576 // NOLINTNEXTLINE(clang-analyzer-unix.Malloc): HashTable_Insert ownership stream
1577 if (!lock)
1578 {
1579 if (!HashTable_Foreach(file->local_streams, local_stream_discard, file))
1580 rc = ERROR_INTERNAL_ERROR;
1581 }
1582
1583 HashTable_Unlock(file->local_streams);
1584 return rc;
1585}
1586
1587static UINT cliprdr_file_context_lock(CliprdrClientContext* context,
1588 const CLIPRDR_LOCK_CLIPBOARD_DATA* lockClipboardData)
1589{
1590 WINPR_ASSERT(context);
1591 WINPR_ASSERT(lockClipboardData);
1592 CliprdrFileContext* file = (context->custom);
1593 return change_lock(file, lockClipboardData->clipDataId, TRUE);
1594}
1595
1596static UINT cliprdr_file_context_unlock(CliprdrClientContext* context,
1597 const CLIPRDR_UNLOCK_CLIPBOARD_DATA* unlockClipboardData)
1598{
1599 WINPR_ASSERT(context);
1600 WINPR_ASSERT(unlockClipboardData);
1601 CliprdrFileContext* file = (context->custom);
1602 return change_lock(file, unlockClipboardData->clipDataId, FALSE);
1603}
1604
1605static UINT cliprdr_file_context_server_file_contents_request(
1606 CliprdrClientContext* context, const CLIPRDR_FILE_CONTENTS_REQUEST* fileContentsRequest)
1607{
1608 UINT error = NO_ERROR;
1609
1610 WINPR_ASSERT(context);
1611 WINPR_ASSERT(fileContentsRequest);
1612
1613 CliprdrFileContext* file = (context->custom);
1614 WINPR_ASSERT(file);
1615
1616 /*
1617 * MS-RDPECLIP 2.2.5.3 File Contents Request PDU (CLIPRDR_FILECONTENTS_REQUEST):
1618 * The FILECONTENTS_SIZE and FILECONTENTS_RANGE flags MUST NOT be set at the same time.
1619 */
1620 if ((fileContentsRequest->dwFlags & (FILECONTENTS_SIZE | FILECONTENTS_RANGE)) ==
1621 (FILECONTENTS_SIZE | FILECONTENTS_RANGE))
1622 {
1623 WLog_Print(file->log, WLOG_ERROR, "invalid CLIPRDR_FILECONTENTS_REQUEST.dwFlags");
1624 return cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1625 }
1626
1627 if (fileContentsRequest->dwFlags & FILECONTENTS_SIZE)
1628 error = cliprdr_file_context_server_file_size_request(file, fileContentsRequest);
1629
1630 if (fileContentsRequest->dwFlags & FILECONTENTS_RANGE)
1631 error = cliprdr_file_context_server_file_range_request(file, fileContentsRequest);
1632
1633 if (error)
1634 {
1635 WLog_Print(file->log, WLOG_ERROR, "failed to handle CLIPRDR_FILECONTENTS_REQUEST: 0x%08X",
1636 error);
1637 return cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1638 }
1639
1640 return CHANNEL_RC_OK;
1641}
1642
1643BOOL cliprdr_file_context_init(CliprdrFileContext* file, CliprdrClientContext* cliprdr)
1644{
1645 WINPR_ASSERT(file);
1646 WINPR_ASSERT(cliprdr);
1647
1648 cliprdr->custom = file;
1649 file->context = cliprdr;
1650
1651 cliprdr->ServerLockClipboardData = cliprdr_file_context_lock;
1652 cliprdr->ServerUnlockClipboardData = cliprdr_file_context_unlock;
1653 cliprdr->ServerFileContentsRequest = cliprdr_file_context_server_file_contents_request;
1654#if defined(WITH_FUSE)
1655 cliprdr->ServerFileContentsResponse = cliprdr_file_context_server_file_contents_response;
1656
1657 CliprdrFuseFile* root_dir = fuse_file_new_root(file);
1658 return root_dir != nullptr;
1659#else
1660 return TRUE;
1661#endif
1662}
1663
1664#if defined(WITH_FUSE)
1665WINPR_ATTR_NODISCARD
1666static bool clear_all_selections(CliprdrFileContext* file_context)
1667{
1668 WINPR_ASSERT(file_context);
1669 WINPR_ASSERT(file_context->inode_table);
1670
1671 HashTable_Lock(file_context->inode_table);
1672 const bool rc = clear_selection(file_context, TRUE, nullptr);
1673
1674 HashTable_Clear(file_context->clip_data_table);
1675 HashTable_Unlock(file_context->inode_table);
1676 return rc;
1677}
1678#endif
1679
1680BOOL cliprdr_file_context_uninit(CliprdrFileContext* file, CliprdrClientContext* cliprdr)
1681{
1682 WINPR_ASSERT(file);
1683 WINPR_ASSERT(cliprdr);
1684
1685 // Clear all data before the channel is closed
1686 // the cleanup handlers are dependent on a working channel.
1687#if defined(WITH_FUSE)
1688 if (file->inode_table)
1689 {
1690 if (!clear_no_cdi_entry(file))
1691 return FALSE;
1692 if (!clear_all_selections(file))
1693 return FALSE;
1694 }
1695#endif
1696
1697 HashTable_Clear(file->local_streams);
1698
1699 file->context = nullptr;
1700
1701#if defined(WITH_FUSE)
1702 cliprdr->ServerFileContentsResponse = nullptr;
1703#endif
1704
1705 return TRUE;
1706}
1707
1708static BOOL cliprdr_file_content_changed_and_update(void* ihash, size_t hsize, const void* data,
1709 size_t size)
1710{
1711
1712 BYTE hash[WINPR_SHA256_DIGEST_LENGTH] = WINPR_C_ARRAY_INIT;
1713
1714 if (hsize < sizeof(hash))
1715 return FALSE;
1716
1717 if (!winpr_Digest(WINPR_MD_SHA256, data, size, hash, sizeof(hash)))
1718 return FALSE;
1719
1720 const BOOL changed = memcmp(hash, ihash, sizeof(hash)) != 0;
1721 if (changed)
1722 memcpy(ihash, hash, sizeof(hash));
1723 return changed;
1724}
1725
1726static BOOL cliprdr_file_client_content_changed_and_update(CliprdrFileContext* file,
1727 const void* data, size_t size)
1728{
1729 WINPR_ASSERT(file);
1730 return cliprdr_file_content_changed_and_update(file->client_data_hash,
1731 sizeof(file->client_data_hash), data, size);
1732}
1733
1734#if defined(WITH_FUSE)
1735static fuse_ino_t get_next_free_inode(CliprdrFileContext* file_context)
1736{
1737 fuse_ino_t ino = 0;
1738
1739 WINPR_ASSERT(file_context);
1740
1741 ino = file_context->next_ino;
1742 while (ino == 0 || ino == FUSE_ROOT_ID ||
1743 HashTable_GetItemValue(file_context->inode_table, (void*)(uintptr_t)ino))
1744 ++ino;
1745
1746 file_context->next_ino = ino + 1;
1747
1748 return ino;
1749}
1750
1751static CliprdrFuseFile* clip_data_dir_new(CliprdrFileContext* file_context, BOOL has_clip_data_id,
1752 UINT32 clip_data_id)
1753{
1754 WINPR_ASSERT(file_context);
1755
1756 UINT64 data_id = clip_data_id;
1757 if (!has_clip_data_id)
1758 data_id = NO_CLIP_DATA_ID;
1759
1760 CliprdrFuseFile* clip_data_dir = fuse_file_new("/%" PRIu64, data_id);
1761 if (!clip_data_dir)
1762 return nullptr;
1763
1764 clip_data_dir->ino = get_next_free_inode(file_context);
1765 clip_data_dir->is_directory = TRUE;
1766 clip_data_dir->is_readonly = TRUE;
1767 clip_data_dir->has_clip_data_id = has_clip_data_id;
1768 clip_data_dir->clip_data_id = clip_data_id;
1769
1770 CliprdrFuseFile* root_dir = get_fuse_file_by_ino(file_context, FUSE_ROOT_ID);
1771 if (!root_dir)
1772 {
1773 WLog_Print(file_context->log, WLOG_ERROR, "FUSE root directory missing");
1774 fuse_file_free(clip_data_dir);
1775 return nullptr;
1776 }
1777 if (!ArrayList_Append(root_dir->children, clip_data_dir))
1778 {
1779 WLog_Print(file_context->log, WLOG_ERROR, "Failed to append FUSE file");
1780 fuse_file_free(clip_data_dir);
1781 return nullptr;
1782 }
1783 clip_data_dir->parent = root_dir;
1784
1785 if (!HashTable_Insert(file_context->inode_table, (void*)(uintptr_t)clip_data_dir->ino,
1786 clip_data_dir))
1787 {
1788 WLog_Print(file_context->log, WLOG_ERROR, "Failed to insert inode into inode table");
1789 ArrayList_Remove(root_dir->children, clip_data_dir);
1790 fuse_file_free(clip_data_dir);
1791 return nullptr;
1792 }
1793
1794 return clip_data_dir;
1795}
1796
1797static char* get_parent_path(const char* filepath)
1798{
1799 const char* base = strrchr(filepath, '/');
1800 WINPR_ASSERT(base);
1801
1802 while ((base > filepath) && (*base == '/'))
1803 --base;
1804
1805 WINPR_ASSERT(base >= filepath);
1806 const size_t parent_path_length = 1ULL + (size_t)(base - filepath);
1807 char* parent_path = calloc(parent_path_length + 1, sizeof(char));
1808 if (!parent_path)
1809 return nullptr;
1810
1811 memcpy(parent_path, filepath, parent_path_length);
1812
1813 return parent_path;
1814}
1815
1816static BOOL is_fuse_file_not_parent(WINPR_ATTR_UNUSED const void* key, void* value, void* arg)
1817{
1818 CliprdrFuseFile* fuse_file = value;
1819 CliprdrFuseFindParentContext* find_context = arg;
1820
1821 if (!fuse_file->is_directory)
1822 return TRUE;
1823
1824 if (strncmp(find_context->parent_path, fuse_file->filename_with_root,
1825 fuse_file->filename_with_root_len + 1) == 0)
1826 {
1827 find_context->parent = fuse_file;
1828 return FALSE;
1829 }
1830
1831 return TRUE;
1832}
1833
1834static CliprdrFuseFile* get_parent_directory(CliprdrFileContext* file_context, const char* path)
1835{
1836 CliprdrFuseFindParentContext find_context = WINPR_C_ARRAY_INIT;
1837
1838 WINPR_ASSERT(file_context);
1839 WINPR_ASSERT(path);
1840
1841 find_context.parent_path = get_parent_path(path);
1842 if (!find_context.parent_path)
1843 return nullptr;
1844
1845 WINPR_ASSERT(!find_context.parent);
1846
1847 if (HashTable_Foreach(file_context->inode_table, is_fuse_file_not_parent, &find_context))
1848 {
1849 free(find_context.parent_path);
1850 return nullptr;
1851 }
1852 WINPR_ASSERT(find_context.parent);
1853
1854 free(find_context.parent_path);
1855
1856 return find_context.parent;
1857}
1858
1859// NOLINTBEGIN(clang-analyzer-unix.Malloc) HashTable_Insert owns fuse_file
1860static BOOL selection_handle_file(CliprdrFileContext* file_context,
1861 CliprdrFuseClipDataEntry* clip_data_entry, uint32_t index,
1862 const FILEDESCRIPTORW* file)
1863{
1864
1865 WINPR_ASSERT(file_context);
1866 WINPR_ASSERT(clip_data_entry);
1867 WINPR_ASSERT(file);
1868
1869 CliprdrFuseFile* clip_data_dir = clip_data_entry->clip_data_dir;
1870 WINPR_ASSERT(clip_data_dir);
1871
1872 char filename[ARRAYSIZE(file->cFileName) * 8] = WINPR_C_ARRAY_INIT;
1873 const SSIZE_T filenamelen = ConvertWCharNToUtf8(file->cFileName, ARRAYSIZE(file->cFileName),
1874 filename, ARRAYSIZE(filename) - 1);
1875 if (filenamelen < 0)
1876 {
1877 WLog_Print(file_context->log, WLOG_ERROR, "Failed to convert filename");
1878 return FALSE;
1879 }
1880
1881 for (size_t j = 0; filename[j]; ++j)
1882 {
1883 if (filename[j] == '\\')
1884 filename[j] = '/';
1885 }
1886
1887 BOOL crc = FALSE;
1888 CliprdrFuseFile* fuse_file = fuse_file_new(
1889 "%.*s/%s", WINPR_ASSERTING_INT_CAST(int, clip_data_dir->filename_with_root_len),
1890 clip_data_dir->filename_with_root, filename);
1891 if (!fuse_file)
1892 {
1893 WLog_Print(file_context->log, WLOG_ERROR, "Failed to create FUSE file");
1894 goto end;
1895 }
1896
1897 fuse_file->parent = get_parent_directory(file_context, fuse_file->filename_with_root);
1898 if (!fuse_file->parent)
1899 {
1900 WLog_Print(file_context->log, WLOG_ERROR, "Found no parent for FUSE file");
1901 goto end;
1902 }
1903
1904 if (!ArrayList_Append(fuse_file->parent->children, fuse_file))
1905 {
1906 WLog_Print(file_context->log, WLOG_ERROR, "Failed to append FUSE file");
1907 goto end;
1908 }
1909
1910 fuse_file->list_idx = index;
1911 fuse_file->ino = get_next_free_inode(file_context);
1912 fuse_file->has_clip_data_id = clip_data_entry->has_clip_data_id;
1913 fuse_file->clip_data_id = clip_data_entry->clip_data_id;
1914 if (file->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1915 fuse_file->is_directory = TRUE;
1916 if (file->dwFileAttributes & FILE_ATTRIBUTE_READONLY)
1917 fuse_file->is_readonly = TRUE;
1918 if (file->dwFlags & FD_FILESIZE)
1919 {
1920 fuse_file->size = ((UINT64)file->nFileSizeHigh << 32) + file->nFileSizeLow;
1921 fuse_file->has_size = TRUE;
1922 }
1923 if (file->dwFlags & FD_WRITESTIME)
1924 {
1925 INT64 filetime = 0;
1926
1927 filetime = file->ftLastWriteTime.dwHighDateTime;
1928 filetime <<= 32;
1929 filetime += file->ftLastWriteTime.dwLowDateTime;
1930
1931 fuse_file->last_write_time_unix =
1932 1LL * filetime / (10LL * 1000LL * 1000LL) - WIN32_FILETIME_TO_UNIX_EPOCH;
1933 fuse_file->has_last_write_time = TRUE;
1934 }
1935
1936 if (!HashTable_Insert(file_context->inode_table, (void*)(uintptr_t)fuse_file->ino, fuse_file))
1937 {
1938 WLog_Print(file_context->log, WLOG_ERROR, "Failed to insert inode into inode table");
1939 goto end;
1940 }
1941
1942 crc = TRUE;
1943
1944end:
1945 if (!crc)
1946 {
1947 fuse_file_free(fuse_file);
1948 return clear_entry_selection(clip_data_entry);
1949 }
1950 return TRUE;
1951}
1952// NOLINTEND(clang-analyzer-unix.Malloc) HashTable_Insert owns fuse_file
1953
1954static BOOL set_selection_for_clip_data_entry(CliprdrFileContext* file_context,
1955 CliprdrFuseClipDataEntry* clip_data_entry,
1956 const FILEDESCRIPTORW* files, UINT32 n_files)
1957{
1958 WINPR_ASSERT(file_context);
1959 WINPR_ASSERT(clip_data_entry);
1960 WINPR_ASSERT(files);
1961
1962 if (clip_data_entry->has_clip_data_id)
1963 WLog_Print(file_context->log, WLOG_DEBUG, "Setting selection for clipDataId %u",
1964 clip_data_entry->clip_data_id);
1965 else
1966 WLog_Print(file_context->log, WLOG_DEBUG, "Setting selection");
1967
1968 for (UINT32 i = 0; i < n_files; ++i)
1969 {
1970 const FILEDESCRIPTORW* file = &files[i];
1971 if (!selection_handle_file(file_context, clip_data_entry, i, file))
1972 return FALSE;
1973 }
1974
1975 if (clip_data_entry->has_clip_data_id)
1976 WLog_Print(file_context->log, WLOG_DEBUG, "Selection set for clipDataId %u",
1977 clip_data_entry->clip_data_id);
1978 else
1979 WLog_Print(file_context->log, WLOG_DEBUG, "Selection set");
1980
1981 return TRUE;
1982}
1983
1984static BOOL update_exposed_path(CliprdrFileContext* file_context, wClipboard* clip,
1985 CliprdrFuseClipDataEntry* clip_data_entry)
1986{
1987 wClipboardDelegate* delegate = nullptr;
1988 CliprdrFuseFile* clip_data_dir = nullptr;
1989
1990 WINPR_ASSERT(file_context);
1991 WINPR_ASSERT(clip);
1992 WINPR_ASSERT(clip_data_entry);
1993
1994 delegate = ClipboardGetDelegate(clip);
1995 WINPR_ASSERT(delegate);
1996
1997 clip_data_dir = clip_data_entry->clip_data_dir;
1998 WINPR_ASSERT(clip_data_dir);
1999
2000 free(file_context->exposed_path);
2001 file_context->exposed_path = GetCombinedPath(file_context->path, clip_data_dir->filename);
2002 if (file_context->exposed_path)
2003 WLog_Print(file_context->log, WLOG_DEBUG, "Updated exposed path to \"%s\"",
2004 file_context->exposed_path);
2005
2006 delegate->basePath = file_context->exposed_path;
2007
2008 return delegate->basePath != nullptr;
2009}
2010#endif
2011
2012BOOL cliprdr_file_context_update_server_data(CliprdrFileContext* file_context, wClipboard* clip,
2013 const void* data, size_t size)
2014{
2015#if defined(WITH_FUSE)
2016 CliprdrFuseClipDataEntry* clip_data_entry = nullptr;
2017 FILEDESCRIPTORW* files = nullptr;
2018 UINT32 n_files = 0;
2019 BOOL rc = FALSE;
2020
2021 WINPR_ASSERT(file_context);
2022 WINPR_ASSERT(clip);
2023 if (size > UINT32_MAX)
2024 return FALSE;
2025
2026 if (cliprdr_parse_file_list(data, (UINT32)size, &files, &n_files))
2027 {
2028 WLog_Print(file_context->log, WLOG_ERROR, "Failed to parse file list");
2029 return FALSE;
2030 }
2031
2032 HashTable_Lock(file_context->inode_table);
2033 HashTable_Lock(file_context->clip_data_table);
2034 if (does_server_support_clipdata_locking(file_context))
2035 clip_data_entry = HashTable_GetItemValue(
2036 file_context->clip_data_table, (void*)(uintptr_t)file_context->current_clip_data_id);
2037 else
2038 clip_data_entry = file_context->clip_data_entry_without_id;
2039
2040 WINPR_ASSERT(clip_data_entry);
2041
2042 if (!clear_entry_selection(clip_data_entry))
2043 goto fail;
2044 WINPR_ASSERT(!clip_data_entry->clip_data_dir);
2045
2046 clip_data_entry->clip_data_dir =
2047 clip_data_dir_new(file_context, does_server_support_clipdata_locking(file_context),
2048 file_context->current_clip_data_id);
2049 if (!clip_data_entry->clip_data_dir)
2050 goto fail;
2051 if (!update_exposed_path(file_context, clip, clip_data_entry))
2052 goto fail;
2053 if (!set_selection_for_clip_data_entry(file_context, clip_data_entry, files, n_files))
2054 goto fail;
2055
2056 rc = TRUE;
2057
2058fail:
2059 HashTable_Unlock(file_context->clip_data_table);
2060 HashTable_Unlock(file_context->inode_table);
2061 free(files);
2062 return rc;
2063#else
2064 return FALSE;
2065#endif
2066}
2067
2068void* cliprdr_file_context_get_context(CliprdrFileContext* file)
2069{
2070 WINPR_ASSERT(file);
2071 return file->clipboard;
2072}
2073
2074void cliprdr_file_session_terminate(CliprdrFileContext* file, BOOL stop_thread)
2075{
2076 if (!file)
2077 return;
2078
2079#if defined(WITH_FUSE)
2080 WINPR_ASSERT(file->fuse_stop_sync);
2081
2082 WLog_Print(file->log, WLOG_DEBUG, "Setting FUSE exit flag");
2083 if (file->fuse_sess)
2084 fuse_session_exit(file->fuse_sess);
2085
2086 if (stop_thread)
2087 {
2088 WLog_Print(file->log, WLOG_DEBUG, "Setting FUSE stop event");
2089 (void)SetEvent(file->fuse_stop_sync);
2090 }
2091#endif
2092 /* not elegant but works for umounting FUSE
2093 fuse_chan must receive an oper buf to unblock fuse_session_receive_buf function.
2094 */
2095#if defined(WITH_FUSE)
2096 WLog_Print(file->log, WLOG_DEBUG, "Forcing FUSE to check exit flag");
2097#endif
2098 (void)winpr_PathFileExists(file->path);
2099}
2100
2101void cliprdr_file_context_free(CliprdrFileContext* file)
2102{
2103 if (!file)
2104 return;
2105
2106#if defined(WITH_FUSE)
2107 if (file->fuse_thread)
2108 {
2109 WINPR_ASSERT(file->fuse_stop_sync);
2110
2111 WLog_Print(file->log, WLOG_DEBUG, "Stopping FUSE thread");
2112 cliprdr_file_session_terminate(file, TRUE);
2113
2114 WLog_Print(file->log, WLOG_DEBUG, "Waiting on FUSE thread");
2115 (void)WaitForSingleObject(file->fuse_thread, INFINITE);
2116 (void)CloseHandle(file->fuse_thread);
2117 }
2118 if (file->fuse_stop_sync)
2119 (void)CloseHandle(file->fuse_stop_sync);
2120 if (file->fuse_start_sync)
2121 (void)CloseHandle(file->fuse_start_sync);
2122
2123 HashTable_Free(file->request_table);
2124 HashTable_Free(file->clip_data_table);
2125 HashTable_Free(file->inode_table);
2126
2127#endif
2128 HashTable_Free(file->local_streams);
2129 winpr_RemoveDirectory(file->path);
2130 free(file->path);
2131 free(file->exposed_path);
2132 free(file);
2133}
2134
2135static BOOL create_base_path(CliprdrFileContext* file)
2136{
2137 WINPR_ASSERT(file);
2138
2139 char base[64] = WINPR_C_ARRAY_INIT;
2140 (void)_snprintf(base, sizeof(base), "com.freerdp.client.cliprdr.%" PRIu32,
2141 GetCurrentProcessId());
2142
2143 file->path = GetKnownSubPath(KNOWN_PATH_TEMP, base);
2144 if (!file->path)
2145 return FALSE;
2146
2147 if (!winpr_PathFileExists(file->path) && !winpr_PathMakePath(file->path, nullptr))
2148 {
2149 WLog_Print(file->log, WLOG_ERROR, "Failed to create directory '%s'", file->path);
2150 return FALSE;
2151 }
2152 return TRUE;
2153}
2154
2155static void cliprdr_local_file_free(CliprdrLocalFile* file)
2156{
2157 const CliprdrLocalFile empty = WINPR_C_ARRAY_INIT;
2158 if (!file)
2159 return;
2160 if (file->fp)
2161 {
2162 WLog_Print(file->context->log, WLOG_DEBUG, "closing file %s, discarding entry", file->name);
2163 (void)fclose(file->fp);
2164 }
2165 free(file->name);
2166 *file = empty;
2167}
2168
2169static BOOL cliprdr_local_file_new(CliprdrFileContext* context, CliprdrLocalFile* f,
2170 const char* path)
2171{
2172 const CliprdrLocalFile empty = WINPR_C_ARRAY_INIT;
2173 WINPR_ASSERT(f);
2174 WINPR_ASSERT(context);
2175 WINPR_ASSERT(path);
2176
2177 *f = empty;
2178 f->context = context;
2179 f->name = winpr_str_url_decode(path, strlen(path));
2180 if (!f->name)
2181 goto fail;
2182
2183 return TRUE;
2184fail:
2185 cliprdr_local_file_free(f);
2186 return FALSE;
2187}
2188
2189static void cliprdr_local_files_free(CliprdrLocalStream* stream)
2190{
2191 WINPR_ASSERT(stream);
2192
2193 for (size_t x = 0; x < stream->count; x++)
2194 cliprdr_local_file_free(&stream->files[x]);
2195 free(stream->files);
2196
2197 stream->files = nullptr;
2198 stream->count = 0;
2199}
2200
2201static void cliprdr_local_stream_free(void* obj)
2202{
2203 CliprdrLocalStream* stream = (CliprdrLocalStream*)obj;
2204 if (stream)
2205 cliprdr_local_files_free(stream);
2206
2207 free(stream);
2208}
2209
2210static BOOL append_entry(CliprdrLocalStream* stream, const char* path)
2211{
2212 CliprdrLocalFile* tmp = realloc(stream->files, sizeof(CliprdrLocalFile) * (stream->count + 1));
2213 if (!tmp)
2214 return FALSE;
2215 stream->files = tmp;
2216 CliprdrLocalFile* f = &stream->files[stream->count++];
2217
2218 return cliprdr_local_file_new(stream->context, f, path);
2219}
2220
2221static BOOL is_directory(const char* path)
2222{
2223 WCHAR* wpath = ConvertUtf8ToWCharAlloc(path, nullptr);
2224 if (!wpath)
2225 return FALSE;
2226
2227 HANDLE hFile = CreateFileW(wpath, 0, FILE_SHARE_DELETE, nullptr, OPEN_EXISTING,
2228 FILE_ATTRIBUTE_NORMAL, nullptr);
2229 free(wpath);
2230
2231 if (hFile == INVALID_HANDLE_VALUE)
2232 return FALSE;
2233
2234 BY_HANDLE_FILE_INFORMATION fileInformation = WINPR_C_ARRAY_INIT;
2235 const BOOL status = GetFileInformationByHandle(hFile, &fileInformation);
2236 (void)CloseHandle(hFile);
2237 if (!status)
2238 return FALSE;
2239
2240 return (fileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
2241}
2242
2243static BOOL add_directory(CliprdrLocalStream* stream, const char* path)
2244{
2245 char* wildcardpath = GetCombinedPath(path, "*");
2246 if (!wildcardpath)
2247 return FALSE;
2248 WCHAR* wpath = ConvertUtf8ToWCharAlloc(wildcardpath, nullptr);
2249 free(wildcardpath);
2250 if (!wpath)
2251 return FALSE;
2252
2253 WIN32_FIND_DATAW FindFileData = WINPR_C_ARRAY_INIT;
2254 HANDLE hFind = FindFirstFileW(wpath, &FindFileData);
2255 free(wpath);
2256
2257 if (hFind == INVALID_HANDLE_VALUE)
2258 return FALSE;
2259
2260 BOOL rc = FALSE;
2261 char* next = nullptr;
2262
2263 WCHAR dotbuffer[6] = WINPR_C_ARRAY_INIT;
2264 WCHAR dotdotbuffer[6] = WINPR_C_ARRAY_INIT;
2265 const WCHAR* dot = InitializeConstWCharFromUtf8(".", dotbuffer, ARRAYSIZE(dotbuffer));
2266 const WCHAR* dotdot = InitializeConstWCharFromUtf8("..", dotdotbuffer, ARRAYSIZE(dotdotbuffer));
2267 do
2268 {
2269 if (_wcscmp(FindFileData.cFileName, dot) == 0)
2270 continue;
2271 if (_wcscmp(FindFileData.cFileName, dotdot) == 0)
2272 continue;
2273
2274 char cFileName[MAX_PATH] = WINPR_C_ARRAY_INIT;
2275 (void)ConvertWCharNToUtf8(FindFileData.cFileName, ARRAYSIZE(FindFileData.cFileName),
2276 cFileName, ARRAYSIZE(cFileName));
2277
2278 free(next);
2279 next = GetCombinedPath(path, cFileName);
2280 if (!next)
2281 goto fail;
2282
2283 if (!append_entry(stream, next))
2284 goto fail;
2285 if (is_directory(next))
2286 {
2287 if (!add_directory(stream, next))
2288 goto fail;
2289 }
2290 } while (FindNextFileW(hFind, &FindFileData));
2291
2292 rc = TRUE;
2293fail:
2294 free(next);
2295 FindClose(hFind);
2296
2297 return rc;
2298}
2299
2300static BOOL cliprdr_local_stream_update(CliprdrLocalStream* stream, const char* data, size_t size)
2301{
2302 BOOL rc = FALSE;
2303 WINPR_ASSERT(stream);
2304 if (size == 0)
2305 return TRUE;
2306
2307 cliprdr_local_files_free(stream);
2308
2309 stream->files = calloc(size, sizeof(CliprdrLocalFile));
2310 if (!stream->files)
2311 return FALSE;
2312
2313 char* copy = strndup(data, size);
2314 if (!copy)
2315 return FALSE;
2316
2317 char* saveptr = nullptr;
2318 char* ptr = strtok_s(copy, "\r\n", &saveptr);
2319 while (ptr)
2320 {
2321 const char* name = ptr;
2322 if (strncmp("file:///", ptr, 8) == 0)
2323 name = &ptr[7];
2324 else if (strncmp("file:/", ptr, 6) == 0)
2325 name = &ptr[5];
2326
2327 if (!append_entry(stream, name))
2328 goto fail;
2329
2330 if (is_directory(name))
2331 {
2332 const BOOL res = add_directory(stream, name);
2333 if (!res)
2334 goto fail;
2335 }
2336 ptr = strtok_s(nullptr, "\r\n", &saveptr);
2337 }
2338
2339 rc = TRUE;
2340fail:
2341 free(copy);
2342 return rc;
2343}
2344
2345CliprdrLocalStream* cliprdr_local_stream_new(CliprdrFileContext* context, UINT32 streamID,
2346 const char* data, size_t size)
2347{
2348 WINPR_ASSERT(context);
2349 CliprdrLocalStream* stream = calloc(1, sizeof(CliprdrLocalStream));
2350 if (!stream)
2351 return nullptr;
2352
2353 stream->context = context;
2354 if (!cliprdr_local_stream_update(stream, data, size))
2355 goto fail;
2356
2357 stream->lockId = streamID;
2358 return stream;
2359
2360fail:
2361 cliprdr_local_stream_free(stream);
2362 return nullptr;
2363}
2364
2365static UINT32 UINTPointerHash(const void* id)
2366{
2367 WINPR_ASSERT(id);
2368 return *((const UINT32*)id);
2369}
2370
2371static BOOL UINTPointerCompare(const void* pointer1, const void* pointer2)
2372{
2373 if (!pointer1 || !pointer2)
2374 return pointer1 == pointer2;
2375
2376 const UINT32* a = pointer1;
2377 const UINT32* b = pointer2;
2378 return *a == *b;
2379}
2380
2381static void* UINTPointerClone(const void* other)
2382{
2383 const UINT32* src = other;
2384 if (!src)
2385 return nullptr;
2386
2387 UINT32* copy = calloc(1, sizeof(UINT32));
2388 if (!copy)
2389 return nullptr;
2390
2391 *copy = *src;
2392 return copy;
2393}
2394
2395#if defined(WITH_FUSE)
2396CliprdrFuseFile* fuse_file_new_root(CliprdrFileContext* file_context)
2397{
2398 CliprdrFuseFile* root_dir = fuse_file_new("/");
2399 if (!root_dir)
2400 return nullptr;
2401
2402 root_dir->ino = FUSE_ROOT_ID;
2403 root_dir->is_directory = TRUE;
2404 root_dir->is_readonly = TRUE;
2405
2406 if (!HashTable_Insert(file_context->inode_table, (void*)(uintptr_t)root_dir->ino, root_dir))
2407 {
2408 fuse_file_free(root_dir);
2409 return nullptr;
2410 }
2411
2412 return root_dir;
2413}
2414#endif
2415
2416CliprdrFileContext* cliprdr_file_context_new(void* context)
2417{
2418 CliprdrFileContext* file = calloc(1, sizeof(CliprdrFileContext));
2419 if (!file)
2420 return nullptr;
2421
2422 file->log = WLog_Get(CLIENT_TAG("common.cliprdr.file"));
2423 file->clipboard = context;
2424
2425 file->local_streams = HashTable_New(FALSE);
2426 if (!file->local_streams)
2427 goto fail;
2428
2429 if (!HashTable_SetHashFunction(file->local_streams, UINTPointerHash))
2430 goto fail;
2431
2432 {
2433 wObject* hkobj = HashTable_KeyObject(file->local_streams);
2434 WINPR_ASSERT(hkobj);
2435 hkobj->fnObjectEquals = UINTPointerCompare;
2436 hkobj->fnObjectFree = free;
2437 hkobj->fnObjectNew = UINTPointerClone;
2438 }
2439
2440 {
2441 wObject* hobj = HashTable_ValueObject(file->local_streams);
2442 WINPR_ASSERT(hobj);
2443 hobj->fnObjectFree = cliprdr_local_stream_free;
2444 }
2445
2446#if defined(WITH_FUSE)
2447 file->inode_table = HashTable_New(FALSE);
2448 file->clip_data_table = HashTable_New(FALSE);
2449 file->request_table = HashTable_New(FALSE);
2450 if (!file->inode_table || !file->clip_data_table || !file->request_table)
2451 goto fail;
2452
2453 {
2454 wObject* ctobj = HashTable_ValueObject(file->request_table);
2455 WINPR_ASSERT(ctobj);
2456 ctobj->fnObjectFree = free;
2457 }
2458 {
2459 wObject* ctobj = HashTable_ValueObject(file->clip_data_table);
2460 WINPR_ASSERT(ctobj);
2461 ctobj->fnObjectFree = clip_data_entry_free;
2462 }
2463#endif
2464
2465 if (!create_base_path(file))
2466 goto fail;
2467
2468#if defined(WITH_FUSE)
2469 if (!(file->fuse_start_sync = CreateEvent(nullptr, TRUE, FALSE, nullptr)))
2470 goto fail;
2471 if (!(file->fuse_stop_sync = CreateEvent(nullptr, TRUE, FALSE, nullptr)))
2472 goto fail;
2473 if (!(file->fuse_thread = CreateThread(nullptr, 0, cliprdr_file_fuse_thread, file, 0, nullptr)))
2474 goto fail;
2475
2476 if (WaitForSingleObject(file->fuse_start_sync, INFINITE) == WAIT_FAILED)
2477 WLog_Print(file->log, WLOG_ERROR, "Failed to wait for start sync");
2478#endif
2479 return file;
2480
2481fail:
2482 WINPR_PRAGMA_DIAG_PUSH
2483 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2484 cliprdr_file_context_free(file);
2485 WINPR_PRAGMA_DIAG_POP
2486 return nullptr;
2487}
2488
2489BOOL local_stream_discard(const void* key, void* value, void* arg)
2490{
2491 CliprdrFileContext* file = arg;
2492 CliprdrLocalStream* stream = value;
2493 WINPR_ASSERT(file);
2494 WINPR_ASSERT(stream);
2495
2496 if (!stream->locked)
2497 HashTable_Remove(file->local_streams, key);
2498 return TRUE;
2499}
2500
2501BOOL cliprdr_file_context_clear(CliprdrFileContext* file)
2502{
2503 WINPR_ASSERT(file);
2504
2505 WLog_Print(file->log, WLOG_DEBUG, "clear file clipboard...");
2506
2507 HashTable_Lock(file->local_streams);
2508 const BOOL res = HashTable_Foreach(file->local_streams, local_stream_discard, file);
2509 HashTable_Unlock(file->local_streams);
2510
2511 memset(file->server_data_hash, 0, sizeof(file->server_data_hash));
2512 memset(file->client_data_hash, 0, sizeof(file->client_data_hash));
2513 return res;
2514}
2515
2516BOOL cliprdr_file_context_update_client_data(CliprdrFileContext* file, const char* data,
2517 size_t size)
2518{
2519 BOOL rc = FALSE;
2520
2521 WINPR_ASSERT(file);
2522 if (!cliprdr_file_client_content_changed_and_update(file, data, size))
2523 return TRUE;
2524
2525 if (!cliprdr_file_context_clear(file))
2526 return FALSE;
2527
2528 UINT32 lockId = file->local_lock_id;
2529
2530 HashTable_Lock(file->local_streams);
2531 CliprdrLocalStream* stream = HashTable_GetItemValue(file->local_streams, &lockId);
2532
2533 WLog_Print(file->log, WLOG_DEBUG, "update client file list (stream=%p)...", (void*)stream);
2534 if (stream)
2535 rc = cliprdr_local_stream_update(stream, data, size);
2536 else
2537 {
2538 stream = cliprdr_local_stream_new(file, lockId, data, size);
2539 rc = HashTable_Insert(file->local_streams, &stream->lockId, stream);
2540 if (!rc)
2541 cliprdr_local_stream_free(stream);
2542 }
2543 // HashTable_Insert owns stream
2544 // NOLINTNEXTLINE(clang-analyzer-unix.Malloc)
2545 HashTable_Unlock(file->local_streams);
2546 return rc;
2547}
2548
2549UINT32 cliprdr_file_context_current_flags(CliprdrFileContext* file)
2550{
2551 WINPR_ASSERT(file);
2552
2553 if ((file->file_capability_flags & CB_STREAM_FILECLIP_ENABLED) == 0)
2554 return 0;
2555
2556 if (!file->file_formats_registered)
2557 return 0;
2558
2559 return CB_STREAM_FILECLIP_ENABLED | CB_FILECLIP_NO_FILE_PATHS |
2560 CB_HUGE_FILE_SUPPORT_ENABLED; // | CB_CAN_LOCK_CLIPDATA;
2561}
2562
2563BOOL cliprdr_file_context_set_locally_available(CliprdrFileContext* file, BOOL available)
2564{
2565 WINPR_ASSERT(file);
2566 file->file_formats_registered = available;
2567 return TRUE;
2568}
2569
2570BOOL cliprdr_file_context_remote_set_flags(CliprdrFileContext* file, UINT32 flags)
2571{
2572 WINPR_ASSERT(file);
2573 file->file_capability_flags = flags;
2574 return TRUE;
2575}
2576
2577UINT32 cliprdr_file_context_remote_get_flags(CliprdrFileContext* file)
2578{
2579 WINPR_ASSERT(file);
2580 return file->file_capability_flags;
2581}
2582
2583BOOL cliprdr_file_context_has_local_support(CliprdrFileContext* file)
2584{
2585 WINPR_UNUSED(file);
2586
2587#if defined(WITH_FUSE)
2588 return TRUE;
2589#else
2590 return FALSE;
2591#endif
2592}
This struct contains function pointer to initialize/free objects.
Definition collections.h:52
OBJECT_FREE_FN fnObjectFree
Definition collections.h:58
OBJECT_EQUALS_FN fnObjectEquals
Definition collections.h:59
OBJECT_NEW_FN fnObjectNew
Definition collections.h:53