sys_info_chromeos.cc revision c7f5f8508d98d5952d42ed7648c2a8f30a4da156
1// 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"
10#include "base/string_tokenizer.h"
11#include "base/string_util.h"
12
13namespace base {
14
15#if defined(GOOGLE_CHROME_BUILD)
16static const char kLinuxStandardBaseVersionKey[] = "GOOGLE_RELEASE";
17#else
18static const char kLinuxStandardBaseVersionKey[] = "DISTRIB_RELEASE";
19#endif
20
21const char kLinuxStandardBaseReleaseFile[] = "/etc/lsb-release";
22
23// static
24void SysInfo::OperatingSystemVersionNumbers(int32 *major_version,
25                                            int32 *minor_version,
26                                            int32 *bugfix_version) {
27  // TODO(cmasone): If this gets called a lot, it may kill performance.
28  // consider using static variables to cache these values?
29  FilePath path(kLinuxStandardBaseReleaseFile);
30  std::string contents;
31  if (file_util::ReadFileToString(path, &contents)) {
32    ParseLsbRelease(contents, major_version, minor_version, bugfix_version);
33  }
34}
35
36// static
37std::string SysInfo::GetLinuxStandardBaseVersionKey() {
38  return std::string(kLinuxStandardBaseVersionKey);
39}
40
41// static
42void SysInfo::ParseLsbRelease(const std::string& lsb_release,
43                              int32 *major_version,
44                              int32 *minor_version,
45                              int32 *bugfix_version) {
46  size_t version_key_index = lsb_release.find(kLinuxStandardBaseVersionKey);
47  if (std::string::npos == version_key_index) {
48    return;
49  }
50  size_t start_index = lsb_release.find_first_of('=', version_key_index);
51  start_index++;  // Move past '='.
52  size_t length = lsb_release.find_first_of('\n', start_index) - start_index;
53  std::string version = lsb_release.substr(start_index, length);
54  StringTokenizer tokenizer(version, ".");
55  for (int i = 0; i < 3 && tokenizer.GetNext(); i++) {
56    if (0 == i) {
57      *major_version = StringToInt(tokenizer.token());
58      *minor_version = *bugfix_version = 0;
59    } else if (1 == i) {
60      *minor_version = StringToInt(tokenizer.token());
61    } else {  // 2 == i
62      *bugfix_version = StringToInt(tokenizer.token());
63    }
64  }
65}
66
67}  // namespace base
68