REGISTER_ASYNC("File_unlinkFile", UnlinkFile);
REGISTER_ASYNC("File_removeDirectory", RemoveDirectory);
REGISTER_ASYNC("File_copyTo", CopyTo);
+ REGISTER_SYNC("FileSystemManager_getCanonicalPath", FileSystemManagerGetCanonicalPath);
#undef REGISTER_SYNC
#undef REGISTER_ASYNC
FilesystemManager::GetInstance().AddListener(this);
}
}
+void FilesystemInstance::FileSystemManagerGetCanonicalPath(const picojson::value& args, picojson::object& out)
+{
+ LoggerD("Enter");
+ //TODO: any privilege needed?
+ CHECK_EXIST(args, "path", out);
+
+ const std::string& path = args.get("path").get<std::string>();
+
+ auto onSuccess = [&](const std::string& canonicalPath) {
+ LoggerD("Enter");
+ ReportSuccess(picojson::value(canonicalPath), out);
+ };
+
+ auto onError = [&](FilesystemError e) {
+ LoggerD("Enter");
+ PrepareError(e, out);
+ };
+
+ FilesystemManager::GetInstance().GetCanonicalPath(path, onSuccess, onError);
+}
+
#undef CHECK_EXIST
} // namespace filesystem
void onFilesystemStateChangeErrorCallback();
void onFilesystemStateChangeSuccessCallback(const common::Storage& storage);
void PrepareError(const FilesystemError& error, picojson::object& out);
+ void FileSystemManagerGetCanonicalPath(const picojson::value& args, picojson::object& out);
};
} // namespace filesystem
#endif
#include <ftw.h>
#undef _XOPEN_SOURCE
+#include <stdlib.h>
#include "common/logger.h"
#include "common/tools.h"
LoggerD("enter");
listener_ = NULL;
}
+
+void FilesystemManager::GetCanonicalPath(const std::string& path,
+ const std::function<void(const std::string&)>& success_cb,
+ const std::function<void(FilesystemError)>& error_cb) {
+ LoggerD("Enter");
+ char *canonicalPath = nullptr;
+
+ SCOPE_EXIT {
+ free(canonicalPath);
+ };
+
+ canonicalPath = realpath(path.c_str(), nullptr);
+ int tmpErrno;
+ if (!canonicalPath) {
+ tmpErrno = errno;
+ LoggerE("Cannot get realpath of %s. Error: %s!", path.c_str(), strerror(tmpErrno));
+ error_cb(FilesystemError::Other);
+ }
+
+ std::string canonicalPathStr(canonicalPath);
+ success_cb(canonicalPathStr);
+}
+
} // namespace filesystem
} // namespace extension
common::StorageState _old, common::StorageState _new);
void AddListener(FilesystemStateChangeListener* listener);
void RemoveListener();
+
+ void GetCanonicalPath(const std::string& path,
+ const std::function<void(const std::string&)>& success_cb,
+ const std::function<void(FilesystemError)>& error_cb);
};
} // namespace filesystem
} // namespace extension
return true;
}
+ function toCanonicalPath(path) {
+ var result = native_.callSync('FileSystemManager_getCanonicalPath', { "path": path});
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
+
+ return native_.getResultObject(result);
+ }
+
function f_isSubDir(fullPathToCheck, fullPath) {
- var realFullPath = toRealPath(fullPath);
- return ((-1 !== fullPathToCheck.indexOf(realFullPath)) && (fullPathToCheck !== realFullPath));
- };
+ var fullCanonicalPathToCheck = toCanonicalPath(toRealPath(fullPathToCheck));
+ var fullCanonicalPath = toCanonicalPath(toRealPath(fullPath));
+
+ if (fullCanonicalPathToCheck === fullCanonicalPath) {
+ return false;
+ }
+
+ return fullCanonicalPathToCheck.indexOf(fullCanonicalPath) === 0;
+ }
function f_isCorrectRelativePath(relativePath) {
return ((0 !== relativePath.indexOf('/'))