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