libc_fs: Implemented and test 'unlink' call

Issue #1048
This commit is contained in:
Sebastian Sumpf 2014-01-21 13:40:49 +01:00 committed by Norman Feske
parent 1b1693e6ff
commit 2e04cc2d87
2 changed files with 33 additions and 1 deletions

View File

@ -744,7 +744,28 @@ class Plugin : public Libc::Plugin
int unlink(const char *path)
{
return -1;
Canonical_path dir_path(path);
dir_path.strip_last_element();
Canonical_path basename(path);
basename.keep_only_last_element();
try {
/*
* Open directory that contains the file to be opened/created
*/
File_system::Dir_handle const dir_handle =
file_system()->dir(dir_path.base(), false);
Node_handle_guard guard(dir_handle);
file_system()->unlink(dir_handle, basename.base() + 1);
} catch (...) {
PERR("unlink(%s) failed", path);
return -1;
}
return 0;
}
ssize_t write(Libc::File_descriptor *fd, const void *buf, ::size_t count)

View File

@ -194,6 +194,13 @@ int main(int argc, char *argv[])
CALL_AND_CHECK(ret, close(fd), ret == 0, "");
CALL_AND_CHECK(ret, stat(file_name, &stat_buf), ret == 0, "file_name=%s", file_name);
/* test 'unlink()' */
CALL_AND_CHECK(ret, unlink(file_name), (ret == 0), "file_name=%s", file_name);
CALL_AND_CHECK(ret, stat(file_name, &stat_buf), (ret == -1), "file_name=%s", file_name);
CALL_AND_CHECK(ret, stat(dir_name2, &stat_buf), (ret == 0), "dir_name=%s", dir_name2);
CALL_AND_CHECK(ret, unlink(dir_name2), (ret == 0), "dir_name=%s", dir_name2);
CALL_AND_CHECK(ret, stat(dir_name2, &stat_buf), (ret == -1), "dir_name=%s", dir_name2);
/* test symbolic links */
if ((symlink("/", "/symlinks_supported") == 0) || (errno != ENOSYS)) {
CALL_AND_CHECK(ret, mkdir("/a", 0777), ((ret == 0) || (errno == EEXIST)), "dir_name=%s", "/a");
@ -216,6 +223,10 @@ int main(int argc, char *argv[])
} else {
printf("file content is correct\n");
}
/* test unlink for symbolic links */
CALL_AND_CHECK(ret, unlink("/c/d"), (ret == 0), "symlink=%s", "/c/d");
CALL_AND_CHECK(ret, stat("/c/d", &stat_buf), (ret == -1), "symlink=%s", "/c/d");
}
if (i < (iterations - 1))