mirror of
https://github.com/zeek/zeek.git
synced 2025-10-03 15:18:20 +00:00
Replace safe_basename/safe_dirname w/ SafeBasename/SafeDirname.
So errors can be better handled.
This commit is contained in:
parent
96ed7aed1a
commit
3046013d69
5 changed files with 65 additions and 41 deletions
|
@ -104,7 +104,7 @@ string IdentifierDocument::GetFieldComments(const string& field) const
|
|||
ScriptDocument::ScriptDocument(const string& arg_name)
|
||||
: Document(),
|
||||
name(arg_name),
|
||||
is_pkg_loader(safe_basename(name) == PACKAGE_LOADER),
|
||||
is_pkg_loader(SafeBasename(name).result == PACKAGE_LOADER),
|
||||
dependencies(), module_usages(), comments(), identifier_docs(), redefs()
|
||||
{
|
||||
}
|
||||
|
|
|
@ -142,7 +142,7 @@ void Manager::File(const string& path)
|
|||
if ( ! doc->IsPkgLoader() )
|
||||
return;
|
||||
|
||||
name = safe_dirname(name);
|
||||
name = SafeDirname(name).result;
|
||||
|
||||
if ( packages.GetDocument(name) )
|
||||
{
|
||||
|
|
14
src/scan.l
14
src/scan.l
|
@ -59,7 +59,7 @@ static string find_relative_file(const string& filename, const string& ext)
|
|||
return string();
|
||||
|
||||
if ( filename[0] == '.' )
|
||||
return find_file(filename, safe_dirname(::filename), ext);
|
||||
return find_file(filename, SafeDirname(::filename).result, ext);
|
||||
else
|
||||
return find_file(filename, bro_path(), ext);
|
||||
}
|
||||
|
@ -275,7 +275,7 @@ when return TOK_WHEN;
|
|||
@DEBUG return TOK_DEBUG; // marks input for debugger
|
||||
|
||||
@DIR {
|
||||
string rval = safe_dirname(::filename);
|
||||
string rval = SafeDirname(::filename).result;
|
||||
|
||||
if ( ! rval.empty() && rval[0] == '.' )
|
||||
{
|
||||
|
@ -291,15 +291,7 @@ when return TOK_WHEN;
|
|||
}
|
||||
|
||||
@FILENAME {
|
||||
char* filename_copy = copy_string(::filename);
|
||||
const char* bname = basename(filename_copy);
|
||||
|
||||
if ( ! bname )
|
||||
reporter->InternalError("basename failed: %s", strerror(errno));
|
||||
|
||||
StringVal* rval = new StringVal(bname);
|
||||
delete [] filename_copy;
|
||||
RET_CONST(rval);
|
||||
RET_CONST(new StringVal(SafeBasename(::filename).result));
|
||||
}
|
||||
|
||||
@load{WS}{FILE} {
|
||||
|
|
46
src/util.cc
46
src/util.cc
|
@ -944,34 +944,36 @@ FILE* open_package(string& path, const string& mode)
|
|||
return 0;
|
||||
}
|
||||
|
||||
string safe_dirname(const char* path)
|
||||
SafePathOp::SafePathOp(PathOpFn fn, const char* path, bool error_aborts)
|
||||
{
|
||||
if ( ! path )
|
||||
return ".";
|
||||
return safe_dirname(string(path));
|
||||
DoFunc(fn, path ? path : "", error_aborts);
|
||||
}
|
||||
|
||||
string safe_dirname(const string& path)
|
||||
SafePathOp::SafePathOp(PathOpFn fn, const string& path, bool error_aborts)
|
||||
{
|
||||
DoFunc(fn, path, error_aborts);
|
||||
}
|
||||
|
||||
void SafePathOp::DoFunc(PathOpFn fn, const string& path, bool error_aborts)
|
||||
{
|
||||
char* tmp = copy_string(path.c_str());
|
||||
string rval = dirname(tmp);
|
||||
delete [] tmp;
|
||||
return rval;
|
||||
char* rval = fn(tmp);
|
||||
|
||||
if ( rval )
|
||||
{
|
||||
result = rval;
|
||||
error = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( error_aborts )
|
||||
reporter->InternalError("Path operation failed on %s: %s",
|
||||
tmp ? tmp : "<null>", strerror(errno));
|
||||
else
|
||||
error = true;
|
||||
}
|
||||
|
||||
string safe_basename(const char* path)
|
||||
{
|
||||
if ( ! path )
|
||||
return ".";
|
||||
return safe_basename(string(path));
|
||||
}
|
||||
|
||||
string safe_basename(const string& path)
|
||||
{
|
||||
char* tmp = copy_string(path.c_str());
|
||||
string rval = basename(tmp);
|
||||
delete [] tmp;
|
||||
return rval;
|
||||
}
|
||||
|
||||
string flatten_script_name(const string& name, const string& prefix)
|
||||
|
@ -981,8 +983,8 @@ string flatten_script_name(const string& name, const string& prefix)
|
|||
if ( ! rval.empty() )
|
||||
rval.append(".");
|
||||
|
||||
if ( safe_basename(name) == PACKAGE_LOADER )
|
||||
rval.append(safe_dirname(name));
|
||||
if ( SafeBasename(name).result == PACKAGE_LOADER )
|
||||
rval.append(SafeDirname(name).result);
|
||||
else
|
||||
rval.append(name);
|
||||
|
||||
|
|
42
src/util.h
42
src/util.h
|
@ -22,6 +22,7 @@
|
|||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <magic.h>
|
||||
#include <libgen.h>
|
||||
#include "config.h"
|
||||
|
||||
#if __STDC__
|
||||
|
@ -208,13 +209,42 @@ extern const char* bro_path();
|
|||
extern const char* bro_magic_path();
|
||||
extern std::string bro_prefixes();
|
||||
|
||||
// Wrappers for dirname(3) that won't modify argument.
|
||||
std::string safe_dirname(const char* path);
|
||||
std::string safe_dirname(const std::string& path);
|
||||
/**
|
||||
* Wrapper class for functions like dirname(3) or basename(3) that won't
|
||||
* modify the path argument and may optionally abort execution on error.
|
||||
*/
|
||||
class SafePathOp {
|
||||
public:
|
||||
|
||||
// Wrappers for basename(3) that won't modify argument.
|
||||
std::string safe_basename(const char* path);
|
||||
std::string safe_basename(const std::string& path);
|
||||
typedef char*(*PathOpFn)(char*);
|
||||
|
||||
SafePathOp(PathOpFn fn, const char* path, bool error_aborts = true);
|
||||
SafePathOp(PathOpFn fn, const std::string& path, bool error_aborts = true);
|
||||
|
||||
std::string result;
|
||||
bool error;
|
||||
|
||||
private:
|
||||
|
||||
void DoFunc(PathOpFn fn, const std::string& path, bool error_aborts = true);
|
||||
};
|
||||
|
||||
class SafeDirname : public SafePathOp {
|
||||
public:
|
||||
|
||||
SafeDirname(const char* path, bool error_aborts = true)
|
||||
: SafePathOp(&dirname, path, error_aborts) { }
|
||||
SafeDirname(const std::string& path, bool error_aborts = true)
|
||||
: SafePathOp(&dirname, path, error_aborts) { }
|
||||
};
|
||||
|
||||
class SafeBasename : public SafePathOp {
|
||||
public:
|
||||
SafeBasename(const char* path, bool error_aborts = true)
|
||||
: SafePathOp(&basename, path, error_aborts) { }
|
||||
SafeBasename(const std::string& path, bool error_aborts = true)
|
||||
: SafePathOp(&basename, path, error_aborts) { }
|
||||
};
|
||||
|
||||
/**
|
||||
* Flatten a script name by replacing '/' path separators with '.'.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue