lldiriterator.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * @file lldiriterator.h
  3. * @brief Definition of directory iterator class
  4. *
  5. * $LicenseInfo:firstyear=2010&license=viewergpl$
  6. *
  7. * Copyright (c) 2010, Linden Research, Inc. (c) 2021 Henri Beauchamp.
  8. *
  9. * Modifications by Henri Beauchamp:
  10. * - Allow a simple iterator without matching pattern.
  11. * - Allow iterating on entries that do *not* match the given pattern.
  12. * - Allow to return sundry information for each found entry.
  13. * - Added LLDirIterator::deleteFilesInDir().
  14. * - Added LLDirIterator::deleteRecursivelyInDir().
  15. * - Proper catching of throw()s and boost::filesystem errors.
  16. * - Got rid of boost::regex in favour of std::regex since we now use C++11.
  17. * - Added support for iterating on logical drives (when passed an empty
  18. * path), under Windows.
  19. *
  20. * Second Life Viewer Source Code
  21. * The source code in this file ("Source Code") is provided by Linden Lab
  22. * to you under the terms of the GNU General Public License, version 2.0
  23. * ("GPL"), unless you have obtained a separate licensing agreement
  24. * ("Other License"), formally executed by you and Linden Lab. Terms of
  25. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  26. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  27. *
  28. * There are special exceptions to the terms and conditions of the GPL as
  29. * it is applied to this Source Code. View the full text of the exception
  30. * in the file doc/FLOSS-exception.txt in this software distribution, or
  31. * online at
  32. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  33. *
  34. * By copying, modifying or distributing this software, you acknowledge
  35. * that you have read and understood your obligations described above,
  36. * and agree to abide by those obligations.
  37. *
  38. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  39. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  40. * COMPLETENESS OR PERFORMANCE.
  41. * $/LicenseInfo$
  42. */
  43. #ifndef LL_LLDIRITERATOR_H
  44. #define LL_LLDIRITERATOR_H
  45. #include <string>
  46. #include <time.h> // For time_t
  47. #include "llerror.h"
  48. // Information requested for each entry, as a bitmap
  49. enum
  50. {
  51. DI_NONE = 0,
  52. DI_ISFILE = 1 << 0, // Regular file (non-directory, non-link)
  53. DI_ISDIR = 1 << 1, // Directory (maybe a link to a directory)
  54. DI_ISLINK = 1 << 2, // Symbolic link (to a file or directory)
  55. DI_ISHIDDEN = 1 << 3, // Hidden file or directory
  56. DI_SIZE = 1 << 4, // File size
  57. DI_TIMESTAMP = 1 << 5, // Last modified time stamp
  58. DI_ALL = ~0
  59. };
  60. // Iterates through directory entries.
  61. class LLDirIterator
  62. {
  63. protected:
  64. LOG_CLASS(LLDirIterator);
  65. public:
  66. // Directory iterator with optional global pattern matching, and file info
  67. // retrieval.
  68. // Wildcards supported in 'mask':
  69. // --------------------------------------------------------------
  70. // | Wildcard | Matches |
  71. // --------------------------------------------------------------
  72. // | * | zero or more characters |
  73. // | ? | exactly one character |
  74. // | [abcde] | exactly one character listed |
  75. // | [a-e] | exactly one character in the given range |
  76. // | [!abcde] | any character that is not listed |
  77. // | [!a-e] | any character that is not in the given range |
  78. // | {abc,xyz} | exactly one entire word in the options given |
  79. // --------------------------------------------------------------
  80. // When 'mask' is ommitted or empty, the iterator becomes a simple one,
  81. // without pattern matching (i.e. all the entries in the directory are
  82. // returned in sequence by next()).
  83. // 'requested_info' is an optionnal bitmap using the flags in the above
  84. // enum.
  85. LLDirIterator(const std::string& dirname, const char* mask = NULL,
  86. U32 requested_info = DI_NONE);
  87. ~LLDirIterator();
  88. LL_INLINE bool isValid() const { return mImpl != NULL; }
  89. LL_INLINE const std::string& getPath() const { return mDirPath; }
  90. // Search for the next matching entry, returning true when a match is
  91. // found, with the matching entry name returned in 'name'.
  92. // When 'not_matching' is set to true, the method returns the next entry
  93. // that does *not* match the glob pattern (which must have been given in
  94. // this case, 'not_matching' being ignored when no pattern was given).
  95. bool next(std::string& name, bool not_matching = false);
  96. // Info for the last matching entry, only usable when the corresponding
  97. // flag was set in the constructor, via 'requested_info'. Trying to use one
  98. // of these methods when the corresponding flag was not set results in an
  99. // llerrs.
  100. bool isFile() const;
  101. bool isDirectory() const;
  102. bool isLink() const;
  103. bool isHidden() const;
  104. size_t getSize() const; // Always returns 0 for non-regular files
  105. time_t getTimeStamp() const; // "Last modified" time stamp
  106. // Utility method to replace the one that was formerly available from LLDir
  107. // via the old (and slow) getNextFileInDir() iteration mechanism. It also
  108. // replaces LLDir::deleteAllNonDirFilesInDir(), when you omit the 'mask'
  109. // parameter (or pass an empty string for it). As a bonus (compared with
  110. // the old LLDir methods), when 'not_matching' is set to true, the method
  111. // deletes all the *files* that do *not* match the glob pattern (which must
  112. // have been given in this case, else it is a no-operation), but still
  113. // delete *symbolic links* matching the pattern.
  114. // Returns the number of deleted files.
  115. static U32 deleteFilesInDir(const std::string& dirname,
  116. const char* mask = NULL,
  117. bool not_matching = false);
  118. // Same as above, but deletes all files in all sub-directories recursively.
  119. // The sub-directories themselves are also removed (when empty, which may
  120. // not be always the case when 'not_matching' is true).
  121. static U32 deleteRecursivelyInDir(const std::string& dirname,
  122. const char* mask = NULL,
  123. bool not_matching = false);
  124. private:
  125. class Impl;
  126. Impl* mImpl;
  127. std::string mDirPath;
  128. };
  129. #endif // LL_LLDIRITERATOR_H