usb_storage.c (3454B)
1 /* 2 * Dummy "stream" operations for USB storage 3 * 4 * Copyright (C) 2018 Linus Torvalds 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 * MA 02110-1301 USA 20 */ 21 22 #ifdef HAVE_CONFIG_H 23 #include "config.h" 24 #endif 25 26 #include <stdlib.h> 27 #include <string.h> 28 #include <errno.h> 29 30 #ifdef _WIN32 31 #define WIN32_LEAN_AND_MEAN 32 #define NOGDI 33 #include <windows.h> 34 #include <sys/types.h> 35 #include <sys/stat.h> 36 #ifndef PATH_MAX 37 #define PATH_MAX MAX_PATH 38 #endif 39 #define dc_stat _stat 40 #define dc_stat_t struct _stat 41 #define DC_S_ISDIR(mode) (((mode) & _S_IFMT) == _S_IFDIR) 42 #else 43 #include <limits.h> 44 #include <sys/types.h> 45 #include <sys/stat.h> 46 #include <fcntl.h> 47 #define dc_stat stat 48 #define dc_stat_t struct stat 49 #define DC_S_ISDIR(mode) S_ISDIR(mode) 50 #endif 51 52 #include "common-private.h" 53 #include "context-private.h" 54 #include "iostream-private.h" 55 #include "iterator-private.h" 56 #include "timer.h" 57 58 // Fake "device" that just contains the directory name that 59 // you can read out of the iostream. All the actual IO is 60 // up to you. 61 typedef struct dc_usbstorage_t { 62 dc_iostream_t base; 63 char pathname[PATH_MAX]; 64 } dc_usbstorage_t; 65 66 static dc_status_t 67 dc_usb_storage_read (dc_iostream_t *iostream, void *data, size_t size, size_t *actual); 68 69 static const dc_iostream_vtable_t dc_usbstorage_vtable = { 70 sizeof(dc_usbstorage_t), 71 NULL, /* set_timeout */ 72 NULL, /* set_latency */ 73 NULL, /* set_break */ 74 NULL, /* set_dtr */ 75 NULL, /* set_rts */ 76 NULL, /* get_lines */ 77 NULL, /* get_available */ 78 NULL, /* configure */ 79 dc_usb_storage_read, /* read */ 80 NULL, /* write */ 81 NULL, /* flush */ 82 NULL, /* purge */ 83 NULL, /* sleep */ 84 NULL, /* close */ 85 }; 86 87 dc_status_t 88 dc_usb_storage_open (dc_iostream_t **out, dc_context_t *context, const char *name) 89 { 90 dc_usbstorage_t *device = NULL; 91 dc_stat_t st; 92 93 if (out == NULL || name == NULL) 94 return DC_STATUS_INVALIDARGS; 95 96 if (*name == '\0') { 97 // that indicates an MTP device 98 INFO (context, "Open MTP device"); 99 } else { 100 INFO (context, "Open: name=%s", name); 101 if (dc_stat(name, &st) < 0 || !DC_S_ISDIR(st.st_mode)) 102 return DC_STATUS_NODEVICE; 103 } 104 // Allocate memory. 105 device = (dc_usbstorage_t *) dc_iostream_allocate (context, &dc_usbstorage_vtable, DC_TRANSPORT_USBSTORAGE); 106 if (device == NULL) { 107 SYSERROR (context, ENOMEM); 108 return DC_STATUS_NOMEMORY; 109 } 110 111 strncpy(device->pathname, name, PATH_MAX); 112 device->pathname[PATH_MAX-1] = 0; 113 114 *out = (dc_iostream_t *) device; 115 return DC_STATUS_SUCCESS; 116 } 117 118 static dc_status_t 119 dc_usb_storage_read (dc_iostream_t *iostream, void *data, size_t size, size_t *actual) 120 { 121 dc_usbstorage_t *device = (dc_usbstorage_t *) iostream; 122 size_t len = strlen(device->pathname); 123 124 if (size <= len) 125 return DC_STATUS_IO; 126 memcpy(data, device->pathname, len+1); 127 if (actual) 128 *actual = len; 129 return DC_STATUS_SUCCESS; 130 }