Lines Matching refs:file
43 int (*load)(struct kmod_file *file);
44 void (*unload)(struct kmod_file *file);
64 static void xz_uncompress_belch(struct kmod_file *file, lzma_ret ret) in xz_uncompress_belch() argument
68 ERR(file->ctx, "xz: %s\n", strerror(ENOMEM)); in xz_uncompress_belch()
71 ERR(file->ctx, "xz: File format not recognized\n"); in xz_uncompress_belch()
74 ERR(file->ctx, "xz: Unsupported compression options\n"); in xz_uncompress_belch()
77 ERR(file->ctx, "xz: File is corrupt\n"); in xz_uncompress_belch()
80 ERR(file->ctx, "xz: Unexpected end of input\n"); in xz_uncompress_belch()
83 ERR(file->ctx, "xz: Internal error (bug)\n"); in xz_uncompress_belch()
88 static int xz_uncompress(lzma_stream *strm, struct kmod_file *file) in xz_uncompress() argument
102 ssize_t rdret = read(file->fd, in_buf, sizeof(in_buf)); in xz_uncompress()
129 xz_uncompress_belch(file, ret); in xz_uncompress()
134 file->xz_used = true; in xz_uncompress()
135 file->memory = p; in xz_uncompress()
136 file->size = total; in xz_uncompress()
143 static int load_xz(struct kmod_file *file) in load_xz() argument
151 ERR(file->ctx, "xz: %s\n", strerror(ENOMEM)); in load_xz()
154 ERR(file->ctx, "xz: Internal error (bug)\n"); in load_xz()
157 ret = xz_uncompress(&strm, file); in load_xz()
162 static void unload_xz(struct kmod_file *file) in unload_xz() argument
164 if (!file->xz_used) in unload_xz()
166 free(file->memory); in unload_xz()
174 static int load_zlib(struct kmod_file *file) in load_zlib() argument
181 file->gzf = gzdopen(file->fd, "rb"); in load_zlib()
182 if (file->gzf == NULL) in load_zlib()
184 file->fd = -1; /* now owned by gzf due gzdopen() */ in load_zlib()
199 r = gzread(file->gzf, p + did, total - did); in load_zlib()
204 const char *gz_errmsg = gzerror(file->gzf, &gzerr); in load_zlib()
206 ERR(file->ctx, "gzip: %s\n", gz_errmsg); in load_zlib()
215 file->memory = p; in load_zlib()
216 file->size = did; in load_zlib()
221 gzclose(file->gzf); in load_zlib()
225 static void unload_zlib(struct kmod_file *file) in unload_zlib() argument
227 if (file->gzf == NULL) in unload_zlib()
229 free(file->memory); in unload_zlib()
230 gzclose(file->gzf); /* closes file->fd */ in unload_zlib()
250 static int load_reg(struct kmod_file *file) in load_reg() argument
254 if (fstat(file->fd, &st) < 0) in load_reg()
257 file->size = st.st_size; in load_reg()
258 file->memory = mmap(NULL, file->size, PROT_READ, MAP_PRIVATE, in load_reg()
259 file->fd, 0); in load_reg()
260 if (file->memory == MAP_FAILED) in load_reg()
262 file->direct = true; in load_reg()
266 static void unload_reg(struct kmod_file *file) in unload_reg() argument
268 munmap(file->memory, file->size); in unload_reg()
275 struct kmod_elf *kmod_file_get_elf(struct kmod_file *file) in kmod_file_get_elf() argument
277 if (file->elf) in kmod_file_get_elf()
278 return file->elf; in kmod_file_get_elf()
280 file->elf = kmod_elf_new(file->memory, file->size); in kmod_file_get_elf()
281 return file->elf; in kmod_file_get_elf()
287 struct kmod_file *file = calloc(1, sizeof(struct kmod_file)); in kmod_file_open() local
292 if (file == NULL) in kmod_file_open()
295 file->fd = open(filename, O_RDONLY|O_CLOEXEC); in kmod_file_open()
296 if (file->fd < 0) { in kmod_file_open()
306 file->direct = false; in kmod_file_open()
315 sz = read_str_safe(file->fd, buf, magic_size_max + 1); in kmod_file_open()
316 lseek(file->fd, 0, SEEK_SET); in kmod_file_open()
330 file->ops = &itr->ops; in kmod_file_open()
333 if (file->ops == NULL) in kmod_file_open()
334 file->ops = ®_ops; in kmod_file_open()
336 err = file->ops->load(file); in kmod_file_open()
337 file->ctx = ctx; in kmod_file_open()
340 if (file->fd >= 0) in kmod_file_open()
341 close(file->fd); in kmod_file_open()
342 free(file); in kmod_file_open()
347 return file; in kmod_file_open()
350 void *kmod_file_get_contents(const struct kmod_file *file) in kmod_file_get_contents() argument
352 return file->memory; in kmod_file_get_contents()
355 off_t kmod_file_get_size(const struct kmod_file *file) in kmod_file_get_size() argument
357 return file->size; in kmod_file_get_size()
360 bool kmod_file_get_direct(const struct kmod_file *file) in kmod_file_get_direct() argument
362 return file->direct; in kmod_file_get_direct()
365 int kmod_file_get_fd(const struct kmod_file *file) in kmod_file_get_fd() argument
367 return file->fd; in kmod_file_get_fd()
370 void kmod_file_unref(struct kmod_file *file) in kmod_file_unref() argument
372 if (file->elf) in kmod_file_unref()
373 kmod_elf_unref(file->elf); in kmod_file_unref()
375 file->ops->unload(file); in kmod_file_unref()
376 if (file->fd >= 0) in kmod_file_unref()
377 close(file->fd); in kmod_file_unref()
378 free(file); in kmod_file_unref()