| Filesystem Library Version 3

Filesystem Home Releases Reference Tutorial FAQ Portability V3 Intro V3 Design Deprecated

Contents
Introduction Documentation Using the library Coding guidelines Cautions Headers Example programs Implementation Macros Building the object-library Notes for Cygwin users Version history with acknowledgements

This is Version 3 of the Filesystem library. Version 2 is not longer supported. 1.49.0 was the last release of Boost to supply Version 2

## Introduction

The Boost.Filesystem library provides facilities to manipulate files and directories, and the paths that identify them.

The features of the library include:

- A modern C++ interface, highly compatible with the C++ standard library.

> > Many users say the interface is their primary motivation for using Boost.Filesystem. They like its use of familiar idioms based on standard library containers, iterators, and algorithms. They like having errors reported by throwing exceptions.

- Portability between operating systems.
  - At the C++ syntax level, it is convenient to learn and use one interface regardless of the operating system.
  - At the semantic level, behavior of code is reasonably portable across operating systems.
  - Dual generic or native path format support encourages program portability, yet still allows communication with users in system specific formats.
- Error handling and reporting via C++ exceptions (the default) or error codes.
  - C++ exceptions are the preferred error reporting mechanism for most applications. The exception thrown includes the detailed error code information important for diagnosing the exact cause of file system errors.
  - Error reporting via error code allows user code that provides detailed error recovery to avoid becoming so littered with try-catch blocks as to be unmaintainable.
- Suitable for a broad spectrum of applications, ranging from simple script-like operations to extremely complex production code.
  - At the simple script-like end of the spectrum, the intent is not to compete with Python, Perl, or shell languages, but rather to provide filesystem operations when C++ is already the language of choice.
  - Finer grained control over operations and error handling is available to support more complex applications or other cases where throwing exceptions isn't desired.

A proposal, N1975, to include Boost.Filesystem in Technical Report 2 has been accepted by the C++ Standards Committee. That proposal was based on version 2 of Boost.Filesystem; presumably the final TR2 form will be based on version 3.

## Documentation

Tutorial - A gentle introduction to the library, with example programs provided for you to experiment with.

Reference - Formal documentation in the style of the C++ standard for every component of the library.

FAQ - Frequently asked questions.

Portability Guide - Help for those concerned with writing code to run on multiple operating systems.

Deprecated Features - Identifies deprecated features and their replacements.

Version 3 Introduction - Aimed at users of prior Boost.Filesystem versions.

Version 3 Design - Historical document from the start of the Version 3 design process.

Original Design - Historical document from the start of the Version 1 design process.

Do List - Boost.Filesystem development work in the pipeline.

## Using the library

Boost.Filesystem is implemented as a separately compiled library, so you must install binaries in a location that can be found by your linker. If you followed the Boost Getting Started instructions, that's already been done for you.

## Coding guidelines

For new code, defining `BOOST_FILESYSTEM_NO_DEPRECATED` before including filesystem headers is strongly recommended. This prevents inadvertent use of old features, particularly legacy function names, that have been replaced and are going to go away in the future.

## Cautions

After reading the tutorial you can dive right into simple, script-like programs using the Filesystem Library! Before doing any serious work, however, there a few cautions to be aware of:

#### Effects and Postconditions not guaranteed in the presence of race-conditions

Filesystem function specifications follow the C++ Standard Library form, specifying behavior in terms of effects and postconditions. If a race-condition exists, a function's postconditions may no longer be true by the time the function returns to the caller.

> Explanation: The state of files and directories is often globally shared, and thus may be changed unexpectedly by other threads, processes, or even other computers having network access to the filesystem. As an example of the difficulties this can cause, note that the following asserts may fail:

> > `assert( exists( "foo" ) == exists( "foo" ) ); // (1)
> > remove_all( "foo" );
> > assert( !exists( "foo" ) ); // (2)
> > assert( is_directory( "foo" ) == is_directory( "foo" ) ); // (3)`

> (1) will fail if a non-existent "foo" comes into existence, or an existent "foo" is removed, between the first and second call to exists(). This could happen if, during the execution of the example code, another thread, process, or computer is also performing operations in the same directory.

> (2) will fail if between the call to remove_all() and the call to exists() a new file or directory named "foo" is created by another thread, process, or computer.

> (3) will fail if another thread, process, or computer removes an existing file "foo" and then creates a directory named "foo", between the example code's two calls to is_directory().

#### May throw exceptions

Unless otherwise specified, Boost.Filesystem functions throw basic_filesystem_error exceptions if they cannot successfully complete their operational specifications. Also, implementations may use C++ Standard Library functions, which may throw std::bad_alloc. These exceptions may be thrown even though the error condition leading to the exception is not explicitly specified in the function's "Throws" paragraph.

All exceptions thrown by the Filesystem Library are implemented by calling boost::throw_exception(). Thus exact behavior may differ depending on BOOST_NO_EXCEPTIONS at the time the filesystem source files are compiled.

Non-throwing versions are provided of several functions that are often used in contexts where error codes may be the preferred way to report an error.

## Headers

The Boost.Filesystem library provides several headers:

- Header <boost/filesystem.hpp> provides access to all features of the library, except file streams.
- Header <boost/filesystem/fstream.hpp> inherits the same components as the C++ Standard Library's fstream header, but files are identified by `const path&` arguments rather that `const char*` arguments.

## Example programs

See the tutorial for example programs.

### Other examples

The programs used to generate the Boost regression test status tables use the Filesystem Library extensively. See:

- process_jam_log.cpp
- compiler_status.cpp

## Implementation

The current implementation supports operating systems which provide the POSIX or Windows API's.

The library is in regular use on Apple OS X, HP-UX, IBM AIX, Linux, Microsoft Windows, SGI IRIX, and Sun Solaris operating systems using a variety of compilers.

## Macros

Users may defined the following macros if desired. Sensible defaults are provided, so users can ignore these macros unless they have special needs.

Macro Name | Default | Effect if defined
`BOOST_FILESYSTEM_NO_DEPRECATED` | Not defined. | Deprecated features are excluded from headers.
`BOOST_FILESYSTEM_DYN_LINK` | Defined if `BOOST_ALL_DYN_LINK` is defined, otherwise not defined. | The Boost.Filesystem library is dynamically linked. If not defined, static linking is assumed.
`BOOST_FILESYSTEM_NO_LIB` | Defined if `BOOST_ALL_NO_LIB` is defined, otherwise not defined. | Boost.Filesystem library does not use the Boost auto-link facility.

User-defined BOOST_POSIX_API and BOOST_WINDOWS_API macros are no longer supported.

## Building the object-library

The object-library will be built automatically if you are using the Boost build system. See Getting Started. It can also be built manually using a Jamfile supplied in directory libs/filesystem/build, or the user can construct an IDE project or make file which includes the object-library source files.

The object-library source files are supplied in directory libs/filesystem/src. These source files implement the library for POSIX or Windows compatible operating systems; no implementation is supplied for other operating systems. Note that many operating systems not normally thought of as POSIX systems, such as mainframe legacy operating systems or embedded operating systems, support POSIX compatible file systems and so will work with the Filesystem Library.

The object-library can be built for static or dynamic (shared/dll) linking. This is controlled by the BOOST_ALL_DYN_LINK or BOOST_FILESYSTEM_DYN_LINK macros. See the Separate Compilation page for a description of the techniques used.

### Note for Cygwin users

Cygwin version 1.7 or later is required because only versions of GCC with wide character strings are supported.

The library's implementation code treats Cygwin as a Windows platform, and thus uses the Windows API and uses Windows path syntax as the native path syntax.

## Version history

### Version 3

Boost 1.??.0 - ???, 2010 - Internationalization via single class `path`. More uniform error handling.

Peter Dimov suggested use of a single path class rather than a `basic_path` class template. That idea was the basis for the Version 3 redesign.

Thanks for comments from Robert Stewart, Zach Laine, Peter Dimov, Gregory Peele, Scott McMurray, John Bytheway, Jeff Flinn, Jeffery Bosboom.

### Version 2

Boost 1.34.0 - May, 2007 - Internationalization via `basic_path` template.

So many people have contributed comments and bug reports that it isn't any longer possible to acknowledge them individually. That said, Peter Dimov and Rob Stewart need to be specially thanked for their many constructive criticisms and suggestions. Terence Wilson and Chris Frey contributed timing programs which helped illuminate performance issues.

### Version 1

Boost 1.30.0 - March, 2003 - Initial official Boost release.

The Filesystem Library was designed and implemented by Beman Dawes. The original directory_iterator and filesystem_error classes were based on prior work from Dietmar Kuehl, as modified by Jan Langer. Thomas Witt was a particular help in later stages of initial development. Peter Dimov and Rob Stewart made many useful suggestions and comments over a long period of time. Howard Hinnant helped with internationalization issues.

Key design requirements and design realities were developed during extensive discussions on the Boost mailing list, followed by comments on the initial implementation. Numerous helpful comments were then received during the Formal Review.

Participants included Aaron Brashears, Alan Bellingham, Aleksey Gurtovoy, Alex Rosenberg, Alisdair Meredith, Andy Glew, Anthony Williams, Baptiste Lepilleur, Beman Dawes, Bill Kempf, Bill Seymour, Carl Daniel, Chris Little, Chuck Allison, Craig Henderson, Dan Nuffer, Dan'l Miller, Daniel Frey, Darin Adler, David Abrahams, David Held, Davlet Panech, Dietmar Kuehl, Douglas Gregor, Dylan Nicholson, Ed Brey, Eric Jensen, Eric Woodruff, Fedder Skovgaard, Gary Powell, Gennaro Prota, Geoff Leyland, George Heintzelman, Giovanni Bajo, Glen Knowles, Hillel Sims, Howard Hinnant, Jaap Suter, James Dennett, Jan Langer, Jani Kajala, Jason Stewart, Jeff Garland, Jens Maurer, Jesse Jones, Jim Hyslop, Joel de Guzman, Joel Young, John Levon, John Maddock, John Williston, Jonathan Caves, Jonathan Biggar, Jurko, Justus Schwartz, Keith Burton, Ken Hagen, Kostya Altukhov, Mark Rodgers, Martin Schuerch, Matt Austern, Matthias Troyer, Mattias Flodin, Michiel Salters, Mickael Pointier, Misha Bergal, Neal Becker, Noel Yap, Parksie, Patrick Hartling, Pavel Vozenilek, Pete Becker, Peter Dimov, Rainer Deyke, Rene Rivera, Rob Lievaart, Rob Stewart, Ron Garcia, Ross Smith, Sashan, Steve Robbins, Thomas Witt, Tom Harris, Toon Knapen, Victor Wagner, Vincent Finn, Vladimir Prus, and Yitzhak Sapir

A lengthy discussion on the C++ committee's library reflector illuminated the "illusion of portability" problem, particularly in postings by PJ Plauger and Pete Becker.

Walter Landry provided much help illuminating symbolic link use cases for version 1.31.0.

Revised 20 March, 2012

© Copyright Beman Dawes, 2002-2005

Use, modification, and distribution are subject to the Boost Software License, Version 1.0. See www.boost.org/LICENSE_1_0.txt