summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/utils.c42
-rw-r--r--src/utils.h4
2 files changed, 34 insertions, 12 deletions
diff --git a/src/utils.c b/src/utils.c
index 28664d4..e30a0b3 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -30,6 +30,7 @@
30#include <stdarg.h> 30#include <stdarg.h>
31#include <time.h> 31#include <time.h>
32#include <sys/time.h> 32#include <sys/time.h>
33#include <errno.h>
33#ifdef __APPLE__ 34#ifdef __APPLE__
34#include <mach/mach_time.h> 35#include <mach/mach_time.h>
35#endif 36#endif
@@ -214,7 +215,7 @@ char *string_concat(const char *str, ...)
214 return result; 215 return result;
215} 216}
216 217
217void buffer_read_from_filename(const char *filename, char **buffer, uint64_t *length) 218int buffer_read_from_filename(const char *filename, char **buffer, uint64_t *length)
218{ 219{
219 FILE *f; 220 FILE *f;
220 uint64_t size; 221 uint64_t size;
@@ -223,7 +224,7 @@ void buffer_read_from_filename(const char *filename, char **buffer, uint64_t *le
223 224
224 f = fopen(filename, "rb"); 225 f = fopen(filename, "rb");
225 if (!f) { 226 if (!f) {
226 return; 227 return 0;
227 } 228 }
228 229
229 fseek(f, 0, SEEK_END); 230 fseek(f, 0, SEEK_END);
@@ -232,26 +233,49 @@ void buffer_read_from_filename(const char *filename, char **buffer, uint64_t *le
232 233
233 if (size == 0) { 234 if (size == 0) {
234 fclose(f); 235 fclose(f);
235 return; 236 return 0;
236 } 237 }
237 238
238 *buffer = (char*)malloc(sizeof(char)*(size+1)); 239 *buffer = (char*)malloc(sizeof(char)*(size+1));
240
241 if (!buffer) {
242 return 0;
243 }
244
245 int ret = 1;
239 if (fread(*buffer, sizeof(char), size, f) != size) { 246 if (fread(*buffer, sizeof(char), size, f) != size) {
240 usbmuxd_log(LL_ERROR, "%s: ERROR: couldn't read %d bytes from %s", __func__, (int)size, filename); 247 usbmuxd_log(LL_ERROR, "%s: ERROR: couldn't read %d bytes from %s", __func__, (int)size, filename);
248 free(buffer);
249 ret = 0;
250 errno = EIO;
241 } 251 }
242 fclose(f); 252 fclose(f);
243 253
244 *length = size; 254 *length = size;
255 return ret;
245} 256}
246 257
247void buffer_write_to_filename(const char *filename, const char *buffer, uint64_t length) 258int buffer_write_to_filename(const char *filename, const char *buffer, uint64_t length)
248{ 259{
249 FILE *f; 260 FILE *f;
250 261
251 f = fopen(filename, "wb"); 262 f = fopen(filename, "wb");
252 if (f) { 263 if (f) {
253 fwrite(buffer, sizeof(char), length, f); 264 size_t written = fwrite(buffer, sizeof(char), length, f);
254 fclose(f); 265 fclose(f);
266
267 if (written == length) {
268 return 1;
269 }
270 else {
271 // Not all data could be written.
272 errno = EIO;
273 return 0;
274 }
275 }
276 else {
277 // Failed to open the file, let the caller know.
278 return 0;
255 } 279 }
256} 280}
257 281
@@ -263,9 +287,7 @@ int plist_read_from_filename(plist_t *plist, const char *filename)
263 if (!filename) 287 if (!filename)
264 return 0; 288 return 0;
265 289
266 buffer_read_from_filename(filename, &buffer, &length); 290 if (!buffer_read_from_filename(filename, &buffer, &length)) {
267
268 if (!buffer) {
269 return 0; 291 return 0;
270 } 292 }
271 293
@@ -295,11 +317,11 @@ int plist_write_to_filename(plist_t plist, const char *filename, enum plist_form
295 else 317 else
296 return 0; 318 return 0;
297 319
298 buffer_write_to_filename(filename, buffer, length); 320 int res = buffer_write_to_filename(filename, buffer, length);
299 321
300 free(buffer); 322 free(buffer);
301 323
302 return 1; 324 return res;
303} 325}
304 326
305#ifndef HAVE_CLOCK_GETTIME 327#ifndef HAVE_CLOCK_GETTIME
diff --git a/src/utils.h b/src/utils.h
index 1137a93..b5cab3f 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -75,8 +75,8 @@ char *stpcpy(char * s1, const char * s2);
75#endif 75#endif
76char *string_concat(const char *str, ...); 76char *string_concat(const char *str, ...);
77 77
78void buffer_read_from_filename(const char *filename, char **buffer, uint64_t *length); 78int buffer_read_from_filename(const char *filename, char **buffer, uint64_t *length);
79void buffer_write_to_filename(const char *filename, const char *buffer, uint64_t length); 79int buffer_write_to_filename(const char *filename, const char *buffer, uint64_t length);
80 80
81enum plist_format_t { 81enum plist_format_t {
82 PLIST_FORMAT_XML, 82 PLIST_FORMAT_XML,