Add archives.h
This commit is contained in:
parent
d8cb560f68
commit
7bae08c08f
9
Makefile
9
Makefile
@ -19,10 +19,19 @@ $(OUTPUT): $(SRC)
|
|||||||
nomime: $(SRC)
|
nomime: $(SRC)
|
||||||
$(CXX) $(SRC) -o $(OUTPUT) -D NO_MIME
|
$(CXX) $(SRC) -o $(OUTPUT) -D NO_MIME
|
||||||
|
|
||||||
|
noemojis: $(SRC)
|
||||||
|
$(CXX) $(SRC) $(CXXFLAGS) -o $(OUTPUT) -D NO_EMOJIS
|
||||||
|
|
||||||
check: $(OUTPUT)
|
check: $(OUTPUT)
|
||||||
$(CXX) $(SRC) $(CXXFLAGS) -o $(OUTPUT)
|
$(CXX) $(SRC) $(CXXFLAGS) -o $(OUTPUT)
|
||||||
rm -f $(OUTPUT)
|
rm -f $(OUTPUT)
|
||||||
|
|
||||||
|
install: $(OUTPUT)
|
||||||
|
cp $(OUTPUT) /usr/bin/
|
||||||
|
|
||||||
|
userinstall: $(OUTPUT)
|
||||||
|
cp $(OUTPUT) ~/.local/bin/
|
||||||
|
|
||||||
# Clean up generated files
|
# Clean up generated files
|
||||||
clean:
|
clean:
|
||||||
rm -f $(OUTPUT)
|
rm -f $(OUTPUT)
|
||||||
|
2
archives.h
Normal file
2
archives.h
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
// MIME types for archives
|
||||||
|
#define ARCHIVES {"application/x-archive", "application/x-cpio", "application/x-shar", "application/x-iso9660-image", "application/x-sbx", "application/x-tar", "application/x-brotli", "application/x-bzip2", "application/vnd.genozip", "application/gzip", "application/x-lzip", "application/x-lzma", "application/x-lzop", "application/x-snappy-framed", "application/x-xz", "application/x-compress", "application/x-compress", "application/zstd", "application/x-7z-compressed", "application/x-ace-compressed", "application/x-astrotite-afa", "application/x-alz-compressed", "application/vnd.android.package-archive", "application/x-freearc", "application/x-arj", "application/x-b1", "application/vnd.ms-cab-compressed", "application/x-cfs-compressed", "application/x-dar", "application/x-dgc-compressed", "application/x-apple-diskimage", "application/x-gca-compressed", "application/java-archive", "application/x-lzh", "application/x-lzx", "application/x-rar-compressed", "application/x-stuffit", "application/x-stuffitx", "application/x-gtar", "application/x-ms-wim", "application/x-xar", "application/zip", "application/x-zoo"}
|
52
devinfo.c
Normal file
52
devinfo.c
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
#include <cstdio>
|
||||||
|
#include <cstring>
|
||||||
|
#include <cerr>
|
||||||
|
#include <blkid/blkid.h>
|
||||||
|
|
||||||
|
|
||||||
|
void printdev (int argc, char *argv[]) {
|
||||||
|
blkid_probe pr = blkid_new_probe_from_filename(argv[1]);
|
||||||
|
if (!pr) {
|
||||||
|
err(1, "Failed to open %s", argv[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get number of partitions
|
||||||
|
blkid_partlist ls;
|
||||||
|
int nparts, i;
|
||||||
|
|
||||||
|
ls = blkid_probe_get_partitions(pr);
|
||||||
|
nparts = blkid_partlist_numof_partitions(ls);
|
||||||
|
printf("Number of partitions:%d\n", nparts);
|
||||||
|
|
||||||
|
if (nparts <= 0){
|
||||||
|
printf("Please enter correct device name! e.g. \"/dev/sdc\"\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get UUID, label and type
|
||||||
|
const char *uuid;
|
||||||
|
const char *label;
|
||||||
|
const char *type;
|
||||||
|
|
||||||
|
for (i = 0; i < nparts; i++) {
|
||||||
|
char dev_name[20];
|
||||||
|
|
||||||
|
sprintf(dev_name, "%s%d", argv[1], (i+1));
|
||||||
|
|
||||||
|
pr = blkid_new_probe_from_filename(dev_name);
|
||||||
|
blkid_do_probe(pr);
|
||||||
|
|
||||||
|
blkid_probe_lookup_value(pr, "UUID", &uuid, NULL);
|
||||||
|
|
||||||
|
blkid_probe_lookup_value(pr, "LABEL", &label, NULL);
|
||||||
|
|
||||||
|
blkid_probe_lookup_value(pr, "TYPE", &type, NULL);
|
||||||
|
|
||||||
|
printf("Name=%s, UUID=%s, LABEL=%s, TYPE=%s\n", dev_name, uuid, label, type);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
blkid_free_probe(pr);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
101
main.cpp
101
main.cpp
@ -7,17 +7,35 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#ifndef NO_MIME
|
#ifndef NO_MIME
|
||||||
#include <magic.h> // Did you remember to run ./configure?
|
#include "mime.h"
|
||||||
#include "archives.h" // Run make nomime
|
#include "archives.h" // Run make nomime
|
||||||
#endif
|
#endif
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <array>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
|
|
||||||
|
#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,
|
||||||
File, Folder,
|
File, Folder,
|
||||||
|
BLKFile, CHRFile,
|
||||||
|
FIFO, Symlink,
|
||||||
|
Socket,
|
||||||
#ifndef NO_MIME
|
#ifndef NO_MIME
|
||||||
Archive, Audio,
|
Archive, Audio,
|
||||||
Application,
|
Application,
|
||||||
@ -26,12 +44,12 @@ enum FileType {
|
|||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
std::vector<int> get_files_in_directory(const std::string& path)
|
std::array<int, 4> get_files_in_directory(const std::string& path)
|
||||||
{
|
{
|
||||||
DIR* dp = opendir(path.c_str());
|
DIR* dp = opendir(path.c_str());
|
||||||
// int normal, hidden;
|
// int normal, hidden;
|
||||||
int normal[2] = {0,0};
|
std::array<int, 2> normal = {0, 0};
|
||||||
int hidden[2] = {0,0};
|
std::array<int, 2> hidden = {0, 0};
|
||||||
if (dp != nullptr) {
|
if (dp != nullptr) {
|
||||||
struct dirent* ep;
|
struct dirent* ep;
|
||||||
while((ep = readdir(dp))) {
|
while((ep = readdir(dp))) {
|
||||||
@ -49,11 +67,11 @@ std::vector<int> get_files_in_directory(const std::string& path)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
perror("Couldn't open the directory");
|
perror("Couldn't open the directory");
|
||||||
std::vector<int> result;
|
std::array<int, 4> result;
|
||||||
result.insert(result.end(), normal[0]);
|
result[0] = normal[0];
|
||||||
result.insert(result.end(), normal[1]);
|
result[1] = normal[1];
|
||||||
result.insert(result.end(), hidden[0]);
|
result[2] = hidden[0];
|
||||||
result.insert(result.end(), hidden[1]);
|
result[3] = hidden[1];
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,6 +113,16 @@ std::string get_name_from_filetype(const enum FileType fs)
|
|||||||
return "File";
|
return "File";
|
||||||
case FileType::Folder:
|
case FileType::Folder:
|
||||||
return "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";
|
||||||
#ifndef NO_MIME
|
#ifndef NO_MIME
|
||||||
case FileType::Text:
|
case FileType::Text:
|
||||||
return "Text";
|
return "Text";
|
||||||
@ -105,8 +133,10 @@ std::string get_name_from_filetype(const enum FileType fs)
|
|||||||
return "Unknown";
|
return "Unknown";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string get_emoji_from_filetype(const enum FileType fs)
|
std::string get_emoji_from_filetype(const enum FileType fs)
|
||||||
{
|
{
|
||||||
|
#ifndef NO_EMOJIS
|
||||||
switch (fs) {
|
switch (fs) {
|
||||||
#ifndef NO_MIME
|
#ifndef NO_MIME
|
||||||
case FileType::Application:
|
case FileType::Application:
|
||||||
@ -122,6 +152,16 @@ std::string get_emoji_from_filetype(const enum FileType fs)
|
|||||||
return "📄";
|
return "📄";
|
||||||
case FileType::Folder:
|
case FileType::Folder:
|
||||||
return "📁";
|
return "📁";
|
||||||
|
case FileType::BLKFile:
|
||||||
|
return "🪄";
|
||||||
|
case FileType::CHRFile:
|
||||||
|
return "🖨️";
|
||||||
|
case FileType::FIFO:
|
||||||
|
return "🤝";
|
||||||
|
case FileType::Socket:
|
||||||
|
return "🔌";
|
||||||
|
case FileType::Symlink:
|
||||||
|
return "🔗";
|
||||||
#ifndef NO_MIME
|
#ifndef NO_MIME
|
||||||
case FileType::Text:
|
case FileType::Text:
|
||||||
return "📝";
|
return "📝";
|
||||||
@ -131,9 +171,14 @@ std::string get_emoji_from_filetype(const enum FileType fs)
|
|||||||
default:
|
default:
|
||||||
return "?";
|
return "?";
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
return "";
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string readable_fs(const long int size /*in bytes*/, const double divide_by = 1024, const std::string& suffix = "B")
|
std::string readable_fs(const long int size /*in bytes*/,
|
||||||
|
const double divide_by = 1024,
|
||||||
|
const std::string& suffix = "B")
|
||||||
{
|
{
|
||||||
if(size < 1024) {
|
if(size < 1024) {
|
||||||
char buffer[128];
|
char buffer[128];
|
||||||
@ -212,11 +257,25 @@ std::string basename(const std::string& filepath) {
|
|||||||
return filepath.substr(filepath.find_last_of("/\\") + 1);
|
return filepath.substr(filepath.find_last_of("/\\") + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FileType getFileType(mode_t st_mode) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
bool absolute = false;
|
bool absolute = false;
|
||||||
|
#ifndef NO_EMOJIS
|
||||||
bool emojis = true;
|
bool emojis = true;
|
||||||
// char* filename = "/home/pavlik/Desktop/twrp-3.7.0_9-0-starlte.img";
|
#else
|
||||||
|
bool emojis = false;
|
||||||
|
#endif
|
||||||
|
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
std::cerr << "Usage:\n\t" << argv[0] << " [args] filename ...\n";
|
std::cerr << "Usage:\n\t" << argv[0] << " [args] filename ...\n";
|
||||||
std::cerr << "Args:" << std::endl;
|
std::cerr << "Args:" << std::endl;
|
||||||
@ -252,7 +311,8 @@ int main(int argc, char *argv[])
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
// start
|
// start
|
||||||
int is_file = S_ISREG(file_stat.st_mode);
|
FileType file = getFileType(file_stat.st_mode);
|
||||||
|
bool is_file = file == FileType::File;
|
||||||
char temp[4097] = {'\0'};
|
char temp[4097] = {'\0'};
|
||||||
if (realpath(c_filename, temp) == nullptr) {
|
if (realpath(c_filename, temp) == nullptr) {
|
||||||
perror("realpath");
|
perror("realpath");
|
||||||
@ -261,14 +321,13 @@ int main(int argc, char *argv[])
|
|||||||
std::string full_name(temp); // = malloc(4097);
|
std::string full_name(temp); // = malloc(4097);
|
||||||
// delete[] temp;
|
// delete[] temp;
|
||||||
std::string name = basename(full_name);
|
std::string name = basename(full_name);
|
||||||
if(!is_file) name += "/";
|
if(file == FileType::Folder) name += "/";
|
||||||
#ifndef NO_MIME
|
#ifndef NO_MIME
|
||||||
std::string mime_type = get_mime(full_name);
|
std::string mime_type = get_mime(full_name);
|
||||||
#endif
|
//file = (is_file) ? FileType::File : FileType::Folder;
|
||||||
FileType file;
|
|
||||||
#ifdef NO_MIME
|
|
||||||
file = (is_file) ? FileType::File : FileType::Folder;
|
|
||||||
#else
|
#else
|
||||||
|
std::string mime_type = "application/octet-stream (no mime)";
|
||||||
|
#endif
|
||||||
if (starts_with("inode/directory", mime_type) || !is_file) {
|
if (starts_with("inode/directory", mime_type) || !is_file) {
|
||||||
file = FileType::Folder;
|
file = FileType::Folder;
|
||||||
} else if (is_archive(mime_type)) {
|
} else if (is_archive(mime_type)) {
|
||||||
@ -284,9 +343,9 @@ int main(int argc, char *argv[])
|
|||||||
} else if (starts_with("text", mime_type)) {
|
} else if (starts_with("text", mime_type)) {
|
||||||
file = FileType::Text;
|
file = FileType::Text;
|
||||||
} else {
|
} else {
|
||||||
file = FileType::Unknown;
|
file = FileType::Binary;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
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());
|
||||||
@ -302,11 +361,13 @@ int main(int argc, char *argv[])
|
|||||||
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("\tFile size: %s or %s (%li bytes)\n", _1024.c_str(), _1000.c_str(), file_stat.st_size);
|
||||||
else printf("\tFile size: %li bytes\n", file_stat.st_size);
|
else printf("\tFile size: %li bytes\n", file_stat.st_size);
|
||||||
} else {
|
} 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("\tContents:\n");
|
||||||
printf("\t\tNormal: %i files and %i folders.\n", insides[0], insides[1]);
|
printf("\t\tNormal: %i files and %i folders.\n", insides[0], insides[1]);
|
||||||
printf("\t\tHidden: %i files and %i folders.\n", insides[2], insides[3]);
|
printf("\t\tHidden: %i files and %i folders.\n", insides[2], insides[3]);
|
||||||
|
} else if (file == FileType::CHRFile) {
|
||||||
|
ListPartitions(full_name);
|
||||||
}
|
}
|
||||||
printf("\tPermissions: %s\n", print_permissions(file_stat.st_mode, !is_file).c_str());
|
printf("\tPermissions: %s\n", print_permissions(file_stat.st_mode, !is_file).c_str());
|
||||||
if (i != last-1) {
|
if (i != last-1) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user