Welcome to mirror list, hosted at ThFree Co, Russian Federation.

scantree.hpp « src « far2l - github.com/elfmz/far2l.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3fdda91b20a72abb215f349be35bf70e63e2b971 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#pragma once

/*
scantree.hpp

Сканирование текущего каталога и, опционально, подкаталогов на
предмет имен файлов
*/
/*
Copyright (c) 1996 Eugene Roshal
Copyright (c) 2000 Far Group
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. The name of the authors may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#include "bitflags.hpp"
#include "array.hpp"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <set>
#include <list>
#include <WinCompat.h>
#include "FARString.hpp"


enum
{
	// FSCANTREE_RETUPDIR causes GetNextName() to return every directory twice:
	// 1. when scanning its parent directory 2. after directory scan is finished
	FSCANTREE_RETUPDIR         = FRS_RETUPDIR,
	FSCANTREE_RECUR            = FRS_RECUR, // enable recursive scan
	FSCANTREE_SCANSYMLINK      = FRS_SCANSYMLINK, // enable scanning symlinks

	// в младшем слове старшие 8 бита служебные!
	FSCANTREE_SECONDDIRNAME    = 0x00004000, // set when FSCANTREE_RETUPDIR is enabled and directory scan is finished

	// здесь те флаги, которые могут выставляться в 3-м параметре SetFindPath()
	FSCANTREE_FILESFIRST       = 0x00010000, // Сканирование каталга за два прохода. Сначала файлы, затем каталоги
	FSCANTREE_NOFILES          = 0x00020000, // Don't return files
	FSCANTREE_NODEVICES        = 0x00040000, // Don't return devices
	FSCANTREE_NOLINKS          = 0x00080000, // Don't return symlinks
	FSCANTREE_CASE_INSENSITIVE = 0x00100000 // Currently affects only english characters
};

class ScannedINodes
{
	std::set< std::pair<uint64_t, uint64_t> > _s;

public:
	inline bool Put(uint64_t d, uint64_t ino)
	{
		return _s.emplace(ino, d).second;
	}
};

class ScanTree
{
		BitFlags Flags;
		std::wstring strFindPath;
		std::wstring strFindMask;

		struct ScanDir
		{
			std::unique_ptr<FindFile> Enumer;
			std::list<FAR_FIND_DATA_EX> Postponed;
			FARString RealPath;
			bool InsideSymlink = false;

			bool GetNext(FAR_FIND_DATA_EX *fdata, bool FilesFirst);
		};
		std::list<ScanDir> ScanDirStack;

		void CheckForEnterSubdir(const FAR_FIND_DATA_EX *fdata);
		void StartEnumSubdir();
		void LeaveSubdir();

	public:
		ScanTree(int RetUpDir, int Recurse = 1, int ScanJunction = -1);

		// 3-й параметр - флаги из старшего слова
		void SetFindPath(const wchar_t *Path,const wchar_t *Mask, const DWORD NewScanFlags = FSCANTREE_FILESFIRST);
		bool GetNextName(FAR_FIND_DATA_EX *fdata, FARString &strFullName);

		void SkipDir();

		bool IsDirSearchDone() const { return Flags.Check(FSCANTREE_SECONDDIRNAME); };
		bool IsInsideSymlink() const { return !ScanDirStack.empty() && ScanDirStack.back().InsideSymlink;};
		bool IsSymlinksScanEnabled() const { return Flags.Check(FSCANTREE_SCANSYMLINK); }
};