Boost logo

Boost :

From: Walter Landry (wlandry_at_[hidden])
Date: 2003-09-18 05:16:18


Greetings,

I have attached patches for a few assorted improvements to the
filesystem library. These patches are only to the posix side of
things. I wouldn't know the first thing of what to do on Windows.

  1) copy_file preserves the permissions bits.

  2) Added a version of current_path that sets the current path.

  3) Added is_link and symlink.

I also implemented link_target as a separate function. I'm not so
sure that this is the best way to do it, and it should probably be
called readlink.

Ideally, it would be nice to be able to set the permissions of files
and directories, but I understand that is another can of worms.

If I've messed something up, please let me know.

Thanks,
Walter Landry
wlandry_at_[hidden]


--- /home/boo/{revisions}//wlandry_at_ucsd.edu--arx/boost/boost--arx/boost--arx--1.0/boost--arx--1.0--patch-11/./boost/filesystem/operations.hpp 2003-09-17 14:18:45.000000000 -0400
+++ /home/boo/arx/arx/src/boost/./boost/filesystem/operations.hpp 2003-09-08 14:19:07.000000000 -0400
@@ -41,6 +41,8 @@
 
     bool is_directory( const path & ph );
 
+ bool is_link(const path &ph);
+
     // VC++ 7.0 and earlier has a serious namespace bug that causes a clash
     // between boost::filesystem::is_empty and the unrelated type trait
     // boost::is_empty. The workaround for those who must use broken versions
@@ -62,10 +64,14 @@
     void rename( const path & from_path,
                  const path & to_path );
 
+ void symlink( const path & from_file_ph,
+ const path & to_file_ph );
+
     void copy_file( const path & from_file_ph,
                     const path & to_file_ph );
 
     path current_path();
+ void current_path( const path & new_path );
     const path & initial_path();
 
     path system_complete( const path & ph );


--- /home/boo/{revisions}//wlandry_at_ucsd.edu--arx/boost/boost--arx/boost--arx--1.0/boost--arx--1.0--patch-11/./libs/filesystem/src/operations_posix_windows.cpp 2003-09-17 14:18:46.000000000 -0400
+++ /home/boo/arx/arx/src/boost/./libs/filesystem/src/operations_posix_windows.cpp 2003-09-09 02:16:27.000000000 -0400
@@ -286,6 +286,23 @@
 # endif
     }
 
+ bool is_link( const path & ph)
+ {
+# ifdef BOOST_POSIX
+ struct stat path_stat;
+ if ( ::lstat( ph.native_file_string().c_str(), &path_stat ) != 0 )
+ boost::throw_exception( filesystem_error(
+ "boost::filesystem::is_link",
+ ph, fs::detail::system_error_code() ) );
+ return S_ISLNK( path_stat.st_mode );
+# else
+ boost::throw_exception( filesystem_error(
+ "boost::filesystem::is_link not implemented on non-posix",
+ ph, fs::detail::system_error_code() ) );
+# endif
+ }
+
+
     bool _is_empty( const path & ph )
     {
 # ifdef BOOST_POSIX
@@ -372,6 +389,18 @@
           old_path, new_path, fs::detail::system_error_code() ) );
     }
 
+ void symlink( const path & from_file_ph,
+ const path & to_file_ph )
+ {
+# ifdef BOOST_POSIX
+ if(::symlink(from_file_ph.native_file_string().c_str(),
+ to_file_ph.native_file_string().c_str()) != 0)
+# endif
+ boost::throw_exception( filesystem_error(
+ "boost::filesystem::symlink",
+ from_file_ph, to_file_ph, fs::detail::system_error_code() ) );
+ }
+
     void copy_file( const path & from_file_ph,
                     const path & to_file_ph )
     {
@@ -382,11 +411,14 @@
       boost::scoped_array<char> buf( new char [buf_sz] );
       int infile, outfile=0; // init quiets compiler warning
 
- if ( (infile = ::open( from_file_ph.string().c_str(),
- O_RDONLY )) < 0
- || (outfile = ::open( to_file_ph.string().c_str(),
- O_WRONLY | O_CREAT | O_EXCL,
- S_IRWXU|S_IRWXG|S_IRWXO )) < 0 )
+ struct stat from_stat;
+
+ if ( (::stat( from_file_ph.string().c_str(), &from_stat ) != 0)
+ || (infile = ::open( from_file_ph.string().c_str(),
+ O_RDONLY )) < 0
+ || (outfile = ::open( to_file_ph.string().c_str(),
+ O_WRONLY | O_CREAT | O_EXCL,
+ from_stat.st_mode)) < 0 )
       {
         if ( infile != 0 ) ::close( infile );
         boost::throw_exception( filesystem_error(
@@ -437,6 +469,16 @@
       return path( buf.get(), native );
     }
 
+ void current_path(const path &new_path)
+ {
+# ifdef BOOST_POSIX
+ if ( ::chdir(new_path.native_file_string().c_str()) != 0 )
+#endif
+ boost::throw_exception(
+ filesystem_error( "boost::filesystem::current_path", path(),
+ fs::detail::system_error_code() ) );
+ }
+
     const path & initial_path()
     {
       static path init_path;


/* Return the target that a symbolic link points to.

  Copyright (C) 2003 Walter Landry
  
  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; version 2 dated June, 1991.

  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.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA */

#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/exception.hpp"

using namespace std;
using namespace boost;
namespace fs=boost::filesystem;
using fs::path;

path link_target(const path &link)
{
  char buffer[PATH_MAX];
  int status=readlink(link.native_file_string().c_str(),buffer,PATH_MAX);

  if(status==-1 || status==PATH_MAX)
    {
      boost::throw_exception(fs::filesystem_error
                             ("link_target",link,
                              fs::detail::system_error_code()));
    }
  else
    {
      buffer[status]='\0';
    }
  return path(buffer);
}

    


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk