util-file: introduce new functions for file size

This patch introduces the FileDataSize and FileTrackedSize functions.
The first one is just a renaming of the initial FilSize function
whereas the other one is using the newly introduced size field as
value.
pull/2514/head
Eric Leblond 9 years ago
parent a098896b28
commit 569cc5d238

@ -633,11 +633,11 @@ static int HTPFileParserTest03(void)
}
if (http_state->files_ts->head == NULL ||
FileSize(http_state->files_ts->head) != 11)
FileDataSize(http_state->files_ts->head) != 11)
{
if (http_state->files_ts->head != NULL)
printf("filedata len not 11 but %"PRIu64": ",
FileSize(http_state->files_ts->head));
FileDataSize(http_state->files_ts->head));
goto end;
}

@ -371,7 +371,7 @@ static void SMTPPruneFiles(FileContainer *files)
if (file->sb->stream_offset == 0)
window = MAX(window, smtp_config.content_inspect_min_size);
uint64_t file_size = FileSize(file);
uint64_t file_size = FileDataSize(file);
uint64_t data_size = file_size - file->sb->stream_offset;
SCLogDebug("window %"PRIu32", file_size %"PRIu64", data_size %"PRIu64,
@ -4840,8 +4840,8 @@ static int SMTPParserTest14(void)
printf("smtp-mime file name is incorrect");
goto end;
}
if (FileSize(file) != filesize){
printf("smtp-mime file size %"PRIu64" is incorrect", FileSize(file));
if (FileTrackedSize(file) != filesize){
printf("smtp-mime file size %"PRIu64" is incorrect", FileDataSize(file));
goto end;
}
static uint8_t org_binary[] = {
@ -5127,7 +5127,7 @@ static int SMTPProcessDataChunkTest05(void){
FAIL_IF(file == NULL);
ret = SMTPProcessDataChunk((uint8_t *)mimemsg, sizeof(mimemsg), state);
FAIL_IF(ret != 0);
FAIL_IF((uint32_t)FileSize(file) != 106);
FAIL_IF((uint32_t)FileDataSize(file) != 106);
SMTPStateFree(smtp_state);
FLOW_DESTROY(&f);
PASS;

@ -110,7 +110,7 @@ static int DetectFileInspect(ThreadVars *tv, DetectEngineThreadCtx *det_ctx,
break;
}
uint64_t file_size = FileSize(file);
uint64_t file_size = FileDataSize(file);
if ((s->file_flags & FILE_SIG_NEED_MAGIC) && file_size == 0) {
SCLogDebug("sig needs file content, but we don't have any");
r = DETECT_ENGINE_INSPECT_SIG_NO_MATCH;

@ -96,7 +96,7 @@ static const uint8_t *DetectEngineSMTPGetBufferForTX(uint64_t tx_id,
const uint8_t *buffer = NULL;
*buffer_len = 0;
*stream_start_offset = 0;
uint64_t file_size = FileSize(curr_file);
uint64_t file_size = FileDataSize(curr_file);
if (det_ctx->smtp_buffers_list_len == 0) {
if (SMTPCreateSpace(det_ctx, 1) < 0)
@ -160,7 +160,7 @@ static const uint8_t *DetectEngineSMTPGetBufferForTX(uint64_t tx_id,
det_ctx->smtp[index].offset = curr_file->content_inspected;
/* updat inspected tracker */
curr_file->content_inspected = FileSize(curr_file);
curr_file->content_inspected = FileDataSize(curr_file);
SCLogDebug("content_inspected %u, offset %u", (uint)curr_file->content_inspected, (uint)det_ctx->smtp[index].offset);

@ -110,7 +110,7 @@ void DetectFilemagicRegister(void)
*/
int FilemagicGlobalLookup(File *file)
{
if (file == NULL || FileSize(file) == 0) {
if (file == NULL || FileDataSize(file) == 0) {
SCReturnInt(-1);
}
@ -121,7 +121,7 @@ int FilemagicGlobalLookup(File *file)
StreamingBufferGetData(file->sb,
&data, &data_len, &offset);
if (offset == 0) {
if (FileSize(file) >= FILEMAGIC_MIN_SIZE) {
if (FileDataSize(file) >= FILEMAGIC_MIN_SIZE) {
file->magic = MagicGlobalLookup(data, data_len);
} else if (file->state >= FILE_STATE_CLOSED) {
file->magic = MagicGlobalLookup(data, data_len);
@ -141,7 +141,7 @@ int FilemagicGlobalLookup(File *file)
*/
int FilemagicThreadLookup(magic_t *ctx, File *file)
{
if (ctx == NULL || file == NULL || FileSize(file) == 0) {
if (ctx == NULL || file == NULL || FileDataSize(file) == 0) {
SCReturnInt(-1);
}
@ -152,7 +152,7 @@ int FilemagicThreadLookup(magic_t *ctx, File *file)
StreamingBufferGetData(file->sb,
&data, &data_len, &offset);
if (offset == 0) {
if (FileSize(file) >= FILEMAGIC_MIN_SIZE) {
if (FileDataSize(file) >= FILEMAGIC_MIN_SIZE) {
file->magic = MagicThreadLookup(ctx, data, data_len);
} else if (file->state >= FILE_STATE_CLOSED) {
file->magic = MagicThreadLookup(ctx, data, data_len);

@ -93,7 +93,7 @@ static int DetectFilesizeMatch (ThreadVars *t, DetectEngineThreadCtx *det_ctx, F
DetectFilesizeData *fsd = (DetectFilesizeData *)m->ctx;
int ret = 0;
uint64_t file_size = FileSize(file);
uint64_t file_size = FileTrackedSize(file);
SCLogDebug("file size %"PRIu64", check %"PRIu64, file_size, fsd->size1);

@ -319,7 +319,7 @@ static void LogFileWriteJsonRecord(LogFileLogThread *aft, const Packet *p, const
break;
}
fprintf(fp, "\"stored\": %s, ", ff->flags & FILE_STORED ? "true" : "false");
fprintf(fp, "\"size\": %"PRIu64" ", FileSize(ff));
fprintf(fp, "\"size\": %"PRIu64" ", FileTrackedSize(ff));
fprintf(fp, "}\n");
fflush(fp);
SCMutexUnlock(&aft->file_ctx->fp_mutex);

@ -291,7 +291,7 @@ static void LogFilestoreLogCloseMetaFile(const File *ff)
fprintf(fp, "STATE: UNKNOWN\n");
break;
}
fprintf(fp, "SIZE: %"PRIu64"\n", FileSize(ff));
fprintf(fp, "SIZE: %"PRIu64"\n", FileTrackedSize(ff));
fclose(fp);
} else {

@ -183,7 +183,7 @@ static TmEcode OutputFiledataLog(ThreadVars *tv, Packet *p, void *thread_data)
/* if we have no data chunks left to log, we should still
* close the logger(s) */
if (FileSize(ff) == ff->content_stored &&
if (FileDataSize(ff) == ff->content_stored &&
(file_trunc || file_close)) {
CallLoggers(tv, store, p, ff, NULL, 0, OUTPUT_FILEDATA_FLAG_CLOSE);
ff->flags |= FILE_STORED;

@ -171,7 +171,7 @@ static void FileWriteJsonRecord(JsonFileLogThread *aft, const Packet *p, const F
if (ff->flags & FILE_STORED) {
json_object_set_new(fjs, "file_id", json_integer(ff->file_id));
}
json_object_set_new(fjs, "size", json_integer(ff->size));
json_object_set_new(fjs, "size", json_integer(FileTrackedSize(ff)));
json_object_set_new(fjs, "tx_id", json_integer(ff->txid));
/* originally just 'file', but due to bug 1127 naming it fileinfo */

@ -267,12 +267,12 @@ int FileMagicSize(void)
}
/**
* \brief get the size of the file
* \brief get the size of the file data
*
* This doesn't reflect how much of the file we have in memory, just the
* total size tracked so far.
* total size of filedata so far.
*/
uint64_t FileSize(const File *file)
uint64_t FileDataSize(const File *file)
{
if (file != NULL && file->sb != NULL) {
SCLogDebug("returning %"PRIu64,
@ -283,6 +283,20 @@ uint64_t FileSize(const File *file)
return 0;
}
/**
* \brief get the size of the file
*
* This doesn't reflect how much of the file we have in memory, just the
* total size of file so far.
*/
uint64_t FileTrackedSize(const File *file)
{
if (file != NULL) {
return file->size;
}
return 0;
}
static int FilePruneFile(File *file)
{
SCEnter();
@ -299,7 +313,7 @@ static int FilePruneFile(File *file)
#endif
uint64_t left_edge = file->content_stored;
if (file->flags & FILE_NOSTORE) {
left_edge = FileSize(file);
left_edge = FileDataSize(file);
}
if (file->flags & FILE_USE_DETECT) {
left_edge = MIN(left_edge, file->content_inspected);
@ -309,7 +323,7 @@ static int FilePruneFile(File *file)
StreamingBufferSlideToOffset(file->sb, left_edge);
}
if (left_edge != FileSize(file)) {
if (left_edge != FileDataSize(file)) {
SCReturnInt(0);
}
@ -516,7 +530,7 @@ static int FileStoreNoStoreCheck(File *ff)
if (ff->flags & FILE_NOSTORE) {
if (ff->state == FILE_STATE_OPENED &&
FileSize(ff) >= (uint64_t)FileMagicSize())
FileDataSize(ff) >= (uint64_t)FileMagicSize())
{
SCReturnInt(1);
}
@ -706,7 +720,7 @@ File *FileOpenFile(FileContainer *ffc, const StreamingBufferConfig *sbcfg,
ff->state = FILE_STATE_ERROR;
SCReturnPtr(NULL, "File");
}
SCLogDebug("file size is now %"PRIu64, FileSize(ff));
SCLogDebug("file size is now %"PRIu64, FileTrackedSize(ff));
}
SCReturnPtr(ff, "File");
@ -1034,7 +1048,7 @@ void FileDisableStoringForFile(File *ff)
SCLogDebug("not storing this file");
ff->flags |= FILE_NOSTORE;
if (ff->state == FILE_STATE_OPENED && FileSize(ff) >= (uint64_t)FileMagicSize()) {
if (ff->state == FILE_STATE_OPENED && FileDataSize(ff) >= (uint64_t)FileMagicSize()) {
if (g_file_force_md5 == 0 && g_file_force_sha1 == 0 && g_file_force_sha256 == 0
&& g_file_force_tracking == 0) {
(void)FileCloseFilePtr(ff, NULL, 0,

@ -214,7 +214,8 @@ void FileStoreFileById(FileContainer *fc, uint32_t);
void FileTruncateAllOpenFiles(FileContainer *);
uint64_t FileSize(const File *file);
uint64_t FileDataSize(const File *file);
uint64_t FileTrackedSize(const File *file);
uint16_t FileFlowToFlags(const Flow *flow, uint8_t direction);

@ -650,7 +650,7 @@ static int LuaCallbackFileInfoPushToStackFromFile(lua_State *luastate, const Fil
lua_pushnumber(luastate, file->file_id);
lua_pushnumber(luastate, file->txid);
lua_pushlstring(luastate, (char *)file->name, file->name_len);
lua_pushnumber(luastate, FileSize(file));
lua_pushnumber(luastate, FileTrackedSize(file));
lua_pushstring (luastate,
#ifdef HAVE_MAGIC
file->magic

Loading…
Cancel
Save