• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
13 namespace base {
14 
15 #if defined(GOOGLE_CHROME_BUILD)
16 static const char kLinuxStandardBaseVersionKey[] = "GOOGLE_RELEASE";
17 #else
18 static const char kLinuxStandardBaseVersionKey[] = "DISTRIB_RELEASE";
19 #endif
20 
21 const char kLinuxStandardBaseReleaseFile[] = "/etc/lsb-release";
22 
23 // static
OperatingSystemVersionNumbers(int32 * major_version,int32 * minor_version,int32 * bugfix_version)24 void 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
GetLinuxStandardBaseVersionKey()37 std::string SysInfo::GetLinuxStandardBaseVersionKey() {
38   return std::string(kLinuxStandardBaseVersionKey);
39 }
40 
41 // static
ParseLsbRelease(const std::string & lsb_release,int32 * major_version,int32 * minor_version,int32 * bugfix_version)42 void 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