diff options
Diffstat (limited to 'utils/MTP/beastpatcher/mtp_libmtp.c')
-rw-r--r-- | utils/MTP/beastpatcher/mtp_libmtp.c | 58 |
1 files changed, 50 insertions, 8 deletions
diff --git a/utils/MTP/beastpatcher/mtp_libmtp.c b/utils/MTP/beastpatcher/mtp_libmtp.c index c29e5277b2..90477fb0e7 100644 --- a/utils/MTP/beastpatcher/mtp_libmtp.c +++ b/utils/MTP/beastpatcher/mtp_libmtp.c | |||
@@ -47,6 +47,8 @@ | |||
47 | #include "libmtp.h" | 47 | #include "libmtp.h" |
48 | #include "mtp_common.h" | 48 | #include "mtp_common.h" |
49 | 49 | ||
50 | static int mtp_send_fileptr(struct mtp_info_t* mtp_info, FILE* fwfile, size_t fwsize); | ||
51 | |||
50 | int mtp_init(struct mtp_info_t* mtp_info) | 52 | int mtp_init(struct mtp_info_t* mtp_info) |
51 | { | 53 | { |
52 | /* Fill the info struct with zeros - mainly for the strings */ | 54 | /* Fill the info struct with zeros - mainly for the strings */ |
@@ -74,8 +76,8 @@ int mtp_scan(struct mtp_info_t* mtp_info) | |||
74 | if (mtp_info->device == NULL) | 76 | if (mtp_info->device == NULL) |
75 | { | 77 | { |
76 | return -1; | 78 | return -1; |
77 | } | 79 | } |
78 | else | 80 | else |
79 | { | 81 | { |
80 | /* NOTE: These strings are filled with zeros in mtp_init() */ | 82 | /* NOTE: These strings are filled with zeros in mtp_init() */ |
81 | #ifndef REALLYOLDMTP | 83 | #ifndef REALLYOLDMTP |
@@ -117,7 +119,6 @@ static int progress(uint64_t const sent, uint64_t const total, | |||
117 | int mtp_send_firmware(struct mtp_info_t* mtp_info, unsigned char* fwbuf, | 119 | int mtp_send_firmware(struct mtp_info_t* mtp_info, unsigned char* fwbuf, |
118 | int fwsize) | 120 | int fwsize) |
119 | { | 121 | { |
120 | LIBMTP_file_t *genfile; | ||
121 | int ret; | 122 | int ret; |
122 | size_t n; | 123 | size_t n; |
123 | FILE* fwfile; | 124 | FILE* fwfile; |
@@ -142,6 +143,20 @@ int mtp_send_firmware(struct mtp_info_t* mtp_info, unsigned char* fwbuf, | |||
142 | /* Reset file pointer */ | 143 | /* Reset file pointer */ |
143 | fseek(fwfile, SEEK_SET, 0); | 144 | fseek(fwfile, SEEK_SET, 0); |
144 | 145 | ||
146 | ret = mtp_send_fileptr(mtp_info, fwfile, fwsize); | ||
147 | |||
148 | /* Close the temporary file - this also deletes it. */ | ||
149 | fclose(fwfile); | ||
150 | |||
151 | return 0; | ||
152 | } | ||
153 | |||
154 | |||
155 | static int mtp_send_fileptr(struct mtp_info_t* mtp_info, FILE* fwfile, size_t fwsize) | ||
156 | { | ||
157 | LIBMTP_file_t* genfile; | ||
158 | int ret; | ||
159 | |||
145 | /* Prepare for uploading firmware */ | 160 | /* Prepare for uploading firmware */ |
146 | genfile = LIBMTP_new_file_t(); | 161 | genfile = LIBMTP_new_file_t(); |
147 | genfile->filetype = LIBMTP_FILETYPE_FIRMWARE; | 162 | genfile->filetype = LIBMTP_FILETYPE_FIRMWARE; |
@@ -149,10 +164,10 @@ int mtp_send_firmware(struct mtp_info_t* mtp_info, unsigned char* fwbuf, | |||
149 | genfile->filesize = fwsize; | 164 | genfile->filesize = fwsize; |
150 | 165 | ||
151 | #ifdef OLDMTP | 166 | #ifdef OLDMTP |
152 | ret = LIBMTP_Send_File_From_File_Descriptor(mtp_info->device, | 167 | ret = LIBMTP_Send_File_From_File_Descriptor(mtp_info->device, |
153 | fileno(fwfile), genfile, progress, NULL, 0); | 168 | fileno(fwfile), genfile, progress, NULL, 0); |
154 | #else | 169 | #else |
155 | ret = LIBMTP_Send_File_From_File_Descriptor(mtp_info->device, | 170 | ret = LIBMTP_Send_File_From_File_Descriptor(mtp_info->device, |
156 | fileno(fwfile), genfile, progress, NULL); | 171 | fileno(fwfile), genfile, progress, NULL); |
157 | #endif | 172 | #endif |
158 | 173 | ||
@@ -167,8 +182,35 @@ int mtp_send_firmware(struct mtp_info_t* mtp_info, unsigned char* fwbuf, | |||
167 | /* Cleanup */ | 182 | /* Cleanup */ |
168 | LIBMTP_destroy_file_t(genfile); | 183 | LIBMTP_destroy_file_t(genfile); |
169 | 184 | ||
170 | /* Close the temporary file - this also deletes it. */ | 185 | return ret; |
171 | fclose(fwfile); | 186 | } |
172 | 187 | ||
173 | return 0; | 188 | |
189 | int mtp_send_file(struct mtp_info_t* mtp_info, const char* filename) | ||
190 | { | ||
191 | FILE* fwfile; | ||
192 | int ret; | ||
193 | #ifdef _LARGEFILE64_SOURCE | ||
194 | struct stat64 sb; | ||
195 | ret = stat64(filename, &sb); | ||
196 | #else | ||
197 | struct stat sb; | ||
198 | ret = stat(filename, &sb); | ||
199 | #endif | ||
200 | if (ret == -1) | ||
201 | { | ||
202 | perror("[ERR] "); | ||
203 | } | ||
204 | |||
205 | fwfile = fopen(filename, "r"); | ||
206 | if (fwfile == NULL) | ||
207 | { | ||
208 | fprintf(stderr,"[ERR] Could not create temporary file.\n"); | ||
209 | return -1; | ||
210 | } | ||
211 | ret = mtp_send_fileptr(mtp_info, fwfile, sb.st_size); | ||
212 | |||
213 | fclose(fwfile); | ||
214 | return ret; | ||
174 | } | 215 | } |
216 | |||