Logo Search packages:      
Sourcecode: libgphoto2 version File versions  Download package

int gp_file_set_data_and_size ( CameraFile file,
char *  data,
unsigned long int  size 
)

Parameters:
file a CameraFile
data 
size 
Returns:
a gphoto2 error code.

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

References CHECK_NULL, GP_ERROR, GP_ERROR_IO_WRITE, GP_FILE_ACCESSTYPE_FD, GP_FILE_ACCESSTYPE_MEMORY, gp_log(), GP_LOG_ERROR, and GP_OK.

Referenced by gp_filesystem_get_file().

{
      CHECK_NULL (file);

      switch (file->accesstype) {
      case GP_FILE_ACCESSTYPE_MEMORY:
            if (file->data)
                  free (file->data);
            file->data = data;
            file->size = size;
            break;
      case GP_FILE_ACCESSTYPE_FD: {
            int curwritten = 0;

            /* truncate */
            if (-1 == lseek (file->fd, 0, SEEK_SET)) {
                  gp_log (GP_LOG_ERROR, "gphoto2-file", "Encountered error %d lseeking to 0.", errno);
                  /* might happen on pipes ... just ignore it */
            }
            if (-1 == ftruncate (file->fd, 0)) {
                  gp_log (GP_LOG_ERROR, "gphoto2-file", "Encountered error %d ftruncating to 0.", errno);
                  /* might happen on pipes ... just ignore it */
            }
            while (curwritten < size) {
                  size_t      res = write (file->fd, data+curwritten, size-curwritten);
                  if (res == -1) {
                        gp_log (GP_LOG_ERROR, "gphoto2-file", "Encountered error %d writing to fd.", errno);
                        return GP_ERROR_IO_WRITE;
                  }
                  if (!res) { /* no progress is bad too */
                        gp_log (GP_LOG_ERROR, "gphoto2-file", "Encountered 0 bytes written to fd.");
                        return GP_ERROR_IO_WRITE;
                  }
                  curwritten += res;
            }
            /* This function takes over the responsibility for "data", aka
             * it has to free it. So we do.
             */
            free (data);
            break;
      }
      default:
            gp_log (GP_LOG_ERROR, "gphoto2-file", "Unknown file access type %d", file->accesstype);
            return GP_ERROR;
      }
      return (GP_OK);
}


Generated by  Doxygen 1.6.0   Back to index