Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion tqftpserv.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "list.h"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid adding a number of patches that builds up a bunch of functionality and then just throw it all in the mix at the end. If this change causes an issue, the bug is in any of the patches leading up to this change.

Also avoid doing multiple things in the patch itself. The commit message quite clearly tells that this adds config file support. (It also adds command line arguments, setting log level and other things, but you need to read the full patch to learn that)

#include "translate.h"
#include "logging.h"
#include "config.h"

#define MAX(x, y) ((x) > (y) ? (x) : (y))

Expand Down Expand Up @@ -603,7 +604,7 @@ static void client_close_and_free(struct tftp_client *client)
free(client);
}

int main()
int main(int argc, char **argv)
{
struct tftp_client *client;
struct tftp_client *next;
Expand All @@ -618,6 +619,27 @@ int main()
int ret;
int fd;

/* Initialize configuration with defaults */
tqftp_config_init_defaults(&tqftp_config);

/* Parse command line arguments */
ret = tqftp_config_parse_args(argc, argv, &tqftp_config);
if (ret == 1) {
/* Help was shown */
return 0;
} else if (ret < 0) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both conditional arms here are generally handled by one entity, which just calls exit().

/* Error in arguments */
return 1;
}

/* Initialize logging */
tqftp_log_init_with_config(&tqftp_config.log_config);

TQFTP_LOG_INFO("TQFTP server starting");
TQFTP_LOG_DEBUG("Configuration: readonly_path=%s, readwrite_path=%s, firmware_base=%s, temp_dir=%s",
tqftp_config.readonly_path, tqftp_config.readwrite_path,
tqftp_config.firmware_base, tqftp_config.temp_dir);

fd = qrtr_open(0);
if (fd < 0) {
TQFTP_LOG_ERR("failed to open qrtr socket");
Expand Down
36 changes: 13 additions & 23 deletions translate.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,7 @@
#include "translate.h"
#include "zstd-decompress.h"
#include "logging.h"
#define READONLY_PATH "/readonly/firmware/image/"
#define READWRITE_PATH "/readwrite/"

#ifndef ANDROID
#define FIRMWARE_BASE "/lib/firmware/"
#define TQFTPSERV_TMP "/tmp/tqftpserv"
#define UPDATES_DIR "updates/"
#else
#define FIRMWARE_BASE "/vendor/firmware/"
#define TQFTPSERV_TMP "/data/vendor/tmp/tqftpserv"
#endif
#include "config.h"

static int open_maybe_compressed(const char *path);

Expand Down Expand Up @@ -134,19 +124,19 @@ static int translate_readonly(const char *file)
}

/* now try with base path */
if (strlen(FIRMWARE_BASE) + strlen(UPDATES_DIR) + strlen(firmware_value) + 1 +
if (strlen(tqftp_config.firmware_base) + strlen(tqftp_config.updates_dir) + strlen(firmware_value) + 1 +
strlen(file) + 1 > sizeof(path))
continue;

strcpy(path, FIRMWARE_BASE);
strcat(path, UPDATES_DIR);
strcpy(path, tqftp_config.firmware_base);
strcat(path, tqftp_config.updates_dir);
strcat(path, firmware_path);
strcat(path, "/");
strcat(path, file);

fd = open_maybe_compressed(path);
if (fd < 0) {
strcpy(path, FIRMWARE_BASE);
strcpy(path, tqftp_config.firmware_base);
strcat(path, firmware_path);
strcat(path, "/");
strcat(path, file);
Expand Down Expand Up @@ -179,17 +169,17 @@ static int translate_readwrite(const char *file, int flags)
int ret;
int fd;

ret = mkdir(TQFTPSERV_TMP, 0700);
ret = mkdir(tqftp_config.temp_dir, 0700);
if (ret < 0 && errno != EEXIST) {
TQFTP_LOG_WARN("failed to create temporary tqftpserv directory %s: %s",
TQFTPSERV_TMP, strerror(errno));
tqftp_config.temp_dir, strerror(errno));
return -1;
}

base = open(TQFTPSERV_TMP, O_RDONLY | O_DIRECTORY);
base = open(tqftp_config.temp_dir, O_RDONLY | O_DIRECTORY);
if (base < 0) {
TQFTP_LOG_WARN("failed to open temporary tqftpserv directory %s: %s",
TQFTPSERV_TMP, strerror(errno));
tqftp_config.temp_dir, strerror(errno));
return -1;
}

Expand All @@ -211,10 +201,10 @@ static int translate_readwrite(const char *file, int flags)
*/
int translate_open(const char *path, int flags)
{
if (!strncmp(path, READONLY_PATH, strlen(READONLY_PATH)))
return translate_readonly(path + strlen(READONLY_PATH));
else if (!strncmp(path, READWRITE_PATH, strlen(READWRITE_PATH)))
return translate_readwrite(path + strlen(READWRITE_PATH), flags);
if (!strncmp(path, tqftp_config.readonly_path, strlen(tqftp_config.readonly_path)))
return translate_readonly(path + strlen(tqftp_config.readonly_path));
else if (!strncmp(path, tqftp_config.readwrite_path, strlen(tqftp_config.readwrite_path)))
return translate_readwrite(path + strlen(tqftp_config.readwrite_path), flags);

TQFTP_LOG_ERR("invalid path %s, rejecting", path);
errno = ENOENT;
Expand Down