Get a pointer to the data and the file's size.
Definition at line 350 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. Referenced by gp_camera_file_get_info(), gp_filesystem_get_file(), and gp_filesystem_set_file_noop(). { CHECK_NULL (file); switch (file->accesstype) { case GP_FILE_ACCESSTYPE_MEMORY: if (data) *data = file->data; if (size) *size = file->size; break; case GP_FILE_ACCESSTYPE_FD: { off_t offset; unsigned long int curread = 0; if (-1 == lseek (file->fd, 0, SEEK_END)) { if (errno == EBADF) return GP_ERROR_IO; /* Might happen for pipes or sockets. Umm. Hard. */ /* FIXME */ } 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 cases */ gp_log (GP_LOG_ERROR, "gphoto2-file", "Encountered error %d lseekin to CUR.", errno); return GP_ERROR_IO_READ; } if (size) *size = offset; if (!data) /* just the size... */ return GP_OK; *data = malloc (offset); if (!*data) return GP_ERROR_NO_MEMORY; while (curread < offset) { unsigned int res = read (file->fd, (char*)((*data)+curread), offset-curread); if (res == -1) { free ((char*)*data); gp_log (GP_LOG_ERROR, "gphoto2-file", "Encountered error %d reading.", errno); return GP_ERROR_IO_READ; } if (res == 0) { free ((char*)*data); gp_log (GP_LOG_ERROR, "gphoto2-file", "No progress during reading."); return GP_ERROR_IO_READ; } curread += res; } break; } default: gp_log (GP_LOG_ERROR, "gphoto2-file", "Unknown file access type %d", file->accesstype); return GP_ERROR; } return (GP_OK); }
|