HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FS_IndexFile.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  * NAME: FS_IndexFile.h ( FS Library, C++)
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __FS_IndexFile__
13 #define __FS_IndexFile__
14 
15 #include "FS_API.h"
16 
17 #include <UT/UT_ArrayStringMap.h>
18 #include <UT/UT_Defines.h>
19 #include <UT/UT_Lock.h>
20 #include <UT/UT_SharedPtr.h>
21 #include <UT/UT_StringHolder.h>
22 #include <UT/UT_ValArray.h>
23 #include <UT/UT_VectorTypes.h>
24 #include <SYS/SYS_Types.h>
25 #include <iosfwd>
26 #include <time.h>
27 
28 class FS_IndexFileHandle;
30 class FS_Reader;
31 class FS_ReaderStream;
32 class FS_Section;
34 class UT_Options;
35 class UT_String;
36 class UT_WorkBuffer;
37 
38 #define FS_SECTION_SEPARATOR UT_SECTION_SEPARATOR_CHAR
39 #define FS_SECTION_SEPARATOR_STRING UT_SECTION_SEPARATOR_STRING
40 #define FS_SECTIONFILE "Sections.list"
41 
42 // This class implements a file that contains several sections that can be found
43 // at certain offsets of a main file.
45 {
46 public:
47  /// Default constructor.
48  FS_IndexFile();
49 
50  /// This constructor takes a path of some sort (e.g. a file path, an oplib:
51  /// path, an opdef: path, an http:// path, etc) to the index file.
52  FS_IndexFile(const char *source);
53 
54  /// This constructor takes a reader that will be used to retrieve section
55  /// data. The index file takes ownership and will delete the reader in the
56  /// destructor. (If null is passed in, this object creates its own default
57  /// FS_Reader, but unless a source is given nothing can be read from such
58  /// an index file.)
59  ///
60  /// If no source is given, closing the index file and reopening it again
61  /// will have no effect on the underlying reader. The reader will only be
62  /// closed and deleted when the index file object is deleted. If a source
63  /// is passed in, though, the reader will be deleted when the index file is
64  /// closed and subsequent openings will use the source path to construct a
65  /// new reader.
66  FS_IndexFile(FS_Reader *reader, const char *source=nullptr);
67 
68  virtual ~FS_IndexFile();
69 
70  // Some basic operations on sections (add, remove, modify).
71  // These functions don't affect the source file. The changes
72  // are only evident when we call writeFile.
73  // Reading is NOT currently threadsafe.
74  inline bool hasSection(const UT_StringRef &section) const
75  { return mySectionTable.contains(section) ||
76  myTempSectionTable.contains(section); }
77  inline bool hasTempSection(const UT_StringRef &section) const
78  { return myTempSectionTable.contains(section); }
79  inline bool hasRegularSection(const UT_StringRef &section) const
80  { return mySectionTable.contains(section); }
81  time_t getSectionModTime(const UT_StringRef &section) const;
82  int getSectionDataSize(const UT_StringRef &section) const;
83  bool readSection(const UT_StringRef &section,
84  char *buffer) const;
85  bool readSection(const UT_StringRef &section,
86  UT_WorkBuffer &buffer) const;
87  bool readOptionsSection(const UT_StringRef &section,
88  UT_Options &options) const;
89  void addSection(const UT_StringRef &section,
90  const char *filename);
91  void addSection(const UT_StringRef &section,
92  const UT_WorkBuffer &buf,
93  time_t modtime = time_t(-1));
94  void addSection(const UT_StringRef &section,
95  const char *buffer,
96  int len, time_t modtime = time_t(-1));
97  void addSection(const UT_StringRef &section,
98  const FS_IndexFile &file);
99  void modifySection(const UT_StringRef &section,
100  const char *buffer,
101  int len, time_t modtime = time_t(-1));
102  void modifySection(const UT_StringRef &section,
103  const UT_WorkBuffer &buf,
104  time_t modtime = time_t(-1));
105  void removeSection(const UT_StringRef &section);
106  void mergeIndexFile(const FS_IndexFile &file, bool overwrite);
107 
108  // Change the ordering of some sections. This is basically a book-keeping
109  // change, and only affects things when this index file is then saved.
110  void moveSections(int first, int last, int offset);
111 
112  // Get the modification time for the index file as a whole.
113  time_t getModTime() const;
114 
115  // Get the size of the whole index file
116  int64 getFileDataSize() const;
117 
118  // Sets the stream filters used for processing the data when it is read
119  // in or written out.
120  void setFilters(FS_WriteFilterFactory *encrypt_factory,
121  FS_IStreamFilterFactory *decrypt_factory);
122 
123  // Gets the decryption and encryption filter factories.
124  FS_WriteFilterFactory *getEncryptionFilter() const;
125  FS_IStreamFilterFactory *getDecryptionFilter() const;
126 
127  // Get an opaque pointer to a section.
128  FS_Section *getSection(const UT_StringRef &section) const;
129 
130  // Get an FS_Reader for reading a section of the index.
131  FS_Reader *getSectionReader(const UT_StringRef &section) const;
132 
133  // These utility functions should only be used by FS_ReaderHelpers
134  FS_ReaderStream *getSectionStream(const UT_StringRef &section) const;
135  FS_ReaderStream *getStreamCopy() const;
136 
137  // Given an opaque section pointer get an FS_IndexFile it represents.
138  FS_IndexFileHandle getIndexFileFromSection(const UT_StringRef &section,
139  const char *source = nullptr) const;
140 
141  // Simple access functions to find out about our sections.
142  int getNumSections() const;
143  const UT_StringHolder &getSectionName(int index) const;
144 
145  // Access our source file name (which may be empty).
146  const UT_StringHolder &getSourceFile() const { return mySourceFile; }
147 
148  // Access functions for our description string.
149  const UT_StringHolder &getDescription() const;
150  void setDescription(const UT_StringHolder &description);
151 
152 
153  // If we are going to be written to a strstream, this function returns
154  // a sensible guess about the buffer size that should be allocated.
155  exint guessStreamSize() const;
156 
157  // Write us out to a stream (with all modifications).
158  // Note: Don't write out an index file to the same file it was loaded
159  // from, since writing implicitly reads the file. Instead, write it to a
160  // separate file and then copy it over. This will also require this index
161  // file to be reloaded.
162  virtual void writeFile(std::ostream &os) const;
163 
164  // This functions are for expanding and collapsing an index file,
165  // much like hexpand and hcollapse do for cpio archives.
166  virtual bool expandToDirectory(const char *destdir);
167  virtual bool collapseFromDirectory(const char *srcdir);
168 
169  // Get the current time, used to set the section modification time.
170  static time_t getCurrentTime()
171  {
172  time_t currtime;
173  return ::time(&currtime);
174  }
175 
176  /// Tests if the index file is copy-protected.
177  bool isCopyProtected() const;
178 
179  /// Tests if the index file is black boxed.
180  bool isBlackBoxed() const;
181 
182  /// Tests if the index file is stored as expanded.
183  bool isExpanded() const
184  { return myIsExpanded; }
185 
186  static bool canWriteFile(const char *filename);
187 
188  bool getSectionFileName(const char *section,
190 
191  static void clearStartupCache();
192 
193  // -----------------------------------------------------------------------
194  // Section data conversion to/from text
195 
196  typedef bool(*ConvertFunc)(const UT_StringHolder &src_path,
197  const UT_WorkBuffer &in_data,
198  UT_WorkBuffer &out_data);
199 
201  {
202  ConversionFuncs(const char *bin_file_pattern = "",
203  const char *text_file_pattern = "",
204  ConvertFunc to_text = nullptr,
205  ConvertFunc from_text = nullptr)
206  : myToText(to_text),
207  myFromText(from_text),
208  myBinFilePattern(bin_file_pattern),
209  myTextFilePattern(text_file_pattern)
210  {}
211 
212  void clear()
213  {
214  myToText = nullptr;
215  myFromText = nullptr;
216  myBinFilePattern.clear();
217  myTextFilePattern.clear();
218  }
219 
220  bool isClear() const
221  {
222  // sufficient to just text first data member
223  return myToText == nullptr;
224  }
225 
226  ConvertFunc myToText;
227  ConvertFunc myFromText;
230  };
232 
233 
234  static bool registerConversionFuncs(const char *bin_file_pattern,
235  const char *text_file_pattern,
236  ConvertFunc to_text,
237  ConvertFunc from_text);
238 
239  static const ConversionList &getConversionsToText();
240  static const ConversionList &getConversionsFromText();
241 
242 
243 protected:
244  void setModified() { myModified = true; }
245 
246  exint writeHeader(std::ostream &os,
247  const UT_Int32Array &section_sizes) const;
248 
249  static ConversionList &_getConversionsToText();
250  static ConversionList &_getConversionsFromText();
251 
252  mutable UT_Lock myLock;
253 
254 private:
256 
257  // Functions for opening and closing our stream.
258  void openStream(const UT_Options *options = nullptr) const;
259  void closeStream() const;
260 
261  // Helper functions for reading and writing ourselves to a directory.
262  static void makeValidFileName(UT_String &filename);
263  bool writeSectionListFile(const char *sectionfile);
264 
265  // Writes out a section to the output stream. If the filter factory
266  // has been set, the filter will be applied to the section data before
267  // it is written out.
268  void writeSection(FS_Section *section,
269  std::ostream &os,
270  bool as_text) const;
271 
272  // Get an FS_Reader for reading a section of the index.
273  FS_Reader *getSectionReader(FS_Section *section) const;
274 
275  // These utility functions should only be used by FS_ReaderHelpers
276  FS_ReaderStream *getSectionStream(FS_Section *section) const;
277 
278  // Read in our index and use it to create our section data structures.
279  void readIndex();
280  void readDirectoryIndex(
281  const UT_String &source_file_path,
282  const UT_String &sections_file_path);
283  void readStreamIndex();
284 
285  // Obtains a size of the i-th section after it has been filtered (if at all)
286  int getWriteFilteredSectionSize( int index ) const;
287 
288  // Recalculates the data sizes of our sections when the read filter is set.
289  void recalculateFilteredSectionSizes();
290 
292 
293  // Data:
294  mutable FS_Reader *myReader;
295  FS_SectionArray mySections;
296  SectionMap mySectionTable;
297  FS_SectionArray myTempSections;
298  SectionMap myTempSectionTable;
299  UT_StringHolder myDescription;
300  UT_StringHolder mySourceFile;
301  FS_WriteFilterFactory *myWriteFilter;
302  FS_IStreamFilterFactory *myReadFilter;
303  time_t myModTime;
304  int64 mySize;
305  int myStreamStart;
306  bool myModified;
307  bool myIsExpanded;
308 
309  friend class FS_Section;
310  friend class FS_EmbeddedSection;
311  friend class FS_DiskSection;
312  friend class FS_IndexFileHandle;
313 };
314 
315 class FS_API FS_IndexFileHandle : public UT_SharedPtr<FS_IndexFile>
316 {
317 public:
320  virtual ~FS_IndexFileHandle();
321 
322  // FIXME:TODO:XXX: This is highly suspicious because we have a non-trivial
323  // destructor. But making this explicit for now for rule of three without
324  // any behaviour changes.
325  FS_IndexFileHandle(const FS_IndexFileHandle &src) = default;
326  FS_IndexFileHandle &operator=(const FS_IndexFileHandle &src) = default;
327 
328 private:
329 
330  friend class FS_Section;
331  friend class FS_EmbeddedSection;
332  friend class FS_DiskSection;
333  friend class FS_IndexFile;
334 };
335 
336 // For UT::ArraySet.
337 namespace UT
338 {
339 template <typename T>
340 struct DefaultClearer;
341 
342 template <>
343 struct DefaultClearer<FS_IndexFile::ConversionFuncs>
344 {
346  static void clear(ValueT &v) { v.clear(); }
347  static bool isClear(const ValueT &v) { return v.isClear(); }
348  static void clearConstruct(ValueT *p)
349  {
350  new ((void *)p) ValueT();
351  }
352  static const bool clearNeedsDestruction = false;
353 };
354 } // namespace UT
355 
356 #endif
357 
GLint first
Definition: glcorearb.h:405
Class for reading files.
Definition: FS_Reader.h:33
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2540
PXL_API const char * getDescription(const ColorSpace *space)
Return the description of the color space.
GT_API const UT_StringHolder filename
UT_StringHolder myTextFilePattern
Definition: FS_IndexFile.h:229
UT_Lock myLock
Definition: FS_IndexFile.h:252
GT_API const UT_StringHolder time
const GLdouble * v
Definition: glcorearb.h:837
ConversionFuncs(const char *bin_file_pattern="", const char *text_file_pattern="", ConvertFunc to_text=nullptr, ConvertFunc from_text=nullptr)
Definition: FS_IndexFile.h:202
int64 exint
Definition: SYS_Types.h:125
bool hasRegularSection(const UT_StringRef &section) const
Definition: FS_IndexFile.h:79
static time_t getCurrentTime()
Definition: FS_IndexFile.h:170
GLuint buffer
Definition: glcorearb.h:660
OutGridT const XformOp bool bool
bool isExpanded() const
Tests if the index file is stored as expanded.
Definition: FS_IndexFile.h:183
UT_ArrayStringMap< ConversionFuncs > ConversionList
Definition: FS_IndexFile.h:231
void setModified()
Definition: FS_IndexFile.h:244
GLintptr offset
Definition: glcorearb.h:665
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:36
bool hasTempSection(const UT_StringRef &section) const
Definition: FS_IndexFile.h:77
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:803
long long int64
Definition: SYS_Types.h:116
virtual bool writeFile(const GA_Detail &g, const char *filename, const GA_SaveOptions *opts, UT_StringArray *errors) const
__hostdev__ uint64_t last(uint32_t i) const
Definition: NanoVDB.h:5976
bool hasSection(const UT_StringRef &section) const
Definition: FS_IndexFile.h:74
A map of string to various well defined value types.
Definition: UT_Options.h:84
const UT_StringHolder & getSourceFile() const
Definition: FS_IndexFile.h:146
LeafData & operator=(const LeafData &)=delete
GLuint index
Definition: glcorearb.h:786
#define FS_API
Definition: FS_API.h:10
UT_StringHolder myBinFilePattern
Definition: FS_IndexFile.h:228
GLenum src
Definition: glcorearb.h:1793