bool RemoveDirectoryIfExists(const char* path) { AWS_LOGSTREAM_INFO(FILE_SYSTEM_UTILS_LOG_TAG, "Removing directory at " << path); if(RemoveDirectoryW(ToLongPath(Aws::Utils::StringUtils::ToWString(path)).c_str())) { AWS_LOGSTREAM_DEBUG(FILE_SYSTEM_UTILS_LOG_TAG, "The remove operation of file at " << path << " Succeeded."); return true; } else { int errorCode = GetLastError(); if (errorCode == ERROR_DIR_NOT_EMPTY) { AWS_LOGSTREAM_ERROR(FILE_SYSTEM_UTILS_LOG_TAG, "The remove operation of file at " << path << " failed. with error code because it was not empty."); } else if(errorCode == ERROR_DIRECTORY) { AWS_LOGSTREAM_DEBUG(FILE_SYSTEM_UTILS_LOG_TAG, "The deletion of directory at " << path << " failed because it doesn't exist."); return true; } AWS_LOGSTREAM_DEBUG(FILE_SYSTEM_UTILS_LOG_TAG, "The remove operation of file at " << path << " failed. with error code " << errorCode); return false; } }
bool InitializePlayerDataSchema(void) { AWS_LOGSTREAM_INFO(SchemaTag, "Checking schema"); ClientConfiguration config; config.scheme = Scheme::HTTP; config.connectTimeoutMs = 30000; config.requestTimeoutMs = 30000; auto credentialsProvider = std::make_shared<ProfileConfigFileAWSCredentialsProvider>(DeveloperCredentialsProfileName); std::unique_ptr<DynamoDBClient> client(new DynamoDBClient(credentialsProvider, config)); DescribeTableRequest describeTableRequest; describeTableRequest.SetTableName(ExampleTableName); DescribeTableOutcome outcome = client->DescribeTable(describeTableRequest); // does the table exist with the properties we expect it to have? if(outcome.IsSuccess()) { const TableDescription& description = outcome.GetResult().GetTable(); if(description.GetAttributeDefinitions().size() == 1) { const AttributeDefinition& attributeDef = description.GetAttributeDefinitions()[0]; if(attributeDef.GetAttributeName() == IdentityColumnName && attributeDef.GetAttributeType() == ScalarAttributeType::S) { if(description.GetTableStatus() == TableStatus::ACTIVE) { AWS_LOGSTREAM_INFO(SchemaTag, "Valid schema exists, skipping creation step"); return true; } } } } AWS_LOGSTREAM_INFO(SchemaTag, "No valid schema found"); // Delete if a non-matching version of the table was found if(outcome.IsSuccess()) { if(!DropPlayerDataTable(client)) { return false; } } // Create the table return CreatePlayerDataTable(client); }
bool RemoveFileIfExists(const char* path) { AWS_LOGSTREAM_INFO(FILE_SYSTEM_UTILS_LOG_TAG, "Deleting file: " << path); int errorCode = unlink(path); AWS_LOGSTREAM_DEBUG(FILE_SYSTEM_UTILS_LOG_TAG, "Deletion of file: " << path << " Returned error code: " << errno); return errorCode == 0 || errno == ENOENT; }
bool CreateDirectoryIfNotExists(const char* path) { AWS_LOGSTREAM_INFO(FILE_SYSTEM_UTILS_LOG_TAG, "Creating directory " << path); int errorCode = mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO); AWS_LOGSTREAM_DEBUG(FILE_SYSTEM_UTILS_LOG_TAG, "Creation of directory " << path << " returned code: " << errno); return errorCode == 0 || errno == EEXIST; }
bool RelocateFileOrDirectory(const char* from, const char* to) { AWS_LOGSTREAM_INFO(FILE_SYSTEM_UTILS_LOG_TAG, "Moving file at " << from << " to " << to); int errorCode = rename(from, to); AWS_LOGSTREAM_DEBUG(FILE_SYSTEM_UTILS_LOG_TAG, "The moving operation of file at " << from << " to " << to << " Returned error code of " << errno); return errorCode == 0; }
void WaitOnTableStatus(const std::unique_ptr<DynamoDBClient>& client, const char* tableName, TableStatusWaitUntil status) { DescribeTableRequest describeTableRequest; describeTableRequest.SetTableName(tableName); bool done = false; while (!done) { DescribeTableOutcome outcome = client->DescribeTable(describeTableRequest); switch(status) { case TableStatusWaitUntil::GONE: if (outcome.IsSuccess()) { AWS_LOGSTREAM_INFO(SchemaTag, ExampleTableName << " table not yet deleted, waiting."); std::this_thread::sleep_for(std::chrono::seconds(1)); } else { assert(DynamoDBErrors::RESOURCE_NOT_FOUND == outcome.GetError().GetErrorType()); done = true; } break; case TableStatusWaitUntil::ACTIVE: assert(outcome.IsSuccess()); if (outcome.GetResult().GetTable().GetTableStatus() == TableStatus::ACTIVE) { done = true; } else { AWS_LOGSTREAM_INFO(SchemaTag, ExampleTableName << " table not yet active, waiting."); std::this_thread::sleep_for(std::chrono::seconds(1)); } break; default: break; } } }
bool RelocateFileOrDirectory(const char* from, const char* to) { AWS_LOGSTREAM_INFO(FILE_SYSTEM_UTILS_LOG_TAG, "Moving file at " << from << " to " << to); if(MoveFileW(ToLongPath(Aws::Utils::StringUtils::ToWString(from)).c_str(), Aws::Utils::StringUtils::ToWString(to).c_str())) { AWS_LOGSTREAM_DEBUG(FILE_SYSTEM_UTILS_LOG_TAG, "The moving operation of file at " << from << " to " << to << " Succeeded."); return true; } else { int errorCode = GetLastError(); AWS_LOGSTREAM_DEBUG(FILE_SYSTEM_UTILS_LOG_TAG, "The moving operation of file at " << from << " to " << to << " Returned error code of " << errorCode); return false; } }
bool RemoveFileIfExists(const char* path) { AWS_LOGSTREAM_INFO(FILE_SYSTEM_UTILS_LOG_TAG, "Deleting file: " << path); if (DeleteFileW(ToLongPath(Aws::Utils::StringUtils::ToWString(path)).c_str())) { AWS_LOGSTREAM_DEBUG(FILE_SYSTEM_UTILS_LOG_TAG, "Successfully deleted file: " << path); return true; } else { DWORD errorCode = GetLastError(); AWS_LOGSTREAM_DEBUG(FILE_SYSTEM_UTILS_LOG_TAG, "Deletion of file: " << path << " Returned error code: " << errorCode); return errorCode == ERROR_FILE_NOT_FOUND; } }
bool DropPlayerDataTable(const std::unique_ptr<DynamoDBClient>& client) { AWS_LOGSTREAM_INFO(SchemaTag, "Dropping old table: " << ExampleTableName); DeleteTableRequest deleteTableRequest; deleteTableRequest.SetTableName(ExampleTableName); DeleteTableOutcome deleteTableOutcome = client->DeleteTable(deleteTableRequest); if (!deleteTableOutcome.IsSuccess()) { AWSError<DynamoDBErrors> error = deleteTableOutcome.GetError(); AWS_LOGSTREAM_ERROR(SchemaTag, "Failed to drop old table: " << ExampleTableName << ": " << error.GetMessage() << "(" << error.GetExceptionName() << ")"); return false; } WaitOnTableStatus(client, ExampleTableName, TableStatusWaitUntil::GONE); return true; }
Aws::String GetHomeDirectory() { static const char* HOME_DIR_ENV_VAR = "USERPROFILE"; AWS_LOGSTREAM_TRACE(FILE_SYSTEM_UTILS_LOG_TAG, "Checking " << HOME_DIR_ENV_VAR << " for the home directory."); Aws::String homeDir = Aws::Environment::GetEnv(HOME_DIR_ENV_VAR); AWS_LOGSTREAM_DEBUG(FILE_SYSTEM_UTILS_LOG_TAG, "Environment value for variable " << HOME_DIR_ENV_VAR << " is " << homeDir); if(homeDir.empty()) { AWS_LOGSTREAM_WARN(FILE_SYSTEM_UTILS_LOG_TAG, "Home dir not stored in environment, trying to fetch manually from the OS."); HANDLE hToken; if (OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &hToken)) { DWORD len = MAX_PATH; WCHAR path[MAX_PATH]; if (GetUserProfileDirectoryW(hToken, path, &len)) { homeDir = Aws::Utils::StringUtils::FromWString(path); } CloseHandle(hToken); } AWS_LOGSTREAM_INFO(FILE_SYSTEM_UTILS_LOG_TAG, "Pulled " << homeDir << " as home directory from the OS."); } Aws::String retVal = (homeDir.size() > 0) ? Aws::Utils::StringUtils::Trim(homeDir.c_str()) : ""; if (!retVal.empty()) { if (retVal.at(retVal.length() - 1) != Aws::FileSystem::PATH_DELIM) { retVal += Aws::FileSystem::PATH_DELIM; } } return retVal; }
bool CreatePlayerDataTable(const std::unique_ptr<DynamoDBClient>& client) { AWS_LOGSTREAM_INFO(SchemaTag, "Creating " << ExampleTableName << " table"); // Build the create request CreateTableRequest createTableRequest; AttributeDefinition identityAttribute; identityAttribute.SetAttributeName(IdentityColumnName); identityAttribute.SetAttributeType(ScalarAttributeType::S); KeySchemaElement identityKeySchemaElement; identityKeySchemaElement.WithAttributeName(IdentityColumnName).WithKeyType(KeyType::HASH); ProvisionedThroughput provisionedThroughput; provisionedThroughput.SetReadCapacityUnits(5); provisionedThroughput.SetWriteCapacityUnits(1); createTableRequest.AddAttributeDefinitions(identityAttribute); createTableRequest.AddKeySchema(identityKeySchemaElement); createTableRequest.WithProvisionedThroughput(provisionedThroughput); createTableRequest.WithTableName(ExampleTableName); CreateTableOutcome createTableOutcome = client->CreateTable(createTableRequest); assert(createTableOutcome.IsSuccess() || createTableOutcome.GetError().GetErrorType() == DynamoDBErrors::RESOURCE_IN_USE); if(!createTableOutcome.IsSuccess()) { AWSError<DynamoDBErrors> error = createTableOutcome.GetError(); AWS_LOGSTREAM_ERROR(SchemaTag, "Failed to create table: " << ExampleTableName << ": " << error.GetMessage().c_str() << "(" << error.GetExceptionName() << ")"); return false; } // Wait for table to become active WaitOnTableStatus(client, ExampleTableName, TableStatusWaitUntil::ACTIVE); return true; }
bool CreateDirectoryIfNotExists(const char* path, bool createParentDirs) { Aws::String directoryName = path; AWS_LOGSTREAM_INFO(FILE_SYSTEM_UTILS_LOG_TAG, "Creating directory " << directoryName); // Create intermediate directories or create the target directory once. for (size_t i = createParentDirs ? 0 : directoryName.size() - 1; i < directoryName.size(); i++) { // Create the intermediate directory if we find a delimiter and the delimiter is not the first char, or if this is the target directory. if (i != 0 && (directoryName[i] == FileSystem::PATH_DELIM || i == directoryName.size() - 1)) { // the last delimeter can be removed safely. if (directoryName[i] == FileSystem::PATH_DELIM) { directoryName[i] = '\0'; } if (CreateDirectoryW(ToLongPath(StringUtils::ToWString(directoryName.c_str())).c_str(), nullptr)) { AWS_LOGSTREAM_DEBUG(FILE_SYSTEM_UTILS_LOG_TAG, "Creation of directory " << directoryName.c_str() << " succeeded."); } else { DWORD errorCode = GetLastError(); if (errorCode != ERROR_ALREADY_EXISTS && errorCode != NO_ERROR) // in vs2013 the errorCode is NO_ERROR { AWS_LOGSTREAM_ERROR(FILE_SYSTEM_UTILS_LOG_TAG, " Creation of directory " << directoryName.c_str() << " returned code: " << errorCode); return false; } AWS_LOGSTREAM_DEBUG(FILE_SYSTEM_UTILS_LOG_TAG, " Creation of directory " << directoryName.c_str() << " returned code: " << errorCode); } // Restore the path. We are good even if we didn't change that char to '\0', because we are ready to return. directoryName[i] = FileSystem::PATH_DELIM; } } return true; }