Skip to content

Commit

Permalink
fix: check space for ramfs for upload file to device
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
  • Loading branch information
zhaojh329 committed Oct 15, 2021
1 parent 861ff74 commit ebd64ec
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <inttypes.h>
#include <sys/statvfs.h>
#include <linux/limits.h>
#include <sys/sysinfo.h>

#include "log/log.h"
#include "file.h"
Expand Down Expand Up @@ -287,13 +288,29 @@ static void start_download_file(struct file_context *ctx, struct buffer *info, i

ment = find_mount_point(savepath);
if (ment) {
if (statvfs(ment->mnt_dir, &sfs) == 0 && ctx->total_size > sfs.f_bavail * sfs.f_frsize) {
send_file_control_msg(ctx->ctlfd, RTTY_FILE_MSG_NO_SPACE, NULL, 0);
uint64_t avail;

if (!strcmp(ment->mnt_type, "ramfs")) {
struct sysinfo si;

if (sysinfo(&si)) {
log_err("download file fail: '%s'\n", strerror(errno));
goto check_space_fail;
}

avail = si.freeram;
} else if (!statvfs(ment->mnt_dir, &sfs)) {
avail = sfs.f_bavail * sfs.f_frsize;
} else {
log_err("download file fail: '%s'\n", strerror(errno));
goto check_space_fail;
}

if (ctx->total_size > avail) {
log_err("download file fail: no enough space\n");
goto check_space_fail;
}
} else {
send_file_control_msg(ctx->ctlfd, RTTY_FILE_MSG_NO_SPACE, NULL, 0);
log_err("download file fail: not found mount point of '%s'\n", savepath);
goto check_space_fail;
}
Expand Down Expand Up @@ -331,6 +348,7 @@ static void start_download_file(struct file_context *ctx, struct buffer *info, i
return;

check_space_fail:
send_file_control_msg(ctx->ctlfd, RTTY_FILE_MSG_NO_SPACE, NULL, 0);
buffer_pull(info, name, len - 4);
open_fail:
file_context_reset(ctx);
Expand Down

0 comments on commit ebd64ec

Please sign in to comment.