-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed variadic arguments for open and added unit tests; Added fcntl;
- Loading branch information
Showing
6 changed files
with
187 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include <rtcheck.h> | ||
#include <memory> | ||
#include <cassert> | ||
#include <fcntl.h> | ||
#include <filesystem> | ||
#include <functional> | ||
#include <rtcheck.h> | ||
#include <stdlib.h> | ||
#include <sys/stat.h> | ||
#include <unistd.h> | ||
|
||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Wdeprecated-declarations" | ||
|
||
int main() | ||
{ | ||
rtc::realtime_context rc; | ||
|
||
namespace fs = std::filesystem; | ||
const fs::path temp_file = std::tmpnam (nullptr); | ||
int fd = creat(temp_file.c_str(), S_IRUSR | S_IWUSR); | ||
assert(fd != -1); | ||
|
||
auto func = [fd] { | ||
struct flock lock{}; | ||
lock.l_type = F_RDLCK; | ||
lock.l_whence = SEEK_SET; | ||
lock.l_start = 0; | ||
lock.l_len = 0; | ||
lock.l_pid = ::getpid(); | ||
|
||
assert(fcntl(fd, F_GETLK, &lock) == 0); | ||
assert(lock.l_type == F_UNLCK); | ||
}; | ||
|
||
close(fd); | ||
std::remove (temp_file.c_str()); | ||
|
||
return 0; | ||
} | ||
|
||
#pragma clang diagnostic pop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include <rtcheck.h> | ||
#include <memory> | ||
#include <cassert> | ||
#include <fcntl.h> | ||
#include <filesystem> | ||
#include <functional> | ||
#include <rtcheck.h> | ||
#include <stdlib.h> | ||
#include <sys/stat.h> | ||
#include <unistd.h> | ||
|
||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Wdeprecated-declarations" | ||
|
||
int main() | ||
{ | ||
rtc::realtime_context rc; | ||
|
||
namespace fs = std::filesystem; | ||
const fs::path temp_file = std::tmpnam (nullptr); | ||
|
||
const int mode = S_IRGRP | S_IROTH | S_IRUSR | S_IWUSR; | ||
const int fd = open (temp_file.c_str(), O_CREAT | O_WRONLY, mode); | ||
assert(fd != -1); | ||
close(fd); | ||
|
||
struct stat st; | ||
assert(stat(temp_file.c_str(), &st) == 0); | ||
|
||
// Mask st_mode to get permission bits only | ||
assert((st.st_mode & 0777) == mode); | ||
|
||
auto res = fs::remove (temp_file); | ||
assert (res); | ||
|
||
return 0; | ||
} | ||
|
||
#pragma clang diagnostic pop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include <cassert> | ||
#include <fcntl.h> | ||
#include <filesystem> | ||
#include <rtcheck.h> | ||
#include <stdlib.h> | ||
#include <sys/stat.h> | ||
#include <unistd.h> | ||
|
||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Wdeprecated-declarations" | ||
|
||
bool test_openCreatesFileWithProperMode() | ||
{ | ||
namespace fs = std::filesystem; | ||
const fs::path temp_file = std::tmpnam (nullptr); | ||
|
||
const int mode = S_IRGRP | S_IROTH | S_IRUSR | S_IWUSR; | ||
const int fd = open (temp_file.c_str(), O_CREAT | O_WRONLY, mode); | ||
assert(fd != -1); | ||
close(fd); | ||
|
||
struct stat st; | ||
assert(stat(temp_file.c_str(), &st) == 0); | ||
|
||
// Mask st_mode to get permission bits only | ||
assert((st.st_mode & 0777) == mode); | ||
|
||
auto res = fs::remove (temp_file); | ||
assert (res); | ||
} | ||
|
||
void test_fcntlFlockDiesWhenRealtime() | ||
{ | ||
namespace fs = std::filesystem; | ||
const fs::path temp_file = std::tmpnam (nullptr); | ||
int fd = creat(temp_file.c_str(), S_IRUSR | S_IWUSR); | ||
assert(fd != -1); | ||
|
||
auto func = [fd] { | ||
struct flock lock{}; | ||
lock.l_type = F_RDLCK; | ||
lock.l_whence = SEEK_SET; | ||
lock.l_start = 0; | ||
lock.l_len = 0; | ||
lock.l_pid = ::getpid(); | ||
|
||
assert(fcntl(fd, F_GETLK, &lock) == 0); | ||
assert(lock.l_type == F_UNLCK); | ||
}; | ||
|
||
close(fd); | ||
std::remove (temp_file.c_str()); | ||
} | ||
|
||
#pragma clang diagnostic pop | ||
|
||
int main() | ||
{ | ||
test_openCreatesFileWithProperMode(); | ||
test_fcntlFlockDiesWhenRealtime(); | ||
|
||
return 0; | ||
} | ||
|