Merge branch 'topic/corelight/file-rename-unlink' of https://github.com/corelight/bro

* 'topic/corelight/file-rename-unlink' of https://github.com/corelight/bro:
  Add rename, unlink, and rmdir bifs.
This commit is contained in:
Jon Siwek 2017-05-01 10:34:34 -05:00
commit 12e809dfa0
5 changed files with 133 additions and 4 deletions

View file

@ -1,4 +1,8 @@
2.5-134 | 2017-05-01 10:34:34 -0500
* Add rename, unlink, and rmdir bifs. (Corelight)
2.5-131 | 2017-04-21 14:27:16 -0700
* Guard more format strings with __attribute__((format)). (Johanna Amann)

View file

@ -1 +1 @@
2.5-131
2.5-134

View file

@ -4039,6 +4039,7 @@ function set_inactivity_timeout%(cid: conn_id, t: interval%): interval
##
## .. bro:see:: active_file open_for_append close write_file
## get_file_name set_buf flush_all mkdir enable_raw_output
## rmdir unlink rename
function open%(f: string%): file
%{
const char* file = f->CheckString();
@ -4058,6 +4059,7 @@ function open%(f: string%): file
##
## .. bro:see:: active_file open close write_file
## get_file_name set_buf flush_all mkdir enable_raw_output
## rmdir unlink rename
function open_for_append%(f: string%): file
%{
return new Val(new BroFile(f->CheckString(), "a"));
@ -4071,6 +4073,7 @@ function open_for_append%(f: string%): file
##
## .. bro:see:: active_file open open_for_append write_file
## get_file_name set_buf flush_all mkdir enable_raw_output
## rmdir unlink rename
function close%(f: file%): bool
%{
return new Val(f->Close(), TYPE_BOOL);
@ -4086,6 +4089,7 @@ function close%(f: file%): bool
##
## .. bro:see:: active_file open open_for_append close
## get_file_name set_buf flush_all mkdir enable_raw_output
## rmdir unlink rename
function write_file%(f: file, data: string%): bool
%{
if ( ! f )
@ -4106,6 +4110,7 @@ function write_file%(f: file, data: string%): bool
##
## .. bro:see:: active_file open open_for_append close
## get_file_name write_file flush_all mkdir enable_raw_output
## rmdir unlink rename
function set_buf%(f: file, buffered: bool%): any
%{
f->SetBuf(buffered);
@ -4118,6 +4123,7 @@ function set_buf%(f: file, buffered: bool%): any
##
## .. bro:see:: active_file open open_for_append close
## get_file_name write_file set_buf mkdir enable_raw_output
## rmdir unlink rename
function flush_all%(%): bool
%{
return new Val(fflush(0) == 0, TYPE_BOOL);
@ -4127,17 +4133,102 @@ function flush_all%(%): bool
##
## f: The directory name.
##
## Returns: Returns true if the operation succeeds or if *f* already exists,
## Returns: True if the operation succeeds or if *f* already exists,
## and false if the file creation fails.
##
## .. bro:see:: active_file open_for_append close write_file
## get_file_name set_buf flush_all enable_raw_output
## rmdir unlink rename
function mkdir%(f: string%): bool
%{
const char* filename = f->CheckString();
if ( mkdir(filename, 0777) < 0 && errno != EEXIST )
if ( mkdir(filename, 0777) < 0 )
{
builtin_error("cannot create directory", @ARG@[0]);
int error = errno;
struct stat filestat;
// check if already exists and is directory.
if ( errno == EEXIST && stat(filename, &filestat) == 0
&& S_ISDIR(filestat.st_mode) )
return new Val(1, TYPE_BOOL);
builtin_error(fmt("cannot create directory '%s': %s", filename,
strerror(error)));
return new Val(0, TYPE_BOOL);
}
else
return new Val(1, TYPE_BOOL);
%}
## Removes a directory.
##
## d: The directory name.
##
## Returns: True if the operation succeeds, and false if the
## directory delete operation fails.
##
## .. bro:see:: active_file open_for_append close write_file
## get_file_name set_buf flush_all enable_raw_output
## mkdir unlink rename
function rmdir%(d: string%): bool
%{
const char* dirname = d->CheckString();
if ( rmdir(dirname) < 0 )
{
builtin_error(fmt("cannot remove directory '%s': %s", dirname,
strerror(errno)));
return new Val(0, TYPE_BOOL);
}
else
return new Val(1, TYPE_BOOL);
%}
## Removes a file from a directory.
##
## f: the file to delete.
##
## Returns: True if the operation succeeds and the file was deleted,
## and false if the deletion fails.
##
## .. bro:see:: active_file open_for_append close write_file
## get_file_name set_buf flush_all enable_raw_output
## mkdir rmdir rename
function unlink%(f: string%): bool
%{
const char* filename = f->CheckString();
if ( unlink(filename) < 0 )
{
builtin_error(fmt("cannot unlink file '%s': %s", filename,
strerror(errno)));
return new Val(0, TYPE_BOOL);
}
else
return new Val(1, TYPE_BOOL);
%}
## Renames a file from src_f to dst_f.
##
## src_f: the name of the file to rename.
##
## dest_f: the name of the file after the rename operation.
##
## Returns: True if the rename succeeds and false otherwise.
##
## .. bro:see:: active_file open_for_append close write_file
## get_file_name set_buf flush_all enable_raw_output
## mkdir rmdir unlink
function rename%(src_f: string, dst_f: string%): bool
%{
const char* src_filename = src_f->CheckString();
const char* dst_filename = dst_f->CheckString();
if ( rename(src_filename, dst_filename) < 0 )
{
builtin_error(fmt("cannot rename file '%s' to '%s': %s", src_filename,
dst_filename, strerror(errno)));
return new Val(0, TYPE_BOOL);
}
else

View file

@ -0,0 +1,10 @@
T
T
T
T
T
T
F
F
F
F

View file

@ -0,0 +1,24 @@
#
# @TEST-EXEC: bro -b %INPUT >out
# @TEST-EXEC: btest-diff out
event bro_init()
{
# Test succesful operations...
print mkdir("testdir");
print mkdir("testdir");
local a = open("testdir/testfile");
close(a);
print rename("testdir/testfile", "testdir/testfile2");
print rename("testdir", "testdir2");
print unlink("testdir2/testfile2");
print rmdir("testdir2");
print unlink("nonexisting");
print rename("a", "b");
print rmdir("nonexisting");
a = open("testfile");
close(a);
print mkdir("testfile");
}