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 || 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) if (http_state->files_ts->head != NULL)
printf("filedata len not 11 but %"PRIu64": ", printf("filedata len not 11 but %"PRIu64": ",
FileSize(http_state->files_ts->head)); FileDataSize(http_state->files_ts->head));
goto end; goto end;
} }

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

@ -110,7 +110,7 @@ static int DetectFileInspect(ThreadVars *tv, DetectEngineThreadCtx *det_ctx,
break; break;
} }
uint64_t file_size = FileSize(file); uint64_t file_size = FileDataSize(file);
if ((s->file_flags & FILE_SIG_NEED_MAGIC) && file_size == 0) { if ((s->file_flags & FILE_SIG_NEED_MAGIC) && file_size == 0) {
SCLogDebug("sig needs file content, but we don't have any"); SCLogDebug("sig needs file content, but we don't have any");
r = DETECT_ENGINE_INSPECT_SIG_NO_MATCH; 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; const uint8_t *buffer = NULL;
*buffer_len = 0; *buffer_len = 0;
*stream_start_offset = 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 (det_ctx->smtp_buffers_list_len == 0) {
if (SMTPCreateSpace(det_ctx, 1) < 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; det_ctx->smtp[index].offset = curr_file->content_inspected;
/* updat inspected tracker */ /* 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); 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) int FilemagicGlobalLookup(File *file)
{ {
if (file == NULL || FileSize(file) == 0) { if (file == NULL || FileDataSize(file) == 0) {
SCReturnInt(-1); SCReturnInt(-1);
} }
@ -121,7 +121,7 @@ int FilemagicGlobalLookup(File *file)
StreamingBufferGetData(file->sb, StreamingBufferGetData(file->sb,
&data, &data_len, &offset); &data, &data_len, &offset);
if (offset == 0) { if (offset == 0) {
if (FileSize(file) >= FILEMAGIC_MIN_SIZE) { if (FileDataSize(file) >= FILEMAGIC_MIN_SIZE) {
file->magic = MagicGlobalLookup(data, data_len); file->magic = MagicGlobalLookup(data, data_len);
} else if (file->state >= FILE_STATE_CLOSED) { } else if (file->state >= FILE_STATE_CLOSED) {
file->magic = MagicGlobalLookup(data, data_len); file->magic = MagicGlobalLookup(data, data_len);
@ -141,7 +141,7 @@ int FilemagicGlobalLookup(File *file)
*/ */
int FilemagicThreadLookup(magic_t *ctx, 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); SCReturnInt(-1);
} }
@ -152,7 +152,7 @@ int FilemagicThreadLookup(magic_t *ctx, File *file)
StreamingBufferGetData(file->sb, StreamingBufferGetData(file->sb,
&data, &data_len, &offset); &data, &data_len, &offset);
if (offset == 0) { if (offset == 0) {
if (FileSize(file) >= FILEMAGIC_MIN_SIZE) { if (FileDataSize(file) >= FILEMAGIC_MIN_SIZE) {
file->magic = MagicThreadLookup(ctx, data, data_len); file->magic = MagicThreadLookup(ctx, data, data_len);
} else if (file->state >= FILE_STATE_CLOSED) { } else if (file->state >= FILE_STATE_CLOSED) {
file->magic = MagicThreadLookup(ctx, data, data_len); 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; DetectFilesizeData *fsd = (DetectFilesizeData *)m->ctx;
int ret = 0; 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); SCLogDebug("file size %"PRIu64", check %"PRIu64, file_size, fsd->size1);

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

@ -291,7 +291,7 @@ static void LogFilestoreLogCloseMetaFile(const File *ff)
fprintf(fp, "STATE: UNKNOWN\n"); fprintf(fp, "STATE: UNKNOWN\n");
break; break;
} }
fprintf(fp, "SIZE: %"PRIu64"\n", FileSize(ff)); fprintf(fp, "SIZE: %"PRIu64"\n", FileTrackedSize(ff));
fclose(fp); fclose(fp);
} else { } 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 /* if we have no data chunks left to log, we should still
* close the logger(s) */ * close the logger(s) */
if (FileSize(ff) == ff->content_stored && if (FileDataSize(ff) == ff->content_stored &&
(file_trunc || file_close)) { (file_trunc || file_close)) {
CallLoggers(tv, store, p, ff, NULL, 0, OUTPUT_FILEDATA_FLAG_CLOSE); CallLoggers(tv, store, p, ff, NULL, 0, OUTPUT_FILEDATA_FLAG_CLOSE);
ff->flags |= FILE_STORED; ff->flags |= FILE_STORED;

@ -171,7 +171,7 @@ static void FileWriteJsonRecord(JsonFileLogThread *aft, const Packet *p, const F
if (ff->flags & FILE_STORED) { if (ff->flags & FILE_STORED) {
json_object_set_new(fjs, "file_id", json_integer(ff->file_id)); 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)); json_object_set_new(fjs, "tx_id", json_integer(ff->txid));
/* originally just 'file', but due to bug 1127 naming it fileinfo */ /* 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 * 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) { if (file != NULL && file->sb != NULL) {
SCLogDebug("returning %"PRIu64, SCLogDebug("returning %"PRIu64,
@ -283,6 +283,20 @@ uint64_t FileSize(const File *file)
return 0; 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) static int FilePruneFile(File *file)
{ {
SCEnter(); SCEnter();
@ -299,7 +313,7 @@ static int FilePruneFile(File *file)
#endif #endif
uint64_t left_edge = file->content_stored; uint64_t left_edge = file->content_stored;
if (file->flags & FILE_NOSTORE) { if (file->flags & FILE_NOSTORE) {
left_edge = FileSize(file); left_edge = FileDataSize(file);
} }
if (file->flags & FILE_USE_DETECT) { if (file->flags & FILE_USE_DETECT) {
left_edge = MIN(left_edge, file->content_inspected); left_edge = MIN(left_edge, file->content_inspected);
@ -309,7 +323,7 @@ static int FilePruneFile(File *file)
StreamingBufferSlideToOffset(file->sb, left_edge); StreamingBufferSlideToOffset(file->sb, left_edge);
} }
if (left_edge != FileSize(file)) { if (left_edge != FileDataSize(file)) {
SCReturnInt(0); SCReturnInt(0);
} }
@ -516,7 +530,7 @@ static int FileStoreNoStoreCheck(File *ff)
if (ff->flags & FILE_NOSTORE) { if (ff->flags & FILE_NOSTORE) {
if (ff->state == FILE_STATE_OPENED && if (ff->state == FILE_STATE_OPENED &&
FileSize(ff) >= (uint64_t)FileMagicSize()) FileDataSize(ff) >= (uint64_t)FileMagicSize())
{ {
SCReturnInt(1); SCReturnInt(1);
} }
@ -706,7 +720,7 @@ File *FileOpenFile(FileContainer *ffc, const StreamingBufferConfig *sbcfg,
ff->state = FILE_STATE_ERROR; ff->state = FILE_STATE_ERROR;
SCReturnPtr(NULL, "File"); SCReturnPtr(NULL, "File");
} }
SCLogDebug("file size is now %"PRIu64, FileSize(ff)); SCLogDebug("file size is now %"PRIu64, FileTrackedSize(ff));
} }
SCReturnPtr(ff, "File"); SCReturnPtr(ff, "File");
@ -1034,7 +1048,7 @@ void FileDisableStoringForFile(File *ff)
SCLogDebug("not storing this file"); SCLogDebug("not storing this file");
ff->flags |= FILE_NOSTORE; 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 if (g_file_force_md5 == 0 && g_file_force_sha1 == 0 && g_file_force_sha256 == 0
&& g_file_force_tracking == 0) { && g_file_force_tracking == 0) {
(void)FileCloseFilePtr(ff, NULL, 0, (void)FileCloseFilePtr(ff, NULL, 0,

@ -214,7 +214,8 @@ void FileStoreFileById(FileContainer *fc, uint32_t);
void FileTruncateAllOpenFiles(FileContainer *); 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); 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->file_id);
lua_pushnumber(luastate, file->txid); lua_pushnumber(luastate, file->txid);
lua_pushlstring(luastate, (char *)file->name, file->name_len); lua_pushlstring(luastate, (char *)file->name, file->name_len);
lua_pushnumber(luastate, FileSize(file)); lua_pushnumber(luastate, FileTrackedSize(file));
lua_pushstring (luastate, lua_pushstring (luastate,
#ifdef HAVE_MAGIC #ifdef HAVE_MAGIC
file->magic file->magic

Loading…
Cancel
Save