blob: e554d73d64e29b4120bff84750f85aaaca104856 [file] [log] [blame]
[email protected]61b8ad72009-07-22 00:35:181// Copyright (c) 2009 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/sys_info.h"
6
7#include "base/basictypes.h"
8#include "base/file_path.h"
9#include "base/file_util.h"
[email protected]e83326f2010-07-31 17:29:2510#include "base/string_number_conversions.h"
[email protected]61b8ad72009-07-22 00:35:1811#include "base/string_tokenizer.h"
[email protected]ba74b0d22010-10-23 05:19:2012#include "base/thread_restrictions.h"
[email protected]61b8ad72009-07-22 00:35:1813
14namespace base {
15
[email protected]3214ef12009-10-19 16:40:5716#if defined(GOOGLE_CHROME_BUILD)
[email protected]61b8ad72009-07-22 00:35:1817static const char kLinuxStandardBaseVersionKey[] = "GOOGLE_RELEASE";
18#else
19static const char kLinuxStandardBaseVersionKey[] = "DISTRIB_RELEASE";
20#endif
21
22const char kLinuxStandardBaseReleaseFile[] = "/etc/lsb-release";
23
24// static
25void SysInfo::OperatingSystemVersionNumbers(int32 *major_version,
26 int32 *minor_version,
27 int32 *bugfix_version) {
[email protected]ba74b0d22010-10-23 05:19:2028 // The other implementations of SysInfo don't block on the disk.
29 // See http://code.google.com/p/chromium/issues/detail?id=60394
30 // Perhaps the caller ought to cache this?
31 // Temporary allowing while we work the bug out.
32 base::ThreadRestrictions::ScopedAllowIO allow_io;
33
[email protected]61b8ad72009-07-22 00:35:1834 // TODO(cmasone): If this gets called a lot, it may kill performance.
35 // consider using static variables to cache these values?
36 FilePath path(kLinuxStandardBaseReleaseFile);
37 std::string contents;
38 if (file_util::ReadFileToString(path, &contents)) {
39 ParseLsbRelease(contents, major_version, minor_version, bugfix_version);
40 }
41}
42
43// static
44std::string SysInfo::GetLinuxStandardBaseVersionKey() {
45 return std::string(kLinuxStandardBaseVersionKey);
46}
47
48// static
49void SysInfo::ParseLsbRelease(const std::string& lsb_release,
50 int32 *major_version,
51 int32 *minor_version,
52 int32 *bugfix_version) {
[email protected]323b8772009-12-09 01:55:0453 size_t version_key_index = lsb_release.find(kLinuxStandardBaseVersionKey);
[email protected]61b8ad72009-07-22 00:35:1854 if (std::string::npos == version_key_index) {
55 return;
56 }
57 size_t start_index = lsb_release.find_first_of('=', version_key_index);
58 start_index++; // Move past '='.
59 size_t length = lsb_release.find_first_of('\n', start_index) - start_index;
60 std::string version = lsb_release.substr(start_index, length);
61 StringTokenizer tokenizer(version, ".");
62 for (int i = 0; i < 3 && tokenizer.GetNext(); i++) {
63 if (0 == i) {
[email protected]c2750c62010-10-22 16:05:4764 StringToInt(tokenizer.token_begin(),
65 tokenizer.token_end(),
66 major_version);
[email protected]61b8ad72009-07-22 00:35:1867 *minor_version = *bugfix_version = 0;
68 } else if (1 == i) {
[email protected]c2750c62010-10-22 16:05:4769 StringToInt(tokenizer.token_begin(),
70 tokenizer.token_end(),
71 minor_version);
[email protected]61b8ad72009-07-22 00:35:1872 } else { // 2 == i
[email protected]c2750c62010-10-22 16:05:4773 StringToInt(tokenizer.token_begin(),
74 tokenizer.token_end(),
75 bugfix_version);
[email protected]61b8ad72009-07-22 00:35:1876 }
77 }
78}
79
80} // namespace base