Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Foundation/IO/FileSystem/Filesystem.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_IO_FileSystem_FileSystem_h_
5#define _Stroika_Foundation_IO_FileSystem_FileSystem_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <filesystem>
10
12#include "Stroika/Foundation/Common/Common.h"
13#include "Stroika/Foundation/Containers/Sequence.h"
14#include "Stroika/Foundation/IO/AccessMode.h"
15#include "Stroika/Foundation/IO/FileSystem/Common.h"
17
18/**
19 * \file
20 *
21 * \note Code-Status: <a href="Code-Status.md#Alpha">Alpha</a>
22 *
23 * TODO:
24 *
25 * @todo http://stroika-bugs.sophists.com/browse/STK-685 -
26 * MAJOR CHANGES required to accomodate std::filesystem
27 * - maybe losing this alltogether, or at least big changes
28 * (like using path class instead of String) - force use of ToPath ()
29 *
30 * @todo http://stroika-bugs.sophists.com/browse/STK-612
31 * Probably rename class IO::FileSystem::FileSystem to IO::FileSystem::Ptr (or FileSystemPtr)
32 *
33 * OLD TODOS this subsumes:
34 * > Terrible name - class FileSystem inside namesapce FileSystem!
35 *
36 * > Bad names. Unwise to have CLASS (singlton mgr) as same name as namespace. Not sure how to fix however.
37 *
38 * > Add virtual interface and subdir for each concrete one. Tons more code to be factored in here.
39 * (leave open but this woudl be a second step - once we have this in 'Ptr' doing rep a natural next step
40 *
41 * > Great IDEA FROM KDJ. I USED TO support abstractfilesystem in stroika. Maybe in old code base. Used for Win32 FS versus UNIX versus MacOS FS.
42 * KDJ's point is this idea should be resurected cuz its useful for stuff like TARFILEs and ZIPFILES or ISO files which act like a FS, and can be treated
43 * that way.
44 *
45 * @todo Consider relationship between windows GetLongPathName() and our CanonicalizePathName...
46 *
47 */
48
50
51 /**
52 */
53 enum class RemoveDirectoryPolicy {
54 eFailIfNotEmpty,
55 eRemoveAnyContainedFiles, // note - this includes the case of included folders which include more files - fully recursive
56
57 eDEFAULT = eFailIfNotEmpty,
58
59 Stroika_Define_Enum_Bounds (eFailIfNotEmpty, eRemoveAnyContainedFiles)
60 };
61
62 static constexpr RemoveDirectoryPolicy eFailIfNotEmpty = RemoveDirectoryPolicy::eFailIfNotEmpty;
63 static constexpr RemoveDirectoryPolicy eRemoveAnyContainedFiles = RemoveDirectoryPolicy::eRemoveAnyContainedFiles;
64
65 /**
66 * SUPER ROUGH DRAFT .... Move much code from Directory and FileUtils as methods here. See KDJ comment above. Do other filesystems.
67 * POSIX, WINDOWS, and MacOS, and ZIPFILE, etc...
68 *
69 * \note SOON TODO REP class and shared_ptr here- http://stroika-bugs.sophists.com/browse/STK-612 -
70 *
71 * \par Example Usage
72 * \code
73 * if (IO::FileSystem::Default ().Access (kProcUptimeFileName_)) {
74 * // do stuff
75 * }
76 * \endcode
77 *
78 */
79 class Ptr {
80 public:
81 /**
82 * Return true if the given access is allowed (for the current user id).
83 * This works for a file or directory.
84 *
85 * @see CheckAccess to avoid test, and just throw if missing
86 *
87 * \par Example Usage
88 * \code
89 * if (IO::FileSystem::Default ().Access (kProcUptimeFileName_)) {
90 * // do stuff
91 * }
92 * \endcode
93 *
94 * \note Similar to IO.File.Exists () in .net https://msdn.microsoft.com/en-us/library/system.io.file.exists(v=vs.110).aspx
95 * \note Similar to POSIX access () - https://linux.die.net/man/2/access
96 */
97 nonvirtual bool Access (const filesystem::path& fileFullPath, AccessMode accessMode = AccessMode::eRead) const noexcept;
98
99 public:
100 /**
101 * Doesn't actually open the file. It's purely advisory. But its helpful to assure
102 * a consistent set of error reporting across different styles of opens. Just call this first,
103 * and it will throw exceptions if the file doesn't exist, or has access privileges issues.
104 *
105 * This works for a file or directory.
106 *
107 * @see Access to avoid throw
108 *
109 * \par Example Usage
110 * \code
111 * void CopyFile (String srcPath, String desPath)
112 * {
113 * IO::FileSystem::Default ().CheckAccess (srcFile, IO::AccessMode::eRead);
114 * CreateDirectoryForFile (destPath);
115 * .. do actual copy ..
116 * }
117 * \endcode
118 */
119 nonvirtual void CheckAccess (const filesystem::path& fileFullPath, AccessMode accessMode = AccessMode::eRead);
120 nonvirtual void CheckAccess (const filesystem::path& fileFullPath, bool checkCanRead, bool checkCanWrite);
121
122 public:
123 // @todo see last_write_time
124 nonvirtual DateTime GetFileLastModificationDate (const filesystem::path& fileName);
125
126 public:
127 // @todo doesn't appear supported by std::filesystem
128 nonvirtual DateTime GetFileLastAccessDate (const filesystem::path& fileName);
129 };
130
131 /**
132 * \note Design Note: why method 'Default ()' instead of just sThe, or something like that?
133 * There is one special interesting filesystem, but the intention was to someday allow different filesystems
134 * to be accessed. For example, treating a zipfile as a filesystem.
135 */
136 Ptr Default ();
137
138}
139
140/*
141 ********************************************************************************
142 ***************************** Implementation Details ***************************
143 ********************************************************************************
144 */
145#include "FileSystem.inl"
146
147#endif /*_Stroika_Foundation_IO_FileSystem_FileSystem_h_*/
#define Stroika_Define_Enum_Bounds(FIRST_ITEM, LAST_ITEM)
nonvirtual void CheckAccess(const filesystem::path &fileFullPath, AccessMode accessMode=AccessMode::eRead)
nonvirtual bool Access(const filesystem::path &fileFullPath, AccessMode accessMode=AccessMode::eRead) const noexcept