81 lines
3.4 KiB
C++
81 lines
3.4 KiB
C++
#include <filesystem>
|
|
#include <iostream>
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
void printPerms(fs::perms p) {
|
|
std::cout << ((p & fs::perms::owner_read) != fs::perms::none ? "r" : "-")
|
|
<< ((p & fs::perms::owner_write) != fs::perms::none ? "w" : "-")
|
|
<< ((p & fs::perms::owner_exec) != fs::perms::none ? "x" : "-")
|
|
<< ((p & fs::perms::group_read) != fs::perms::none ? "r" : "-")
|
|
<< ((p & fs::perms::group_write) != fs::perms::none ? "w" : "-")
|
|
<< ((p & fs::perms::group_exec) != fs::perms::none ? "x" : "-")
|
|
<< ((p & fs::perms::others_read) != fs::perms::none ? "r" : "-")
|
|
<< ((p & fs::perms::others_write) != fs::perms::none ? "w" : "-")
|
|
<< ((p & fs::perms::others_exec) != fs::perms::none ? "x" : "-")
|
|
<< '\n';
|
|
}
|
|
|
|
void printPermissionsForDir() {
|
|
for (const auto& entry : fs::directory_iterator(fs::current_path().string() + "/project")) {
|
|
if (fs::is_regular_file(entry.path())) {
|
|
std::cout << "File: "
|
|
<< std::left << std::setw(20)
|
|
<< entry.path().filename()
|
|
<< " | Permissions: ";
|
|
printPerms(fs::status(entry.path()).permissions());
|
|
}
|
|
}
|
|
std::cout << std::string(80, '_') << "\n\n";
|
|
}
|
|
|
|
void changePermissions() {
|
|
for (const auto& entry : fs::directory_iterator(fs::current_path().string() + "/project")) {
|
|
if (fs::is_regular_file(entry.path())) {
|
|
fs::perms p = fs::perms::none;
|
|
using per = fs::perms;
|
|
if(entry.path().extension() == ".exe"){
|
|
p = per::owner_all | per::group_read | per::group_write | per::others_read | per::others_write;
|
|
} else if(entry.path().extension() == ".cpp" || entry.path().extension() == ".hpp"){
|
|
p = per::owner_read | per::owner_write | per::group_read | per::others_read;
|
|
}
|
|
fs::permissions(entry.path(), p);
|
|
}
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
printPermissionsForDir();
|
|
changePermissions();
|
|
printPermissionsForDir();
|
|
|
|
// Part 2 unncoment later
|
|
auto print_last_write_time = [](std::filesystem::file_time_type const& ftime) {
|
|
std::time_t time = std::chrono::system_clock::to_time_t(
|
|
std::chrono::file_clock::to_sys(ftime));
|
|
std::cout << "File write time is " << std::asctime(std::localtime(&time));
|
|
};
|
|
|
|
fs::path last_mod;
|
|
fs::path longest;
|
|
for (const auto& entry : fs::directory_iterator(fs::current_path().string() + "/project")) {
|
|
if (fs::is_regular_file(entry.path())) {
|
|
if(last_mod == ""){
|
|
last_mod = entry.path();
|
|
longest = entry.path();
|
|
continue;
|
|
}
|
|
|
|
if(fs::last_write_time(last_mod) < fs::last_write_time(entry.path())){
|
|
last_mod = entry.path();
|
|
}
|
|
if(fs::last_write_time(longest) > fs::last_write_time(entry.path())){
|
|
longest = entry.path();
|
|
}
|
|
}
|
|
}
|
|
std::cout << "Last modified file is: " << last_mod << '\n';
|
|
print_last_write_time(std::filesystem::last_write_time(last_mod));
|
|
std::cout << "files which were longest unmodified: " << longest << std::endl;
|
|
print_last_write_time(std::filesystem::last_write_time(longest));
|
|
}
|