This commit is contained in:
parent
283dd16393
commit
79a724e837
91
main.cpp
91
main.cpp
@ -1,3 +1,12 @@
|
|||||||
|
// == 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
|
||||||
|
|
||||||
// Set this if you don't want MIME types
|
// Set this if you don't want MIME types
|
||||||
//#define NO_MIME
|
//#define NO_MIME
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@ -7,9 +16,11 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#ifndef NO_MIME
|
#ifndef NO_MIME
|
||||||
#include "mime.h"
|
#include <magic.h> // On error try running `make nomime`
|
||||||
#include "archives.h" // Run make nomime
|
#else
|
||||||
|
#include "mime.cpp"
|
||||||
#endif
|
#endif
|
||||||
|
#include "archives.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <array>
|
#include <array>
|
||||||
@ -17,18 +28,6 @@
|
|||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include "devinfo.c"
|
|
||||||
|
|
||||||
void ListPartitions(const std::string& device) {
|
|
||||||
std::ifstream file("/proc/partitions");
|
|
||||||
std::string line;
|
|
||||||
|
|
||||||
while (std::getline(file, line)) {
|
|
||||||
if (line.find(device) != std::string::npos) {
|
|
||||||
std::cout << line << std::endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
enum FileType {
|
enum FileType {
|
||||||
Unknown,
|
Unknown,
|
||||||
@ -36,12 +35,10 @@ enum FileType {
|
|||||||
BLKFile, CHRFile,
|
BLKFile, CHRFile,
|
||||||
FIFO, Symlink,
|
FIFO, Symlink,
|
||||||
Socket,
|
Socket,
|
||||||
#ifndef NO_MIME
|
|
||||||
Archive, Audio,
|
Archive, Audio,
|
||||||
Application,
|
Application,
|
||||||
Video, Binary,
|
Video, Binary,
|
||||||
Text
|
Text
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
|
||||||
std::array<int, 4> get_files_in_directory(const std::string& path)
|
std::array<int, 4> get_files_in_directory(const std::string& path)
|
||||||
@ -79,7 +76,6 @@ bool starts_with(const std::string& prefix, const std::string& string) {
|
|||||||
return string.rfind(prefix, 0) == 0;
|
return string.rfind(prefix, 0) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef NO_MIME
|
|
||||||
bool is_archive(const std::string& mime_type)
|
bool is_archive(const std::string& mime_type)
|
||||||
{
|
{
|
||||||
const std::string archives[] = ARCHIVES;
|
const std::string archives[] = ARCHIVES;
|
||||||
@ -94,12 +90,10 @@ bool is_archive(const std::string& mime_type)
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
std::string get_name_from_filetype(const enum FileType fs)
|
std::string get_name_from_filetype(const enum FileType fs)
|
||||||
{
|
{
|
||||||
switch (fs) {
|
switch (fs) {
|
||||||
#ifndef NO_MIME
|
|
||||||
case FileType::Application:
|
case FileType::Application:
|
||||||
return "Application";
|
return "Application";
|
||||||
case FileType::Archive:
|
case FileType::Archive:
|
||||||
@ -108,7 +102,6 @@ std::string get_name_from_filetype(const enum FileType fs)
|
|||||||
return "Audio";
|
return "Audio";
|
||||||
case FileType::Binary:
|
case FileType::Binary:
|
||||||
return "Binary";
|
return "Binary";
|
||||||
#endif
|
|
||||||
case FileType::File:
|
case FileType::File:
|
||||||
return "File";
|
return "File";
|
||||||
case FileType::Folder:
|
case FileType::Folder:
|
||||||
@ -122,13 +115,11 @@ std::string get_name_from_filetype(const enum FileType fs)
|
|||||||
case FileType::Socket:
|
case FileType::Socket:
|
||||||
return "UNIX socket";
|
return "UNIX socket";
|
||||||
case FileType::Symlink:
|
case FileType::Symlink:
|
||||||
return "Symbolic";
|
return "Symbolic link";
|
||||||
#ifndef NO_MIME
|
|
||||||
case FileType::Text:
|
case FileType::Text:
|
||||||
return "Text";
|
return "Text";
|
||||||
case FileType::Video:
|
case FileType::Video:
|
||||||
return "Video";
|
return "Video";
|
||||||
#endif
|
|
||||||
default:
|
default:
|
||||||
return "Unknown";
|
return "Unknown";
|
||||||
}
|
}
|
||||||
@ -138,7 +129,6 @@ std::string get_emoji_from_filetype(const enum FileType fs)
|
|||||||
{
|
{
|
||||||
#ifndef NO_EMOJIS
|
#ifndef NO_EMOJIS
|
||||||
switch (fs) {
|
switch (fs) {
|
||||||
#ifndef NO_MIME
|
|
||||||
case FileType::Application:
|
case FileType::Application:
|
||||||
return "⚙️";
|
return "⚙️";
|
||||||
case FileType::Archive:
|
case FileType::Archive:
|
||||||
@ -147,7 +137,6 @@ std::string get_emoji_from_filetype(const enum FileType fs)
|
|||||||
return "🎵";
|
return "🎵";
|
||||||
case FileType::Binary:
|
case FileType::Binary:
|
||||||
return "⚙️";
|
return "⚙️";
|
||||||
#endif
|
|
||||||
case FileType::File:
|
case FileType::File:
|
||||||
return "📄";
|
return "📄";
|
||||||
case FileType::Folder:
|
case FileType::Folder:
|
||||||
@ -162,12 +151,10 @@ std::string get_emoji_from_filetype(const enum FileType fs)
|
|||||||
return "🔌";
|
return "🔌";
|
||||||
case FileType::Symlink:
|
case FileType::Symlink:
|
||||||
return "🔗";
|
return "🔗";
|
||||||
#ifndef NO_MIME
|
|
||||||
case FileType::Text:
|
case FileType::Text:
|
||||||
return "📝";
|
return "📝";
|
||||||
case FileType::Video:
|
case FileType::Video:
|
||||||
return "🎥";
|
return "🎥";
|
||||||
#endif
|
|
||||||
default:
|
default:
|
||||||
return "?";
|
return "?";
|
||||||
}
|
}
|
||||||
@ -199,9 +186,9 @@ std::string readable_fs(const long int size /*in bytes*/,
|
|||||||
return str_result;
|
return str_result;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef NO_MIME
|
|
||||||
std::string get_mime(const std::string& filename)
|
std::string get_mime(const std::string& filename)
|
||||||
{
|
{
|
||||||
|
#ifndef NO_MIME
|
||||||
const char* file_path = filename.c_str();
|
const char* file_path = filename.c_str();
|
||||||
const auto magic_cookie = static_cast<const magic_t>(magic_open(MAGIC_MIME_TYPE));
|
const auto magic_cookie = static_cast<const magic_t>(magic_open(MAGIC_MIME_TYPE));
|
||||||
if (magic_cookie == nullptr) {
|
if (magic_cookie == nullptr) {
|
||||||
@ -228,10 +215,12 @@ std::string get_mime(const std::string& filename)
|
|||||||
|
|
||||||
// Close libmagic
|
// Close libmagic
|
||||||
magic_close(magic_cookie);
|
magic_close(magic_cookie);
|
||||||
|
#else
|
||||||
|
std::string extension = filename.substr(filename.find_last_of(".")+1);
|
||||||
|
std::string result = guessMime(extension);
|
||||||
|
#endif
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
std::string print_permissions(mode_t mode, bool is_dir) {
|
std::string print_permissions(mode_t mode, bool is_dir) {
|
||||||
std::string user;
|
std::string user;
|
||||||
@ -265,6 +254,7 @@ FileType getFileType(mode_t st_mode) {
|
|||||||
if(S_ISLNK(st_mode)) return FileType::Symlink;
|
if(S_ISLNK(st_mode)) return FileType::Symlink;
|
||||||
if(S_ISSOCK(st_mode)) return FileType::Socket;
|
if(S_ISSOCK(st_mode)) return FileType::Socket;
|
||||||
if(S_ISREG(st_mode)) return FileType::File;
|
if(S_ISREG(st_mode)) return FileType::File;
|
||||||
|
return FileType::Unknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
@ -300,7 +290,9 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t last = files.size();
|
size_t last = files.size();
|
||||||
for (size_t i = 0; i < last; ++i) {
|
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];
|
const std::string& filename = files[i];
|
||||||
char* c_filename = const_cast<char*>(filename.c_str());
|
char* c_filename = const_cast<char*>(filename.c_str());
|
||||||
|
|
||||||
@ -322,15 +314,10 @@ int main(int argc, char *argv[])
|
|||||||
// delete[] temp;
|
// delete[] temp;
|
||||||
std::string name = basename(full_name);
|
std::string name = basename(full_name);
|
||||||
if(file == FileType::Folder) name += "/";
|
if(file == FileType::Folder) name += "/";
|
||||||
#ifndef NO_MIME
|
|
||||||
std::string mime_type = get_mime(full_name);
|
std::string mime_type = get_mime(full_name);
|
||||||
//file = (is_file) ? FileType::File : FileType::Folder;
|
//file = (is_file) ? FileType::File : FileType::Folder;
|
||||||
#else
|
if (file == FileType::File) {
|
||||||
std::string mime_type = "application/octet-stream (no mime)";
|
if (is_archive(mime_type)) {
|
||||||
#endif
|
|
||||||
if (starts_with("inode/directory", mime_type) || !is_file) {
|
|
||||||
file = FileType::Folder;
|
|
||||||
} else if (is_archive(mime_type)) {
|
|
||||||
file = FileType::Archive;
|
file = FileType::Archive;
|
||||||
} else if (starts_with("application/octet-stream", mime_type)) {
|
} else if (starts_with("application/octet-stream", mime_type)) {
|
||||||
file = FileType::Binary;
|
file = FileType::Binary;
|
||||||
@ -345,31 +332,31 @@ int main(int argc, char *argv[])
|
|||||||
} else {
|
} else {
|
||||||
file = FileType::Binary;
|
file = FileType::Binary;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
std::string type = get_name_from_filetype(file);
|
std::string type = get_name_from_filetype(file);
|
||||||
std::string emoji = get_emoji_from_filetype(file);
|
std::string emoji = get_emoji_from_filetype(file);
|
||||||
if (emojis) printf("%s ", emoji.c_str());
|
if (emojis) printf("%s ", emoji.c_str());
|
||||||
printf("\033[1m%s\033[0m:\n", name.c_str());
|
printf("\033[1m%s\033[0m:\n", name.c_str());
|
||||||
printf("\tName: %s\n", name.c_str());
|
printf("%sName: %s\n", TABS, name.c_str());
|
||||||
printf("\tType: %s\n", type.c_str());
|
printf("%sType: %s\n", TABS, type.c_str());
|
||||||
if (absolute) printf("\tAbsolute path: %s\n", full_name.c_str());
|
if (absolute) printf("%sAbsolute path: %s\n", TABS, full_name.c_str());
|
||||||
#ifndef NO_MIME
|
printf("%sMIME: %s\n", TABS, mime_type.c_str());
|
||||||
printf("\tMIME: %s\n", mime_type.c_str());
|
|
||||||
#endif
|
|
||||||
if (is_file) {
|
if (is_file) {
|
||||||
std::string _1024 = readable_fs(file_stat.st_size, 1000, "B");
|
std::string _1024 = readable_fs(file_stat.st_size, 1000, "B");
|
||||||
std::string _1000 = readable_fs(file_stat.st_size, 1024, "iB");
|
std::string _1000 = readable_fs(file_stat.st_size, 1024, "iB");
|
||||||
if (file_stat.st_size >= 1000) printf("\tFile size: %s or %s (%li bytes)\n", _1024.c_str(), _1000.c_str(), file_stat.st_size);
|
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("\tFile size: %li bytes\n", file_stat.st_size);
|
else printf("%sFile size: %li bytes\n", TABS, file_stat.st_size);
|
||||||
} else if (file == FileType::Folder) {
|
} else if (file == FileType::Folder) {
|
||||||
auto insides = get_files_in_directory(full_name);
|
auto insides = get_files_in_directory(full_name);
|
||||||
printf("\tContents:\n");
|
printf("%sContents:\n", TABS);
|
||||||
printf("\t\tNormal: %i files and %i folders.\n", insides[0], insides[1]);
|
printf("%s%sNormal: %i files and %i folders.\n", TABS, TABS, insides[0], insides[1]);
|
||||||
printf("\t\tHidden: %i files and %i folders.\n", insides[2], insides[3]);
|
printf("%s%sHidden: %i files and %i folders.\n", TABS, TABS, insides[2], insides[3]);
|
||||||
} else if (file == FileType::CHRFile) {
|
// Coming soon
|
||||||
ListPartitions(full_name);
|
//} else if (file == FileType::CHRFile) {
|
||||||
|
// ListPartitions(full_name);
|
||||||
}
|
}
|
||||||
printf("\tPermissions: %s\n", print_permissions(file_stat.st_mode, !is_file).c_str());
|
printf("%sPermissions: %s\n", TABS, print_permissions(file_stat.st_mode, (file == FileType::Folder)).c_str());
|
||||||
if (i != last-1) {
|
if (i != last-1) {
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user