|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "cuttlefish/host/libs/metrics/session_id.h" |
| 18 | + |
| 19 | +#include <string> |
| 20 | +#include <string_view> |
| 21 | + |
| 22 | +#include <uuid/uuid.h> |
| 23 | + |
| 24 | +#include "cuttlefish/common/libs/utils/files.h" |
| 25 | +#include "cuttlefish/common/libs/utils/result.h" |
| 26 | + |
| 27 | +namespace cuttlefish { |
| 28 | +namespace { |
| 29 | + |
| 30 | +constexpr char kSessionIdFileName[] = "metrics_session_id.txt"; |
| 31 | +constexpr int kUuidStringLength = 36; // per uuid_unparse(3) |
| 32 | + |
| 33 | +std::string GenerateUuid() { |
| 34 | + uuid_t uuid; |
| 35 | + uuid_generate_random(uuid); |
| 36 | + std::string uuid_str = std::string(kUuidStringLength, 'x'); |
| 37 | + uuid_unparse(uuid, uuid_str.data()); |
| 38 | + return uuid_str; |
| 39 | +} |
| 40 | + |
| 41 | +} // namespace |
| 42 | + |
| 43 | +Result<std::string> ReadSessionIdFile(const std::string& metrics_directory) { |
| 44 | + return CF_EXPECT(ReadFileContents(metrics_directory + kSessionIdFileName)); |
| 45 | +} |
| 46 | + |
| 47 | +Result<void> GenerateSessionIdFile(const std::string& metrics_directory) { |
| 48 | + const std::string session_id = GenerateUuid(); |
| 49 | + CF_EXPECT(WriteNewFile(metrics_directory + kSessionIdFileName, session_id)); |
| 50 | + return {}; |
| 51 | +} |
| 52 | + |
| 53 | +} // namespace cuttlefish |
0 commit comments