Skip to content

Commit e3887d5

Browse files
author
Omar Marzouk
committed
Existing File Deletion when Opened in Write Mode
1 parent e8cf9c0 commit e3887d5

File tree

3 files changed

+91
-55
lines changed

3 files changed

+91
-55
lines changed

tensorflow_io/core/filesystems/dfs/dfs_filesystem.cc

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ void Cleanup(TF_Filesystem* filesystem) {
164164
delete daos;
165165
}
166166

167-
void NewFile(const TF_Filesystem* filesystem, const char* path, mode_t mode,
167+
void NewFile(const TF_Filesystem* filesystem, const char* path, File_Mode mode,
168168
int flags, dfs_obj_t** obj, TF_Status* status) {
169169
int rc;
170170
auto daos = static_cast<DFS*>(filesystem->plugin_filesystem)->Load();
@@ -181,8 +181,7 @@ void NewFile(const TF_Filesystem* filesystem, const char* path, mode_t mode,
181181
void NewWritableFile(const TF_Filesystem* filesystem, const char* path,
182182
TF_WritableFile* file, TF_Status* status) {
183183
dfs_obj_t* obj = NULL;
184-
NewFile(filesystem, path, S_IWUSR | S_IFREG, O_WRONLY | O_CREAT, &obj,
185-
status);
184+
NewFile(filesystem, path, WRITE, S_IWUSR | S_IFREG, &obj, status);
186185
if (TF_GetCode(status) != TF_OK) return;
187186
auto daos = static_cast<DFS*>(filesystem->plugin_filesystem)->Load();
188187
if (!daos) {
@@ -197,7 +196,7 @@ void NewWritableFile(const TF_Filesystem* filesystem, const char* path,
197196
void NewRandomAccessFile(const TF_Filesystem* filesystem, const char* path,
198197
TF_RandomAccessFile* file, TF_Status* status) {
199198
dfs_obj_t* obj = NULL;
200-
NewFile(filesystem, path, S_IRUSR | S_IFREG, O_RDONLY, &obj, status);
199+
NewFile(filesystem, path, READ, S_IRUSR | S_IFREG, &obj, status);
201200
if (TF_GetCode(status) != TF_OK) return;
202201
auto daos = static_cast<DFS*>(filesystem->plugin_filesystem)->Load();
203202
if (!daos) {
@@ -215,8 +214,7 @@ void NewRandomAccessFile(const TF_Filesystem* filesystem, const char* path,
215214
void NewAppendableFile(const TF_Filesystem* filesystem, const char* path,
216215
TF_WritableFile* file, TF_Status* status) {
217216
dfs_obj_t* obj = NULL;
218-
NewFile(filesystem, path, S_IWUSR | S_IFREG, O_WRONLY | O_CREAT | O_APPEND,
219-
&obj, status);
217+
NewFile(filesystem, path, APPEND, S_IWUSR | S_IFREG, &obj, status);
220218
if (TF_GetCode(status) != TF_OK) return;
221219
auto daos = static_cast<DFS*>(filesystem->plugin_filesystem)->Load();
222220
if (!daos) {
@@ -296,41 +294,19 @@ void DeleteFileSystemEntry(const TF_Filesystem* filesystem, const char* path,
296294
int rc;
297295
std::string pool, cont, dir_path;
298296
auto daos = static_cast<DFS*>(filesystem->plugin_filesystem)->Load();
297+
299298
if (!daos) {
300299
TF_SetStatus(status, TF_INTERNAL, "Error initializng DAOS API");
301300
return;
302301
}
303-
rc = daos->Setup(path, pool, cont, dir_path, status);
304-
if (rc) return;
305302

306-
dfs_obj_t* temp_obj;
307-
rc = daos->dfsPathExists(dir_path, &temp_obj, 0);
308-
if (rc) {
309-
TF_SetStatus(status, TF_NOT_FOUND, "");
310-
return;
311-
}
312-
if (!is_dir && S_ISDIR(temp_obj->mode)) {
313-
TF_SetStatus(status, TF_FAILED_PRECONDITION, "");
314-
return;
315-
}
316-
dfs_release(temp_obj);
317-
318-
size_t dir_start = dir_path.rfind("/") + 1;
319-
std::string dir = dir_path.substr(dir_start);
320-
dfs_obj_t* parent;
321-
rc = daos->dfsFindParent(dir_path, &parent);
303+
rc = daos->Setup(path, pool, cont, dir_path, status);
322304
if (rc) {
323-
TF_SetStatus(status, TF_NOT_FOUND, "");
305+
TF_SetStatus(status, TF_INTERNAL, "Error initializng DAOS API");
324306
return;
325307
}
326308

327-
rc = dfs_remove(daos->daos_fs, parent, dir.c_str(), recursive, NULL);
328-
if (rc) {
329-
TF_SetStatus(status, TF_INTERNAL, "Error Deleting Directory");
330-
} else {
331-
TF_SetStatus(status, TF_OK, "");
332-
}
333-
dfs_release(parent);
309+
daos->dfsDeleteObject(dir_path, is_dir, recursive, status);
334310
}
335311

336312
void DeleteSingleDir(const TF_Filesystem* filesystem, const char* path,
@@ -596,18 +572,21 @@ int GetChildren(const TF_Filesystem* filesystem, const char* path,
596572
rc = daos->dfsPathExists(dir_path, &obj, 0);
597573
if (rc) {
598574
TF_SetStatus(status, TF_NOT_FOUND, "");
575+
dfs_release(obj);
599576
return -1;
600577
}
601578

602579
if (!S_ISDIR(obj->mode)) {
603580
TF_SetStatus(status, TF_FAILED_PRECONDITION, "");
581+
dfs_release(obj);
604582
return -1;
605583
}
606584

607585
std::vector<std::string> children;
608586
rc = daos->dfsReadDir(obj, children);
609587
if (rc) {
610588
TF_SetStatus(status, TF_INTERNAL, "");
589+
dfs_release(obj);
611590
return -1;
612591
}
613592

tensorflow_io/core/filesystems/dfs/dfs_utils.cc

Lines changed: 75 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,33 @@ size_t GetStorageSize(std::string size) {
2222
size.pop_back();
2323
curr_scale *= 1024;
2424
return (size_t)atoi(size.c_str()) * curr_scale;
25-
break;
2625
case 'M':
2726
size.pop_back();
2827
curr_scale *= 1024 * 1024;
2928
return (size_t)atoi(size.c_str()) * curr_scale;
30-
break;
3129
case 'G':
3230
size.pop_back();
3331
curr_scale *= 1024 * 1024 * 1024;
3432
return (size_t)atoi(size.c_str()) * curr_scale;
35-
break;
3633
case 'T':
3734
size.pop_back();
3835
curr_scale *= 1024 * 1024 * 1024;
3936
return (size_t)atoi(size.c_str()) * curr_scale * 1024;
40-
break;
4137
default:
4238
return atoi(size.c_str());
43-
break;
39+
}
40+
}
41+
42+
mode_t GetFlags(File_Mode mode) {
43+
switch (mode) {
44+
case READ:
45+
return O_RDONLY;
46+
case WRITE:
47+
return O_WRONLY | O_CREAT;
48+
case APPEND:
49+
return O_WRONLY | O_APPEND | O_CREAT;
50+
default:
51+
return -1;
4452
}
4553
}
4654

@@ -74,8 +82,8 @@ int ParseUUID(const std::string& str, uuid_t uuid) {
7482
}
7583

7684
void CopyEntries(char*** entries, std::vector<std::string>& results) {
77-
*entries = static_cast<char**>(tensorflow::io::plugin_memory_allocate(
78-
results.size() * sizeof((*entries)[0])));
85+
*entries = static_cast<char**>(
86+
tensorflow::io::plugin_memory_allocate(results.size() * sizeof(char*)));
7987

8088
for (uint32_t i = 0; i < results.size(); i++) {
8189
(*entries)[i] = static_cast<char*>(tensorflow::io::plugin_memory_allocate(
@@ -267,10 +275,47 @@ int DFS::ClearConnections() {
267275
return rc;
268276
}
269277

270-
void DFS::dfsNewFile(std::string& file_path, mode_t mode, int flags,
278+
int DFS::dfsDeleteObject(std::string& dir_path, bool is_dir, bool recursive,
279+
TF_Status* status) {
280+
dfs_obj_t* temp_obj;
281+
int rc = dfsPathExists(dir_path, &temp_obj, 0);
282+
if (rc) {
283+
TF_SetStatus(status, TF_NOT_FOUND, "");
284+
return -1;
285+
}
286+
if (!is_dir && S_ISDIR(temp_obj->mode)) {
287+
TF_SetStatus(status, TF_FAILED_PRECONDITION, "");
288+
return -1;
289+
}
290+
dfs_release(temp_obj);
291+
292+
size_t dir_start = dir_path.rfind("/") + 1;
293+
std::string dir = dir_path.substr(dir_start);
294+
dfs_obj_t* parent;
295+
rc = dfsFindParent(dir_path, &parent);
296+
if (rc) {
297+
TF_SetStatus(status, TF_NOT_FOUND, "");
298+
return -1;
299+
}
300+
301+
rc = dfs_remove(daos_fs, parent, dir.c_str(), recursive, NULL);
302+
303+
dfs_release(parent);
304+
305+
if (rc) {
306+
TF_SetStatus(status, TF_INTERNAL, "Error Deleting Existing Object");
307+
} else {
308+
TF_SetStatus(status, TF_OK, "");
309+
}
310+
311+
return rc;
312+
}
313+
314+
void DFS::dfsNewFile(std::string& file_path, File_Mode file_mode, int flags,
271315
dfs_obj_t** obj, TF_Status* status) {
272316
int rc;
273317
dfs_obj_t* temp_obj;
318+
mode_t open_flags;
274319
rc = dfsPathExists(file_path, &temp_obj, 0);
275320
if (rc && flags == O_RDONLY) {
276321
TF_SetStatus(status, TF_NOT_FOUND, "");
@@ -287,6 +332,13 @@ void DFS::dfsNewFile(std::string& file_path, mode_t mode, int flags,
287332
dfs_release(temp_obj);
288333
}
289334

335+
if (!rc && file_mode == WRITE) {
336+
rc = dfsDeleteObject(file_path, false, false, status);
337+
if (rc) return;
338+
}
339+
340+
open_flags = GetFlags(file_mode);
341+
290342
dfs_obj_t* parent;
291343
rc = dfsFindParent(file_path, &parent);
292344
if (rc) {
@@ -302,8 +354,8 @@ void DFS::dfsNewFile(std::string& file_path, mode_t mode, int flags,
302354
size_t file_start = file_path.rfind("/") + 1;
303355
std::string file_name = file_path.substr(file_start);
304356

305-
rc = dfs_open(daos_fs, parent, file_name.c_str(), mode, flags, 0, 0, NULL,
306-
obj);
357+
rc = dfs_open(daos_fs, parent, file_name.c_str(), flags, open_flags, 0, 0,
358+
NULL, obj);
307359
if (rc) {
308360
TF_SetStatus(status, TF_INTERNAL, "Error Creating Writable File");
309361
return;
@@ -372,14 +424,17 @@ int DFS::dfsReadDir(dfs_obj_t* obj, std::vector<std::string>& children) {
372424
daos_anchor_t anchor = {0};
373425
uint32_t nr = STACK;
374426
struct dirent* dirs = (struct dirent*)malloc(nr * sizeof(struct dirent));
427+
while (!daos_anchor_is_eof(&anchor)) {
428+
rc = dfs_readdir(daos_fs, obj, &anchor, &nr, dirs);
429+
if (rc) {
430+
return rc;
431+
}
375432

376-
rc = dfs_readdir(daos_fs, obj, &anchor, &nr, dirs);
377-
if (rc) {
378-
return rc;
379-
}
380-
381-
for (uint32_t i = 0; i < nr; i++) {
382-
children.push_back(dirs[i].d_name);
433+
for (uint32_t i = 0; i < nr; i++) {
434+
// std::cout << "Before File " << dirs[i].d_name << std::endl;
435+
children.emplace_back(dirs[i].d_name);
436+
// std::cout << "After File " << dirs[i].d_name << std::endl;
437+
}
383438
}
384439

385440
free(dirs);
@@ -498,11 +553,11 @@ int ReadBuffer::WaitEvent() {
498553
if (valid) return 0;
499554
bool event_status;
500555
daos_event_test(event, -1, &event_status);
501-
if (event_status) {
556+
if (event_status && event->ev_error == DER_SUCCESS) {
502557
valid = true;
503558
return 0;
504559
}
505-
return -1;
560+
return event->ev_error;
506561
}
507562

508563
int ReadBuffer::AbortEvent() {
@@ -535,7 +590,7 @@ int ReadBuffer::ReadSync(dfs_t* daos_fs, dfs_obj_t* file, const size_t off) {
535590
valid = false;
536591
buffer_offset = off;
537592
rc = dfs_read(daos_fs, file, &rsgl, off, &read_size, NULL);
538-
valid = true;
593+
if (!rc) valid = true;
539594
return rc;
540595
}
541596

tensorflow_io/core/filesystems/dfs/dfs_utils.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ typedef struct pool_info {
127127

128128
typedef std::pair<std::string, daos_handle_t> id_handle_t;
129129

130-
// enum used while wildcard matching
131-
enum Children_Status { NON_MATCHING, MATCHING_DIR, OK };
130+
enum File_Mode { READ, WRITE, APPEND };
132131

133132
std::string GetStorageString(uint64_t size);
134133

@@ -176,7 +175,7 @@ class DFS {
176175

177176
int ClearConnections();
178177

179-
void dfsNewFile(std::string& file_path, mode_t mode, int flags,
178+
void dfsNewFile(std::string& file_path, File_Mode mode, int flags,
180179
dfs_obj_t** obj, TF_Status* status);
181180

182181
int dfsPathExists(std::string& file, dfs_obj_t** obj, int release_obj = 1);
@@ -185,6 +184,9 @@ class DFS {
185184

186185
int dfsCreateDir(std::string& dir_path, TF_Status* status);
187186

187+
int dfsDeleteObject(std::string& dir_path, bool is_dir, bool recursive,
188+
TF_Status* status);
189+
188190
bool isRoot(std::string& file_path);
189191

190192
int dfsReadDir(dfs_obj_t* obj, std::vector<std::string>& children);

0 commit comments

Comments
 (0)