1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #ifndef INCLUDED_COMPHELPER_BACKUPFILEHELPER_HXX
11 #define INCLUDED_COMPHELPER_BACKUPFILEHELPER_HXX
13 #include <sal/config.h>
15 #include <comphelper/comphelperdllapi.h>
16 #include <rtl/ustring.hxx>
18 #include <string_view>
23 /** Helper class to backup/restore a single file
25 * This is a general class to manage backups/restore of the file
26 * given by the URL. The container holding the backups is created
27 * aside the original file, e.g for 'test.txt' a container
28 * called '.test.pack' will be used. If it was not yet backed-up
29 * this container file will be created at the 1st backup and deleted
30 * when the last gets removed. The container holds a stack with a
31 * maximum given number (in the constructor) of copies, these are by
32 * default compressed, but don't have to be (see tryPush).
34 * Due to being on a low system level here, no UNO API and not much
35 * other tooling can be used, as a consequence for the container a
36 * own simple format is used and e.g. the zip lib directly.
38 * You need to hand over the URL of the file to look at and
39 * a maximum number of allowed copies. That number is internally
40 * limited to an absolute max of 10 (see implementation). The number
41 * of allowed copies is limited to [1..max].
43 * Calling tryPush() will check if there is no backup yet or if
44 * there is one that the file has changed. If yes, a new copy is
45 * created on a kind of 'stack' of copies. The return value can
46 * be used to see if a backup was indeed created.
48 * Calling tryPop() will do the opposite: if a backup is available,
49 * delete the orig file and re-instantiate the backup. The backup
50 * is taken off the 'stack' of copies. The return value can be
51 * used to check if this was done.
53 * isPopPossible can be called to see if there is a backup available
54 * before calling tryPop().
56 * The 'stack' of copies works by using the same path, filename
57 * and extension, but adding a '_1' -> '_(num_of_copy)' to it.
59 class COMPHELPER_DLLPUBLIC BackupFileHelper
63 std::set
< OUString
> maDirs
;
64 std::set
< std::pair
< OUString
, OUString
> > maFiles
;
66 sal_uInt16 mnNumBackups
;
73 // internal flag if _exit() was called already - a hint to evtl.
74 // not create copies of potentially not well-defined data. This
75 // may be used in destructors of static instances - which unfortunately
76 // get called on WNT but not on linux. Thus I thought about encapsulating
77 // in some '#ifdefs', but it's just more safe to always do it and
78 // allows to add a SAL_WARN when one of these destructors is called
80 static bool mbExitWasCalled
;
82 // internal detector if SafeModeName dir exists
83 static bool mbSafeModeDirExists
;
85 // internal upper limit (max) of allowed backups
86 static sal_uInt16 mnMaxAllowedBackups
;
88 // path to User's configuration directory and derived strings
89 static OUString maInitialBaseURL
;
90 static OUString maUserConfigBaseURL
;
91 static OUString maUserConfigWorkURL
;
92 static OUString maRegModName
;
93 static OUString maExt
;
95 // get path to User's configuration directory (created on-demand)
96 static const OUString
& getInitialBaseURL();
98 // the name of the SafeMode directory for temporary processing
99 static const OUString
& getSafeModeName();
102 /** Constructor to handle Backups of the given file, will internally
103 * detect configuration values and URL to initial registrymodifications
104 * and thus the User configuration directory
108 // allow to set static global flag when app had to call _exit()
109 static void setExitWasCalled();
111 // This call initializes the state of the UserDirectory as needed, it may
112 // initialize to SafeMode configuration or return from it by moving files
114 static void reactOnSafeMode(bool bSafeMode
);
116 /** tries to create a new backup, if there is none yet, or if the
117 * last differs from the base file. It will then put a new version
118 * on the 'stack' of copies and evtl. delete the oldest backup.
119 * Also may cleanup older backups when NumBackups given in the
120 * constructor has changed.
122 * tryPushExtensionInfo is the specialized version for ExtensionInfo
125 void tryPushExtensionInfo();
127 /** finds out if a restore is possible
130 * returns true if a restore to an older backup is possible
132 * isPopPossibleExtensionInfo is the specialized version for ExtensionInfo
134 bool isPopPossible();
135 bool isPopPossibleExtensionInfo() const;
137 /** tries to execute a restore. Will overwrite the base file
138 * in that case and take one version off the 'stack' of copies.
139 * Also may cleanup older backups when NumBackups given in the
140 * constructor has changed.
142 * tryPopExtensionInfo is the specialized version for ExtensionInfo
145 void tryPopExtensionInfo();
147 /** tries to iterate the extensions and to disable all of them
149 static bool isTryDisableAllExtensionsPossible();
150 static void tryDisableAllExtensions();
152 /** Deinstall all User Extensions (installed for User only)
154 static bool isTryDeinstallUserExtensionsPossible();
155 static void tryDeinstallUserExtensions();
157 /** Reset shared Extensions
159 static bool isTryResetSharedExtensionsPossible();
160 static void tryResetSharedExtensions();
162 /** Reset bundled Extensions
164 static bool isTryResetBundledExtensionsPossible();
165 static void tryResetBundledExtensions();
167 /// Disables OpenGL and OpenCL
168 static void tryDisableHWAcceleration();
170 /** resets User-Customizations like Settings and UserInterface modifications
172 static bool isTryResetCustomizationsPossible();
173 static void tryResetCustomizations();
175 /** resets the whole UserProfile
177 static void tryResetUserProfile();
179 /** Return the profile url */
180 static const OUString
& getUserProfileURL();
182 /** Return the url of the backed up profile (when in safe mode) */
183 static const OUString
& getUserProfileWorkURL();
186 // internal helper methods
187 static OUString
getPackURL();
188 static const std::vector
< OUString
>& getCustomizationDirNames();
189 static const std::vector
< OUString
>& getCustomizationFileNames();
192 bool tryPush_Files(const std::set
< OUString
>& rDirs
, const std::set
< std::pair
< OUString
, OUString
> >& rFiles
, std::u16string_view rSourceURL
, const OUString
& rTargetURL
);
193 bool tryPush_file(std::u16string_view rSourceURL
, std::u16string_view rTargetURL
, std::u16string_view rName
, std::u16string_view rExt
);
195 // file pop possibilities helper
196 bool isPopPossible_files(const std::set
< OUString
>& rDirs
, const std::set
< std::pair
< OUString
, OUString
> >& rFiles
, std::u16string_view rSourceURL
, std::u16string_view rTargetURL
);
197 static bool isPopPossible_file(std::u16string_view rSourceURL
, std::u16string_view rTargetURL
, std::u16string_view rName
, std::u16string_view rExt
);
200 bool tryPop_files(const std::set
< OUString
>& rDirs
, const std::set
< std::pair
< OUString
, OUString
> >& rFiles
, std::u16string_view rSourceURL
, const OUString
& rTargetURL
);
201 bool tryPop_file(std::u16string_view rSourceURL
, std::u16string_view rTargetURL
, std::u16string_view rName
, std::u16string_view rExt
);
203 // ExtensionInfo helpers
204 bool tryPush_extensionInfo(std::u16string_view rTargetURL
);
205 static bool isPopPossible_extensionInfo(std::u16string_view rTargetURL
);
206 bool tryPop_extensionInfo(std::u16string_view rTargetURL
);
208 // FileDirInfo helpers
209 void fillDirFileInfo();
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */