libtrashcan is a cross-platform C library for moving files and directories to the trashcan.
Note: The library is no longer being actively developed.
- Linux
- FreeBSD, OpenBSD, NetBSD
- Windows Vista, Windows Server 2008 and later versions
- macOS
When you are developing a software in C that allows to view, edit and save files, you may also want to provide the functionality to delete files. Implementing file deletion with the function specified by POSIX allows cross-platform development. However, remove()
and rmdir()
happen to permanently delete files and directories. If your software is only supposed to move the files to the system's trashcan / recycling bin, then libtrashcan can simplify the cross-platform development, as it implements the soft delete functionality for you.
For Linux and *BSD the library partially implements the FreeDesktop.org trash specification v1.0. On Windows it uses the IFileOperation
interface and also handles COM initialization. The NSFileManager
is utilized on macOS.
The function int trashcan_soft_delete(const char *path)
is provided on all platforms. It takes a path to a file or directory, tries to move it to the trashcan and returns a status code. Additional platform dependent functions with different signatures are provided, e.g. to control COM initialization on Windows. The complete API is documented in the trashcan.h file. An example application that uses libtrashcan is provided with example.c.
In order to use libtrashcan you need to include trashcan.h
in your source code, build and link the library. An example project is provided that demonstrates this with CMake. Note that on macOS it is required to link the Core Foundation and Cocoa framework.
The example project is a CLI application that takes a path to a file or directory as an argument and tries to move it to the trash. It can be build by executing the following commands on your terminal/shell/command line if you have CMake and a compiler is installed:
mkdir build
cd ./build
cmake ..
cmake --build . --config Release
The project is distributed under the MIT license.
- Change of API: All function are prefixed with
trashcan
to avoid name collisions - Contribution by Mark Wagner (Carnildo): Add
extern "C"
to permit use in C++ - Bug fix for issue #10: Reference count is now decremented for the
IShellItem *pSI
- Bug fix for issue #12: Reference count for the
IFileOperation *pfo
is only decremented whenCoCreateInstance
is successful - Bug fix: Correct path construction when
HOME
orXDG_DATA_HOME
is set to the root directory
- Support for macOS
- Correct multibyte to wide character conversion on Windows, supports UTF-8 encoded strings
- API gives access to the "core" functions on Windows that allow to use
wchar_t*
directly
- Initial version