pmt: first codes.

This commit is contained in:
2025-07-21 12:08:45 +03:00
commit d65867c249
22 changed files with 2542 additions and 0 deletions

View File

@@ -0,0 +1,83 @@
/*
Copyright 2025 Yağız Zengin
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sys/stat.h>
#include <unistd.h>
#include <libhelper/lib.hpp>
namespace Helper {
bool hasSuperUser()
{
return (getuid() == 0);
}
bool isExists(const std::string_view entry)
{
struct stat st;
return (stat(entry.data(), &st) == 0);
}
bool fileIsExists(const std::string_view file)
{
struct stat st;
if (stat(file.data(), &st) != 0) return false;
return S_ISREG(st.st_mode);
}
bool directoryIsExists(const std::string_view directory)
{
struct stat st;
if (stat(directory.data(), &st) != 0) return false;
return S_ISDIR(st.st_mode);
}
bool linkIsExists(const std::string_view entry)
{
return (isLink(entry) || isHardLink(entry));
}
bool isLink(const std::string_view entry)
{
struct stat st;
if (lstat(entry.data(), &st) != 0) return false;
return S_ISLNK(st.st_mode);
}
bool isSymbolicLink(const std::string_view entry)
{
return isLink(entry);
}
bool isHardLink(const std::string_view entry)
{
struct stat st;
if (lstat(entry.data(), &st) != 0) return false;
return (st.st_nlink >= 2);
}
bool areLinked(const std::string_view entry1, const std::string_view entry2)
{
const std::string_view st1 = (isSymbolicLink(entry1)) ? readSymlink(entry1) : entry1;
const std::string_view st2 = (isSymbolicLink(entry2)) ? readSymlink(entry2) : entry2;
return (st1 == st2);
}
} // namespace Helper