forked from Open-CMSIS-Pack/devtools
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathRteFsUtils.h
More file actions
438 lines (411 loc) · 17.7 KB
/
Copy pathRteFsUtils.h
File metadata and controls
438 lines (411 loc) · 17.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#ifndef RteFsUtils_H
#define RteFsUtils_H
/******************************************************************************/
/* RTE - CMSIS Run-Time Environment */
/******************************************************************************/
/** @file RteFsUtils.h
* @brief CMSIS RTE Data Model
*/
/******************************************************************************/
/*
* Copyright (c) 2020-2026 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
/******************************************************************************/
#include "RteUtils.h"
#if defined(_MSC_VER) && (_MSC_VER < 1920)
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
#include <filesystem>
namespace fs = std::filesystem;
#endif
// Utility class for file and directory operations
class RteFsUtils
{
private:
RteFsUtils() {};
public:
typedef std::vector<fs::path> PathVec;
/**
* @brief make path canonical
* @param path given path to be canonicalized
* @return canonicalized path
*/
static std::string MakePathCanonical(const std::string& path);
/**
* @brief back up file given by 'src' with new extension '.<4-digit number>', e.g. '.0001'
* @param src file to be backed up
* @param deleteExisting true if file to be backed up exists
* @return backed up file name
*/
static std::string BackupFile(const std::string& src, bool deleteExisting = false);
/**
* @brief Move file 'existing' into 'newFile'
* @param existing existing file
* @param newFile new file
* @return true if file is successfully moved
*/
static bool MoveExistingFile(const std::string& existing, const std::string& newFile);
/**
* @brief copy file 'src' into 'dst', create destination directory if necessary
* @param src source file name
* @param dst destination file name
* @param backup true if destination file is to be backed up
* @return true if file is copied
*/
static bool CopyCheckFile(const std::string& src, const std::string& dst, bool backup);
/**
* @brief create file and directories if necessary
* @param file name of file which is to be created
* @param content string to be stored in the created file
* @return true if file is created
*/
static bool CreateTextFile(const std::string& file, const std::string& content);
/**
* @brief copy string to file in binary mode. Previous content of file is destroyed.
* @param fileName name of file
* @param fileBuffer string to be saved in file
* @param backup true if file is to be backed up
* @return true if buffer is copied to file
*/
static bool CopyBufferToFile(const std::string& fileName, const std::string& fileBuffer, bool backup);
/**
* @brief replace occurrences of "%Instance%" in content of file 'src' with 'nInstance' and copy content to destination file 'dst'
* @param src source file
* @param dst destination file
* @param nInstance number to replace occurrences of "%Instance%"
* @param backup true to back up destination file
* @return true if copying file is processed
*/
static bool CopyMergeFile(const std::string& src, const std::string& dst, int nInstance, bool backup);
/**
* @brief compare files content with normalized line endings
* @param fileName1 name of first file to be compared
* @param fileName2 name of second file to be compared
* @return true if files are equal
*/
static bool CmpFiles(const std::string& fileName1, const std::string& fileName2);
/**
* @brief compare file content with given string 'buffer'
* @param fileName name of file to be compared
* @param buffer contain string to be compared
* @return true if file content is equal to given string
*/
static bool CmpFileMem(const std::string& fileName, const std::string& buffer);
/**
* @brief replace occurrences of "%Instance%" in file content with 'nInst' and store content in string 'buffer'
* @param fileName name of file to be processed
* @param nInst number to replace "%Instance%"
* @param buffer contain processed file content
* @return true if file content is processed
*/
static bool ExpandFile(const std::string& fileName, int nInst, std::string& buffer);
/**
* @brief store file content in string 'buffer'
* @param fileName name of file to be read
* @param buffer string containing file content
* @return true if file content can be read
*/
static bool ReadFile(const std::string& fileName, std::string& buffer);
/**
* @brief set file read-only permission for owner, group and others
* @param path file path
* @param bReadOnly true to set read-only otherwise all permissions are granted to owner, group and others
* @return true if file access permissions are set
*/
static bool SetFileReadOnly(const std::string& path, bool bReadOnly);
/**
* @brief set read-only permission for directory including subdirectories
* @param path directory
* @param bReadOnly true to set read-only otherwise all permissions are granted to owner, group and others
* @return true if file access permissions for directory including subdirectories are set
*/
static bool SetTreeReadOnly(const std::string& path, bool bReadOnly = true);
/**
* @brief delete file with retries and delay between retries
* @param path file to be deleted
* @param retries number of retries
* @param delay time lag in milliseconds between retries
* @return true if file is deleted
*/
static bool DeleteFileAutoRetry(const std::string& path, unsigned int retries = 5, unsigned int delay = 1);
/**
* @brief remove directory with retries and delay between retries
* @param path directory to be removed
* @param retries number of retries
* @param delay time lag in milliseconds between retries
* @return true if directory is removed
*/
static bool RemoveDirectoryAutoRetry(const std::string& path, int retries = 5, int delay = 1);
/**
* @brief rename file with retries and delay between retries, create destination directory if necessary
* @param existingFileName name of source file
* @param newFileName name of destination file
* @param retries number of retries
* @param delay time lag in milliseconds between retries
* @return true if file is renamed
*/
static bool MoveFileExAutoRetry(const std::string& existingFileName, const std::string& newFileName, unsigned int retries = 5, unsigned int delay = 1);
/**
* @brief copy file with retries and delay between retries, create destination directory if necessary
* @param existingFileName name of source file
* @param newFileName name of destination file
* @param retries number of retries
* @param delay time lag in milliseconds between retries
* @return true if file is copied
*/
static bool CopyFileExAutoRetry(const std::string& existingFileName, const std::string& newFileName, unsigned int retries = 5, unsigned int delay = 1);
/**
* @brief copy source file/directory include subdirectories to destination one
* @param src source file/directory
* @param dst destination file/directory
* @return true if file or directory is copied
*/
static bool CopyTree(const std::string& src, const std::string& dst);
/**
* @brief delete a directory including subdirectories
* @param path name of directory
* @return true if directory is deleted
*/
static bool DeleteTree(const std::string& path);
/**
* @brief remove a file or an empty directory
* @param file name of file or empty directory
* @return true if file or empty directory is deleted
*/
static bool RemoveFile(const std::string& file);
/**
* @brief remove files and directory including subdirectories
* @param dir name of directory
* @return true if files and directories are removed
*/
static bool RemoveDir(const std::string& dir);
/**
* @brief check if a given file or directory exists
* @param path name of file or directory to be examined
* @return true if file or directory exists
*/
static bool Exists(const std::string& path);
/**
* @brief check if given path is a directory
* @param path given path
* @return true if given path is a directory
*/
static bool IsDirectory(const std::string& path);
/**
* @brief check if given path is a regular file
* @param path name of given path
* @return true if given path is a regular file
*/
static bool IsRegularFile(const std::string& path);
/**
* @brief check if given path is a regular file with execute permissions
* @param path path to be checked
* @return true if path is an executable
*/
static bool IsExecutableFile(const std::string& path);
/**
* @brief check if given path is relative or absolute
* @param path path to be checked
* @return true if path is relative, false if path is absolute
*/
static bool IsRelative(const std::string& path);
/**
* @brief make path to absolute one
* @param path path to be processed
* @return filesystem::path containing the absolute path or an empty one in case error has happened
*/
static fs::path AbsolutePath(const std::string& path);
/**
* @brief get parent path
* @param path path to be processed
* @return string containing the parent path
*/
static std::string ParentPath(const std::string& path);
/**
* @brief checks if two paths are equivalent
* @param p1 first path to be compared
* @param p2 second path to be compared
* @return true if p1 == p2 or both refer to the same file or both are empty
*/
static bool Equivalent(const std::string& p1, const std::string& p2);
/**
* @brief get lexically normalized path
* @param path path to be processed
* @return string containing the lexically normalized path
*/
static std::string LexicallyNormal(const std::string& path);
/**
* @brief determine relative path in respect to base directory
* @param path path to be processed
* @param base base directory
* @param withHeadingDot true if returned value should contain heading dot '.'
* @return relative path in respect to base directory
*/
static std::string RelativePath(const std::string& path, const std::string& base, bool withHeadingDot = false);
/**
* @brief determine absolute path of the current working directory
* @param withTrailingSlash true if returned value should contain trailing slash '/'
* @return absolute path of the current working directory
*/
static std::string GetCurrentFolder(bool withTrailingSlash = true);
/**
* @brief change current working directory to path
* @param path path to use as current working directory
*/
static void SetCurrentFolder(const std::string &path);
/**
* @brief determine first file owning the given extension in given folder
* @param folder name of path where first file with given extension is to be found
* @param extension extension of file to be found
* @return string containing name of first file without path found with given extension
*/
static std::string FindFirstFileWithExt(const std::string & folder, const char *extension);
/**
* @brief look for all files in given folder, ordered by version comparator (applicable for CMSIS packs)
* @param folder directory to look for files
* @param files contain all files in folder ordered by version comparator (applicable for CMSIS packs)
*/
static void GetFilesSorted(const std::string &folder, std::set<std::string, VersionCmp::Greater> &files);
/**
* @brief look for installed pack version which matches the given version range
* @param path folder where installed pack version is to be searched
* @param versionRange given version range
* @return string containing installed pack version
*/
static std::string GetInstalledPackVersion(const std::string &path, const std::string &versionRange);
/**
* @brief ensure that directory including subdirectories exists, creating them if necessary
* @param filePath directory to be ensured
* @return true if directory exists
*/
static bool MakeSureFilePath(const std::string &filePath);
/**
* @brief create directory including subdirectories
* @param path directory to be created
* @return true if directory is successfully created
*/
static bool CreateDirectories(const std::string &path);
/**
* @brief normalize path by ensuring that it's absolute and canonical
* @param path folder to be normalized
* @param base root folder to form absolute
*/
static void NormalizePath(std::string& path, const std::string& base = RteUtils::EMPTY_STRING);
/**
* @brief scan given directory for regular files with given extension
* @param path directory to look in
* @param typeExt file extension to look for
* @return a vector of filesystem::path representing files with given extension
*/
static PathVec FindFiles(const std::string& path, const std::string& typeExt);
/**
* @brief scan given directory for regular files matching given pattern
* @param dir directory to look in
* @param wildCardPattern wild card pattern to match
* @return a vector of filesystem::path representing files with given extension
*/
static PathVec GrepFiles(const std::string& dir, const std::string& wildCardPattern);
/**
* @brief scan given directory for regular files matching given pattern
* @param fileNames list of strings to receive filenames
* @param dir directory to look in
* @param wildCardPattern wild card pattern to match
*/
static void GrepFileNames(std::list<std::string>& fileNames, const std::string& dir, const std::string& wildCardPattern);
/**
* @brief determine number of regular files found in given folder
* @param folder directory to look for
* @return number of regular files in given folder
*/
static int CountFilesInFolder(const std::string& folder);
/**
* @brief scan directory including subdirectories for regular files depending on certain criteria
* @param files result containing regular files matching extension
* @param extension file extension, must be supplied with leading dot, e.g. ".pdsc"
* @param path directory to look for
* @param depth directory depth to look for
* @param bAlwaysSearchSubfolders true to consider subdirectories
*/
static void GetMatchingFiles(std::list<std::string>& files, const std::string& extension, const std::string& path, int depth, bool bAlwaysSearchSubfolders);
/**
* @brief scan directory including subdirectories for package description files *.pdsc
* @param files result containing package description files *.pdsc
* @param path directory to look for
* @param depth directory depth to look for
*/
static void GetPackageDescriptionFiles(std::list<std::string>& files, const std::string& path, int depth);
/**
* @brief scan directory including subdirectories for package files *.pack
* @param files result containing package files *.pack
* @param path directory to look for
* @param depth directory depth to look for
*/
static void GetPackageFiles(std::list<std::string>& files, const std::string& path, int depth);
/**
* @brief create a string according to the syntax <path>_<extPrefix>_<index> as file name. The postfix index is increased to ensure that file does not exist
* @param path path to be included
* @param extPrefix prefix of extension to be included
* @return string according to the syntax <path>_<extPrefix>_<index>
*/
static std::string CreateExtendedName(const std::string& path, const std::string& extPrefix); // creates a name "<path>_<extPrefix>_<index>
/**
* @brief find file using regular expression
* @param search paths
* @param regular expression
* @param path to the file found
* @return true if file is found successfully, false otherwise
*/
static bool FindFileRegEx(const std::vector<std::string>& searchPaths, const std::string& regEx, std::string& file);
/**
* @brief get absolute file path from local url
* @param url starting with the pattern 'file://localhost/' or 'file:///' or 'file:/'
* @return string containing the absolute path
*/
static std::string GetAbsPathFromLocalUrl(const std::string& url);
/**
* @brief finds file in directories relative to the base path
* @param fileName file name to search for
* @param baseDir path to look in
* @param relSearchOrder vector of relative directories to look in
* @return absolute file name if found, empty string otherwise
*/
static std::string FindFile(const std::string& fileName, const std::string& baseDir,
const std::vector<std::string>& relSearchOrder);
/**
* @brief finds file in ./, ../etc/, and ../../etc directories relative to the base
* @param fileName file name to search for
* @param baseDir path to look in
* @return absolute file name if found, empty string otherwise
*/
static std::string FindFileInEtc(const std::string& fileName, const std::string& baseDir);
/**
* @brief get file category according to file extension
* @param filename with extension
* @return string category
*/
static const std::string& FileCategoryFromExtension(const std::string& file);
/**
* @brief find file using regular expression (non recursively)
* @param search path
* @param regular expression
* @param path to the file found
* @return true if file is found successfully, false otherwise
*/
static bool FindFileWithPattern(const std::string& searchPath, const std::string& pattern, std::string& file);
/**
* @brief returns file/dir modification time
* @param path absolute file/directory path
* @return fs::file_time_type
*/
static fs::file_time_type GetModificationTime(const std::string& path);
/**
* @brief converts time to string
* @param timeStamp fs::file_time_type to convert
* @return string representing time
*/
static std::string FileTimeToString(const fs::file_time_type& timeStamp);
};
#endif // RteFsUtils_H