fileinfo/main.cpp

367 lines
9.8 KiB
C++
Raw Permalink Normal View History

2024-10-17 20:17:44 +05:30
// == Config, imports are on line N
#define TABS " " // What will be used instead of tabs?
#define MAX_FILES 50 // Maximum number of files that fileinfo will process
#define FILESIZE "n" // "mib" = 1024 bytes == 1 Mbyte
// "mb" = 1000 bytes == 1 Mbyte
// "b" = Don't simplify sizes
// "n" = Use both
2024-08-23 22:31:16 +05:30
// Set this if you don't want MIME types
//#define NO_MIME
2024-08-20 23:21:21 +05:30
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <sys/stat.h>
#include <cstring>
#include <unistd.h>
2024-08-23 22:43:15 +05:30
#ifndef NO_MIME
2024-10-17 20:17:44 +05:30
#include <magic.h> // On error try running `make nomime`
#else
#include "mime.cpp"
2024-08-23 22:31:16 +05:30
#endif
2024-10-17 20:17:44 +05:30
#include "archives.h"
2024-08-20 23:21:21 +05:30
#include <string>
#include <vector>
2024-10-16 21:16:18 +05:30
#include <array>
2024-08-20 23:21:21 +05:30
#include <sys/types.h>
#include <dirent.h>
2024-10-16 21:16:18 +05:30
#include <fstream>
2024-08-20 23:21:21 +05:30
enum FileType {
2024-10-17 20:17:44 +05:30
Unknown,
File, Folder,
BLKFile, CHRFile,
FIFO, Symlink,
Socket,
Archive, Audio,
Application,
Video, Binary,
Text
2024-08-20 23:21:21 +05:30
};
2024-10-16 21:16:18 +05:30
std::array<int, 4> get_files_in_directory(const std::string& path)
2024-08-20 23:21:21 +05:30
{
2024-10-17 20:17:44 +05:30
DIR* dp = opendir(path.c_str());
// int normal, hidden;
std::array<int, 2> normal = {0, 0};
std::array<int, 2> hidden = {0, 0};
if (dp != nullptr) {
struct dirent* ep;
while((ep = readdir(dp))) {
// if (ep->d_name[0] != '.') i++;
if (strcmp(ep->d_name, ".") != 0 && strcmp(ep->d_name, "..") != 0) {
if (ep->d_name[0] == '.') {
hidden[(ep->d_type == DT_DIR) ? 1 : 0]++;
}else {
normal[(ep->d_type == DT_DIR) ? 1 : 0]++;
2024-08-20 23:21:21 +05:30
}
2024-10-17 20:17:44 +05:30
}
2024-08-20 23:21:21 +05:30
}
2024-10-17 20:17:44 +05:30
(void)closedir(dp);
}
else
perror("Couldn't open the directory");
std::array<int, 4> result;
result[0] = normal[0];
result[1] = normal[1];
result[2] = hidden[0];
result[3] = hidden[1];
return result;
2024-08-20 23:21:21 +05:30
}
bool starts_with(const std::string& prefix, const std::string& string) {
2024-10-17 20:17:44 +05:30
return string.rfind(prefix, 0) == 0;
2024-08-20 23:21:21 +05:30
}
bool is_archive(const std::string& mime_type)
{
2024-10-17 20:17:44 +05:30
const std::string archives[] = ARCHIVES;
int len = sizeof(archives)/sizeof(archives[0]);
2024-08-20 23:21:21 +05:30
2024-10-17 20:17:44 +05:30
for(int i = 0; i < len; ++i)
{
if(starts_with(archives[i], mime_type))
2024-08-20 23:21:21 +05:30
{
2024-10-17 20:17:44 +05:30
return true;
2024-08-20 23:21:21 +05:30
}
2024-10-17 20:17:44 +05:30
}
return false;
2024-08-20 23:21:21 +05:30
}
std::string get_name_from_filetype(const enum FileType fs)
{
2024-10-17 20:17:44 +05:30
switch (fs) {
case FileType::Application:
return "Application";
case FileType::Archive:
return "Archive";
case FileType::Audio:
return "Audio";
case FileType::Binary:
return "Binary";
case FileType::File:
return "File";
case FileType::Folder:
return "Folder";
case FileType::BLKFile:
return "Block special file";
case FileType::CHRFile:
return "Character special file";
case FileType::FIFO:
return "Pipe or FIFO special file";
case FileType::Socket:
return "UNIX socket";
case FileType::Symlink:
return "Symbolic link";
case FileType::Text:
return "Text";
case FileType::Video:
return "Video";
default:
return "Unknown";
}
2024-08-20 23:21:21 +05:30
}
2024-10-16 21:16:18 +05:30
2024-08-20 23:21:21 +05:30
std::string get_emoji_from_filetype(const enum FileType fs)
{
2024-10-17 20:17:44 +05:30
#ifndef NO_EMOJIS
switch (fs) {
case FileType::Application:
return "⚙️";
case FileType::Archive:
return "🗜️";
case FileType::Audio:
return "🎵";
case FileType::Binary:
return "⚙️";
case FileType::File:
return "📄";
case FileType::Folder:
return "📁";
case FileType::BLKFile:
return "🪄";
case FileType::CHRFile:
return "🖨️";
case FileType::FIFO:
return "🤝";
case FileType::Socket:
return "🔌";
case FileType::Symlink:
return "🔗";
case FileType::Text:
return "📝";
case FileType::Video:
return "🎥";
default:
return "?";
}
#else
return "";
#endif
2024-08-20 23:21:21 +05:30
}
2024-10-16 21:16:18 +05:30
std::string readable_fs(const long int size /*in bytes*/,
2024-10-17 20:17:44 +05:30
const double divide_by = 1024,
const std::string& suffix = "B")
2024-08-20 23:21:21 +05:30
{
2024-10-17 20:17:44 +05:30
if(size < 1024) {
2024-08-20 23:21:21 +05:30
char buffer[128];
2024-10-17 20:17:44 +05:30
sprintf(buffer, "%i B", static_cast<int>(size));
std::string result(buffer);
return buffer;
}
auto result = static_cast<double>(size);
int i = 0;
const std::string units[] = {"", "K", "M", "G", "T", "P", "E", "Z", "Y"};
while (result > divide_by) {
result /= divide_by;
i++;
}
char buffer[128];
sprintf(buffer, "%.*f %s%s", i, result, units[i].c_str(), suffix.c_str());
std::string str_result(buffer);
return str_result;
2024-08-20 23:21:21 +05:30
}
std::string get_mime(const std::string& filename)
{
2024-10-17 20:17:44 +05:30
#ifndef NO_MIME
const char* file_path = filename.c_str();
const auto magic_cookie = static_cast<const magic_t>(magic_open(MAGIC_MIME_TYPE));
if (magic_cookie == nullptr) {
std::cerr << "Unable to initialize libmagic\n";
return "";
}
2024-08-20 23:21:21 +05:30
2024-10-17 20:17:44 +05:30
// Load definitions from the mime types database
if (magic_load(magic_cookie, nullptr) != 0) {
std::cerr << "Unable to load database definitions\n";
magic_close(magic_cookie);
return "";
}
2024-08-20 23:21:21 +05:30
2024-10-17 20:17:44 +05:30
// Determines the MIME type of the file
const char *mime_type = magic_file(magic_cookie, file_path);
if (mime_type == nullptr) {
std::cerr << "Unable to determine the MIME type of the file\n";
2024-08-20 23:21:21 +05:30
magic_close(magic_cookie);
2024-10-17 20:17:44 +05:30
return "";
}
std::string result(mime_type);
2024-08-20 23:21:21 +05:30
2024-10-17 20:17:44 +05:30
// Close libmagic
magic_close(magic_cookie);
#else
std::string extension = filename.substr(filename.find_last_of(".")+1);
std::string result = guessMime(extension);
#endif
return result;
2024-08-20 23:21:21 +05:30
}
std::string print_permissions(mode_t mode, bool is_dir) {
2024-10-17 20:17:44 +05:30
std::string user;
user += (mode & S_IRUSR) ? "r" : "-";
user += (mode & S_IWUSR) ? "w" : "-",
user += (mode & S_IXUSR) ? "x" : "-";
2024-08-20 23:21:21 +05:30
2024-10-17 20:17:44 +05:30
std::string group;
group += (mode & S_IRGRP) ? "r" : "-";
group += (mode & S_IWGRP) ? "w" : "-";
group += (mode & S_IXGRP) ? "x" : "-";
2024-08-20 23:21:21 +05:30
2024-10-17 20:17:44 +05:30
std::string other;
other += (mode & S_IROTH) ? "r" : "-";
other += (mode & S_IWOTH) ? "w" : "-";
other += (mode & S_IXOTH) ? "x" : "-";
2024-08-20 23:21:21 +05:30
2024-10-14 02:55:37 +05:30
2024-10-17 20:17:44 +05:30
return (is_dir ? "d" : "-") + user + group + other;
2024-08-20 23:21:21 +05:30
}
std::string basename(const std::string& filepath) {
2024-10-17 20:17:44 +05:30
return filepath.substr(filepath.find_last_of("/\\") + 1);
2024-08-20 23:21:21 +05:30
}
2024-10-16 21:16:18 +05:30
FileType getFileType(mode_t st_mode) {
2024-10-17 20:17:44 +05:30
if(S_ISBLK(st_mode)) return FileType::BLKFile;
if(S_ISCHR(st_mode)) return FileType::CHRFile;
if(S_ISDIR(st_mode)) return FileType::Folder;
if(S_ISFIFO(st_mode)) return FileType::FIFO;
if(S_ISLNK(st_mode)) return FileType::Symlink;
if(S_ISSOCK(st_mode)) return FileType::Socket;
if(S_ISREG(st_mode)) return FileType::File;
return FileType::Unknown;
2024-10-16 21:16:18 +05:30
}
2024-08-20 23:21:21 +05:30
int main(int argc, char *argv[])
{
2024-10-17 20:17:44 +05:30
bool absolute = false;
#ifndef NO_EMOJIS
bool emojis = true;
#else
bool emojis = false;
#endif
2024-10-16 21:16:18 +05:30
2024-10-17 20:17:44 +05:30
if (argc < 2) {
std::cerr << "Usage:\n\t" << argv[0] << " [args] filename ...\n";
std::cerr << "Args:" << std::endl;
std::cerr << "\t-A Output absolute path" << std::endl;
std::cerr << "\t-E Do not show emojis" << std::endl;
return 255;
}
std::vector<std::string> files;
for (int i = 1; i < argc; ++i) {
if (argv[i][0] != '-') {
std::string temp(argv[i]);
files.insert(files.end(), temp);
} else {
std::string arg(argv[i]);
if(arg == "-A") {
absolute = true;
}
if (arg == "-E") {
emojis = false;
}
2024-08-20 23:21:21 +05:30
}
2024-10-17 20:17:44 +05:30
}
2024-08-20 23:21:21 +05:30
2024-10-17 20:17:44 +05:30
size_t last = files.size();
if (last > MAX_FILES)
printf("Too much files, showing only %i...\n", MAX_FILES);
for (size_t i = 0; i < last || i < MAX_FILES; ++i) {
const std::string& filename = files[i];
char* c_filename = const_cast<char*>(filename.c_str());
2024-08-20 23:21:21 +05:30
2024-10-17 20:17:44 +05:30
struct stat file_stat = {};
2024-08-20 23:21:21 +05:30
2024-10-17 20:17:44 +05:30
if (stat(filename.c_str(), &file_stat) != 0) {
perror("stat");
return 1;
}
// start
FileType file = getFileType(file_stat.st_mode);
bool is_file = file == FileType::File;
char temp[4097] = {'\0'};
if (realpath(c_filename, temp) == nullptr) {
perror("realpath");
return 1;
}
std::string full_name(temp); // = malloc(4097);
// delete[] temp;
std::string name = basename(full_name);
if(file == FileType::Folder) name += "/";
std::string mime_type = get_mime(full_name);
//file = (is_file) ? FileType::File : FileType::Folder;
if (file == FileType::File) {
if (is_archive(mime_type)) {
file = FileType::Archive;
} else if (starts_with("application/octet-stream", mime_type)) {
file = FileType::Binary;
} else if (starts_with("application", mime_type)) {
file = FileType::Application;
} else if (starts_with("audio", mime_type)) {
file = FileType::Audio;
} else if (starts_with("video", mime_type)) {
file = FileType::Video;
} else if (starts_with("text", mime_type)) {
file = FileType::Text;
} else {
file = FileType::Binary;
}
}
std::string type = get_name_from_filetype(file);
std::string emoji = get_emoji_from_filetype(file);
if (emojis) printf("%s ", emoji.c_str());
printf("\033[1m%s\033[0m:\n", name.c_str());
printf("%sName: %s\n", TABS, name.c_str());
printf("%sType: %s\n", TABS, type.c_str());
if (absolute) printf("%sAbsolute path: %s\n", TABS, full_name.c_str());
printf("%sMIME: %s\n", TABS, mime_type.c_str());
if (is_file) {
std::string _1024 = readable_fs(file_stat.st_size, 1000, "B");
std::string _1000 = readable_fs(file_stat.st_size, 1024, "iB");
if (file_stat.st_size >= 1000) printf("%sFile size: %s or %s (%li bytes)\n", TABS, _1024.c_str(), _1000.c_str(), file_stat.st_size);
else printf("%sFile size: %li bytes\n", TABS, file_stat.st_size);
} else if (file == FileType::Folder) {
auto insides = get_files_in_directory(full_name);
printf("%sContents:\n", TABS);
printf("%s%sNormal: %i files and %i folders.\n", TABS, TABS, insides[0], insides[1]);
printf("%s%sHidden: %i files and %i folders.\n", TABS, TABS, insides[2], insides[3]);
// Coming soon
//} else if (file == FileType::CHRFile) {
// ListPartitions(full_name);
}
printf("%sPermissions: %s\n", TABS, print_permissions(file_stat.st_mode, (file == FileType::Folder)).c_str());
if (i != last-1) {
printf("\n");
2024-08-20 23:21:21 +05:30
}
2024-10-17 20:17:44 +05:30
}
2024-10-14 02:55:37 +05:30
2024-10-17 20:17:44 +05:30
return 0;
2024-08-20 23:21:21 +05:30
}