return true;
}
-bool FilesystemFile::Write(const FilesystemBuffer& data, size_t offset) {
+bool FilesystemFile::Write(const FilesystemBuffer& data, size_t offset, bool rewrite) {
LoggerD("Enter");
- FILE* file = fopen(path.c_str(), "r+");
+
+ FILE* file = nullptr;
+ if (rewrite) {
+ file = fopen(path.c_str(), "w");
+ } else {
+ file = fopen(path.c_str(), "r+");
+ }
+
if (!file) {
LoggerE("Cannot open file %s to write!", path.c_str());
return false;
FilesystemFile(const std::string& path_);
bool Read(FilesystemBuffer* data, size_t offset, size_t length);
- bool Write(const FilesystemBuffer& data, size_t offset);
+ bool Write(const FilesystemBuffer& data, size_t offset, bool rewrite);
};
} // namespace filesystem
CHECK_EXIST(args, "location", out)
CHECK_EXIST(args, "data", out)
CHECK_EXIST(args, "offset", out)
+ CHECK_EXIST(args, "rewrite", out)
double callback_id = args.get("callbackId").get<double>();
const std::string& location = args.get("location").get<std::string>();
const std::string& data = args.get("data").get<std::string>();
size_t offset = static_cast<size_t>(args.get("location").get<double>());
+ bool rewrite = static_cast<size_t>(args.get("rewrite").get<bool>());
auto onSuccess = [this, callback_id]() {
LoggerD("enter");
location,
data,
offset,
+ rewrite,
onSuccess,
onError));
}
CHECK_EXIST(args, "location", out)
CHECK_EXIST(args, "data", out)
CHECK_EXIST(args, "offset", out)
+ CHECK_EXIST(args, "rewrite", out)
const std::string& location = args.get("location").get<std::string>();
const std::string& data = args.get("data").get<std::string>();
size_t offset = static_cast<size_t>(args.get("offset").get<double>());
+ bool rewrite = static_cast<size_t>(args.get("rewrite").get<bool>());
auto onSuccess = [this, &out]() {
LoggerD("enter");
};
FilesystemManager::GetInstance().FileWrite(
- location, data, offset, onSuccess, onError);
+ location, data, offset, rewrite, onSuccess, onError);
}
void FilesystemInstance::FileStat(const picojson::value& args,
const std::string& path,
const std::string& data,
size_t offset,
+ bool rewrite,
const std::function<void()>& success_cb,
const std::function<void(FilesystemError)>& error_cb) {
return;
}
- if (file.Write(buffer, offset)) {
+ if (file.Write(buffer, offset, rewrite)) {
success_cb();
} else {
LoggerE("Cannot write to file %s!", path.c_str());
void FileWrite(const std::string& path,
const std::string& data,
size_t offset,
+ bool rewrite,
const std::function<void()>& success_cb,
const std::function<void(FilesystemError)>& error_cb);
value: false,
writable: true,
enumerable: false
+ },
+ _rewrite: {
+ value: mode === 'w' ? true : false,
+ writable: true,
+ enumerable: false
}
});
}
var data = {
location: commonFS_.toRealPath(this._file.fullPath),
offset: this.position,
- data: Base64.encodeString(args.stringData)
+ data: Base64.encodeString(args.stringData),
+ rewrite: this._rewrite
};
var result = native_.callSync('File_writeSync', data);
can_change_size = true;
this.position = this.position + args.stringData.length;
can_change_size = false;
+ this._rewrite = false;
};
FileStream.prototype.write = function() {
var data = {
location: commonFS_.toRealPath(this._file.fullPath),
offset: this.position,
- data: Base64.encode(args.byteData)
+ data: Base64.encode(args.byteData),
+ rewrite: this._rewrite
};
var result = native_.callSync('File_writeSync', data);
can_change_size = true;
this.position = this.position + args.byteData.length;
can_change_size = false;
+ this._rewrite = false;
};
FileStream.prototype.writeBytes = function() {
var data = {
location: commonFS_.toRealPath(this._file.fullPath),
offset: this.position,
- data: args.base64Data
+ data: args.base64Data,
+ rewrite: this._rewrite
};
var result = native_.callSync('File_writeSync', data);
can_change_size = true;
this.position += decoded.length;
can_change_size = false;
+ this._rewrite = false;
};
FileStream.prototype.writeBase64 = function() {