Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
FFMG committed Jul 31, 2016
2 parents 47bc3d1 + a24ea4a commit 3ec80e7
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 45 deletions.
5 changes: 5 additions & 0 deletions monitor/ActionMonitor/Action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "activebatchaction.h"
#include "activecmdaction.h"
#include "activecomaction.h"
#include "activeexeaction.h"

#include "os\os.h"
#include "ActivePowershellAction.h"
Expand Down Expand Up @@ -463,6 +464,10 @@ ActiveAction* Action::CreateActiveActionDirect(CWnd* pWnd, const MYODD_STRING& s
{
return new ActiveComAction(*this, hTopHWnd, szCommandLine);
}
if (myodd::files::IsExtension(m_szFile, _T("exe")))
{
return new ActiveExeAction(*this, hTopHWnd, szCommandLine, isPrivileged);
}

// run the default action.
return new ActiveDefaultAction( *this, hTopHWnd, szCommandLine, isPrivileged );
Expand Down
2 changes: 2 additions & 0 deletions monitor/ActionMonitor/ActionMonitor.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@
<ClCompile Include="ActiveCmdAction.cpp" />
<ClCompile Include="ActiveComAction.cpp" />
<ClCompile Include="ActiveDefaultAction.cpp" />
<ClCompile Include="ActiveExeAction.cpp" />
<ClCompile Include="ActiveLuaAction.cpp" />
<ClCompile Include="ActivePluginAction.cpp" />
<ClCompile Include="ActivePowershellAction.cpp" />
Expand Down Expand Up @@ -249,6 +250,7 @@
<ClInclude Include="ActiveCmdAction.h" />
<ClInclude Include="ActiveComAction.h" />
<ClInclude Include="ActiveDefaultAction.h" />
<ClInclude Include="ActiveExeAction.h" />
<ClInclude Include="ActiveLuaAction.h" />
<ClInclude Include="ActivePluginAction.h" />
<ClInclude Include="ActivePowershellAction.h" />
Expand Down
6 changes: 6 additions & 0 deletions monitor/ActionMonitor/ActionMonitor.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@
<ClCompile Include="ActiveComAction.cpp">
<Filter>actions</Filter>
</ClCompile>
<ClCompile Include="ActiveExeAction.cpp">
<Filter>actions</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="ActionMonitor.rc">
Expand Down Expand Up @@ -265,6 +268,9 @@
<ClInclude Include="ActiveComAction.h">
<Filter>actions</Filter>
</ClInclude>
<ClInclude Include="ActiveExeAction.h">
<Filter>actions</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Image Include="res\ActionMonitor.ico">
Expand Down
2 changes: 2 additions & 0 deletions monitor/ActionMonitor/ActionMonitor64.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@
<ClCompile Include="ActiveCmdAction.cpp" />
<ClCompile Include="ActiveComAction.cpp" />
<ClCompile Include="ActiveDefaultAction.cpp" />
<ClCompile Include="ActiveExeAction.cpp" />
<ClCompile Include="ActiveLuaAction.cpp" />
<ClCompile Include="ActivePluginAction.cpp" />
<ClCompile Include="ActivePowershellAction.cpp" />
Expand Down Expand Up @@ -248,6 +249,7 @@
<ClInclude Include="ActiveCmdAction.h" />
<ClInclude Include="ActiveComAction.h" />
<ClInclude Include="ActiveDefaultAction.h" />
<ClInclude Include="ActiveExeAction.h" />
<ClInclude Include="ActiveLuaAction.h" />
<ClInclude Include="ActivePluginAction.h" />
<ClInclude Include="ActivePowershellAction.h" />
Expand Down
6 changes: 6 additions & 0 deletions monitor/ActionMonitor/ActionMonitor64.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@
<ClCompile Include="ActiveComAction.cpp">
<Filter>actions</Filter>
</ClCompile>
<ClCompile Include="ActiveExeAction.cpp">
<Filter>actions</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="ActionMonitor.rc">
Expand Down Expand Up @@ -262,6 +265,9 @@
<ClInclude Include="ActiveComAction.h">
<Filter>actions</Filter>
</ClInclude>
<ClInclude Include="ActiveExeAction.h">
<Filter>actions</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Image Include="res\ActionMonitor.ico">
Expand Down
43 changes: 43 additions & 0 deletions monitor/ActionMonitor/ActiveExeAction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "stdafx.h"
#include "ActiveExeAction.h"
#include "ActionMonitor.h"

/**
* The Default active contructor
* @param const Action& src the action that is now active.
* @param HWND hTopHWnd the window that was on top at the time the command was given.
* @param const MYODD_STRING& szCommandLine the given command line that is, the words after the command itself
* @param bool isPrivileged if this action is privileged or not.
*/
ActiveExeAction::ActiveExeAction(const Action& src, HWND hTopHWnd, const MYODD_STRING& szCommandLine, bool isPrivileged) :
ActiveAction( src, hTopHWnd, szCommandLine, isPrivileged )
{
}

ActiveExeAction::~ActiveExeAction()
{
}

bool ActiveExeAction::OnDeInitialize()
{
// nothing to do.
return true;
}

bool ActiveExeAction::OnInitialize()
{
// all good
return true;
}

void ActiveExeAction::OnExecuteInThread()
{
auto szFile = File();
auto szExt = myodd::files::GetExtension( szFile );;
auto szCommand = Command();

// we just launch the exe by itself.
std::vector<MYODD_STRING> argv;
argv.push_back( szFile);
Execute(argv, IsPrivileged(), nullptr );
}
15 changes: 15 additions & 0 deletions monitor/ActionMonitor/ActiveExeAction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once
#include "ActiveAction.h"

class ActiveExeAction :
public ActiveAction
{
public:
ActiveExeAction(const Action& src, HWND hTopHWnd, const MYODD_STRING& szCommandLine, bool isPrivileged);
virtual ~ActiveExeAction();

protected:
virtual bool OnInitialize();
virtual bool OnDeInitialize();
virtual void OnExecuteInThread();
};
12 changes: 6 additions & 6 deletions monitor/ActionMonitor/res/version.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#include <tchar.h>

#define VERSION_MAJOR 0
#define VERSION_MINOR 6
#define VERSION_MAINTENANCE 16
#define VERSION_BUILD 14
#define VERSION_MINOR 7
#define VERSION_MAINTENANCE 1
#define VERSION_BUILD 4

#define VERSION_HOUR 9
#define VERSION_MINUTE 38
#define VERSION_HOUR 8
#define VERSION_MINUTE 40

#define VERSION_YEAR 2016
#define VERSION_MONTH 7
#define VERSION_DAY 28
#define VERSION_DAY 31

//
// No need to edit below...
Expand Down
102 changes: 66 additions & 36 deletions myodd/files/files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1263,22 +1263,20 @@ MYODD_STRING GetBaseFromFile
}

/**
* Get the absolute path given a relative path.
* It is up to the user to delete the container value.
* @param MYODD_STRING& container for the return value, unset in case of error.
* @param const MYODD_STRING& the relative path we would like to get the absolute path from.
* @param const MYODD_STRING& the original path, if empty we will use the current directory as origin.
* @return bool if we were able to get the absolute path, false if the given path is unrealistic/impossible.
*/
* Get the absolute path given a relative path.
* It is up to the user to delete the container value.
* @param MYODD_STRING& container for the return value, unset in case of error.
* @param const MYODD_STRING& the relative path we would like to get the absolute path from.
* @param const MYODD_STRING& the original path, if empty we will use the current directory as origin.
* if the path is '.' or '.\xxx' the current path will be added to it.
* @return bool if we were able to get the absolute path, false if the given path is unrealistic/impossible.
*/
bool GetAbsolutePath( MYODD_STRING& dest, const MYODD_STRING& givenRelative, const MYODD_STRING& givenOrigin )
{
//
// first do some house-keeping
//

// reset the result.
dest.clear();

// make copies.
auto copyOfRelative = givenRelative;
auto copyOfOrigin = givenOrigin;
Expand All @@ -1299,37 +1297,51 @@ bool GetAbsolutePath( MYODD_STRING& dest, const MYODD_STRING& givenRelative, con
// makes it easier if we are adding the working directory.
files::AddTrailingBackSlash(copyOfOrigin);

// there is a special case for the origin been '.\' or '.'
// in this case we want to assume that the user wants the 'current directory'
regex::Regex2::Matches matches;
// save the unexpanded variables.
auto unExpandedCopyOfOrigin = copyOfOrigin;
auto unExpandedCopyOfRelative = copyOfRelative;

// expand the relative path.
files::ExpandEnvironment(copyOfRelative, copyOfRelative);

// assume we will not be using the origin, (the path given was good enough).
auto useOrigin = false;

MYODD_STRING pathToEvaluate;

// ^((?:\.[\/\\])+)
const auto pattern = _T("^((?:\\.[\\/\\\\])+)");
if (regex::Regex2::Match(pattern, copyOfOrigin, matches ) > 1 )
// (?:\\\\[^.]|[a-zA-Z]:[\\\/])
if (!regex::Regex2::Search(_T("(?:\\\\\\\\[^.]|[a-zA-Z]:[\\\\\\/])"), copyOfRelative))
{
// because we added a trailing back slash earlier
// we can replace the all the '././././' with the current directory
// even if the user just gave us a '.', (as it would be converted to '.'
// but if they gave us '.aaa' then this is not valid and would not be replace anyway.
MYODD_CHAR lpBuffer[T_MAX_PATH] = { 0 };
if (0 == ::GetCurrentDirectory(T_MAX_PATH, lpBuffer))
// we will be using the origin hrere.
useOrigin = true;

// we need to expand the origine now.
files::ExpandEnvironment(copyOfOrigin, copyOfOrigin);

// there is a special case for the origin been '.\' or '.'
// in this case we want to assume that the user wants the 'current directory'
regex::Regex2::Matches matches;

// if the origin has a '.' or './' then we need to add the current path to it.
// that's an assumption made to make the life of the caller easier.
// ^((?:\.[\/\\])+)
const auto pattern = _T("^((?:\\.[\\/\\\\])+)");
if (regex::Regex2::Match(pattern, copyOfOrigin, matches) > 1)
{
// could not get the current directory???
return false;
// we can replace the all the '././././' with the
// current directory even if the user just gave us a '.'

// join the current directory and the origin without the '.' or './'.
files::Join(copyOfOrigin, GetAppPath(true), copyOfOrigin.substr(matches[1].length()));
}

// join the current directory and the remainder.
files::Join( copyOfOrigin, lpBuffer, copyOfOrigin.substr( matches[1].length()));
// we can now join the origin and relative path.
files::Join(pathToEvaluate, copyOfOrigin, copyOfRelative);
}
else
{
pathToEvaluate = copyOfRelative;
}

// we can now join them both
MYODD_STRING pathToEvaluate;
files::Join(pathToEvaluate, copyOfOrigin, copyOfRelative);

// Expand the environment variable.
// but we save the 'before' value.
const auto unexpandedPathToEvaluate = pathToEvaluate;
ExpandEnvironment(pathToEvaluate, pathToEvaluate);

// now we need to get the absolute path of both of them.
std::vector<MYODD_STRING> partsOfPathToEvaluate;
Expand All @@ -1351,6 +1363,7 @@ bool GetAbsolutePath( MYODD_STRING& dest, const MYODD_STRING& givenRelative, con
if( evaluatedParts.size() == 0 )
{
// nope, we cannot go back onto ourselves.
dest.clear();
return false;
}

Expand All @@ -1376,10 +1389,27 @@ bool GetAbsolutePath( MYODD_STRING& dest, const MYODD_STRING& givenRelative, con

// now check if we need to unexpand the data.
// this is where we use the before expand value.
if (0 != strings::Compare(pathToEvaluate, unexpandedPathToEvaluate, false))
if (0 != strings::Compare(copyOfOrigin, unExpandedCopyOfOrigin, false) ||
(0 != strings::Compare(copyOfRelative, unExpandedCopyOfRelative, false))
)
{
files::UnExpandEnvironment(dest, dest);
}

// was the original file UNC?
// or, if we did not use the original, was the relative path UNC
if (useOrigin)
{
if (myodd::regex::Regex2::Search(_T("(?:\\\\{2})"), unExpandedCopyOfOrigin))
{
dest = _T("\\\\") + dest;
}
}
else if (myodd::regex::Regex2::Search(_T("(?:\\\\{2})"), unExpandedCopyOfRelative))
{
dest = _T("\\\\") + dest;
}

// success!
return true;
}
Expand Down
53 changes: 50 additions & 3 deletions myoddtest/files/testfiles_absolutepath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ struct MyOddFilesAbsolutePath : testing::Test, testing::WithParamInterface<test_

TEST_P(MyOddFilesAbsolutePath, TestAbsolutePath)
{
auto relative = GetParam().relative;
auto origin = GetParam().origin;
auto result = GetParam().result;
const auto relative = GetParam().relative;
const auto origin = GetParam().origin;
const auto result = GetParam().result;

// copy them
auto relative1 = relative;
auto origin1 = origin;
auto relative2 = relative;
auto origin2 = origin;

MYODD_STRING dest;
ASSERT_EQ(result, myodd::files::GetAbsolutePath(dest, relative, origin ));
Expand All @@ -35,6 +41,27 @@ TEST_P(MyOddFilesAbsolutePath, TestAbsolutePath)
auto expected = GetParam().expected;
ASSERT_EQ(expected, dest);
}
else
{
// false should clear the result.
ASSERT_EQ(L"", dest);
}

// now test that the destination is set to whatever but 'relative'/'origin' are not changed.
ASSERT_EQ(result, myodd::files::GetAbsolutePath(relative1, relative1, origin1));

// nothing should have changed origin
ASSERT_EQ(origin1, origin2);
if (result)
{
auto expected = GetParam().expected;
ASSERT_EQ(expected, relative1);
}
else
{
// false should clear the result.
ASSERT_EQ(L"", relative1 );
}
}

INSTANTIATE_TEST_CASE_P(TestAbsolutePath, MyOddFilesAbsolutePath,
Expand Down Expand Up @@ -76,6 +103,26 @@ INSTANTIATE_TEST_CASE_P(TestVariousEdgeCases, MyOddFilesAbsolutePath,
test_absolutepath{ L"\\Test\\", L"..\\somewhere\\", L"", false }
));

INSTANTIATE_TEST_CASE_P(TestEdgecases, MyOddFilesAbsolutePath,
testing::Values(
// origins are ignore.s
test_absolutepath{ L"c:/somefile.txt", L"c:\\dira\\dirb\\", L"c:\\somefile.txt", true },
test_absolutepath{ L"c:\\somefile.txt", L"c:\\dira\\dirb\\", L"c:\\somefile.txt", true },
// still has to correct the ..\..\ inside the path
test_absolutepath{ L"c:\\dira\\dirb\\..\\..\\dirc\\somefile.txt", L"c:\\dira\\dirb\\", L"c:\\dirc\\somefile.txt", true },
// test UNC path
test_absolutepath{ L"\\\\dira\\dirb\\..\\..\\dirc\\somefile.txt", L"c:\\dira\\dirb\\", L"\\\\dirc\\somefile.txt", true }
));

INSTANTIATE_TEST_CASE_P(TestUNC, MyOddFilesAbsolutePath,
testing::Values(
// origins are ignored.
test_absolutepath{ L"\\\\somefile.txt", L"c:\\dira\\dirb\\", L"\\\\somefile.txt", true },
test_absolutepath{ L"..\\dira\\dirb", L"\\\\dirx\\diry\\", L"\\\\dirx\\dira\\dirb", true },
// still has to correct the ..\..\ inside the path
test_absolutepath{ L"\\\\dira\\dirb\\..\\..\\dirc\\somefile.txt", L"c:\\dira\\dirb\\", L"\\\\dirc\\somefile.txt", true }
));

TEST(TestAbsolutePathExpandValues, TestExpendingValues )
{
MYODD_STRING exp;
Expand Down

0 comments on commit 3ec80e7

Please sign in to comment.