Author SHA1 Message Date
Konrad Dybcio 96f76d8481 treewide: meson 2024-04-23 19:30:30 +02:00
5 changed files with 17 additions and 181 deletions

No files matched your search

+15 -27
View File
@@ -1,44 +1,32 @@
# SPDX-License-Identifier: BSD-3-Clause
project('tqftpserv',
'c',
version: '1.1',
default_options: [
'warning_level=1',
'buildtype=release',
])
prefix = get_option('prefix')
zstd_dep = dependency('libzstd')
# Not required to build the executable, only to install unit file
systemd = dependency('systemd', required : false)
if systemd.found()
systemd_system_unit_dir = get_option('systemd-unit-prefix')
if systemd_system_unit_dir == ''
systemd_system_unit_dir = systemd.get_variable(
pkgconfig : 'systemdsystemunitdir',
pkgconfig_define: ['prefix', prefix])
endif
systemd = dependency('systemd')
systemd_system_unit_dir = get_option('systemd-unit-prefix')
if systemd_system_unit_dir == ''
systemd_system_unit_dir = systemd.get_variable(
pkgconfig : 'systemdsystemunitdir',
pkgconfig_define: ['prefix', prefix])
endif
qrtr_dep = dependency('qrtr')
tqftpserv_srcs = ['translate.c',
'tqftpserv.c',
'zstd-decompress.c']
'tqftpserv.c']
executable('tqftpserv',
tqftpserv_srcs,
dependencies : [qrtr_dep, zstd_dep],
dependencies : qrtr_dep,
install : true)
if systemd.found()
systemd_unit_conf = configuration_data()
systemd_unit_conf.set('prefix', prefix)
configure_file(
input : 'tqftpserv.service.in',
output : 'tqftpserv.service',
configuration : systemd_unit_conf,
install_dir : systemd_system_unit_dir)
endif
systemd_unit_conf = configuration_data()
systemd_unit_conf.set('prefix', prefix)
configure_file(
input : 'tqftpserv.service.in',
output : 'tqftpserv.service',
configuration : systemd_unit_conf,
install_dir : systemd_system_unit_dir)
-4
View File
@@ -15,7 +15,6 @@
#include "list.h"
#include "translate.h"
#include "zstd-decompress.h"
#define MAX(x, y) ((x) > (y) ? (x) : (y))
@@ -571,8 +570,6 @@ int main(int argc, char **argv)
exit(1);
}
zstd_init();
for (;;) {
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
@@ -674,7 +671,6 @@ int main(int argc, char **argv)
}
close(fd);
zstd_free();
return 0;
}
+2 -31
View File
@@ -16,7 +16,6 @@
#include <unistd.h>
#include "translate.h"
#include "zstd-decompress.h"
#define READONLY_PATH "/readonly/firmware/image/"
#define READWRITE_PATH "/readwrite/"
@@ -29,8 +28,6 @@
#define TQFTPSERV_TMP "/data/vendor/tmp/tqftpserv"
#endif
static int open_maybe_compressed(const char *path);
static void read_fw_path_from_sysfs(char *outbuffer, size_t bufsize)
{
size_t pathsize;
@@ -121,7 +118,7 @@ static int translate_readonly(const char *file)
strcat(path, "/");
strcat(path, file);
fd = open_maybe_compressed(path);
fd = open(path, O_RDONLY);
if (fd >= 0)
break;
if (errno != ENOENT)
@@ -138,8 +135,7 @@ static int translate_readonly(const char *file)
strcat(path, "/");
strcat(path, file);
fd = open_maybe_compressed(path);
fd = open(path, O_RDONLY);
if (fd >= 0)
break;
@@ -205,28 +201,3 @@ int translate_open(const char *path, int flags)
errno = ENOENT;
return -1;
}
/* linux-firmware uses .zst as file extension */
#define ZSTD_EXTENSION ".zst"
/**
* open_maybe_compressed() - open a file and maybe decompress it if necessary
* @filename: path to a file that may be compressed (should not include compression format extension)
*
* Return: opened fd on success, -1 on error
*/
static int open_maybe_compressed(const char *path)
{
int fd = -1;
if (access(path, F_OK) == 0) {
fd = open(path, O_RDONLY);
} else {
char *path_with_zstd_extension = NULL;
asprintf(&path_with_zstd_extension, "%s%s", path, ZSTD_EXTENSION);
fd = zstd_decompress_file(path_with_zstd_extension);
free(path_with_zstd_extension);
}
return fd;
}
-104
View File
@@ -1,104 +0,0 @@
// SPDX-License-Identifier: BSD-3-Clause
/*
* Copyright (c) 2024, Stefan Hansson
*/
/* For memfd_create */
#define _GNU_SOURCE
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <zstd.h>
#include "zstd-decompress.h"
static ZSTD_DCtx *zstd_context = NULL;
/**
* zstd_init() - set up state for decompression. Needs to be called before zstd_decompress_file()
*/
void zstd_init()
{
zstd_context = ZSTD_createDCtx();
}
/**
* zstd_free() - free state used for decompression. zstd_decompress_file() may not be called after this
*/
void zstd_free()
{
ZSTD_freeDCtx(zstd_context);
}
/**
* zstd_decompress_file() - decompress a zstd-compressed file
* @filename: path to a file to decompress
*
* Return: opened fd on success, -1 on error
*/
int zstd_decompress_file(const char *filename)
{
/* Figure out the size of the file. */
struct stat file_stat;
if (stat(filename, &file_stat) == -1) {
perror("stat failed");
return -1;
}
const size_t file_size = file_stat.st_size;
const int input_file_fd = open(filename, 0);
if (input_file_fd == -1) {
perror("open failed");
return -1;
}
void* const compressed_buffer = mmap(NULL, file_size, PROT_READ, MAP_POPULATE | MAP_PRIVATE, input_file_fd, 0);
if (compressed_buffer == MAP_FAILED) {
perror("mmap failed");
close(input_file_fd);
return -1;
}
close(input_file_fd);
const unsigned long long decompressed_size = ZSTD_getFrameContentSize(compressed_buffer, file_size);
if (decompressed_size == ZSTD_CONTENTSIZE_UNKNOWN) {
fprintf(stderr, "Content size could not be determined for %s\n", filename);
return -1;
}
if (decompressed_size == ZSTD_CONTENTSIZE_ERROR) {
fprintf(stderr, "Error getting content size for %s\n", filename);
return -1;
}
void* const decompressed_buffer = malloc((size_t)decompressed_size);
if (decompressed_buffer == NULL) {
perror("malloc failed");
return -1;
}
const size_t return_size = ZSTD_decompressDCtx(zstd_context, decompressed_buffer, decompressed_size, compressed_buffer, file_size);
if (ZSTD_isError(return_size)) {
fprintf(stderr, "ZSTD_decompress failed: %s\n", ZSTD_getErrorName(return_size));
return -1;
}
const int output_file_fd = memfd_create(filename, 0);
if (output_file_fd == -1) {
perror("memfd_create failed");
return -1;
}
if (write(output_file_fd, decompressed_buffer, decompressed_size) != decompressed_size) {
perror("write failed");
close(output_file_fd);
return -1;
}
return output_file_fd;
}
-15
View File
@@ -1,15 +0,0 @@
// SPDX-License-Identifier: BSD-3-Clause
/*
* Copyright (c) 2024, Stefan Hansson
*/
#ifndef __ZSTD_DECOMPRESS_H__
#define __ZSTD_DECOMPRESS_H__
#include <stdbool.h>
void zstd_init();
void zstd_free();
int zstd_decompress_file(const char *filename);
#endif