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