Skip to content

Commit

Permalink
Merge pull request #528 from npoltorapavlo/RDK-30538
Browse files Browse the repository at this point in the history
parse server.document-root
  • Loading branch information
bobseamon authored Dec 3, 2020
2 parents 84f033c + dee7d8f commit 46f9a7a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
66 changes: 61 additions & 5 deletions UsbAccess/UsbAccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const string WPEFramework::Plugin::UsbAccess::METHOD_GET_FILE_LIST = "getFileLis
const string WPEFramework::Plugin::UsbAccess::METHOD_CREATE_LINK = "createLink";
const string WPEFramework::Plugin::UsbAccess::METHOD_CLEAR_LINK = "clearLink";
const string WPEFramework::Plugin::UsbAccess::USB_MOUNT_PATH = "/run/media/sda"; // platco
const string WPEFramework::Plugin::UsbAccess::LINK_PATH = "/opt/www/usbdrive";
const string WPEFramework::Plugin::UsbAccess::LINK_URL_HTTP = "http://localhost:50050/usbdrive";
const string WPEFramework::Plugin::UsbAccess::LIGHTTPD_CONF_PATH = "/etc/lighttpd/lighttpd.conf";

using namespace std;

Expand Down Expand Up @@ -126,11 +126,12 @@ namespace WPEFramework {
LOGINFOMETHOD();

bool success = false;
int rc = symlink(USB_MOUNT_PATH.c_str(), LINK_PATH.c_str());
string linkPath = getLinkPath();
int rc = symlink(USB_MOUNT_PATH.c_str(), linkPath.c_str());

if (0 == rc)
{
LOGINFO("symlink %s created", LINK_PATH.c_str());
LOGINFO("symlink %s created", linkPath.c_str());
response["baseURL"] = LINK_URL_HTTP;
success = true;
}
Expand All @@ -148,11 +149,12 @@ namespace WPEFramework {
LOGINFOMETHOD();

bool success = false;
int rc = remove(LINK_PATH.c_str());
string linkPath = getLinkPath();
int rc = remove(linkPath.c_str());

if (0 == rc)
{
LOGINFO("symlink %s removed", LINK_PATH.c_str());
LOGINFO("symlink %s removed", linkPath.c_str());
success = true;
}
else
Expand All @@ -163,5 +165,59 @@ namespace WPEFramework {

returnResponse(success);
}

string UsbAccess::getLinkPath() const
{
static string path;

static bool initedOnce = false;
if (!initedOnce)
{
FILE* f = fopen(LIGHTTPD_CONF_PATH.c_str(), "r");
if (f != nullptr)
{
std::vector<char> buf;
buf.resize(1024);

while (fgets(buf.data(), buf.size(), f))
{
if (strstr(buf.data(), "server.document-root") == buf.data())
{
std::string s(buf.data());

size_t begin = s.find_first_of('"');
size_t end = std::string::npos;
if (std::string::npos != begin)
end = s.find_first_of('"', begin + 2);

if (std::string::npos != begin && std::string::npos != end)
{
path = s.substr(begin + 1, end - begin - 1);
break;
}
else
LOGERR("Failed to parse line: ", s.c_str());
}
}
fclose(f);

if (path.empty())
LOGERR("Failed to parse conf %s", LIGHTTPD_CONF_PATH.c_str());
else
{
if (path[path.size() - 1] != '/')
path += "/";
path += "usbdrive";
LOGINFO("Link path is: %s", path.c_str());
}
}
else
LOGERR("Failed to open conf %s:%s", LIGHTTPD_CONF_PATH.c_str(), strerror(errno));

initedOnce = true;
}

return path;
}
} // namespace Plugin
} // namespace WPEFramework
4 changes: 3 additions & 1 deletion UsbAccess/UsbAccess.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ namespace WPEFramework {
//events
//other
static const string USB_MOUNT_PATH;
static const string LINK_PATH;
static const string LINK_URL_HTTP;
static const string LIGHTTPD_CONF_PATH;

private/*registered methods (wrappers)*/:

Expand All @@ -43,6 +43,8 @@ namespace WPEFramework {
private/*internal methods*/:
UsbAccess(const UsbAccess&) = delete;
UsbAccess& operator=(const UsbAccess&) = delete;

string getLinkPath() const;
};
} // namespace Plugin
} // namespace WPEFramework

0 comments on commit 46f9a7a

Please sign in to comment.