~ubuntu-branches/ubuntu/saucy/libgphoto2/saucy-proposed

« back to all changes in this revision

Viewing changes to camlibs/directory/directory.c

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2013-07-31 07:00:09 UTC
  • mfrom: (1.4.4)
  • Revision ID: package-import@ubuntu.com-20130731070009-enrbvg3hry64cxl1
Tags: 2.5.2-0ubuntu1
* New upstream release.
* Drop 01-increase_max_entries.patch, 02-libusbx_no_debug.patch,
  03-libusbx-fixes.patch: fixed upstream.
* Add libxml2-dev build dependency for new optional features.
* ABI changes: libgphoto2-2 → libgphoto2-6,
  libgphoto2-port0 → libgphoto2-port10
* debian/libgphoto2-dev-doc.install: Adjust to changed HTML API doc folder
  name.
* debian/libgphoto2-port10.install: Adjust for changed libgphoto-port ABI.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20
20
 * Boston, MA 02111-1307, USA.
21
21
 */
 
22
 
 
23
#define _BSD_SOURCE
 
24
 
22
25
#include "config.h"
23
26
 
24
27
#include <errno.h>
84
87
        {"tif",  GP_MIME_TIFF},
85
88
        {"ppm",  GP_MIME_PPM},
86
89
        {"pgm",  GP_MIME_PGM},
87
 
        {"ogg",  GP_MIME_OGG},
88
90
        {"avi",  GP_MIME_AVI},
89
91
        {"mov",  GP_MIME_QUICKTIME},
90
92
        {"moov", GP_MIME_QUICKTIME},
96
98
        {"mpg",  GP_MIME_MPEG},
97
99
        {"mpeg", GP_MIME_MPEG},
98
100
        {"mpe",  GP_MIME_MPEG},
 
101
        {"ogg",  GP_MIME_OGG},
99
102
        {"mts",  GP_MIME_AVCHD},
100
103
        {"m2ts", GP_MIME_AVCHD},
101
104
        {"pbm", "image/x-portable-bitmap"},
195
198
}
196
199
 
197
200
static int
 
201
_get_mountpoint(GPPort *port, char **path) {
 
202
        GPPortInfo      info;
 
203
        int ret;
 
204
        char *p;
 
205
 
 
206
        ret = gp_port_get_info (port, &info);
 
207
        if (ret < GP_OK)
 
208
                return ret;
 
209
        ret = gp_port_info_get_path (info, path);
 
210
        if (ret < GP_OK)
 
211
                return ret;
 
212
        p = strchr (*path, ':');
 
213
        if (p) *path = p+1;
 
214
        return GP_OK;
 
215
}
 
216
 
 
217
static int
198
218
_get_path (GPPort *port, const char *folder, const char *file, char *path, unsigned int size) {
199
219
        if (port->type == GP_PORT_DISK) {
200
 
                GPPortInfo      info;
201
220
                int ret;
202
221
                char *xpath;
203
222
 
204
 
                ret = gp_port_get_info (port, &info);
205
 
                if (ret < GP_OK)
206
 
                        return ret;
207
 
                xpath = strchr (info.path,':');
208
 
                if (!xpath) xpath = info.path; else xpath++;
 
223
                ret = _get_mountpoint (port, &xpath);
209
224
                snprintf (path, size, "%s/%s/%s", xpath, folder, file);
210
225
        } else {
211
226
                /* old style access */
223
238
        gp_system_dirent de;
224
239
        char buf[1024], f[1024];
225
240
        unsigned int id, n;
 
241
        int ret;
226
242
        Camera *camera = (Camera*)data;
227
243
 
228
244
        if (camera->port->type == GP_PORT_DISK) {
229
 
                char            *path;
230
 
                GPPortInfo      info;
231
 
                int             ret;
 
245
                char *path;
232
246
 
233
 
                ret = gp_port_get_info (camera->port, &info);
 
247
                ret = _get_mountpoint (camera->port, &path);
234
248
                if (ret < GP_OK)
235
249
                        return ret;
236
 
                path = strchr (info.path,':');
237
 
                if (!path) path = info.path; else path++;
238
250
                snprintf (f, sizeof(f), "%s/%s/", path, folder);
239
 
                gp_log (GP_LOG_DEBUG, "directory/file_list_func", "%s", f);
240
251
                /* UNIX / is empty, or we recurse through the whole fs */
241
252
                if (    (!strcmp(path, "") || !strcmp(path, "/")) &&
242
253
                        !strcmp(folder,"/")
305
316
        Camera *camera = (Camera*)data;
306
317
 
307
318
        if (camera->port->type == GP_PORT_DISK) {
308
 
                char            *path;
309
 
                GPPortInfo      info;
310
 
                int             ret;
 
319
                char *path;
 
320
                int ret;
311
321
 
312
 
                ret = gp_port_get_info (camera->port, &info);
 
322
                ret = _get_mountpoint (camera->port, &path);
313
323
                if (ret < GP_OK)
314
324
                        return ret;
315
 
                path = strchr (info.path,':');
316
 
                if (!path) path = info.path; else path++;
317
325
 
318
326
                snprintf (f, sizeof(f), "%s/%s/", path, folder);
319
 
                gp_log (GP_LOG_DEBUG, "directory/folder_list_func", "%s", f);
320
327
                /* UNIX / is empty, or we recurse through the whole fs */
321
328
                if (    (!strcmp(path, "") || !strcmp(path, "/")) &&
322
329
                        !strcmp(folder,"/")
343
350
        dir = gp_system_opendir (f);
344
351
        if (!dir)
345
352
                return GP_ERROR;
346
 
        id = gp_context_progress_start (context, n, _("Listing folders in '%s'..."), folder);
 
353
        id = gp_context_progress_start (context, n, _("Listing folders in "
 
354
                                        "'%s'..."), folder);
347
355
        n = 0;
348
356
        while ((de = gp_system_readdir (dir))) {
349
357
                const char * filename = NULL;
361
369
                        
362
370
                        /* lstat ... do not follow symlinks */
363
371
                        if (lstat (buf, &st) != 0) {
364
 
                                gp_context_error (context, _("Could not get information about '%s' (%m)."), buf);
 
372
                                int saved_errno = errno;
 
373
                                gp_context_error (context, _("Could not get information "
 
374
                                                             "about '%s' (%s)."),
 
375
                                                  buf, strerror(saved_errno));
365
376
                                return GP_ERROR;
366
377
                        }
367
378
                        if (S_ISDIR (st.st_mode)) {
385
396
        Camera *camera = (Camera*)data;
386
397
        int result;
387
398
 
388
 
        gp_log (GP_LOG_DEBUG, "directory/get_info_func", "%s %s", folder, file);
389
399
        result = _get_path (camera->port, folder, file, path, sizeof(path));
390
400
        if (result < GP_OK)
391
401
                return result;
392
402
 
393
403
        if (lstat (path, &st) != 0) {
 
404
                int saved_errno = errno;
394
405
                gp_context_error (context, _("Could not get information "
395
 
                        "about '%s' in '%s' (%m)."), file, folder);
 
406
                                             "about '%s' in '%s' (%s)."),
 
407
                                  file, folder, strerror(saved_errno));
396
408
                return (GP_ERROR);
397
409
        }
398
410
 
399
411
        info->preview.fields = GP_FILE_INFO_NONE;
400
 
        info->file.fields = GP_FILE_INFO_SIZE | GP_FILE_INFO_NAME |
 
412
        info->file.fields = GP_FILE_INFO_SIZE | 
401
413
                            GP_FILE_INFO_TYPE | GP_FILE_INFO_PERMISSIONS |
402
414
                            GP_FILE_INFO_MTIME;
403
415
 
407
419
                info->file.permissions |= GP_FILE_PERM_READ;
408
420
        if (st.st_mode & S_IWUSR)
409
421
                info->file.permissions |= GP_FILE_PERM_DELETE;
410
 
        strcpy (info->file.name, file);
411
422
        info->file.size = st.st_size;
412
423
        mime_type = get_mime_type (file);
413
424
        if (!mime_type)
414
 
                mime_type = "application/octet-stream";
 
425
                mime_type = GP_MIME_UNKNOWN;
415
426
        strcpy (info->file.type, mime_type);
416
 
 
417
427
        return (GP_OK);
418
428
}
419
429
 
422
432
               CameraFileInfo info, void *data, GPContext *context)
423
433
{
424
434
        int retval;
425
 
        char path[1024];
 
435
        char path_old[1024], path_new[1024], path[1024];
426
436
        Camera *camera = (Camera*)data;
427
437
 
428
438
        retval = _get_path (camera->port, folder, file, path, sizeof(path));
439
449
                utimbuf.actime  = info.file.mtime;
440
450
                utimbuf.modtime = info.file.mtime;
441
451
                if (utime (path, &utimbuf) != 0) {
 
452
                        int saved_errno = errno;
442
453
                        gp_context_error (context, _("Could not change "
443
 
                                "time of file '%s' in '%s' (%m)."),
444
 
                                file, folder);
 
454
                                                     "time of file '%s' in '%s' (%s)."),
 
455
                                          file, folder, strerror(saved_errno));
445
456
                        return (GP_ERROR);
446
457
                }
447
458
        }
504
515
        unsigned int buf_len;
505
516
#endif /* HAVE_LIBEXIF */
506
517
        Camera *camera = (Camera*)user_data;
507
 
        struct stat st;
508
518
 
509
519
        result = _get_path (camera->port, folder, filename, path, sizeof(path));
510
 
        gp_log (GP_LOG_DEBUG, "directory/get_file_func", "%s %s",folder,filename);
511
520
        if (result < GP_OK)
512
521
                return result;
513
 
        gp_log (GP_LOG_DEBUG, "directory/get_file_func", "->%s",path);
514
 
 
515
 
        if (lstat (path, &st) != 0) {
516
 
                gp_context_error (context, _("Could not get information "
517
 
                        "about '%s' in '%s' (%m)."), filename, folder);
518
 
                return (GP_ERROR);
519
 
        }
520
 
        gp_file_set_mtime (file, st.st_mtime);
 
522
 
 
523
        if (-1 == lstat(path,&stbuf))
 
524
                return GP_ERROR_IO_READ;
 
525
 
 
526
        gp_file_set_mtime (file, stbuf.st_mtime);
521
527
 
522
528
        switch (type) {
523
529
        case GP_FILE_TYPE_NORMAL:
544
550
        default:
545
551
                return (GP_ERROR_NOT_SUPPORTED);
546
552
        }
547
 
 
548
 
        if (-1 == fstat(fd,&stbuf)) {
549
 
                close (fd);
550
 
                return GP_ERROR_IO_READ;
551
 
        }
552
553
#define BLOCKSIZE 65536
553
554
        /* do it in 64kb blocks */
554
555
        buf = malloc(BLOCKSIZE);
556
557
                close (fd);
557
558
                return GP_ERROR_NO_MEMORY;
558
559
        }
 
560
        if (-1 == fstat(fd,&stbuf)) {
 
561
                free (buf);
 
562
                close (fd);
 
563
                return GP_ERROR_IO_READ;
 
564
        }
559
565
 
560
566
        curread = 0;
561
567
        id = gp_context_progress_start (context, (1.0*stbuf.st_size/BLOCKSIZE), _("Getting file..."));
652
658
                return result;
653
659
        result = unlink (path);
654
660
        if (result) {
 
661
                int saved_errno = errno;
655
662
                gp_context_error (context, _("Could not delete file '%s' "
656
 
                        "in folder '%s' (error code %i: %m)."),
657
 
                        file, folder, result);
 
663
                                             "in folder '%s' (error code %i: %s)."),
 
664
                                  file, folder, result, strerror(saved_errno));
658
665
                return (GP_ERROR);
659
666
        }
660
667
 
662
669
}
663
670
 
664
671
static int
665
 
put_file_func (CameraFilesystem *fs, const char *folder,
666
 
               CameraFile *file, void *data, GPContext *context)
 
672
put_file_func (CameraFilesystem *fs, const char *folder, const char *name,
 
673
               CameraFileType type, CameraFile *file, void *data, GPContext *context)
667
674
{
668
675
        char path[2048];
669
 
        const char *name;
670
676
        int result;
671
677
#ifdef DEBUG
672
678
        unsigned int i, id;
673
679
#endif
674
680
        Camera *camera = (Camera*)data;
675
681
 
676
 
        gp_file_get_name (file, &name);
677
 
 
678
682
        result = _get_path (camera->port, folder, name, path, sizeof(path));
679
683
        if (result < GP_OK)
680
684
                return result;
735
739
 
736
740
#if defined(linux) && defined(HAVE_STATFS)
737
741
        struct  statfs          stfs;
738
 
        char            *xpath;
739
 
        int             ret;
740
 
        GPPortInfo      info;
 
742
        char *xpath;
 
743
        int ret;
741
744
 
742
 
        ret = gp_port_get_info (camera->port, &info);
 
745
        ret = _get_mountpoint (camera->port, &xpath);
743
746
        if (ret < GP_OK)
744
747
                return ret;
745
 
        xpath = strchr (info.path,':');
746
 
        if (!xpath) xpath = info.path; else xpath++;
747
 
 
748
748
        if (-1 == statfs (xpath, &stfs))
749
749
                return GP_ERROR_NOT_SUPPORTED;
750
750