$include_dir="/home/hyper-archives/boost-commit/include"; include("$include_dir/msg-header.inc") ?>
Subject: [Boost-commit] svn:boost r56934 - in sandbox/filesystem-v3: boost/filesystem libs/filesystem/doc libs/filesystem/src libs/filesystem/test
From: bdawes_at_[hidden]
Date: 2009-10-16 17:08:41
Author: bemandawes
Date: 2009-10-16 17:08:40 EDT (Fri, 16 Oct 2009)
New Revision: 56934
URL: http://svn.boost.org/trac/boost/changeset/56934
Log:
Add file_size() function to set size of regular files.
Text files modified: 
   sandbox/filesystem-v3/boost/filesystem/operations.hpp          |     7 +++                                     
   sandbox/filesystem-v3/libs/filesystem/doc/reference.html       |    12 ++++++                                  
   sandbox/filesystem-v3/libs/filesystem/src/operations.cpp       |    65 +++++++++++++++------------------------ 
   sandbox/filesystem-v3/libs/filesystem/test/operations_test.cpp |    26 ++++++++++++++++                        
   4 files changed, 68 insertions(+), 42 deletions(-)
Modified: sandbox/filesystem-v3/boost/filesystem/operations.hpp
==============================================================================
--- sandbox/filesystem-v3/boost/filesystem/operations.hpp	(original)
+++ sandbox/filesystem-v3/boost/filesystem/operations.hpp	2009-10-16 17:08:40 EDT (Fri, 16 Oct 2009)
@@ -239,6 +239,9 @@
                    system::error_code & ec = throws() );
 
   BOOST_FILESYSTEM_DECL
+  void file_size( const path & p, uintmax_t size, system::error_code & ec = throws() );
+
+  BOOST_FILESYSTEM_DECL
   boost::uintmax_t hard_link_count( const path & p, system::error_code & ec = throws() );
 
   //  initial_path() declaration precedes complete()
@@ -255,12 +258,14 @@
   path read_symlink( const path & p, system::error_code & ec = throws() );
 
   BOOST_FILESYSTEM_DECL
+    // For standardization, if the committee doesn't like "remove", consider "eliminate"
   bool remove( const path & p, system::error_code & ec = throws() );
 
   BOOST_FILESYSTEM_DECL
   boost::uintmax_t remove_all( const path & p,
                    system::error_code & ec = throws() );
 
+  
   BOOST_FILESYSTEM_DECL
   void rename( const path & from, const path & to,
                    system::error_code & ec = throws() );
@@ -274,7 +279,7 @@
 # endif
 
   BOOST_FILESYSTEM_DECL
-  path system_complete( const path & p, system::error_code & ec = throws() ); 
+  path system_complete( const path & p, system::error_code & ec = throws() );
 
 //--------------------------------------------------------------------------------------//
 //                                                                                      //
Modified: sandbox/filesystem-v3/libs/filesystem/doc/reference.html
==============================================================================
--- sandbox/filesystem-v3/libs/filesystem/doc/reference.html	(original)
+++ sandbox/filesystem-v3/libs/filesystem/doc/reference.html	2009-10-16 17:08:40 EDT (Fri, 16 Oct 2009)
@@ -2150,6 +2150,16 @@
   <p style="font-size: 10pt"><i>Throws:</i> <code>filesystem_error</code><span style="background-color: #FFFFFF">
   </span>if <code>!exists(p) || !is_regular_file(p)</code>.</p>
 </blockquote>
+<pre>void <a name="file_size">file_size</a>(const path& p, <span style="background-color: #FFFFFF; ">uintmax_t new_size, </span>system::error_code& ec=throws());</pre>
+<blockquote>
+<p><i>Postcondition:</i> <code>file_size() == new_size</code>.</p>
+  <p style="font-size: 10pt"><i>Throws:</i> <code>filesystem_error</code><span style="background-color: #FFFFFF">
+  </span>if <code>!exists(p) || !is_regular_file(p)</code>.</p>
+  <p style="font-size: 10pt"><i>Remarks:</i> Achieves its postcondition as if by 
+  POSIX <code>
+  <a href="http://www.opengroup.org/onlinepubs/000095399/functions/truncate.html">
+  truncate()</a></code>.</p>
+</blockquote>
 <pre>const path& <a name="initial_path">initial_path</a>(system::error_code& ec=throws());</pre>
 <blockquote>
   <p><i>Returns:</i> <code>current_path()</code> at the time of entry to <code>
@@ -3140,7 +3150,7 @@
 <p>Distributed under the Boost Software License, Version 1.0. See
 <a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a></p>
 <p>Revised
-<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B %Y" startspan -->10 October 2009<!--webbot bot="Timestamp" endspan i-checksum="32658" --></p>
+<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B %Y" startspan -->16 October 2009<!--webbot bot="Timestamp" endspan i-checksum="32670" --></p>
 
 </body>
 
Modified: sandbox/filesystem-v3/libs/filesystem/src/operations.cpp
==============================================================================
--- sandbox/filesystem-v3/libs/filesystem/src/operations.cpp	(original)
+++ sandbox/filesystem-v3/libs/filesystem/src/operations.cpp	2009-10-16 17:08:40 EDT (Fri, 16 Oct 2009)
@@ -125,6 +125,7 @@
 
 # if defined(BOOST_POSIX_API)
 
+//  POSIX uses a 0 return to indicate success
 #   define BOOST_ERRNO    errno 
 #   define BOOST_SET_CURRENT_DIRECTORY(P) (::chdir(P) == 0)
 #   define BOOST_CREATE_DIRECTORY(P) (::mkdir( P, S_IRWXU|S_IRWXG|S_IRWXO ) == 0)
@@ -136,12 +137,14 @@
          || ::mkdir( to.c_str(),from_stat.st_mode ) != 0))
 #   define BOOST_COPY_FILE(F,T,FailIfExistsBool) copy_file_api(F, T, FailIfExistsBool)
 #   define BOOST_MOVE_FILE(F,T) (::rename(F, T) == 0)
+#   define BOOST_SET_FILE_SIZE(P,SZ) (::truncate( P, SZ ) == 0)
 
 #   define BOOST_ERROR_NOT_SUPPORTED ENOSYS
 #   define BOOST_ERROR_ALREADY_EXISTS EEXIST
 
 # else  // BOOST_WINDOWS_API
 
+//  Windows uses a non-0 return to indicate success
 #   define BOOST_ERRNO    ::GetLastError() 
 #   define BOOST_SET_CURRENT_DIRECTORY(P) (::SetCurrentDirectoryW(P) != 0)
 #   define BOOST_CREATE_DIRECTORY(P) (::CreateDirectoryW( P, 0 ) != 0)
@@ -152,6 +155,7 @@
 #   define BOOST_COPY_DIRECTORY(F,T) (::CreateDirectoryExW( F, T, 0 ) != 0)
 #   define BOOST_COPY_FILE(F,T,FailIfExistsBool) (::CopyFileW(F, T, FailIfExistsBool) != 0)
 #   define BOOST_MOVE_FILE(F,T) (::MoveFileW( F, T ) != 0)
+#   define BOOST_SET_FILE_SIZE(P,SZ) (file_size_file_api( P, SZ ) != 0)
 #   define BOOST_READ_SYMLINK(P,T)
 
 #   define BOOST_ERROR_ALREADY_EXISTS ERROR_ALREADY_EXISTS
@@ -203,7 +207,7 @@
     else  
     { //  error
       if ( &ec == &throws() )
-        throw/*_exception*/( filesystem_error( message,
+        throw_exception( filesystem_error( message,
           p, BOOST_ERRNO, system_category ) );
       else
         ec.assign( BOOST_ERRNO, system_category );
@@ -390,6 +394,18 @@
       ::GetFullPathNameW( src.c_str(), static_cast<DWORD>(len), buf, p ));
   }
 
+  BOOL file_size_file_api( const wchar_t * p, boost::uintmax_t size )
+  {
+    HANDLE handle = CreateFileW( p, GENERIC_WRITE, 0, 0, OPEN_EXISTING,
+                                FILE_ATTRIBUTE_NORMAL, 0 );
+    LARGE_INTEGER sz;
+    sz.QuadPart = size;
+    return handle != INVALID_HANDLE_VALUE
+      && ::SetFilePointerEx( handle, sz, 0, FILE_BEGIN )
+      && ::SetEndOfFile( handle )
+      && ::CloseHandle( handle );
+  }
+
   //  Windows kernel32.dll functions that may or may not be present
   //  must be accessed through pointers
 
@@ -413,6 +429,8 @@
     ::GetProcAddress(
       ::GetModuleHandle(TEXT("kernel32.dll")), "CreateSymbolicLinkW") );
 
+
+
 //--------------------------------------------------------------------------------------//
 //                                                                                      //
 //                            POSIX-specific helpers                                    //
@@ -917,6 +935,12 @@
   }
 
   BOOST_FILESYSTEM_DECL
+  void file_size( const path & p, uintmax_t size, system::error_code & ec )
+  {
+    error( !BOOST_SET_FILE_SIZE( p.c_str(), size ), p, ec, "boost::filesystem::file_size");
+  }
+
+  BOOST_FILESYSTEM_DECL
   boost::uintmax_t hard_link_count( const path & p, system::error_code & ec )
   {
 #   ifdef BOOST_WINDOWS_API
@@ -1363,45 +1387,6 @@
 #   endif
   }
 
-//
-//#   else // BOOST_POSIX_API
-//
-//
-//
-//      // suggested by Walter Landry
-//      BOOST_FILESYSTEM_DECL bool
-//      symbolic_link_exists_api( const std::string & ph )
-//      {
-//        struct stat path_stat;
-//        return ::lstat( ph.c_str(), &path_stat ) == 0
-//          && S_ISLNK( path_stat.st_mode );
-//      }
-//
-//
-//      BOOST_FILESYSTEM_DECL error_code
-//      remove_api( const std::string & ph )
-//      {
-//        if ( posix_remove( ph.c_str() ) == 0 )
-//          return ok;
-//        int error = errno;
-//        // POSIX says "If the directory is not an empty directory, rmdir()
-//        // shall fail and set errno to EEXIST or ENOTEMPTY."
-//        // Linux uses ENOTEMPTY, Solaris uses EEXIST.
-//        if ( error == EEXIST ) error = ENOTEMPTY;
-//
-//        error_code ec;
-//
-//        // ignore errors if post-condition satisfied
-//        return status_api(ph, ec).type() == file_not_found
-//          ? ok : error_code( error, system_category ) ;
-//      }
-//
-//
-//
-//
-//#   endif
-//    } // namespace detail
-
 //--------------------------------------------------------------------------------------//
 //                                                                                      //
 //                                 directory_entry                                      //
Modified: sandbox/filesystem-v3/libs/filesystem/test/operations_test.cpp
==============================================================================
--- sandbox/filesystem-v3/libs/filesystem/test/operations_test.cpp	(original)
+++ sandbox/filesystem-v3/libs/filesystem/test/operations_test.cpp	2009-10-16 17:08:40 EDT (Fri, 16 Oct 2009)
@@ -731,6 +731,31 @@
     BOOST_TEST( fs::is_directory( p ) );
   }
 
+  //  file_size_tests  ----------------------------------------------------//
+
+  void file_size_tests()
+  {
+    std::cout << "file_size_tests..." << std::endl;
+
+    const std::string f ("file_size_test.txt");
+
+    fs::remove( f );
+    create_file( f, "1234567890" );
+
+    BOOST_TEST( fs::exists(f) );
+    BOOST_TEST_EQ( fs::file_size(f), 10 );
+    fs::file_size( f, 5 );
+    BOOST_TEST( fs::exists(f) );
+    BOOST_TEST_EQ( fs::file_size(f), 5 );
+    fs::file_size( f, 15 );
+    BOOST_TEST( fs::exists(f) );
+    BOOST_TEST_EQ( fs::file_size(f), 15 );
+
+    error_code ec;
+    fs::file_size( "no such file", 15, ec );
+    BOOST_TEST( ec );
+  }
+
   //  _tests  ----------------------------------------------------//
 
   void _tests()
@@ -949,6 +974,7 @@
   
   create_hard_link_tests();
   create_symlink_tests();
+  file_size_tests();
 
   // copy_file() tests
   std::cout << "begin copy_file test..." << std::endl;