FileCopier.h
Go to the documentation of this file.
1 /****
2  * FileCopier.h
3  *
4  * Created August 2018 by mikee471
5  *
6  * Copyright 2019 mikee47 <mike@sillyhouse.net>
7  *
8  * This file is part of the IFS Library
9  *
10  * This library is free software: you can redistribute it and/or modify it under the terms of the
11  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
12  *
13  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15  * See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with this library.
18  * If not, see <https://www.gnu.org/licenses/>.
19  *
20  ****/
21 
22 #pragma once
23 
24 #include "FileSystem.h"
25 #include "File.h"
26 
27 #define IFS_FILECOPIER_OPERATION_MAP(XX) \
28  XX(stat) \
29  XX(open) \
30  XX(create) \
31  XX(mkdir) \
32  XX(read) \
33  XX(write) \
34  XX(enumattr) \
35  XX(setattr)
36 
37 namespace IFS
38 {
43 {
44 public:
45  enum class Operation {
46 #define XX(tag) tag,
48 #undef XX
49  };
50 
54  struct ErrorInfo {
57  const String& path;
58  int errorCode;
60 
63  {
64  }
65 
67  : fileSys(*obj.getFileSystem()), operation(operation), path(path), errorCode(obj.getLastError())
68  {
69  }
70 
72  : ErrorInfo(obj, operation, path)
73  {
74  this->attr = attr;
75  }
76 
77  operator String() const
78  {
79  String s;
80  s += operation;
81  s += "(\"";
82  s += path;
83  s += '"';
84  if(operation == Operation::setattr) {
85  s += ", ";
86  s += attr;
87  }
88  s += F("): ");
90  return s;
91  }
92  };
93 
97  using ErrorHandler = Delegate<bool(const ErrorInfo& info)>;
98 
99  FileCopier(FileSystem& srcfs, FileSystem& dstfs);
100 
101  bool copyFile(const String& srcFileName, const String& dstFileName);
102  bool copyDir(const String& srcPath, const String& dstPath);
103  bool copyAttributes(const String& srcPath, const String& dstPath);
104 
105  void onError(ErrorHandler callback)
106  {
107  errorHandler = callback;
108  }
109 
110 private:
111  bool copyFile(const String& srcPath, const String& dstPath, const Stat& stat);
112  bool copyAttributes(File& src, File& dst, const String& srcPath, const String& dstPath);
113 
114  bool handleError(const ErrorInfo& info);
115 
116  FileSystem& srcfs;
117  FileSystem& dstfs;
118  ErrorHandler errorHandler;
119  IFileSystem::Attributes dstAttr;
120 };
121 
122 } // namespace IFS
123 
ErrorInfo(FsBase &obj, Operation operation, const String &path, AttributeTag attr)
Definition: FileCopier.h:71
String toString(IFS::FileCopier::Operation operation)
Wraps up all file access methods.
Definition: File.h:46
Delegate< bool(const ErrorInfo &info)> ErrorHandler
Return true to ignore error and continue copying, false to stop.
Definition: FileCopier.h:97
The String class.
Definition: WString.h:136
const String & path
Definition: FileCopier.h:57
Definition: DirectoryTemplate.h:36
Installable File System base class.
Definition: Components/IFS/src/include/IFS/FileSystem.h:39
FileCopier(FileSystem &srcfs, FileSystem &dstfs)
File Status structure.
Definition: Stat.h:71
Class to manage copying of files and directories including attributes.
Definition: FileCopier.h:42
void onError(ErrorHandler callback)
Definition: FileCopier.h:105
ErrorInfo(FsBase &obj, Operation operation, const String &path)
Definition: FileCopier.h:66
Definition: FsBase.h:51
bool copyDir(const String &srcPath, const String &dstPath)
FileSystem & fileSys
Definition: FileCopier.h:55
#define IFS_FILECOPIER_OPERATION_MAP(XX)
Definition: FileCopier.h:27
IFS::FileSystem * getFileSystem()
Get the currently active file system, if any.
Definition: Core/FileSystem.h:64
bool copyFile(const String &srcFileName, const String &dstFileName)
AttributeTag
Identifies a specific attribute.
Definition: Attribute.h:64
AttributeTag attr
Definition: FileCopier.h:59
Operation
Definition: FileCopier.h:45
bool copyAttributes(const String &srcPath, const String &dstPath)
#define F(string_literal)
Wrap a string literal stored in flash and access it using a String object.
Definition: WString.h:113
virtual String getErrorString(int err)
get the text for a returned error code
Definition: IFileSystem.h:214
int errorCode
Definition: FileCopier.h:58
Operation operation
Definition: FileCopier.h:56
ErrorInfo(FileSystem &fileSys, Operation operation, const String &path, int errorCode)
Definition: FileCopier.h:61
Error information passed to callback.
Definition: FileCopier.h:54