diff --git a/hphp/runtime/base/file.cpp b/hphp/runtime/base/file.cpp index 105bb3234..3096d16e4 100644 --- a/hphp/runtime/base/file.cpp +++ b/hphp/runtime/base/file.cpp @@ -46,24 +46,10 @@ IMPLEMENT_REQUEST_LOCAL(FileData, s_file_data); const int File::USE_INCLUDE_PATH = 1; -String File::TranslatePath(CStrRef filename, bool useFileCache /* = false */, - bool keepRelative /*= false */) { +String File::TranslatePathKeepRelative(CStrRef filename) { String canonicalized(Util::canonicalize(filename.data(), filename.size()), AttachString); - if (useFileCache) { - String translated = TranslatePath(canonicalized, false); - if (!translated.empty() && access(translated.data(), F_OK) < 0 && - StaticContentCache::TheFileCache) { - if (StaticContentCache::TheFileCache->exists(canonicalized.data(), - false)) { - // we use file cache's file name to make stat() work - translated = String(RuntimeOption::FileCache); - } - } - return translated; - } - if (RuntimeOption::SafeFileAccess) { const vector &allowedDirectories = VirtualHost::GetAllowedDirectories(); @@ -92,7 +78,13 @@ String File::TranslatePath(CStrRef filename, bool useFileCache /* = false */, } } - if (canonicalized.charAt(0) == '/' || keepRelative) { + return canonicalized; +} + +String File::TranslatePath(CStrRef filename) { + String canonicalized = TranslatePathKeepRelative(filename); + + if (canonicalized.charAt(0) == '/') { return canonicalized; } @@ -103,6 +95,21 @@ String File::TranslatePath(CStrRef filename, bool useFileCache /* = false */, return cwd + "/" + canonicalized; } +String File::TranslatePathWithFileCache(CStrRef filename) { + String canonicalized(Util::canonicalize(filename.data(), + filename.size()), AttachString); + String translated = TranslatePath(canonicalized); + if (!translated.empty() && access(translated.data(), F_OK) < 0 && + StaticContentCache::TheFileCache) { + if (StaticContentCache::TheFileCache->exists(canonicalized.data(), + false)) { + // we use file cache's file name to make stat() work + translated = String(RuntimeOption::FileCache); + } + } + return translated; +} + String File::TranslateCommand(CStrRef cmd) { //TODO: security checking return cmd; diff --git a/hphp/runtime/base/file.h b/hphp/runtime/base/file.h index 89de19fd7..0ada1dd69 100644 --- a/hphp/runtime/base/file.h +++ b/hphp/runtime/base/file.h @@ -49,8 +49,11 @@ DECLARE_EXTERN_REQUEST_LOCAL(FileData, s_file_data); */ class File : public SweepableResourceData { public: - static String TranslatePath(CStrRef filename, bool useFileCache = false, - bool keepRelative = false); + static String TranslatePath(CStrRef filename); + // Same as TranslatePath except doesn't make paths absolute + static String TranslatePathKeepRelative(CStrRef filename); + // Same as TranslatePath except checks the file cache on miss + static String TranslatePathWithFileCache(CStrRef filename); static String TranslateCommand(CStrRef cmd); static Variant Open(CStrRef filename, CStrRef mode, int options = 0, CVarRef context = uninit_null()); diff --git a/hphp/runtime/base/file_repository.cpp b/hphp/runtime/base/file_repository.cpp index bd39dcd92..45197e889 100644 --- a/hphp/runtime/base/file_repository.cpp +++ b/hphp/runtime/base/file_repository.cpp @@ -494,7 +494,7 @@ static bool findFileWrapper(CStrRef file, void* ctx) { // TranslatePath() will canonicalize the path and also check // whether the file is in an allowed directory. - String translatedPath = File::TranslatePath(file, false, true); + String translatedPath = File::TranslatePathKeepRelative(file); if (file[0] != '/') { if (HPHP::Eval::FileRepository::findFile(translatedPath.get(), context->s)) { diff --git a/hphp/runtime/ext/ext_fb.cpp b/hphp/runtime/ext/ext_fb.cpp index 844ebfeca..36a607546 100644 --- a/hphp/runtime/ext/ext_fb.cpp +++ b/hphp/runtime/ext/ext_fb.cpp @@ -1615,7 +1615,7 @@ extern Array stat_impl(struct stat*); // ext_file.cpp template static Variant do_lazy_stat(Function dostat, CStrRef filename) { struct stat sb; - if (dostat(File::TranslatePath(filename, true).c_str(), &sb)) { + if (dostat(File::TranslatePathWithFileCache(filename).c_str(), &sb)) { Logger::Verbose("%s/%d: %s", __FUNCTION__, __LINE__, Util::safe_strerror(errno).c_str()); return false; diff --git a/hphp/runtime/ext/ext_file.cpp b/hphp/runtime/ext/ext_file.cpp index 031e3af60..b58ba0717 100644 --- a/hphp/runtime/ext/ext_file.cpp +++ b/hphp/runtime/ext/ext_file.cpp @@ -103,8 +103,7 @@ static int accessSyscall( bool useFileCache = false) { Stream::Wrapper* w = Stream::getWrapperFromURI(path); if (useFileCache && dynamic_cast(w)) { - // This if block is needed for useFileCache - return ::access(File::TranslatePath(path, useFileCache).data(), mode); + return ::access(File::TranslatePathWithFileCache(path).data(), mode); } return w->access(path, mode); } @@ -115,8 +114,7 @@ static int statSyscall( bool useFileCache = false) { Stream::Wrapper* w = Stream::getWrapperFromURI(path); if (useFileCache && dynamic_cast(w)) { - // This if block is needed for useFileCache - return ::stat(File::TranslatePath(path, useFileCache).data(), buf); + return ::stat(File::TranslatePathWithFileCache(path).data(), buf); } return w->stat(path, buf); } @@ -127,8 +125,7 @@ static int lstatSyscall( bool useFileCache = false) { Stream::Wrapper* w = Stream::getWrapperFromURI(path); if (useFileCache && dynamic_cast(w)) { - // This if block is needed for useFileCache - return ::lstat(File::TranslatePath(path, useFileCache).data(), buf); + return ::lstat(File::TranslatePathWithFileCache(path).data(), buf); } return w->lstat(path, buf); }