Symlink file type support.

This commit is contained in:
2026-10-31 18:14:31 -04:00
parent 05c2885d18
commit 52fe05e3e4
4 changed files with 33 additions and 9 deletions
+3 -2
View File
@@ -18,12 +18,13 @@ struct Entry
unsigned day; unsigned day;
long int hour; long int hour;
long int minute; long int minute;
bool is_dir; enum Type { Directory, Symlink, Normal };
Type type;
bool is_exe; bool is_exe;
}; };
bool is_exe( std::filesystem::directory_entry entry ); bool is_exe( std::filesystem::directory_entry entry );
std::string drwx( std::filesystem::directory_entry entry ); std::string drwx( std::filesystem::directory_entry entry, Entry::Type t );
std::string format_entry( const Entry &entry, std::size_t widest ); std::string format_entry( const Entry &entry, std::size_t widest );
+15 -4
View File
@@ -50,10 +50,16 @@ getEntries( const std::filesystem::path where )
} }
}; };
entries.push_back( Entry{ entry.path().filename(), drwx( entry ), entry.is_directory() ? 0 : get_file_size( entry.path() ), const auto type= Alepha::Utility::evaluate <=[&]
{
if( entry.is_symlink() ) return Entry::Symlink;
if( entry.is_directory() ) return Entry::Directory;
return Entry::Normal;
};
entries.push_back( Entry{ entry.path().filename(), drwx( entry, type ), type == Entry::Normal ? get_file_size( entry.path() ) : 0,
static_cast< int >( ymd.year() ), static_cast< unsigned >( ymd.month() ), static_cast< unsigned >( ymd.day() ), static_cast< int >( ymd.year() ), static_cast< unsigned >( ymd.month() ), static_cast< unsigned >( ymd.day() ),
time_of_day.hours().count(), time_of_day.minutes().count(), time_of_day.hours().count(), time_of_day.minutes().count(),
entry.is_directory(), is_exe( entry ) } ); type, is_exe( entry ) } );
} }
std::sort( begin( entries ), end( entries ), []( const auto &lhs, const auto &rhs ) { return lhs.name < rhs.name; } ); std::sort( begin( entries ), end( entries ), []( const auto &lhs, const auto &rhs ) { return lhs.name < rhs.name; } );
@@ -124,12 +130,17 @@ main( const int argcnt, const char *const argvec[] )
{ {
++fileCount; ++fileCount;
totalSize+= entry.size; totalSize+= entry.size;
std::cout << format_entry( entry, widest ) << " "; std::cout << format_entry( entry, widest ) << "";
if( entry.is_dir ) if( entry.type == Entry::Directory )
{ {
std::cout << ""; std::cout << "";
std::cout << std::setw( C::sizeWidth - 1 ) << std::setfill( ' ' ) << std::right << "<DIR>" << ' '; std::cout << std::setw( C::sizeWidth - 1 ) << std::setfill( ' ' ) << std::right << "<DIR>" << ' ';
} }
else if( entry.type == Entry::Symlink )
{
std::cout << "";
std::cout << std::setw( C::sizeWidth - 1 ) << std::setfill( ' ' ) << std::right << "<LINK>" << ' ';
}
else else
{ {
//std::cout << " "; //std::cout << " ";
+5 -1
View File
@@ -9,10 +9,14 @@ format_entry( const Entry &entry, const std::size_t widest )
std::ostringstream oss; std::ostringstream oss;
oss << "["; oss << "[";
if( entry.is_dir ) if( entry.type == Entry::Directory )
{ {
oss << "1;35;95"; oss << "1;35;95";
} }
else if( entry.type == Entry::Symlink )
{
oss << "1;32;92";
}
else if( entry.is_exe ) else if( entry.is_exe )
{ {
oss << "1;36;96"; oss << "1;36;96";
+10 -2
View File
@@ -3,6 +3,7 @@
#include <sstream> #include <sstream>
#include <algorithm> #include <algorithm>
#include <filesystem> #include <filesystem>
#include <map>
bool bool
is_exe( const std::filesystem::directory_entry entry ) is_exe( const std::filesystem::directory_entry entry )
@@ -13,8 +14,15 @@ is_exe( const std::filesystem::directory_entry entry )
return ( perms::none != ( p & ( perms::owner_exec | perms::group_exec | perms::others_exec ) ) ); return ( perms::none != ( p & ( perms::owner_exec | perms::group_exec | perms::others_exec ) ) );
} }
static const std::map< Entry::Type, char > typeMap=
{
{ Entry::Normal, '-' },
{ Entry::Directory, 'd' },
{ Entry::Symlink, 'l' },
};
std::string std::string
drwx( const std::filesystem::directory_entry entry ) drwx( const std::filesystem::directory_entry entry, const Entry::Type type )
{ {
auto p= status( entry.path() ).permissions(); auto p= status( entry.path() ).permissions();
@@ -24,7 +32,7 @@ drwx( const std::filesystem::directory_entry entry )
{ {
oss << (perms::none == (perm bitand p) ? '-' : op); oss << (perms::none == (perm bitand p) ? '-' : op);
}; };
oss << ( entry.is_directory() ? 'd' : '-' ); oss << typeMap.at( type );
show('r', perms::owner_read); show('r', perms::owner_read);
show('w', perms::owner_write); show('w', perms::owner_write);
show('x', perms::owner_exec); show('x', perms::owner_exec);