File: vfs_lookup.cpp

package info (click to toggle)
0ad 0.28.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 182,352 kB
  • sloc: cpp: 201,989; javascript: 19,730; ansic: 15,057; python: 6,597; sh: 2,046; perl: 1,232; xml: 543; java: 533; makefile: 105
file content (167 lines) | stat: -rw-r--r-- 5,616 bytes parent folder | download | duplicates (2)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* Copyright (C) 2025 Wildfire Games.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/*
 * look up directories/files by traversing path components.
 */

#include "precompiled.h"

#include "vfs_lookup.h"

#include "lib/debug.h"
#include "lib/file/common/real_directory.h"
#include "lib/file/file.h"
#include "lib/file/file_system.h"
#include "lib/file/vfs/vfs.h"	// error codes
#include "lib/file/vfs/vfs_populate.h"
#include "lib/file/vfs/vfs_tree.h"
#include "lib/os_path.h"
#include "lib/path.h"
#include "lib/posix/posix_filesystem.h"
#include "lib/sysdep/filesystem.h"

#include <cerrno>
#include <string>

static Status CreateDirectory(const OsPath& path)
{
	{
		const mode_t mode = S_IRWXU; // 0700 as prescribed by XDG basedir
		const int ret = wmkdir(path, mode);
		if(ret == 0)	// success
			return INFO::OK;
	}

	// Failed because the directory already exists.
	// Return 'success' to attach the existing directory.
	if(errno == EEXIST)
	{
		// But first ensure it's really a directory
		// (otherwise, a file is "in the way" and needs to be deleted).
		struct stat s;
		const int ret = wstat(path, &s);
		ENSURE(ret == 0);	// (wmkdir said it existed)
		ENSURE(S_ISDIR(s.st_mode));
		return INFO::OK;
	}

	if (errno == EACCES)
		return ERR::FILE_ACCESS;

	// unexpected failure
	debug_printf("wmkdir failed with errno=%d\n", errno);
	DEBUG_WARN_ERR(ERR::LOGIC);
	WARN_RETURN(StatusFromErrno());
}


Status vfs_Lookup(const VfsPath& pathname, VfsDirectory* startDirectory, VfsDirectory*& directory, VfsFile** pfile, size_t flags)
{
	// extract and validate flags (ensure no unknown bits are set)
	const bool addMissingDirectories    = (flags & VFS_LOOKUP_ADD) != 0;
	const bool skipPopulate = (flags & VFS_LOOKUP_SKIP_POPULATE) != 0;
	const bool realPath = (flags & VFS_LOOKUP_REAL_PATH) != 0;
	ENSURE((flags & ~(VFS_LOOKUP_ADD|VFS_LOOKUP_SKIP_POPULATE|VFS_LOOKUP_REAL_PATH)) == 0);

	directory = startDirectory;
	if (pfile)
		*pfile = 0;

	if (!skipPopulate)
		RETURN_STATUS_IF_ERR(vfs_Populate(directory));

	// early-out for pathname == "" when mounting into VFS root
	if (pathname.empty())	// (prevent iterator error in loop end condition)
	{
		// Preserve a guarantee that if pfile then we either return an error or set *pfile,
		// and if looking for a real path ensure an associated directory.
		if (pfile || (realPath && !directory->AssociatedDirectory()))
			return ERR::VFS_FILE_NOT_FOUND;
		else
			return INFO::OK;
	}

	// for each directory component:
	size_t pos = 0;	// (needed outside of loop)
	for(;;)
	{
		const size_t nextSlash = pathname.string().find_first_of('/', pos);
		if (nextSlash == VfsPath::String::npos)
			break;
		const VfsPath subdirectoryName = pathname.string().substr(pos, nextSlash-pos);
		pos = nextSlash+1;

		VfsDirectory* subdirectory = directory->GetSubdirectory(subdirectoryName);
		if (!subdirectory)
		{
			if (addMissingDirectories)
				subdirectory = directory->AddSubdirectory(subdirectoryName);
			else
				return ERR::VFS_DIR_NOT_FOUND;	// NOWARN
		}
		// When looking for a real path, we need to keep the path of the highest priority subdirectory.
		// If the current directory has an associated directory, and the subdir does not / is lower priority,
		// we will overwrite it.
		PRealDirectory realDir = directory->AssociatedDirectory();
		if (realPath && realDir &&
		    (!subdirectory->AssociatedDirectory() ||
		    realDir->Priority() > subdirectory->AssociatedDirectory()->Priority()))
		{
			OsPath currentPath = directory->AssociatedDirectory()->Path();
			currentPath = currentPath / subdirectoryName / "";

			// Only actually create the directory if we're in LOOKUP_ADD mode.
			if (addMissingDirectories)
				RETURN_STATUS_IF_ERR(CreateDirectory(currentPath));
			else if (!DirectoryExists(currentPath))
				return ERR::VFS_DIR_NOT_FOUND;

			// Propagate priority and flags to the subdirectory.
			// If it already existed, it will be replaced & the memory freed.
			PRealDirectory realDirectory(new RealDirectory(currentPath,
				realDir ? realDir->Priority() : 0,
				realDir ? realDir->Flags() : 0)
			);
			RETURN_STATUS_IF_ERR(vfs_Attach(subdirectory, realDirectory));
		}

		if (!skipPopulate)
			RETURN_STATUS_IF_ERR(vfs_Populate(subdirectory));

		directory = subdirectory;
	}

	if (realPath && !directory->AssociatedDirectory())
		return ERR::VFS_DIR_NOT_FOUND;

	if (pfile)
	{
		ENSURE(!pathname.IsDirectory());
		const VfsPath filename = pathname.string().substr(pos);
		*pfile = directory->GetFile(filename);
		if (!*pfile)
			return ERR::VFS_FILE_NOT_FOUND;	// NOWARN
	}

	return INFO::OK;
}