refractor untar

This commit is contained in:
Martijn Scheepers
2026-02-27 11:55:07 +01:00
parent b659508cde
commit baaab652ae
4 changed files with 19 additions and 102 deletions

View File

@@ -34,7 +34,7 @@ enum tar_state {
TAR_WRITE_ERROR,
TAR_FILE_ERROR,
TAR_SOURCE_EOF,
TAR_CHECKSUM_MISMACH,
TAR_CHECKSUM_MISMATCH,
};
class Tar

View File

@@ -327,8 +327,8 @@ int main()
// mbed_trace_config_set(TRACE_ACTIVE_LEVEL_ALL);
//mbed_trace_config_set(TRACE_ACTIVE_LEVEL_INFO);
mbed_trace_config_set(TRACE_ACTIVE_LEVEL_NONE);
mbed_trace_config_set(TRACE_ACTIVE_LEVEL_INFO);
//mbed_trace_config_set(TRACE_ACTIVE_LEVEL_NONE);
tr_debug("this is debug msg"); //-> "[DBG ][main]: this is a debug msg"
tr_info("this is info msg"); //-> "[INFO][main]: this is an info msg"

View File

@@ -25,7 +25,10 @@ namespace HTTPWebServer
std::string Request::getHeader(std::string name)
{
std::transform(name.begin(), name.end(), name.begin(), [](unsigned char c)
{ return std::tolower(c); });
{
return std::tolower(c);
});
if (_headers.find(name) != _headers.end())
{
return _headers[name];
@@ -162,60 +165,6 @@ namespace HTTPWebServer
return _url;
}
// bool Request::writeMultipartData(mbed::File *file)
// {
// // printf("writeMultipartData len:%d\r\n", _contentLength);
// bool start_found = false;
// int len = _contentLength;
// char buff[1024];
// while (len > 0)
// {
// nsapi_size_or_error_t recv_ret = _socket->recv(buff, sizeof(buff));
// // printf("ret: %d - %d - %d\r\n", recv_ret, len, (100*len + _contentLength/2)/_contentLength);
// if (recv_ret < 0)
// {
// return false;
// }
// if (!start_found)
// {
// for (int i = 0; i < (recv_ret - 4); i++)
// {
// // printf("memcmp = %d : %d\r\n", i, memcmp(&buff[i], "\r\n\r\n", 4));
// // printf("%c %c %c %c\r\n",buff[i], buff[i + 1], buff[i + 2], buff[i + 3]);
// if (memcmp(&buff[i], "\r\n\r\n", 4) == 0)
// {
// int size = recv_ret - i - 4;
// start_found = true;
// // printf("i:%d size:%d recv:%d\r\n", i, size, recv_ret);
// // printf("write size: %d\r\n", size);
// // ssize_t res = file->write(ptr, size);
// ssize_t res = file->write(&buff[i + 4], size);
// // printf("write %d\r\n", res);
// if (res != size)
// {
// printf("write error\r\n");
// }
// break;
// }
// }
// }
// else
// {
// file->write(buff, recv_ret);
// // ssize_t res = file->write(buff, recv_ret);
// // printf("write %d\r\n", res);
// }
// len = len - recv_ret;
// }
// return true;
// }
bool Request::writeMultipartData(FIL *file)
{
printf("writeMultipartData len:%d\r\n", _contentLength);
@@ -226,6 +175,7 @@ namespace HTTPWebServer
// printf("%s - %s\n", it->first.c_str(), it->second.c_str());
// }
//prepares or allocates a contiguous data area to the file
int res = ACSFileSystem::expandFile(file, _contentLength, 1);
if (res != FR_OK)
{

View File

@@ -82,16 +82,13 @@ tar_state Tar::extract(FIL *tarfile, const char *folder)
if (!verify_checksum(block))
{
tr_error("Checksum failure");
return TAR_CHECKSUM_MISMACH;
return TAR_CHECKSUM_MISMATCH;
}
if (folder != NULL)
{
tr_info("use folder: %s", folder);
tr_info("use block: %s", block);
// memset(fullpath, 0, TAR_FILENAME_LENGTH);
// strcat(fullpath, folder);
// strcat(fullpath, block);
fullpath.clear();
fullpath.append(folder);
fullpath.append(block);
@@ -99,10 +96,8 @@ tar_state Tar::extract(FIL *tarfile, const char *folder)
else
{
tr_info("use block: %s", block);
//strncpy(fullpath, block, TAR_FILENAME_LENGTH);
fullpath = block;
}
//tr_info("full path: %s", fullpath);
};
tr_info("full path: %s", fullpath.c_str());
filesize = parseoct(&block[TAR_FILESIZE_OFFSET], TAR_FILESIZE_LENGTH);
@@ -130,15 +125,11 @@ tar_state Tar::extract(FIL *tarfile, const char *folder)
tr_info("Ignoring FIFO");
break;
default:
//tr_info("Extracting file %s", fullpath);
tr_info("Extracting file %s", fullpath.c_str());
currentfile = new FIL;
if (ACSFileSystem::openFile(fullpath.c_str(), currentfile, O_RDWR | O_CREAT) != MBED_SUCCESS)
//if (ACSFileSystem::openFile(fullpath, currentfile, O_RDWR | O_CREAT) != MBED_SUCCESS)
// if (ACSFileSystem::openFile(fullpath.c_str(), currentfile, FA_OPEN_APPEND | FA_CREATE_ALWAYS) != MBED_SUCCESS)
{
//tr_error("Open file failed: %s", fullpath);
tr_error("Open file failed: %s", fullpath.c_str());
delete currentfile;
return TAR_FILE_ERROR;
@@ -161,42 +152,18 @@ tar_state Tar::extract(FIL *tarfile, const char *folder)
{
if (bytes_read == 0)
{
if (filesize > TAR_LARGE_BLOCKSIZE)
ssize_t to_read = (filesize > TAR_LARGE_BLOCKSIZE) ? TAR_LARGE_BLOCKSIZE : TAR_BLOCKSIZE;
printf("to_read : %d\r\n", to_read);
bytes_read = ACSFileSystem::readFile(tarfile, block, to_read);
if (bytes_read < to_read)
{
bytes_read = ACSFileSystem::readFile(tarfile, block, TAR_LARGE_BLOCKSIZE);
if (bytes_read < TAR_LARGE_BLOCKSIZE)
{
tr_error("Data short read: Expected %d, got %d", TAR_LARGE_BLOCKSIZE, bytes_read);
return TAR_SHORT_READ;
}
if (filesize < TAR_LARGE_BLOCKSIZE)
{
bytes_read = filesize;
}
tr_error("Data short read: Expected %d, got %d", to_read, bytes_read);
return TAR_SHORT_READ;
}
else
if (filesize < to_read)
{
bytes_read = ACSFileSystem::readFile(tarfile, block, TAR_BLOCKSIZE);
if (bytes_read < TAR_BLOCKSIZE)
{
tr_error("Data short read: Expected %d, got %d", TAR_BLOCKSIZE, bytes_read);
return TAR_SHORT_READ;
}
if (filesize < TAR_BLOCKSIZE)
{
bytes_read = filesize;
}
// bytes_read = ACSFileSystem::readFile(tarfile, block, filesize);
// if (bytes_read < filesize)
// {
// tr_error("Data short read: Expected %d, got %d", filesize, bytes_read);
// return TAR_SHORT_READ;
// }
// //if (filesize < TAR_BLOCKSIZE)
// //{
// bytes_read = filesize;
// //}
bytes_read = filesize;
}
printf("bytes_read 2 : %d\r\n", bytes_read);
}