~registry/kmod/master

« back to all changes in this revision

Viewing changes to libkmod/libkmod-file.c

  • Committer: Lucas De Marchi
  • Date: 2023-06-09 17:45:55 UTC
  • Revision ID: git-v1:e539827635db486ecc70801250f9f73f652cd8d0
libkmod: Keep track of compression type

Do not only set the type as direct, but also keep track of the
compression being used. This will allow using the in-kernel compression
in future.

Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
58
58
        gzFile gzf;
59
59
#endif
60
60
        int fd;
61
 
        bool direct;
 
61
        enum kmod_file_compression_type compression;
62
62
        off_t size;
63
63
        void *memory;
64
64
        const struct file_ops *ops;
376
376
 
377
377
static const struct comp_type {
378
378
        size_t magic_size;
 
379
        enum kmod_file_compression_type compression;
379
380
        const char *magic_bytes;
380
381
        const struct file_ops ops;
381
382
} comp_types[] = {
382
383
#ifdef ENABLE_ZSTD
383
 
        {sizeof(magic_zstd), magic_zstd, {load_zstd, unload_zstd}},
 
384
        {sizeof(magic_zstd),    KMOD_FILE_COMPRESSION_ZSTD, magic_zstd, {load_zstd, unload_zstd}},
384
385
#endif
385
386
#ifdef ENABLE_XZ
386
 
        {sizeof(magic_xz), magic_xz, {load_xz, unload_xz}},
 
387
        {sizeof(magic_xz),      KMOD_FILE_COMPRESSION_XZ, magic_xz, {load_xz, unload_xz}},
387
388
#endif
388
389
#ifdef ENABLE_ZLIB
389
 
        {sizeof(magic_zlib), magic_zlib, {load_zlib, unload_zlib}},
 
390
        {sizeof(magic_zlib),    KMOD_FILE_COMPRESSION_ZLIB, magic_zlib, {load_zlib, unload_zlib}},
390
391
#endif
391
 
        {0, NULL, {NULL, NULL}}
 
392
        {0,                     KMOD_FILE_COMPRESSION_NONE, NULL, {NULL, NULL}}
392
393
};
393
394
 
394
395
static int load_reg(struct kmod_file *file)
403
404
                            file->fd, 0);
404
405
        if (file->memory == MAP_FAILED)
405
406
                return -errno;
406
 
        file->direct = true;
 
407
 
407
408
        return 0;
408
409
}
409
410
 
448
449
                        magic_size_max = itr->magic_size;
449
450
        }
450
451
 
451
 
        file->direct = false;
452
452
        if (magic_size_max > 0) {
453
453
                char *buf = alloca(magic_size_max + 1);
454
454
                ssize_t sz;
468
468
                }
469
469
 
470
470
                for (itr = comp_types; itr->ops.load != NULL; itr++) {
471
 
                        if (memcmp(buf, itr->magic_bytes, itr->magic_size) == 0)
 
471
                        if (memcmp(buf, itr->magic_bytes, itr->magic_size) == 0) {
 
472
                                file->ops = &itr->ops;
 
473
                                file->compression = itr->compression;
472
474
                                break;
 
475
                        }
473
476
                }
474
 
                if (itr->ops.load != NULL)
475
 
                        file->ops = &itr->ops;
476
477
        }
477
478
 
478
 
        if (file->ops == NULL)
 
479
        if (file->ops == NULL) {
479
480
                file->ops = &reg_ops;
 
481
                file->compression = KMOD_FILE_COMPRESSION_NONE;
 
482
        }
480
483
 
481
484
        file->ctx = ctx;
482
485
 
516
519
 
517
520
bool kmod_file_get_direct(const struct kmod_file *file)
518
521
{
519
 
        return file->direct;
 
522
        return file->compression == KMOD_FILE_COMPRESSION_NONE;
520
523
}
521
524
 
522
525
int kmod_file_get_fd(const struct kmod_file *file)