Logo Search packages:      
Sourcecode: libgphoto2 version File versions

int gp_file_save ( CameraFile file,
const char *  filename 
)

Parameters:
file a CameraFile
filename 
Returns:
a gphoto2 error code.

Definition at line 418 of file gphoto2-file.c.

References CHECK_NULL, GP_ERROR, GP_ERROR_IO, GP_ERROR_IO_READ, GP_ERROR_NO_MEMORY, GP_FILE_ACCESSTYPE_FD, GP_FILE_ACCESSTYPE_MEMORY, gp_log(), GP_LOG_ERROR, and GP_OK.

{
      FILE *fp;
      struct utimbuf u;

      CHECK_NULL (file && filename);

      switch (file->accesstype) {
      case GP_FILE_ACCESSTYPE_MEMORY:
            if (!(fp = fopen (filename, "wb")))
                  return GP_ERROR;
            if (fwrite (file->data, (size_t)sizeof(char), (size_t)file->size, fp) != (size_t)file->size) {
                  gp_log (GP_LOG_ERROR, "libgphoto2",
                        "Not enough space on device in "
                        "order to save '%s'.", filename);
                  fclose (fp);
                  unlink (filename);
                  return GP_ERROR;
            }
            fclose (fp);
            break;
      case GP_FILE_ACCESSTYPE_FD: {
            char *data;
            unsigned long int curread = 0;
            off_t offset;

            if (-1 == lseek (file->fd, 0, SEEK_END))
                  return GP_ERROR_IO;
            if (-1 == (offset = lseek (file->fd, 0, SEEK_CUR))) {
                  /* should not happen if we passed the above case */
                  gp_log (GP_LOG_ERROR, "gphoto2-file", "Encountered error %d lseekin to CUR.", errno);
                  return GP_ERROR_IO_READ;
            }
            if (-1 == lseek (file->fd, 0, SEEK_SET)) {
                  /* should not happen if we passed the above case */
                  gp_log (GP_LOG_ERROR, "gphoto2-file", "Encountered error %d lseekin to BEGIN.", errno);
                  return GP_ERROR_IO_READ;
            }
            data = malloc(65536);
            if (!data)
                  return GP_ERROR_NO_MEMORY;
            if (!(fp = fopen (filename, "wb")))
                  return GP_ERROR;
            while (curread < offset) {
                  int toread, res;

                  toread = 65536;
                  if (toread > (offset-curread))
                        toread = offset-curread;
                  res = read (file->fd, data, toread);
                  if (res <= 0) {
                        free (data);
                        fclose (fp);
                        unlink (filename);
                        return GP_ERROR_IO_READ;
                  }
                  if (fwrite (data, 1, res, fp) != res) {
                        gp_log (GP_LOG_ERROR, "libgphoto2",
                              "Not enough space on device in "
                              "order to save '%s'.", filename);
                        free (data);
                        fclose (fp);
                        unlink (filename);
                        return GP_ERROR;
                  }
                  curread += res;
            }
            free (data);
            fclose (fp);
            break;
      }
      default:
            gp_log (GP_LOG_ERROR, "gphoto2-file", "Unknown file access type %d", file->accesstype);
            return GP_ERROR;
      }

      if (file->mtime) {
            u.actime = file->mtime;
            u.modtime = file->mtime;
            utime (filename, &u);
      }

      return (GP_OK);
}


Generated by  Doxygen 1.6.0   Back to index