From ad00787a433c1e6c122131d873bbe0c95da3526b Mon Sep 17 00:00:00 2001 From: m0_54007497 Date: Tue, 26 Aug 2025 15:46:35 +0800 Subject: [PATCH 1/7] addlog Signed-off-by: m0_54007497 --- .../include/drm/v1_0/clearplay_log.h | 116 ++++++++++++++++++ .../src/media_key_session_service.cpp | 30 +++-- 2 files changed, 138 insertions(+), 8 deletions(-) create mode 100644 clearplay/hdi_service/include/drm/v1_0/clearplay_log.h diff --git a/clearplay/hdi_service/include/drm/v1_0/clearplay_log.h b/clearplay/hdi_service/include/drm/v1_0/clearplay_log.h new file mode 100644 index 0000000000..e933dc204b --- /dev/null +++ b/clearplay/hdi_service/include/drm/v1_0/clearplay_log.h @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CLEARPLAY_LOG_H +#define CLEARPLAY_LOG_H + +#include "hdf_base.h" +#include "hilog/log.h" + +namespace OHOS { +namespace HDI { +namespace ClearPlay { +namespace v1_0 { + +#ifdef CLEARPLAY_HILOGF +#undef CLEARPLAY_HILOGF +#endif + +#ifdef CLEARPLAY_HILOGE +#undef CLEARPLAY_HILOGE +#endif + +#ifdef CLEARPLAY_HILOGW +#undef CLEARPLAY_HILOGW +#endif + +#ifdef CLEARPLAY_HILOGI +#undef CLEARPLAY_HILOGI +#endif + +#ifdef CLEARPLAY_HILOGD +#undef CLEARPLAY_HILOGD +#endif + +namespace { +// ClearPlay reserved domain id range +constexpr unsigned int CLEARPLAY_DOMAIN_ID_START = 0xD002A00; +constexpr unsigned int CLEARPLAY_DOMAIN_ID_END = CLEARPLAY_DOMAIN_ID_START + 32; +constexpr unsigned int TEST_DOMAIN_ID = 0xD000F10; +} // namespace + +enum ClearPlayLogLabel { + COMP_APP = 0, + COMP_FWK = 1, + COMP_SVC = 2, + COMP_HDI = 3, + COMP_DRV = 4, + FEATURE_PLAY, + FEATURE_LICENSE, + LABEL_TEST, + LABEL_END, +}; + +enum ClearPlayLogDomain { + DOMAIN_APP = CLEARPLAY_DOMAIN_ID_START + COMP_APP, + DOMAIN_FRAMEWORK, + DOMAIN_SERVICE, + DOMAIN_HDI, + DOMAIN_DRIVER, + DOMAIN_FEATURE_PLAY, + DOMAIN_FEATURE_LICENSE, + DOMAIN_TEST = TEST_DOMAIN_ID, + DOMAIN_END = CLEARPLAY_DOMAIN_ID_END, +}; + +struct ClearPlayLogLabelDomain { + uint32_t domainId; + const char* tag; +}; + +static const ClearPlayLogLabelDomain CLEARPLAY_LABEL[LABEL_END] = { + {DOMAIN_APP, "ClearPlayApp" }, + {DOMAIN_FRAMEWORK, "ClearPlayFwk" }, + {DOMAIN_SERVICE, "ClearPlaySvc" }, + {DOMAIN_HDI, "ClearPlayHdi" }, + {DOMAIN_DRIVER, "ClearPlayDrv" }, + {DOMAIN_FEATURE_PLAY, "ClearPlayFeature" }, + {DOMAIN_FEATURE_LICENSE, "ClearPlayLicense" }, + {DOMAIN_TEST, "ClearPlayTest" }, +}; + +#define CLEARPLAY_HILOGF(domain, ...) \ + ((void)HILOG_IMPL(LOG_CORE, LOG_FATAL, OHOS::HDI::ClearPlay::v1_0::CLEARPLAY_LABEL[domain].domainId, \ + OHOS::HDI::ClearPlay::v1_0::CLEARPLAY_LABEL[domain].tag, ##__VA_ARGS__)) +#define CLEARPLAY_HILOGE(domain, ...) \ + ((void)HILOG_IMPL(LOG_CORE, LOG_ERROR, OHOS::HDI::ClearPlay::v1_0::CLEARPLAY_LABEL[domain].domainId, \ + OHOS::HDI::ClearPlay::v1_0::CLEARPLAY_LABEL[domain].tag, ##__VA_ARGS__)) +#define CLEARPLAY_HILOGW(domain, ...) \ + ((void)HILOG_IMPL(LOG_CORE, LOG_WARN, OHOS::HDI::ClearPlay::v1_0::CLEARPLAY_LABEL[domain].domainId, \ + OHOS::HDI::ClearPlay::v1_0::CLEARPLAY_LABEL[domain].tag, ##__VA_ARGS__)) +#define CLEARPLAY_HILOGI(domain, ...) \ + ((void)HILOG_IMPL(LOG_CORE, LOG_INFO, OHOS::HDI::ClearPlay::v1_0::CLEARPLAY_LABEL[domain].domainId, \ + OHOS::HDI::ClearPlay::v1_0::CLEARPLAY_LABEL[domain].tag, ##__VA_ARGS__)) +#define CLEARPLAY_HILOGD(domain, ...) \ + ((void)HILOG_IMPL(LOG_CORE, LOG_DEBUG, OHOS::HDI::ClearPlay::v1_0::CLEARPLAY_LABEL[domain].domainId, \ + OHOS::HDI::ClearPlay::v1_0::CLEARPLAY_LABEL[domain].tag, ##__VA_ARGS__)) + + +} // namespace v1_0 +} // namespace ClearPlay +} // namespace HDI +} // namespace OHOS + +#endif // CLEARPLAY_LOG_H diff --git a/clearplay/hdi_service/src/media_key_session_service.cpp b/clearplay/hdi_service/src/media_key_session_service.cpp index a81d03cd55..fb3707af4b 100644 --- a/clearplay/hdi_service/src/media_key_session_service.cpp +++ b/clearplay/hdi_service/src/media_key_session_service.cpp @@ -373,43 +373,57 @@ int32_t MediaKeySessionService::GetErrorDecryptNumber() int32_t MediaKeySessionService::GetOfflineKeyFromFile() { - HDF_LOGI("%{public}s: start", __func__); + OHOS::HDI::ClearPlay::v1_0::CLEARPLAY_HILOGI( + OHOS::HDI::ClearPlay::v1_0::COMP_SVC, "%{public}s: start", __func__); + FILE *offlineKeyFile = fopen(offlineKeyFileName, "r+"); if (offlineKeyFile == NULL) { - HDF_LOGE("%{public}s: open: \"%{public}s\" failed", __func__, offlineKeyFileName); + OHOS::HDI::ClearPlay::v1_0::CLEARPLAY_HILOGE( + OHOS::HDI::ClearPlay::v1_0::COMP_SVC, "%{public}s: open \"%{public}s\" failed", __func__, offlineKeyFileName); // file do not exist, is allright return HDF_SUCCESS; } + char keyIdBase64Chars[keyIdMaxLength]; char keyValueBase64Chars[keyIdMaxLength]; - while (fscanf_s(offlineKeyFile, "%s %s", keyIdBase64Chars, sizeof(keyIdBase64Chars), keyValueBase64Chars, - sizeof(keyValueBase64Chars)) != EOF) { + while (fscanf_s(offlineKeyFile, "%s %s", keyIdBase64Chars, sizeof(keyIdBase64Chars), + keyValueBase64Chars, sizeof(keyValueBase64Chars)) != EOF) { std::string tempKeyIdBase64 = keyIdBase64Chars; std::string tempKeyValueBase64 = keyValueBase64Chars; tempKeyIdBase64.erase(std::remove(tempKeyIdBase64.begin(), tempKeyIdBase64.end(), '\0'), tempKeyIdBase64.end()); offlineKeyIdAndKeyValueBase64_[tempKeyIdBase64] = tempKeyValueBase64; } + fclose(offlineKeyFile); - HDF_LOGI("%{public}s: end", __func__); + + OHOS::HDI::ClearPlay::v1_0::CLEARPLAY_HILOGI( + OHOS::HDI::ClearPlay::v1_0::COMP_SVC, "%{public}s: end", __func__); return HDF_SUCCESS; } int32_t MediaKeySessionService::SetOfflineKeyToFile() { - HDF_LOGI("%{public}s: start", __func__); + OHOS::HDI::ClearPlay::v1_0::CLEARPLAY_HILOGI( + OHOS::HDI::ClearPlay::v1_0::COMP_SVC, "%{public}s: start", __func__); + FILE *offlineKeyFile = fopen(offlineKeyFileName, "w+"); if (offlineKeyFile == NULL) { offlineKeyIdAndKeyValueBase64_.clear(); - HDF_LOGE("%{public}s: create failed, ret: %{public}s", __func__, strerror(errno)); + OHOS::HDI::ClearPlay::v1_0::CLEARPLAY_HILOGE( + OHOS::HDI::ClearPlay::v1_0::COMP_SVC, "%{public}s: create failed, ret: %{public}s", __func__, strerror(errno)); return HDF_FAILURE; } + for (auto &keyIdValueBase64Pair : offlineKeyIdAndKeyValueBase64_) { fprintf(offlineKeyFile, "%s %s\n", keyIdValueBase64Pair.first.c_str(), keyIdValueBase64Pair.second.c_str()); fflush(offlineKeyFile); } + offlineKeyIdAndKeyValueBase64_.clear(); fclose(offlineKeyFile); - HDF_LOGI("%{public}s: end", __func__); + + OHOS::HDI::ClearPlay::v1_0::CLEARPLAY_HILOGI( + OHOS::HDI::ClearPlay::v1_0::COMP_SVC, "%{public}s: end", __func__); return HDF_SUCCESS; } } // V1_0 -- Gitee From 406a4a15d0a8ddab1dd8291f317cbfd06a6caef7 Mon Sep 17 00:00:00 2001 From: m0_54007497 Date: Tue, 26 Aug 2025 16:05:01 +0800 Subject: [PATCH 2/7] debug Signed-off-by: m0_54007497 --- .../src/media_key_session_service.cpp | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/clearplay/hdi_service/src/media_key_session_service.cpp b/clearplay/hdi_service/src/media_key_session_service.cpp index fb3707af4b..136188f6cd 100644 --- a/clearplay/hdi_service/src/media_key_session_service.cpp +++ b/clearplay/hdi_service/src/media_key_session_service.cpp @@ -16,6 +16,7 @@ #include "v1_0/media_key_session_service.h" #include "v1_0/media_key_system_types.h" #include "v1_0/media_decrypt_module_service.h" +#include "v1_0/clearplay_log.h" #include #include #include @@ -373,13 +374,11 @@ int32_t MediaKeySessionService::GetErrorDecryptNumber() int32_t MediaKeySessionService::GetOfflineKeyFromFile() { - OHOS::HDI::ClearPlay::v1_0::CLEARPLAY_HILOGI( - OHOS::HDI::ClearPlay::v1_0::COMP_SVC, "%{public}s: start", __func__); + CLEARPLAY_HILOGI(COMP_SVC, "%{public}s: start", __func__); FILE *offlineKeyFile = fopen(offlineKeyFileName, "r+"); - if (offlineKeyFile == NULL) { - OHOS::HDI::ClearPlay::v1_0::CLEARPLAY_HILOGE( - OHOS::HDI::ClearPlay::v1_0::COMP_SVC, "%{public}s: open \"%{public}s\" failed", __func__, offlineKeyFileName); + if (offlineKeyFile == nullptr) { + CLEARPLAY_HILOGE(COMP_SVC, "%{public}s: open \"%{public}s\" failed", __func__, offlineKeyFileName); // file do not exist, is allright return HDF_SUCCESS; } @@ -396,21 +395,18 @@ int32_t MediaKeySessionService::GetOfflineKeyFromFile() fclose(offlineKeyFile); - OHOS::HDI::ClearPlay::v1_0::CLEARPLAY_HILOGI( - OHOS::HDI::ClearPlay::v1_0::COMP_SVC, "%{public}s: end", __func__); + CLEARPLAY_HILOGI(COMP_SVC, "%{public}s: end", __func__); return HDF_SUCCESS; } int32_t MediaKeySessionService::SetOfflineKeyToFile() { - OHOS::HDI::ClearPlay::v1_0::CLEARPLAY_HILOGI( - OHOS::HDI::ClearPlay::v1_0::COMP_SVC, "%{public}s: start", __func__); + CLEARPLAY_HILOGI(COMP_SVC, "%{public}s: start", __func__); FILE *offlineKeyFile = fopen(offlineKeyFileName, "w+"); - if (offlineKeyFile == NULL) { + if (offlineKeyFile == nullptr) { offlineKeyIdAndKeyValueBase64_.clear(); - OHOS::HDI::ClearPlay::v1_0::CLEARPLAY_HILOGE( - OHOS::HDI::ClearPlay::v1_0::COMP_SVC, "%{public}s: create failed, ret: %{public}s", __func__, strerror(errno)); + CLEARPLAY_HILOGE(COMP_SVC, "%{public}s: create failed, ret: %{public}s", __func__, strerror(errno)); return HDF_FAILURE; } @@ -422,10 +418,10 @@ int32_t MediaKeySessionService::SetOfflineKeyToFile() offlineKeyIdAndKeyValueBase64_.clear(); fclose(offlineKeyFile); - OHOS::HDI::ClearPlay::v1_0::CLEARPLAY_HILOGI( - OHOS::HDI::ClearPlay::v1_0::COMP_SVC, "%{public}s: end", __func__); + CLEARPLAY_HILOGI(COMP_SVC, "%{public}s: end", __func__); return HDF_SUCCESS; } + } // V1_0 } // Drm } // HDI -- Gitee From ed879dc2b5f318f8d0bf9305d98b1ac80b0692bd Mon Sep 17 00:00:00 2001 From: m0_54007497 Date: Tue, 26 Aug 2025 16:13:24 +0800 Subject: [PATCH 3/7] debug Signed-off-by: m0_54007497 --- clearplay/hdi_service/src/media_key_session_service.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clearplay/hdi_service/src/media_key_session_service.cpp b/clearplay/hdi_service/src/media_key_session_service.cpp index 136188f6cd..583d59da32 100644 --- a/clearplay/hdi_service/src/media_key_session_service.cpp +++ b/clearplay/hdi_service/src/media_key_session_service.cpp @@ -27,7 +27,7 @@ #include "securec.h" #define HDF_LOG_TAG media_key_session_service - +using OHOS::HDI::ClearPlay::v1_0::COMP_SVC; namespace OHOS { namespace HDI { namespace Drm { -- Gitee From 51020222f19f9965dc21de622cd77a54143a0dd8 Mon Sep 17 00:00:00 2001 From: m0_54007497 Date: Tue, 26 Aug 2025 17:31:25 +0800 Subject: [PATCH 4/7] debug Signed-off-by: m0_54007497 --- .../src/media_key_session_service.cpp | 36 +++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/clearplay/hdi_service/src/media_key_session_service.cpp b/clearplay/hdi_service/src/media_key_session_service.cpp index 583d59da32..e3be4b4808 100644 --- a/clearplay/hdi_service/src/media_key_session_service.cpp +++ b/clearplay/hdi_service/src/media_key_session_service.cpp @@ -22,10 +22,14 @@ #include #include #include +#include +#include +#include #include "base64_utils.h" #include "data_parser.h" #include "securec.h" + #define HDF_LOG_TAG media_key_session_service using OHOS::HDI::ClearPlay::v1_0::COMP_SVC; namespace OHOS { @@ -376,11 +380,20 @@ int32_t MediaKeySessionService::GetOfflineKeyFromFile() { CLEARPLAY_HILOGI(COMP_SVC, "%{public}s: start", __func__); - FILE *offlineKeyFile = fopen(offlineKeyFileName, "r+"); + // 使用 open 代替 fopen, 并设置权限 0666,如果文件不存在就创建 + int fd = open(offlineKeyFileName, O_RDWR | O_CREAT, 0666); + if (fd < 0) { + CLEARPLAY_HILOGE(COMP_SVC, "%{public}s: open \"%{public}s\" failed, ret: %{public}s", + __func__, offlineKeyFileName, strerror(errno)); + return HDF_FAILURE; + } + + FILE* offlineKeyFile = fdopen(fd, "r+"); if (offlineKeyFile == nullptr) { - CLEARPLAY_HILOGE(COMP_SVC, "%{public}s: open \"%{public}s\" failed", __func__, offlineKeyFileName); - // file do not exist, is allright - return HDF_SUCCESS; + CLOSE(fd); + CLEARPLAY_HILOGE(COMP_SVC, "%{public}s: fdopen failed, ret: %{public}s", + __func__, strerror(errno)); + return HDF_FAILURE; } char keyIdBase64Chars[keyIdMaxLength]; @@ -403,10 +416,21 @@ int32_t MediaKeySessionService::SetOfflineKeyToFile() { CLEARPLAY_HILOGI(COMP_SVC, "%{public}s: start", __func__); - FILE *offlineKeyFile = fopen(offlineKeyFileName, "w+"); + // open 文件并设置 0666 权限 + int fd = open(offlineKeyFileName, O_RDWR | O_CREAT | O_TRUNC, 0666); + if (fd < 0) { + offlineKeyIdAndKeyValueBase64_.clear(); + CLEARPLAY_HILOGE(COMP_SVC, "%{public}s: open/create failed, ret: %{public}s", + __func__, strerror(errno)); + return HDF_FAILURE; + } + + FILE* offlineKeyFile = fdopen(fd, "w+"); if (offlineKeyFile == nullptr) { + CLOSE(fd); offlineKeyIdAndKeyValueBase64_.clear(); - CLEARPLAY_HILOGE(COMP_SVC, "%{public}s: create failed, ret: %{public}s", __func__, strerror(errno)); + CLEARPLAY_HILOGE(COMP_SVC, "%{public}s: fdopen failed, ret: %{public}s", + __func__, strerror(errno)); return HDF_FAILURE; } -- Gitee From 5f2c3b192ccec010b41a7af8b0a518b0a7db9720 Mon Sep 17 00:00:00 2001 From: m0_54007497 Date: Tue, 26 Aug 2025 17:47:38 +0800 Subject: [PATCH 5/7] debug Signed-off-by: m0_54007497 --- .../hdi_service/src/media_key_session_service.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/clearplay/hdi_service/src/media_key_session_service.cpp b/clearplay/hdi_service/src/media_key_session_service.cpp index e3be4b4808..18ecd1624b 100644 --- a/clearplay/hdi_service/src/media_key_session_service.cpp +++ b/clearplay/hdi_service/src/media_key_session_service.cpp @@ -380,8 +380,7 @@ int32_t MediaKeySessionService::GetOfflineKeyFromFile() { CLEARPLAY_HILOGI(COMP_SVC, "%{public}s: start", __func__); - // 使用 open 代替 fopen, 并设置权限 0666,如果文件不存在就创建 - int fd = open(offlineKeyFileName, O_RDWR | O_CREAT, 0666); + int fd = open(offlineKeyFileName, O_RDWR | O_CREAT, 0666); // 创建文件权限 0666 if (fd < 0) { CLEARPLAY_HILOGE(COMP_SVC, "%{public}s: open \"%{public}s\" failed, ret: %{public}s", __func__, offlineKeyFileName, strerror(errno)); @@ -390,7 +389,7 @@ int32_t MediaKeySessionService::GetOfflineKeyFromFile() FILE* offlineKeyFile = fdopen(fd, "r+"); if (offlineKeyFile == nullptr) { - CLOSE(fd); + close(fd); // 改为 close CLEARPLAY_HILOGE(COMP_SVC, "%{public}s: fdopen failed, ret: %{public}s", __func__, strerror(errno)); return HDF_FAILURE; @@ -416,8 +415,7 @@ int32_t MediaKeySessionService::SetOfflineKeyToFile() { CLEARPLAY_HILOGI(COMP_SVC, "%{public}s: start", __func__); - // open 文件并设置 0666 权限 - int fd = open(offlineKeyFileName, O_RDWR | O_CREAT | O_TRUNC, 0666); + int fd = open(offlineKeyFileName, O_RDWR | O_CREAT | O_TRUNC, 0666); // 创建或覆盖文件,权限 0666 if (fd < 0) { offlineKeyIdAndKeyValueBase64_.clear(); CLEARPLAY_HILOGE(COMP_SVC, "%{public}s: open/create failed, ret: %{public}s", @@ -427,7 +425,7 @@ int32_t MediaKeySessionService::SetOfflineKeyToFile() FILE* offlineKeyFile = fdopen(fd, "w+"); if (offlineKeyFile == nullptr) { - CLOSE(fd); + close(fd); // 改为 close offlineKeyIdAndKeyValueBase64_.clear(); CLEARPLAY_HILOGE(COMP_SVC, "%{public}s: fdopen failed, ret: %{public}s", __func__, strerror(errno)); -- Gitee From a86c507dcd0f713589934948c55b56c1c14a7b22 Mon Sep 17 00:00:00 2001 From: m0_54007497 Date: Tue, 26 Aug 2025 18:49:15 +0800 Subject: [PATCH 6/7] change filepath Signed-off-by: m0_54007497 --- .../hdi_service/include/drm/v1_0/media_key_session_service.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clearplay/hdi_service/include/drm/v1_0/media_key_session_service.h b/clearplay/hdi_service/include/drm/v1_0/media_key_session_service.h index 5842458fa6..f1115b0857 100644 --- a/clearplay/hdi_service/include/drm/v1_0/media_key_session_service.h +++ b/clearplay/hdi_service/include/drm/v1_0/media_key_session_service.h @@ -81,7 +81,7 @@ private: sptr sessionCallback_; std::mutex offlineKeyMutex_; std::map offlineKeyIdAndKeyValueBase64_; - const char* offlineKeyFileName = "/data/local/traces/offline_key.txt"; + const char* offlineKeyFileName = "/data/local/tmp/offline_key.txt"; const int keyIdMaxLength = 255; OHOS::sptr vdiCallbackObj; static const int32_t topThree = 3; -- Gitee From e15b63e5b34ec580b21c0aff452cb982944389f6 Mon Sep 17 00:00:00 2001 From: m0_54007497 Date: Wed, 27 Aug 2025 19:36:11 +0800 Subject: [PATCH 7/7] addlog Signed-off-by: m0_54007497 --- .../src/media_key_session_service.cpp | 67 ++++++++++--------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/clearplay/hdi_service/src/media_key_session_service.cpp b/clearplay/hdi_service/src/media_key_session_service.cpp index 18ecd1624b..7a9008e0ab 100644 --- a/clearplay/hdi_service/src/media_key_session_service.cpp +++ b/clearplay/hdi_service/src/media_key_session_service.cpp @@ -380,28 +380,23 @@ int32_t MediaKeySessionService::GetOfflineKeyFromFile() { CLEARPLAY_HILOGI(COMP_SVC, "%{public}s: start", __func__); - int fd = open(offlineKeyFileName, O_RDWR | O_CREAT, 0666); // 创建文件权限 0666 - if (fd < 0) { - CLEARPLAY_HILOGE(COMP_SVC, "%{public}s: open \"%{public}s\" failed, ret: %{public}s", - __func__, offlineKeyFileName, strerror(errno)); - return HDF_FAILURE; - } - - FILE* offlineKeyFile = fdopen(fd, "r+"); + FILE *offlineKeyFile = fopen(offlineKeyFileName, "r+"); // 读写方式打开 if (offlineKeyFile == nullptr) { - close(fd); // 改为 close - CLEARPLAY_HILOGE(COMP_SVC, "%{public}s: fdopen failed, ret: %{public}s", - __func__, strerror(errno)); - return HDF_FAILURE; + int saved = errno; + CLEARPLAY_HILOGE(COMP_SVC, + "%{public}s: fopen \"%s\" failed, errno=%d (%s)", + __func__, offlineKeyFileName, saved, strerror(saved)); + // 如果文件不存在,不算错误,返回成功即可 + return HDF_SUCCESS; } char keyIdBase64Chars[keyIdMaxLength]; char keyValueBase64Chars[keyIdMaxLength]; - while (fscanf_s(offlineKeyFile, "%s %s", keyIdBase64Chars, sizeof(keyIdBase64Chars), - keyValueBase64Chars, sizeof(keyValueBase64Chars)) != EOF) { + while (fscanf(offlineKeyFile, "%s %s", keyIdBase64Chars, keyValueBase64Chars) != EOF) { std::string tempKeyIdBase64 = keyIdBase64Chars; std::string tempKeyValueBase64 = keyValueBase64Chars; - tempKeyIdBase64.erase(std::remove(tempKeyIdBase64.begin(), tempKeyIdBase64.end(), '\0'), tempKeyIdBase64.end()); + tempKeyIdBase64.erase(std::remove(tempKeyIdBase64.begin(), tempKeyIdBase64.end(), '\0'), + tempKeyIdBase64.end()); offlineKeyIdAndKeyValueBase64_[tempKeyIdBase64] = tempKeyValueBase64; } @@ -415,30 +410,42 @@ int32_t MediaKeySessionService::SetOfflineKeyToFile() { CLEARPLAY_HILOGI(COMP_SVC, "%{public}s: start", __func__); - int fd = open(offlineKeyFileName, O_RDWR | O_CREAT | O_TRUNC, 0666); // 创建或覆盖文件,权限 0666 - if (fd < 0) { - offlineKeyIdAndKeyValueBase64_.clear(); - CLEARPLAY_HILOGE(COMP_SVC, "%{public}s: open/create failed, ret: %{public}s", - __func__, strerror(errno)); - return HDF_FAILURE; - } - - FILE* offlineKeyFile = fdopen(fd, "w+"); + FILE *offlineKeyFile = fopen(offlineKeyFileName, "w+"); // 创建/覆盖写 if (offlineKeyFile == nullptr) { - close(fd); // 改为 close + int saved = errno; offlineKeyIdAndKeyValueBase64_.clear(); - CLEARPLAY_HILOGE(COMP_SVC, "%{public}s: fdopen failed, ret: %{public}s", - __func__, strerror(errno)); + CLEARPLAY_HILOGE(COMP_SVC, + "%{public}s: fopen \"%s\" failed, errno=%d (%s)", + __func__, offlineKeyFileName, saved, strerror(saved)); return HDF_FAILURE; } for (auto &keyIdValueBase64Pair : offlineKeyIdAndKeyValueBase64_) { - fprintf(offlineKeyFile, "%s %s\n", keyIdValueBase64Pair.first.c_str(), keyIdValueBase64Pair.second.c_str()); - fflush(offlineKeyFile); + if (fprintf(offlineKeyFile, "%s %s\n", + keyIdValueBase64Pair.first.c_str(), + keyIdValueBase64Pair.second.c_str()) < 0) { + int saved = errno; + fclose(offlineKeyFile); + offlineKeyIdAndKeyValueBase64_.clear(); + CLEARPLAY_HILOGE(COMP_SVC, + "%{public}s: fprintf failed, errno=%d (%s)", + __func__, saved, strerror(saved)); + return HDF_FAILURE; + } + } + + if (fflush(offlineKeyFile) != 0) { + int saved = errno; + fclose(offlineKeyFile); + offlineKeyIdAndKeyValueBase64_.clear(); + CLEARPLAY_HILOGE(COMP_SVC, + "%{public}s: fflush failed, errno=%d (%s)", + __func__, saved, strerror(saved)); + return HDF_FAILURE; } - offlineKeyIdAndKeyValueBase64_.clear(); fclose(offlineKeyFile); + offlineKeyIdAndKeyValueBase64_.clear(); CLEARPLAY_HILOGI(COMP_SVC, "%{public}s: end", __func__); return HDF_SUCCESS; -- Gitee