Skip to content

Commit 5d3dd64

Browse files
lambdageekmarek-safar
authored andcommitted
[utils] Add mono_path_filename_in_basedir
1 parent efe65c3 commit 5d3dd64

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

mono/utils/mono-path.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,27 @@ mono_path_resolve_symlinks (const char *path)
177177
#endif
178178
}
179179

180+
/**
181+
* mono_path_filename_in_basedir:
182+
*
183+
* Return \c TRUE if \p filename is in \p basedir
184+
*
185+
* Both paths must be absolute and using \c G_DIR_SEPARATOR for directory separators.
186+
* If the file is in a subdirectory of \p basedir, returns \c FALSE.
187+
* This function doesn't touch a filesystem, it looks solely at path names.
188+
*/
189+
gboolean
190+
mono_path_filename_in_basedir (const char *filename, const char *basedir)
191+
{
192+
char *p = NULL;
193+
if ((p = strstr (filename, basedir))) {
194+
p += strlen (basedir);
195+
if (*p != G_DIR_SEPARATOR)
196+
return FALSE;
197+
/* if it's in a subdir of the basedir, it doesn't count. */
198+
if (strchr (p, G_DIR_SEPARATOR))
199+
return FALSE;
200+
return TRUE;
201+
}
202+
return FALSE;
203+
}

mono/utils/mono-path.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
MONO_API gchar *mono_path_resolve_symlinks (const char *path);
1212
MONO_API gchar *mono_path_canonicalize (const char *path);
13+
gboolean mono_path_filename_in_basedir (const char *filename, const char *basedir);
1314

1415
#endif /* __MONO_PATH_H */
1516

0 commit comments

Comments
 (0)