HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PDG_FileUtils.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * COMMENTS:
7  */
8 
9 #ifndef PDG_FILE_UTILS_H
10 #define PDG_FILE_UTILS_H
11 
12 #include "PDG_API.h"
13 
14 #include "PDG_File.h"
15 
16 #include <UT/UT_Array.h>
17 #include <UT/UT_ArrayStringMap.h>
18 #include <UT/UT_NonCopyable.h>
19 #include <UT/UT_StringHolder.h>
20 #include <UT/UT_StringMap.h>
21 #include <UT/UT_StringSet.h>
22 #include <UT/UT_TBBSpinLock.h>
23 
24 class PDG_CustomHandlers;
25 class PDG_Scheduler;
26 class PDG_WorkItem;
27 
28 class UT_FileStat;
29 
30 /*
31  * Struct for defining a file type attribute in PDG, and utility functions for
32  * doing extension -> tag lookup and saving file data.
33  */
35 {
36 public:
37  /// Pair of viewer application, and whether the application supports
38  /// open multiple files
39  using ViewerPair = std::pair<UT_StringHolder, bool>;
40 
41  /// Default tag for any file
43 
44  /// Default tag for an archive
46 
47  /// Default tag for an archive
48  static const UT_StringHolder theCsvTag;
49 
50  /// Default tag for geometry
51  static const UT_StringHolder theGeoTag;
52 
53  /// Default tag for USD
54  static const UT_StringHolder theUSDTag;
55 
56  /// Default tag for imgages
58 
59  /// Default tag for text data
61 
62  /// Default tag for videos
64 
65  /// Default tag for hip files
66  static const UT_StringHolder theHipTag;
67 
68  /// Default tag for hda files
69  static const UT_StringHolder theHdaTag;
70 
71  /// Default tag for Python script files
73 
74  /// Default tag for directories
76 
77  /// Default tag for sym links
79 
80  /// Default viewer application
82 
83  /// Enumeration of file transfer destination paths
85  {
86  /// The default temp file directory. Most files go here, unless
87  /// they're one of the special cases below
89 
90  /// The file goes into the scheduler's workingDir instead of the
91  /// temporary file directory
93 
94  /// The file is a python script and goes to the scheduler's
95  /// scriptDir, which normally a special path on the temp dir that
96  /// gets added to the Python path
98 
99  /// The file is a digital asset and goes to the tempDir/otls
100  /// directory, which is added to the OTL search path.
102  };
103 
104 public:
105  /// Returns the set of available tags
106  const UT_StringSet& tags() const;
107 
108  /// Returns the sorted list of tags for the given prefix
109  void tags(const UT_StringHolder& prefix,
110  UT_StringArray& sorted_tags) const;
111 
112  /// Returns true if the tag is viewable
113  bool isViewable(const UT_StringHolder& tag) const;
114 
115  /// Looks up the viewer application for a tag
116  const ViewerPair& tagToViewer(const UT_StringHolder& tag) const;
117 
118  /// Loops up the viewer application for a path
119  const ViewerPair& pathToViewer(const UT_StringHolder& path) const;
120 
121 
122  /// Looks up the file tag for a full file path
123  const UT_StringHolder& pathToTag(const UT_StringHolder& path) const;
124 
125  /// Looks up the file tag for an extension
126  const UT_StringHolder& extensionToTag(const char* ext,
127  const UT_StringHolder& def) const;
128 
129  /// Return the extension of the given path which may include an archive dot suffix
130  /// like .bgeo.sc
131  UT_StringHolder pathToExtension(const UT_StringHolder& path) const;
132 
133  /// Adds a tag to the system, without establishing an extension - >tag
134  /// mapping. For non-file based tags
135  void addTag(const UT_StringHolder& tag);
136 
137  /// Adds an extension -> tag mapping
138  void addExtensionTag(const UT_StringHolder& ext,
139  const UT_StringHolder& tag);
140 
141  /// Adds a tag -> viewer application mapping
142  void addTagViewer(const UT_StringHolder& tag,
143  const UT_StringHolder& viewer,
144  bool is_multiviewer=false);
145 
146  /// Checks if a given file path is cached
147  bool checkCached(
148  const UT_StringHolder& local_path,
149  const PDG_File& file,
150  const PDG_WorkItem* work_item) const;
151 
152  /// Returns the hash/mod time of the file
153  bool statFile(
154  const UT_StringHolder& local_path,
155  const PDG_File& file,
156  const PDG_WorkItem* work_item,
157  PDG_File::Hash& file_hash,
158  exint& file_size) const;
159 
160  /// Called when a work item should delete files, and runs any custom
161  /// handlers that match the item
162  void dirtyFiles(
163  const PDG_WorkItem* work_item,
164  const PDG_File::Set& files,
165  bool delete_items,
166  bool remove_outputs) const;
167 
168  /// Transfers a file from a local path to a remote path accessible by the
169  /// specified scheduler. The work item indicates what depends on the file,
170  /// and may be null if the file transfer was initiated for a global file
171  /// dependency.
172  bool transferFile(
173  const PDG_Scheduler* scheduler,
174  const PDG_WorkItem* work_item,
175  const PDG_File& file,
176  TransferType default_type,
177  bool check_type,
178  UT_WorkBuffer& errors) const;
179 
180  /// Transfers a file from a local path to a remote path accessible by the
181  /// specified scheduler, and preserves the directory structure relative to
182  /// the specified root path. The work item indicates what depends on the
183  /// file, and may be null if the file transfer was initiated for a global
184  /// file dependency.
185  bool transferFile(
186  const PDG_Scheduler* scheduler,
187  const PDG_WorkItem* work_item,
188  const PDG_File& file,
189  const UT_StringHolder& root_path,
190  const UT_StringHolder& sub_path,
191  TransferType default_type,
192  bool check_type,
193  UT_WorkBuffer& errors) const;
194 
195  /// Returns the path that a local file will be transfered to, based on
196  /// the transfer type and the file extension. Always looks up the extension
197  /// in the transfer type mapping, and only uses the default transfer type
198  /// as a fallback.
199  UT_StringHolder formatTransferPath(
200  const PDG_Scheduler* scheduler,
201  const UT_StringHolder& local_path,
202  TransferType default_type,
203  bool local,
204  bool check_type) const;
205 
206  /// Returns the path that a local file will be transfer to, based on
207  /// the transfer type, extension, and the relative path of the local file
208  /// to the root file path. For example, if the root path is $HIP and the
209  /// local_path is $HIP/sub_dir/file.bgeo.sc, the file will be copied tp
210  /// {dest}/{sub_path}/sub_dir/file.bgeo.sc where "dest" is determined by
211  /// the specified scheduler and file type.
212  UT_StringHolder formatTransferPath(
213  const PDG_Scheduler* scheduler,
214  const UT_StringHolder& root_path,
215  const UT_StringHolder& local_path,
216  const UT_StringHolder& sub_path,
217  TransferType default_type,
218  bool local,
219  bool check_type) const;
220 
221  /// Compares the file paths and returns 0 if they're equal, -1 if the left
222  /// path is ordered first, and 1 if the right path is ordered first. The
223  /// paths are first compared directly as strings, and if they're not equal
224  /// the paths are normalized to remove extra slashes or .. tokens, then
225  /// compared again.
226  static int compareFilePaths(
227  const UT_StringHolder& left,
228  const UT_StringHolder& right);
229 
230  /// Makes intermediate directories
231  static bool makeIntermediateDirs(const char* full_path);
232 
233  /// Formats a URL given a scheme and url body
234  static UT_StringHolder makeURL(const UT_StringHolder& scheme,
235  const UT_StringHolder& path);
236 
237  /// Returns true if the specified list of files has any duplicates, and
238  /// returns the set of duplicates.
239  static bool hasDuplicateFiles(
240  const PDG_File::Array& files,
241  PDG_File::Set& duplicates);
242 
243 protected:
244  friend class PDG_TypeRegistry;
245 
246  /// Initializes the file mapping object and optionally loads in the
247  /// default, built-in tags/extension mappings.
249  const PDG_CustomHandlers* handlers,
250  bool init_defaults);
251 
252 private:
253  /// Caches an id for completed transfers, to potentially skip future
254  /// transfers of the same file path. By default, the mod time of the
255  /// file is used as the cache value.
256  struct TransferCacheEntry
257  {
258  UT_ArrayStringMap<exint> myCache;
259  UT_TBBSpinLock myLock;
260  };
261 
262  using TransferCache = UT_StringMap<TransferCacheEntry>;
263 
264 private:
265  void init();
266 
267 
268  bool transferFile(
269  TransferCacheEntry* transfer_cache,
270  const PDG_Scheduler* scheduler,
271  const PDG_WorkItem* work_item,
272  const UT_FileStat& local_stat,
273  const PDG_File& file,
274  const UT_StringHolder& local_path,
275  const UT_StringHolder& dest_path,
276  UT_WorkBuffer& errors) const;
277  exint transferFileDefault(
278  const UT_FileStat& local_stat,
279  const UT_StringHolder& local_path,
280  const UT_StringHolder& dest_path,
281  exint last_update,
282  UT_WorkBuffer& errors) const;
283  UT_StringHolder formatTransferPath(
284  const PDG_Scheduler* scheduler,
286  const UT_StringHolder& sub_path,
287  TransferType transfer_type,
288  bool local) const;
289 
290 private:
291  // Custom handler table, for methods that may need to call into user-
292  // defined handlers before running the standard logic.
293  const PDG_CustomHandlers* myHandlers;
294 
295  // Mapping of file extension to file tag, e.g. .bgeo -> file/geo or
296  // .png -> file/image
298 
299  // Mapping of tags to result viewers, e.g. file/image -> mplay
300  UT_StringMap<ViewerPair> myViewerMap;
301 
302  // Scheduler name -> file transfer cache
303  mutable TransferCache myTransferCache;
304 
305  // Map of file extension -> transfer path
306  UT_StringMap<TransferType> myTransferTypes;
307 
308  // Set of all available tags
309  UT_StringSet myTags;
310 
311  // Lock for guarding concurrent access to myTransferCache
312  mutable UT_TBBSpinLock myTransferCacheLock;
313 };
314 
315 #endif
static const UT_StringHolder theTextTag
Default tag for text data.
Definition: PDG_FileUtils.h:60
static const UT_StringHolder theSymLinkTag
Default tag for sym links.
Definition: PDG_FileUtils.h:78
static const UT_StringHolder theUSDTag
Default tag for USD.
Definition: PDG_FileUtils.h:54
static const UT_StringHolder theHdaTag
Default tag for hda files.
Definition: PDG_FileUtils.h:69
PUGI__FN const char_t * local_name(const xpath_node &node)
Definition: pugixml.cpp:8471
static const UT_StringHolder thePythonScriptTag
Default tag for Python script files.
Definition: PDG_FileUtils.h:72
GLint left
Definition: glcorearb.h:2005
static const UT_StringHolder theImageTag
Default tag for imgages.
Definition: PDG_FileUtils.h:57
#define PDG_API
Definition: PDG_API.h:23
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
GLdouble right
Definition: glad.h:2817
int64 exint
Definition: SYS_Types.h:125
static const ViewerPair theDefaultViewer
Default viewer application.
Definition: PDG_FileUtils.h:81
static const UT_StringHolder theDirectoryTag
Default tag for directories.
Definition: PDG_FileUtils.h:75
int64 Hash
The file hash/modtime type.
Definition: PDG_File.h:39
OIIO_UTIL_API uint64_t file_size(string_view path) noexcept
std::pair< UT_StringHolder, bool > ViewerPair
Definition: PDG_FileUtils.h:39
static const UT_StringHolder theArchiveTag
Default tag for an archive.
Definition: PDG_FileUtils.h:45
static const UT_StringHolder theFileTag
Default tag for any file.
Definition: PDG_FileUtils.h:42
static const UT_StringHolder theCsvTag
Default tag for an archive.
Definition: PDG_FileUtils.h:48
TransferType
Enumeration of file transfer destination paths.
Definition: PDG_FileUtils.h:84
static const UT_StringHolder theGeoTag
Default tag for geometry.
Definition: PDG_FileUtils.h:51
static const UT_StringHolder theHipTag
Default tag for hip files.
Definition: PDG_FileUtils.h:66
static const UT_StringHolder theVideoTag
Default tag for videos.
Definition: PDG_FileUtils.h:63