Skip to content

Commit

Permalink
Add API to create empty files with usblink_put_file
Browse files Browse the repository at this point in the history
  • Loading branch information
Vogtinator committed Oct 13, 2024
1 parent 49b2b69 commit d51aac5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
29 changes: 18 additions & 11 deletions core/usblink.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ void put_file_next(struct packet *in) {
teardown:
throttle_timer_on();
put_file_state = 0;
fclose(put_file);
if (put_file)
fclose(put_file);
put_file = NULL;
}

Expand Down Expand Up @@ -527,7 +528,7 @@ void usblink_received_packet(const uint8_t *data, uint32_t size) {
bool usblink_put_file(const char *local, const char *remote, usblink_progress_cb callback, void *user_data) {
mode = File_Send;

char *dot = strrchr(local, '.');
char *dot = local ? strrchr(local, '.') : NULL;
// TODO (thanks for the reminder, Excale :P) : Filter depending on which model is being emulated
if (dot && (!strcmp(dot, ".tno") || !strcmp(dot, ".tnc")
|| !strcmp(dot, ".tco") || !strcmp(dot, ".tcc")
Expand All @@ -542,17 +543,23 @@ bool usblink_put_file(const char *local, const char *remote, usblink_progress_cb
current_user_data = user_data;
current_file_callback = callback;

FILE *f = fopen_utf8(local, "rb");
if (!f) {
gui_perror(local);
return 0;
}
if (put_file)
fclose(put_file);
put_file = f;
fseek(f, 0, SEEK_END);
put_file_size_orig = put_file_size = ftell(f);
fseek(f, 0, SEEK_SET);

if (local && local[0] != '\0') {
put_file = fopen_utf8(local, "rb");
if (!put_file) {
gui_perror(local);
return 0;
}
fseek(put_file, 0, SEEK_END);
put_file_size_orig = put_file_size = ftell(put_file);
fseek(put_file, 0, SEEK_SET);
} else {
put_file = NULL;
put_file_size_orig = put_file_size = 0;
}

put_file_state = SENDING_03;

/* Send the first packet */
Expand Down
1 change: 1 addition & 0 deletions core/usblink.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ typedef void (*usblink_progress_cb)(int progress, void *user_data);
void usblink_delete(const char *path, bool is_dir, usblink_progress_cb callback, void *user_data);
void usblink_dirlist(const char *path, usblink_dirlist_cb callback, void *user_data);
bool usblink_get_file(const char *path, const char *dest, usblink_progress_cb callback, void *user_data);
/* local = NULL or empty creates an empty file */
bool usblink_put_file(const char *local, const char *remote, usblink_progress_cb callback, void *user_data);
void usblink_new_dir(const char *path, usblink_progress_cb callback, void *user_data);
void usblink_move(const char *old_path, const char *new_path, usblink_progress_cb callback, void *user_data);
Expand Down

0 comments on commit d51aac5

Please sign in to comment.