Add More Base Path Helpers to DeviceFiles

(This is a merge of http://go/wvgerrit/13910/ from the Widevine CDM
repository.)

DeviceFiles has a lot of repeated code whenever it needs to check for
file existence or remove a file. When reading and writing files, it
has wrappers that handle this repeated burden. This change adds
wrappers for the other functionality used by DeviceFiles as well, to
reduce duplication.

Change-Id: If959b504672c1b907346d28f31648d8028de8bdf
This commit is contained in:
John "Juce" Bruce
2015-03-31 15:40:27 -07:00
parent 54f3b6d376
commit c66badec60
2 changed files with 87 additions and 50 deletions

View File

@@ -3,6 +3,8 @@
#ifndef WVCDM_CORE_DEVICE_FILES_H_
#define WVCDM_CORE_DEVICE_FILES_H_
#include <unistd.h>
#include "scoped_ptr.h"
#include "wv_cdm_types.h"
@@ -81,9 +83,14 @@ class DeviceFiles {
CdmKeyResponse* license_response);
private:
// Helpers that wrap the File interface and automatically handle hashing, as
// well as adding the device files base path to to the file name.
bool StoreFileWithHash(const char* name, const std::string& serialized_file);
bool StoreFileRaw(const char* name, const std::string& serialized_file);
bool RetrieveHashedFile(const char* name, std::string* serialized_file);
bool FileExists(const char* name);
bool RemoveFile(const char* name);
ssize_t GetFileSize(const char* name);
// Certificate and offline licenses are now stored in security
// level specific directories. In an earlier version they were

View File

@@ -35,6 +35,7 @@ const char kCertificateFileName[] = "cert.bin";
const char kUsageInfoFileNamePrefix[] = "usage";
const char kUsageInfoFileNameExt[] = ".bin";
const char kLicenseFileNameExt[] = ".lic";
const char kEmptyFileName[] = "";
const char kWildcard[] = "*";
const char kDirectoryDelimiter = '/';
const char* kSecurityLevelPathCompatibilityExclusionList[] = {"ay64.dat"};
@@ -268,15 +269,9 @@ bool DeviceFiles::DeleteLicense(const std::string& key_set_id) {
return false;
}
std::string path;
if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) {
LOGW("DeviceFiles::DeleteLicense: Unable to get base path");
return false;
}
path.append(key_set_id);
path.append(kLicenseFileNameExt);
return file_->Remove(path);
std::string file_name(key_set_id);
file_name.append(kLicenseFileNameExt);
return RemoveFile(file_name.c_str());
}
bool DeviceFiles::DeleteAllLicenses() {
@@ -285,15 +280,9 @@ bool DeviceFiles::DeleteAllLicenses() {
return false;
}
std::string path;
if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) {
LOGW("DeviceFiles::DeleteAllLicenses: Unable to get base path");
return false;
}
path.append(kWildcard);
path.append(kLicenseFileNameExt);
return file_->Remove(path);
std::string file_name(kWildcard);
file_name.append(kLicenseFileNameExt);
return RemoveFile(file_name.c_str());
}
bool DeviceFiles::DeleteAllFiles() {
@@ -302,13 +291,9 @@ bool DeviceFiles::DeleteAllFiles() {
return false;
}
std::string path;
if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) {
LOGW("DeviceFiles::DeleteAllFiles: Unable to get base path");
return false;
}
return file_->Remove(path);
// We pass an empty string to RemoveFile to delete the device files base
// directory itself.
return RemoveFile(kEmptyFileName);
}
bool DeviceFiles::LicenseExists(const std::string& key_set_id) {
@@ -317,15 +302,9 @@ bool DeviceFiles::LicenseExists(const std::string& key_set_id) {
return false;
}
std::string path;
if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) {
LOGW("DeviceFiles::LicenseExists: Unable to get base path");
return false;
}
path.append(key_set_id);
path.append(kLicenseFileNameExt);
return file_->Exists(path);
std::string file_name(key_set_id);
file_name.append(kLicenseFileNameExt);
return FileExists(file_name.c_str());
}
bool DeviceFiles::ReserveLicenseId(const std::string& key_set_id) {
@@ -429,14 +408,9 @@ bool DeviceFiles::DeleteAllUsageInfoForApp(
return false;
}
provider_session_tokens->clear();
std::string path;
if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) {
LOGW("DeviceFiles::DeleteAllUsageInfoForApp: Unable to get base path");
return false;
}
std::string file_name = GetUsageInfoFileName(app_id);
path.append(file_name);
if (!file_->Exists(path)) return true;
if (!FileExists(file_name.c_str())) return true;
std::string serialized_file;
if (RetrieveHashedFile(file_name.c_str(), &serialized_file)) {
video_widevine_client::sdk::File file_proto;
@@ -451,7 +425,7 @@ bool DeviceFiles::DeleteAllUsageInfoForApp(
} else {
LOGW("DeviceFiles::DeleteAllUsageInfoForApp: Unable to retrieve file");
}
return file_->Remove(path);
return RemoveFile(file_name.c_str());
}
bool DeviceFiles::RetrieveUsageInfo(
@@ -472,14 +446,7 @@ bool DeviceFiles::RetrieveUsageInfo(
std::string serialized_file;
std::string file_name = GetUsageInfoFileName(app_id);
if (!RetrieveHashedFile(file_name.c_str(), &serialized_file)) {
std::string path;
if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) {
return false;
}
path += file_name;
if (!file_->Exists(path) || 0 == file_->FileSize(path)) {
if (!FileExists(file_name.c_str()) || GetFileSize(file_name.c_str()) == 0) {
usage_info->resize(0);
return true;
}
@@ -696,6 +663,69 @@ bool DeviceFiles::RetrieveHashedFile(const char* name,
return true;
}
bool DeviceFiles::FileExists(const char* name) {
if (!file_.get()) {
LOGW("DeviceFiles::FileExists: Invalid file handle");
return false;
}
if (!name) {
LOGW("DeviceFiles::FileExists: Unspecified file name parameter");
return false;
}
std::string path;
if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) {
LOGW("DeviceFiles::FileExists: Unable to get base path");
return false;
}
path += name;
return file_->Exists(path);
}
bool DeviceFiles::RemoveFile(const char* name) {
if (!file_.get()) {
LOGW("DeviceFiles::RemoveFile: Invalid file handle");
return false;
}
if (!name) {
LOGW("DeviceFiles::RemoveFile: Unspecified file name parameter");
return false;
}
std::string path;
if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) {
LOGW("DeviceFiles::RemoveFile: Unable to get base path");
return false;
}
path += name;
return file_->Remove(path);
}
ssize_t DeviceFiles::GetFileSize(const char* name) {
if (!file_.get()) {
LOGW("DeviceFiles::GetFileSize: Invalid file handle");
return -1;
}
if (!name) {
LOGW("DeviceFiles::GetFileSize: Unspecified file name parameter");
return -1;
}
std::string path;
if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) {
LOGW("DeviceFiles::GetFileSize: Unable to get base path");
return -1;
}
path += name;
return file_->FileSize(path);
}
void DeviceFiles::SecurityLevelPathBackwardCompatibility() {
std::string path;
if (!Properties::GetDeviceFilesBasePath(security_level_, &path)) {