提交 d9dd6aa0 编写于 作者: M mindspore-ci-bot 提交者: Gitee

!277 fix: use correct permissions when opening mindrecord files

Merge pull request !277 from guozhijian/fix_read_with_exact_option
......@@ -512,6 +512,10 @@ MSRStatus ShardIndexGenerator::ExecuteTransaction(const int &shard_no, const std
std::fstream in;
in.open(common::SafeCStr(shard_address), std::ios::in | std::ios::binary);
if (!in.good()) {
MS_LOG(ERROR) << "File could not opened";
return FAILED;
}
(void)sqlite3_exec(db.second, "BEGIN TRANSACTION;", nullptr, nullptr, nullptr);
for (int raw_page_id : raw_page_ids) {
auto sql = GenerateRawSQL(fields_);
......
......@@ -125,13 +125,10 @@ MSRStatus ShardReader::Open() {
for (const auto &file : file_paths_) {
std::shared_ptr<std::fstream> fs = std::make_shared<std::fstream>();
fs->open(common::SafeCStr(file), std::ios::in | std::ios::out | std::ios::binary);
if (fs->fail()) {
fs->open(common::SafeCStr(file), std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary);
if (fs->fail()) {
MS_LOG(ERROR) << "File could not opened";
return FAILED;
}
fs->open(common::SafeCStr(file), std::ios::in | std::ios::binary);
if (!fs->good()) {
MS_LOG(ERROR) << "File could not opened";
return FAILED;
}
MS_LOG(INFO) << "Open shard file successfully.";
file_streams_.push_back(fs);
......@@ -146,13 +143,10 @@ MSRStatus ShardReader::Open(int n_consumer) {
for (const auto &file : file_paths_) {
for (int j = 0; j < n_consumer; ++j) {
std::shared_ptr<std::fstream> fs = std::make_shared<std::fstream>();
fs->open(common::SafeCStr(file), std::ios::in | std::ios::out | std::ios::binary);
if (fs->fail()) {
fs->open(common::SafeCStr(file), std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary);
if (fs->fail()) {
MS_LOG(ERROR) << "File could not opened";
return FAILED;
}
fs->open(common::SafeCStr(file), std::ios::in | std::ios::binary);
if (!fs->good()) {
MS_LOG(ERROR) << "File could not opened";
return FAILED;
}
file_streams_random_[j].push_back(fs);
}
......@@ -311,12 +305,10 @@ MSRStatus ShardReader::ReadAllRowsInShard(int shard_id, const std::string &sql,
std::string file_name = file_paths_[shard_id];
std::shared_ptr<std::fstream> fs = std::make_shared<std::fstream>();
if (!all_in_index_) {
fs->open(common::SafeCStr(file_name), std::ios::in | std::ios::out | std::ios::binary);
if (fs->fail()) {
fs->open(common::SafeCStr(file_name), std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary);
if (fs->fail()) {
MS_LOG(ERROR) << "File could not opened";
}
fs->open(common::SafeCStr(file_name), std::ios::in | std::ios::binary);
if (!fs->good()) {
MS_LOG(ERROR) << "File could not opened";
return FAILED;
}
}
sqlite3_free(errmsg);
......@@ -520,8 +512,8 @@ std::pair<MSRStatus, std::vector<json>> ShardReader::GetLabelsFromBinaryFile(
std::string file_name = file_paths_[shard_id];
std::vector<json> res;
std::shared_ptr<std::fstream> fs = std::make_shared<std::fstream>();
fs->open(common::SafeCStr(file_name), std::ios::in | std::ios::out | std::ios::binary);
if (fs->fail()) {
fs->open(common::SafeCStr(file_name), std::ios::in | std::ios::binary);
if (!fs->good()) {
MS_LOG(ERROR) << "File could not opened";
return {FAILED, {}};
}
......
......@@ -76,16 +76,27 @@ MSRStatus ShardWriter::Open(const std::vector<std::string> &paths, bool append)
// Open files
for (const auto &file : file_paths_) {
std::shared_ptr<std::fstream> fs = std::make_shared<std::fstream>();
fs->open(common::SafeCStr(file), std::ios::in | std::ios::out | std::ios::binary);
if (fs->fail()) {
fs->open(common::SafeCStr(file), std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary);
if (fs->fail()) {
MS_LOG(ERROR) << "File could not opened";
if (!append) {
// if not append and mindrecord file exist, return FAILED
fs->open(common::SafeCStr(file), std::ios::in | std::ios::binary);
if (fs->good()) {
MS_LOG(ERROR) << "MindRecord file already existed.";
fs->close();
return FAILED;
}
fs->close();
// open the mindrecord file to write
fs->open(common::SafeCStr(file), std::ios::out | std::ios::binary);
if (!fs->good()) {
MS_LOG(ERROR) << "MindRecord file could not opened.";
return FAILED;
}
} else {
if (!append) {
MS_LOG(ERROR) << "MindRecord file already existed";
// open the mindrecord file to append
fs->open(common::SafeCStr(file), std::ios::out | std::ios::in | std::ios::binary);
if (!fs->good()) {
MS_LOG(ERROR) << "MindRecord file could not opened for append.";
return FAILED;
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册