+ Code formatted.
[Verification] TCT passrate 100%
Change-Id: Ia56a19e1089dd1f4a9b0d0d0f7ae33cf75a5f2da
Signed-off-by: Arkadiusz Pietraszek <a.pietraszek@partner.samsung.com>
continue;
}
character = getc(file);
- if(EOF == character) {
+ if (EOF == character) {
return false;
}
buf.push_back(character);
FilesystemManager::GetInstance().GetCanonicalPath(path, onSuccess, onError);
}
-
namespace {
FILE* OpenFile(const std::string& path, const std::string& fopen_mode) {
std::string first_nonexistent_parent = FilesystemUtils::Dirname(path);
struct ::stat buf;
- while (!FilesystemUtils::CheckIfExists(FilesystemUtils::Dirname(first_nonexistent_parent),
- &buf)) {
- first_nonexistent_parent = FilesystemUtils::Dirname(first_nonexistent_parent);
+ while (
+ !FilesystemUtils::CheckIfExists(FilesystemUtils::Dirname(first_nonexistent_parent), &buf)) {
+ first_nonexistent_parent = FilesystemUtils::Dirname(first_nonexistent_parent);
}
FilesystemUtils::Mkdir(FilesystemUtils::Dirname(path), true);
return file;
}
- std::system_error fopen_error = std::system_error(errno, std::generic_category(), "Could not open file");
+ std::system_error fopen_error =
+ std::system_error(errno, std::generic_category(), "Could not open file");
try {
FilesystemUtils::RemoveDirectoryRecursively(first_nonexistent_parent);
throw fopen_error;
}
-} //namespace
+} // namespace
void FilesystemInstance::FileSystemManagerOpenFile(const picojson::value& args,
picojson::object& out) {
file = OpenFile(path, open_mode);
}
} catch (const std::system_error& error) {
- FilesystemUtils::TranslateException(error, out);
- return;
+ FilesystemUtils::TranslateException(error, out);
+ return;
}
opened_files.emplace(std::make_pair(unique_id, std::make_shared<FileHandle>(file)));
try {
FilesystemUtils::Mkdir(path, make_parents);
- ReportSuccess(obj);
+ ReportSuccess(picojson::value(path), obj);
} catch (const std::system_error& e) {
FilesystemUtils::TranslateException(e, obj);
}
picojson::value response = picojson::value(picojson::object());
picojson::object& obj = response.get<picojson::object>();
obj["callbackId"] = picojson::value(callback_id);
+ SCOPE_EXIT {
+ this->PostMessage(response.serialize().c_str());
+ };
try {
+ struct stat buf {};
+ if (!FilesystemUtils::CheckIfExists(path, &buf) || !FilesystemUtils::CheckIfFile(buf)) {
+ LogAndReportError(NotFoundException("Given path does not point to file."), obj);
+ return;
+ }
FilesystemUtils::Unlink(path);
- ReportSuccess(obj);
+ ReportSuccess(picojson::value(FilesystemUtils::Dirname(path)), obj);
} catch (const std::system_error& e) {
FilesystemUtils::TranslateException(e, obj);
}
- this->PostMessage(response.serialize().c_str());
});
ReportSuccess(out);
picojson::value response = picojson::value(picojson::object());
picojson::object& obj = response.get<picojson::object>();
obj["callbackId"] = picojson::value(callback_id);
+ SCOPE_EXIT {
+ this->PostMessage(response.serialize().c_str());
+ };
try {
struct stat buf {};
} else {
FilesystemUtils::RemoveDirectory(path);
}
- ReportSuccess(obj);
+ ReportSuccess(picojson::value(FilesystemUtils::Dirname(path)), obj);
} catch (const std::system_error& e) {
FilesystemUtils::TranslateException(e, obj);
}
- this->PostMessage(response.serialize().c_str());
});
ReportSuccess(out);
void FilesystemInstance::FileSystemManagerCopyFile(const picojson::value& args,
picojson::object& out) {
- // TODO: currently does not create file with the same name as src if dest is a directory
ScopeLogger();
CHECK_PRIVILEGE_ACCESS(kPrivilegeFilesystemWrite, &out);
CHECK_PRIVILEGE_ACCESS(kPrivilegeFilesystemRead, &out);
picojson::value response = picojson::value(picojson::object());
picojson::object& obj = response.get<picojson::object>();
obj["callbackId"] = picojson::value(callback_id);
+ SCOPE_EXIT {
+ this->PostMessage(response.serialize().c_str());
+ };
try {
struct stat buf {};
return;
}
buf = {};
- if (!FilesystemUtils::CheckIfExists(destination_path, &buf) ||
- !FilesystemUtils::CheckIfDir(buf)) {
- LogAndReportError(NotFoundException("Given path does not point to directory."), obj);
- return;
- }
-
- buf = {};
- std::string new_path = destination_path + '/' + FilesystemUtils::PosixBasename(path);
- if (!overwrite && FilesystemUtils::CheckIfExists(new_path, &buf)) {
- LogAndReportError(IOException("File or directory with conflicting name already exists."),
- obj);
+ if (FilesystemUtils::CheckIfExists(destination_path, &buf) && !overwrite) {
+ LogAndReportError(
+ IOException("Given path points to an existing resource, overwriting is not allowed."),
+ obj);
return;
}
FilesystemUtils::CopyFile(path, destination_path, overwrite);
- ReportSuccess(obj);
+ ReportSuccess(picojson::value(destination_path), obj);
} catch (const std::system_error& e) {
FilesystemUtils::TranslateException(e, obj);
}
- this->PostMessage(response.serialize().c_str());
});
ReportSuccess(out);
picojson::value response = picojson::value(picojson::object());
picojson::object& obj = response.get<picojson::object>();
obj["callbackId"] = picojson::value(callback_id);
+ SCOPE_EXIT {
+ this->PostMessage(response.serialize().c_str());
+ };
try {
struct stat buf {};
return;
}
buf = {};
- if (!FilesystemUtils::CheckIfExists(destination_path, &buf) ||
- !FilesystemUtils::CheckIfDir(buf)) {
- LogAndReportError(NotFoundException("Given path does not point to directory."), obj);
- return;
- }
- std::string new_path = destination_path + '/' + FilesystemUtils::PosixBasename(path);
- buf = {};
- if (FilesystemUtils::CheckIfExists(new_path, &buf) && !FilesystemUtils::CheckIfDir(buf)) {
+ bool exists = FilesystemUtils::CheckIfExists(destination_path, &buf);
+ if (exists && !FilesystemUtils::CheckIfDir(buf)) {
LogAndReportError(IOException("File with conflicting name already exists."), obj);
return;
+ } else if (!exists) {
+ FilesystemUtils::Mkdir(destination_path);
}
FilesystemUtils::CopyDirectory(path, destination_path, overwrite);
- ReportSuccess(obj);
+ ReportSuccess(picojson::value(destination_path), obj);
} catch (const std::system_error& e) {
FilesystemUtils::TranslateException(e, obj);
}
- this->PostMessage(response.serialize().c_str());
});
ReportSuccess(out);
picojson::value response = picojson::value(picojson::object());
picojson::object& obj = response.get<picojson::object>();
obj["callbackId"] = picojson::value(callback_id);
+ SCOPE_EXIT {
+ this->PostMessage(response.serialize().c_str());
+ };
try {
struct stat buf {};
return;
}
FilesystemUtils::MoveFile(path, new_path, overwrite);
- ReportSuccess(obj);
+ ReportSuccess(picojson::value(new_path), obj);
} catch (const std::system_error& e) {
FilesystemUtils::TranslateException(e, obj);
}
- this->PostMessage(response.serialize().c_str());
});
ReportSuccess(out);
picojson::value response = picojson::value(picojson::object());
picojson::object& obj = response.get<picojson::object>();
obj["callbackId"] = picojson::value(callback_id);
+ SCOPE_EXIT {
+ this->PostMessage(response.serialize().c_str());
+ };
try {
struct stat buf {};
buf = {};
std::string new_path = destination_path + '/' + FilesystemUtils::PosixBasename(path);
if (FilesystemUtils::CheckIfExists(new_path, &buf) && !FilesystemUtils::CheckIfDir(buf)) {
- LogAndReportError(IOException("File or directory with conflicting name already exists."), obj);
+ LogAndReportError(IOException("File or directory with conflicting name already exists."),
+ obj);
return;
}
FilesystemUtils::MoveDirectory(path, destination_path, overwrite);
- ReportSuccess(obj);
+ ReportSuccess(picojson::value(new_path), obj);
} catch (const std::system_error& e) {
FilesystemUtils::TranslateException(e, obj);
}
- this->PostMessage(response.serialize().c_str());
});
ReportSuccess(out);
picojson::value response = picojson::value(picojson::object());
picojson::object& obj = response.get<picojson::object>();
obj["callbackId"] = picojson::value(callback_id);
+ SCOPE_EXIT {
+ this->PostMessage(response.serialize().c_str());
+ };
try {
struct stat buf {};
return;
}
FilesystemUtils::Rename(path, new_path);
- ReportSuccess(obj);
+ ReportSuccess(picojson::value(new_path), obj);
} catch (const std::system_error& e) {
FilesystemUtils::TranslateException(e, obj);
}
- this->PostMessage(response.serialize().c_str());
});
ReportSuccess(out);
if (!(std::feof(
handle->file_handle))) { // read number of characters if not whole file read
LoggerD("count parameter given: %d", count);
- if (!whole_file && !add_utf8_chars_to_buffer(handle->file_handle, buf, count - char_count,
+ if (!whole_file &&
+ !add_utf8_chars_to_buffer(handle->file_handle, buf, count - char_count,
expected_extension_bytes)) {
LogAndReportError(
IOException("File doesn't contain UTF-8 encoded string with given length"), out);
int ret = fflush(handle->file_handle);
if (ret) {
int errsv = errno;
- std::string error_message =
- std::string("flush failed, error message: ") + strerror(errsv);
+ std::string error_message = std::string("flush failed, error message: ") + strerror(errsv);
LogAndReportError(IOException(error_message.c_str()), out);
return;
}
int ret = fsync(fileno(handle->file_handle));
if (ret) {
int errsv = errno;
- std::string error_message =
- std::string("sync failed, error message: ") + strerror(errsv);
+ std::string error_message = std::string("sync failed, error message: ") + strerror(errsv);
LogAndReportError(IOException(error_message.c_str()), out);
return;
}
handle->file_handle = nullptr;
if (ret) {
int errsv = errno;
- std::string error_message =
- std::string("close failed, error message: ") + strerror(errsv);
+ std::string error_message = std::string("close failed, error message: ") + strerror(errsv);
LogAndReportError(IOException(error_message.c_str()), out);
return;
}
return dir;
}
-void CopyFile(const std::string& src, const std::string& dest, bool overwrite) {
- ScopeLogger("From: %s; To %s", src.c_str(), dest.c_str());
+void CopyFileOverExistingDirectory(const std::string& src, const std::string& dest,
+ bool overwrite) {
+ ScopeLogger("From: %s, To %s", src.c_str(), dest.c_str());
struct stat buf {};
if (CheckIfExists(dest, &buf) && CheckIfDir(buf)) {
if (overwrite) {
"Failed to copy file: overwrite is not allowed."};
}
}
+ CopyFile(src, dest, overwrite);
+}
+
+void CopyFile(const std::string& src, const std::string& dest, bool overwrite) {
+ ScopeLogger("From: %s; To %s", src.c_str(), dest.c_str());
GError* error = nullptr;
auto source_ptr = std::unique_ptr<GFile, decltype(&g_object_unref)>(
}
void CopyDirectory(const std::string& src, const std::string& dest, bool overwrite) {
- ScopeLogger("From: %s; To %s", src.c_str(), dest.c_str());
- std::string dest_dir = dest + '/' + PosixBasename(src);
- struct stat buf {};
- bool exists = CheckIfExists(dest_dir, &buf);
- if (exists && !CheckIfDir(buf)) {
- if (overwrite) {
- Unlink(dest_dir);
- Mkdir(dest_dir);
- } else {
- throw std::system_error{EIO, std::generic_category(),
- "Failed to copy directory: overwrite is not allowed."};
- }
- } else if (!exists) {
- Mkdir(dest_dir);
- }
-
+ ScopeLogger("From: %s, To %s", src.c_str(), dest.c_str());
ListDirectory(src, [&](const char* name, unsigned char type) {
if (DT_DIR == type) {
+ std::string dest_dir = dest + '/' + name;
+ struct stat buf {};
+ bool exists = CheckIfExists(dest_dir, &buf);
+ if (exists && !CheckIfDir(buf)) {
+ if (overwrite) {
+ Unlink(dest_dir);
+ Mkdir(dest_dir);
+ } else {
+ throw std::system_error{EIO, std::generic_category(),
+ "Failed to copy directory: overwriting is not allowed."};
+ }
+ } else if (!exists) {
+ Mkdir(dest_dir);
+ }
+
CopyDirectory(src + '/' + name, dest_dir, overwrite);
} else { // copying of regular files as well as other types of items pointed by src
- CopyFile(src + '/' + name, dest_dir + '/' + name, overwrite);
+ CopyFileOverExistingDirectory(src + '/' + name, dest + '/' + name, overwrite);
}
// Set errno to 0 to prevent from reporting readdir error after successful iterating through
// directory.
"Failed to open directory: "s + std::strerror(errno)};
}
- std::unique_ptr<DIR, void (*)(DIR*)> dir_ptr(d, [](DIR * d) {
+ std::unique_ptr<DIR, void (*)(DIR*)> dir_ptr(d, [](DIR* d) {
if (::closedir(d)) {
LoggerW("closedir failed");
}
}
void RemoveDirectoryRecursively(const std::string& path) {
- ScopeLogger("%s", path);
+ ScopeLogger("%s", path.c_str());
auto res =
nftw(path.c_str(),
[](const char* fpath, const struct stat* sb, int typeflag, struct FTW* ftwbuf) -> int {
if ((this.mode === 'w') || (this.mode === 'a')) {
throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle state is write-only');
}
- var data = {
- id: this.id,
- count: args.count,
- inputEncoding: args.inputEncoding
- };
+ var data = {id: this.id, count: args.count, inputEncoding: args.inputEncoding};
var result = native_.callSync('FileHandle_readString', data);
if (native_.isFailure(result)) {
if ((this.mode === 'w') || (this.mode === 'a')) {
throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle state is write-only');
}
- var data = {id: this.id, size: args.size, inputEncoding: args.inputEncoding, blocking: false};
+ var data =
+ {id: this.id, size: args.size, inputEncoding: args.inputEncoding, blocking: false};
var callback = function(result) {
if (native_.isFailure(result)) {
if ('r' === this.mode) {
throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle state is read-only');
}
- var data = {id: this.id, string: args.string, outputEncoding: args.outputEncoding, blocking: false};
+ var data = {
+ id: this.id,
+ string: args.string,
+ outputEncoding: args.outputEncoding,
+ blocking: false
+ };
var callback = function(result) {
if (native_.isFailure(result)) {
throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle state is read-only');
}
- var encodedData = ArrayToString(new Uint8Array(blobToUint8Array(args.blob)));
+ var encodedData = ArrayToString(blobToUint8Array(args.blob));
var data = {id: this.id, data: encodedData};
var result = native_.callSync('FileHandle_writeData', data);
if (native_.isFailure(result)) {
throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle state is read-only');
}
- var encodedData = ArrayToString(new Uint8Array(blobToUint8Array(args.blob)));
+ var encodedData = ArrayToString(blobToUint8Array(args.blob));
var data = {id: this.id, data: encodedData, blocking: false};
var callback = function(result) {
if (native_.isFailure(result)) {
if (!data.path) {
throw new WebAPIException(
- WebAPIException.NOT_FOUND_ERR, 'Invalid path: ' + args.path);
+ WebAPIException.INVALID_VALUES_ERR, 'Invalid path: ' + args.path);
}
var callback = function(result) {
if (native_.isFailure(result)) {
native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
} else {
- native_.callIfPossible(args.successCallback);
+ var path = native_.getResultObject(result);
+ native_.callIfPossible(args.successCallback, commonFS_.toVirtualPath(path));
}
};
if (!data.path) {
throw new WebAPIException(
- WebAPIException.NOT_FOUND_ERR, 'Invalid path: ' + args.path);
+ WebAPIException.INVALID_VALUES_ERR, 'Invalid path: ' + args.path);
}
var callback = function(result) {
if (native_.isFailure(result)) {
native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
} else {
- native_.callIfPossible(args.successCallback);
+ var path = native_.getResultObject(result);
+ native_.callIfPossible(args.successCallback, commonFS_.toVirtualPath(path));
}
};
if (native_.isFailure(result)) {
native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
} else {
- native_.callIfPossible(args.successCallback);
+ var path = native_.getResultObject(result);
+ native_.callIfPossible(args.successCallback, commonFS_.toVirtualPath(path));
}
};
if (native_.isFailure(result)) {
native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
} else {
- native_.callIfPossible(args.successCallback);
+ var path = native_.getResultObject(result);
+ native_.callIfPossible(args.successCallback, commonFS_.toVirtualPath(path));
}
};
FileSystemManager.prototype.copyDirectory = function() {
var args = validator_.validateArgs(arguments, [
- {name: 'path', type: types_.STRING},
- {name: 'destinationPath', type: types_.STRING},
+ {name: 'path', type: types_.STRING}, {name: 'destinationPath', type: types_.STRING},
{name: 'overwrite', type: types_.BOOLEAN, optional: true},
{name: 'successCallback', type: types_.FUNCTION, optional: true, nullable: true},
{name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true}
if (native_.isFailure(result)) {
native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
} else {
- native_.callIfPossible(args.successCallback);
+ var path = native_.getResultObject(result);
+ native_.callIfPossible(args.successCallback, commonFS_.toVirtualPath(path));
}
};
FileSystemManager.prototype.moveFile = function() {
var args = validator_.validateArgs(arguments, [
- {name: 'path', type: types_.STRING},
- {name: 'destinationPath', type: types_.STRING},
+ {name: 'path', type: types_.STRING}, {name: 'destinationPath', type: types_.STRING},
{name: 'overwrite', type: types_.BOOLEAN, optional: true},
{name: 'successCallback', type: types_.FUNCTION, optional: true, nullable: true},
{name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true}
if (native_.isFailure(result)) {
native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
} else {
- native_.callIfPossible(args.successCallback);
+ var path = native_.getResultObject(result);
+ native_.callIfPossible(args.successCallback, commonFS_.toVirtualPath(path));
}
};
FileSystemManager.prototype.moveDirectory = function() {
var args = validator_.validateArgs(arguments, [
- {name: 'path', type: types_.STRING},
- {name: 'destinationPath', type: types_.STRING},
+ {name: 'path', type: types_.STRING}, {name: 'destinationPath', type: types_.STRING},
{name: 'overwrite', type: types_.BOOLEAN, optional: true},
{name: 'successCallback', type: types_.FUNCTION, optional: true, nullable: true},
{name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true}
if (native_.isFailure(result)) {
native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
} else {
- native_.callIfPossible(args.successCallback);
+ var path = native_.getResultObject(result);
+ native_.callIfPossible(args.successCallback, commonFS_.toVirtualPath(path));
}
};
FileSystemManager.prototype.rename = function() {
var args = validator_.validateArgs(arguments, [
- {name: 'path', type: types_.STRING},
- {name: 'newName', type: types_.STRING},
+ {name: 'path', type: types_.STRING}, {name: 'newName', type: types_.STRING},
{name: 'successCallback', type: types_.FUNCTION, optional: true, nullable: true},
{name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true}
]);
if (native_.isFailure(result)) {
native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
} else {
- native_.callIfPossible(args.successCallback);
+ var path = native_.getResultObject(result);
+ native_.callIfPossible(args.successCallback, commonFS_.toVirtualPath(path));
}
};
FileSystemManager.prototype.isFile = function() {
var args = validator_.validateArgs(arguments, [{name: 'path', type: types_.STRING}]);
-
// The toRealPath function will convert any string to absolute path, if possible.
// The function returns undefined for path, which starts with not-existing virtual root.
var realPath = commonFS_.toRealPath(args.path);
FileSystemManager.prototype.isDirectory = function() {
var args = validator_.validateArgs(arguments, [{name: 'path', type: types_.STRING}]);
-
// The toRealPath function will convert any string to absolute path, if possible.
// The function returns undefined for path, which starts with not-existing virtual root.
var realPath = commonFS_.toRealPath(args.path);
FileSystemManager.prototype.pathExists = function() {
var args = validator_.validateArgs(arguments, [{name: 'path', type: types_.STRING}]);
-
// The toRealPath function will convert any string to absolute path, if possible.
// The function returns undefined for path, which starts with not-existing virtual root.
var realPath = commonFS_.toRealPath(args.path);
if (!args.has.mode) {
args.mode = 'rw';
} else if ('rwo' == args.mode) {
- throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR, 'rwo mode was introduced in version 5.0 and is not supported in earlier version methods');
+ throw new WebAPIException(
+ WebAPIException.INVALID_VALUES_ERR,
+ 'rwo mode was introduced in version 5.0 and is not supported in earlier version methods');
}
// resolving a path on unmounted storage should result in exception