static hFILE *hopen_fd(const char *filename, const char *mode) { hFILE_fd *fp = NULL; int fd = open(filename, hfile_oflags(mode), 0666); if (fd < 0) goto error; fp = (hFILE_fd *) hfile_init(sizeof (hFILE_fd), mode, blksize(fd)); if (fp == NULL) goto error; fp->fd = fd; fp->is_socket = 0; fp->base.backend = &fd_backend; return &fp->base; error: if (fd >= 0) { int save = errno; (void) close(fd); errno = save; } hfile_destroy((hFILE *) fp); return NULL; }
hFILE *hopen_irods(const char *filename, const char *mode) { hFILE_irods *fp; rodsPath_t path; dataObjInp_t args; int ret; // Initialise the iRODS connection if this is the first use. if (irods.conn == NULL) { if (irods_init() < 0) return NULL; } if (strncmp(filename, "irods:", 6) == 0) filename += 6; else { errno = EINVAL; return NULL; } fp = (hFILE_irods *) hfile_init(sizeof (hFILE_irods), mode, 0); if (fp == NULL) return NULL; strncpy(path.inPath, filename, MAX_NAME_LEN-1); path.inPath[MAX_NAME_LEN-1] = '\0'; ret = parseRodsPath(&path, &irods.env); if (ret < 0) goto error; memset(&args, 0, sizeof args); strcpy(args.objPath, path.outPath); args.openFlags = hfile_oflags(mode); if (args.openFlags & O_CREAT) { args.createMode = 0666; addKeyVal(&args.condInput, DEST_RESC_NAME_KW,irods.env.rodsDefResource); } ret = rcDataObjOpen(irods.conn, &args); if (ret < 0) goto error; fp->descriptor = ret; fp->base.backend = &irods_backend; return &fp->base; error: hfile_destroy((hFILE *) fp); set_errno(ret); return NULL; }
static hFILE *hopen_fd(const char *filename, const char *mode) { hFILE_fd *fp = NULL; int fd = open(filename, hfile_oflags(mode), 0666); if (fd < 0) goto error; off_t currentPos = lseek(fd, (size_t)0, SEEK_CUR); // Get the file size off_t fileSize = lseek(fd, (size_t)0, SEEK_END); // Seek back to the begining of file lseek(fd, currentPos, SEEK_SET); size_t lenstr = strlen(filename); // For small files that are not indexes and given rb mode, // we are just going to load them into memory to avoid // repeatedly fetching the same data from disk. if ((fileSize < (1 << 17)) && strcmp(mode, "rb") == 0 && lenstr > 4 && (strcmp(filename + lenstr - 4, ".pbi") != 0 && strcmp(filename + lenstr - 4, ".bai") != 0) ) { char* buffer = malloc(fileSize); ssize_t n = read(fd, buffer, fileSize); if (n < 0) goto error; close(fd); return hopen_mem(buffer, mode, fileSize, 1); } else { fp = (hFILE_fd *) hfile_init(sizeof (hFILE_fd), mode, blksize(fd)); if (fp == NULL) goto error; fp->fd = fd; fp->is_socket = 0; fp->base.backend = &fd_backend; return &fp->base; } error: if (fd >= 0) { int save = errno; (void) close(fd); errno = save; } hfile_destroy((hFILE *) fp); return NULL; }