2025-02-24 05:59:17 +01:00
|
|
|
/* Zutils - Utilities dealing with compressed files
|
|
|
|
Copyright (C) 2009-2020 Antonio Diaz Diaz.
|
2025-02-24 05:45:53 +01:00
|
|
|
|
2025-02-24 05:59:17 +01:00
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
2025-02-24 05:45:53 +01:00
|
|
|
|
2025-02-24 05:59:17 +01:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2025-02-24 05:45:53 +01:00
|
|
|
|
2025-02-24 05:59:17 +01:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2025-02-24 05:45:53 +01:00
|
|
|
*/
|
|
|
|
|
2025-02-24 05:59:17 +01:00
|
|
|
/* Returns true if full_name is a regular file with an enabled extension
|
|
|
|
or (a link to) a directory. */
|
2025-02-24 05:57:59 +01:00
|
|
|
bool test_full_name( const std::string & full_name, const struct stat * stp,
|
|
|
|
const bool follow )
|
|
|
|
{
|
|
|
|
struct stat st, st2;
|
|
|
|
if( follow && stat( full_name.c_str(), &st ) != 0 ) return false;
|
|
|
|
if( !follow && lstat( full_name.c_str(), &st ) != 0 ) return false;
|
|
|
|
if( S_ISREG( st.st_mode ) ) // regular file
|
|
|
|
return enabled_format( extension_format( extension_index( full_name ) ) );
|
|
|
|
if( !S_ISDIR( st.st_mode ) ) return false;
|
|
|
|
|
|
|
|
std::string prev_dir( full_name );
|
|
|
|
bool loop = ( stp && st.st_ino == stp->st_ino && st.st_dev == stp->st_dev );
|
|
|
|
if( !loop )
|
|
|
|
for( unsigned i = prev_dir.size(); i > 1; )
|
|
|
|
{
|
|
|
|
while( i > 0 && prev_dir[i-1] != '/' ) --i;
|
|
|
|
if( i == 0 ) break;
|
|
|
|
if( i > 1 ) --i; // remove trailing slash except at root dir
|
|
|
|
prev_dir.resize( i );
|
|
|
|
if( stat( prev_dir.c_str(), &st2 ) != 0 || !S_ISDIR( st2.st_mode ) ||
|
|
|
|
( st.st_ino == st2.st_ino && st.st_dev == st2.st_dev ) )
|
|
|
|
{ loop = true; break; }
|
|
|
|
}
|
|
|
|
if( loop ) // full_name already visited or above tree
|
2025-02-24 05:59:17 +01:00
|
|
|
show_file_error( full_name.c_str(), "warning: Recursive directory loop." );
|
2025-02-24 05:57:59 +01:00
|
|
|
return !loop; // (link to) directory
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-02-24 05:59:17 +01:00
|
|
|
/* Returns in input_filename the next filename, or "." for stdin.
|
|
|
|
("." was chosen because it is not a valid filename).
|
|
|
|
Sets 'error' to true if a directory fails to open. */
|
2025-02-24 05:45:53 +01:00
|
|
|
bool next_filename( std::list< std::string > & filenames,
|
|
|
|
std::string & input_filename, bool & error,
|
2025-02-24 05:57:59 +01:00
|
|
|
const int recursive, const bool ignore_stdin = false,
|
2025-02-24 05:45:53 +01:00
|
|
|
const bool no_messages = false )
|
|
|
|
{
|
|
|
|
while( !filenames.empty() )
|
|
|
|
{
|
|
|
|
input_filename = filenames.front();
|
|
|
|
filenames.pop_front();
|
2025-02-24 05:59:17 +01:00
|
|
|
if( input_filename == "-" )
|
2025-02-24 05:45:53 +01:00
|
|
|
{
|
|
|
|
if( ignore_stdin ) continue;
|
2025-02-24 05:59:17 +01:00
|
|
|
input_filename = "."; return true;
|
2025-02-24 05:45:53 +01:00
|
|
|
}
|
2025-02-24 05:57:59 +01:00
|
|
|
struct stat st;
|
|
|
|
if( stat( input_filename.c_str(), &st ) == 0 && S_ISDIR( st.st_mode ) )
|
2025-02-24 05:45:53 +01:00
|
|
|
{
|
2025-02-24 05:57:59 +01:00
|
|
|
if( recursive )
|
2025-02-24 05:45:53 +01:00
|
|
|
{
|
|
|
|
DIR * const dirp = opendir( input_filename.c_str() );
|
|
|
|
if( !dirp )
|
|
|
|
{
|
|
|
|
if( !no_messages )
|
2025-02-24 05:57:59 +01:00
|
|
|
show_file_error( input_filename.c_str(), "Can't open directory", errno );
|
2025-02-24 05:45:53 +01:00
|
|
|
error = true; continue;
|
|
|
|
}
|
2025-02-24 05:57:59 +01:00
|
|
|
for( unsigned i = input_filename.size();
|
|
|
|
i > 1 && input_filename[i-1] == '/'; --i )
|
|
|
|
input_filename.resize( i - 1 ); // remove trailing slashes
|
|
|
|
struct stat stdot, *stdotp = 0;
|
|
|
|
if( input_filename[0] != '/' ) // relative path
|
|
|
|
{
|
|
|
|
if( input_filename == "." ) input_filename.clear();
|
|
|
|
if( stat( ".", &stdot ) == 0 && S_ISDIR( stdot.st_mode ) )
|
|
|
|
stdotp = &stdot;
|
|
|
|
}
|
|
|
|
if( input_filename.size() && input_filename != "/" )
|
|
|
|
input_filename += '/';
|
2025-02-24 05:45:53 +01:00
|
|
|
std::list< std::string > tmp_list;
|
|
|
|
while( true )
|
|
|
|
{
|
|
|
|
const struct dirent * const entryp = readdir( dirp );
|
|
|
|
if( !entryp ) { closedir( dirp ); break; }
|
2025-02-24 05:51:09 +01:00
|
|
|
const std::string tmp_name( entryp->d_name );
|
|
|
|
if( tmp_name == "." || tmp_name == ".." ) continue;
|
2025-02-24 05:57:59 +01:00
|
|
|
const std::string full_name( input_filename + tmp_name );
|
|
|
|
if( test_full_name( full_name, stdotp, recursive == 2 ) )
|
2025-02-24 05:51:09 +01:00
|
|
|
tmp_list.push_back( full_name );
|
2025-02-24 05:45:53 +01:00
|
|
|
}
|
|
|
|
filenames.splice( filenames.begin(), tmp_list );
|
|
|
|
}
|
2025-02-24 05:57:59 +01:00
|
|
|
continue;
|
2025-02-24 05:45:53 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
input_filename.clear();
|
|
|
|
return false;
|
|
|
|
}
|