1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2  *
3  * Copyright (C) 2017 Red Hat, Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Author: Vojtech Trefny <vtrefny@redhat.com>
20  *
21  */
22 
23 #include <glib.h>
24 #include <gudev/gudev.h>
25 #include <stdlib.h>
26 
27 #include "udiskslinuxmdraidhelpers.h"
28 #include "udiskslogging.h"
29 
30 gboolean
mdraid_has_redundancy(const gchar * raid_level)31 mdraid_has_redundancy (const gchar *raid_level)
32 {
33   return raid_level != NULL &&
34          g_str_has_prefix (raid_level, "raid") &&
35          g_strcmp0 (raid_level, "raid0") != 0;
36 }
37 
38 gboolean
mdraid_has_stripes(const gchar * raid_level)39 mdraid_has_stripes (const gchar *raid_level)
40 {
41   return raid_level != NULL &&
42          g_str_has_prefix (raid_level, "raid") &&
43          g_strcmp0 (raid_level, "raid1") != 0;
44 }
45 
46 gchar *
read_sysfs_attr(GUdevDevice * device,const gchar * attr)47 read_sysfs_attr (GUdevDevice *device,
48                  const gchar *attr)
49 {
50   gchar *ret = NULL;
51   gchar *path = NULL;
52   GError *error = NULL;
53 
54   g_return_val_if_fail (G_UDEV_IS_DEVICE (device), NULL);
55 
56   path = g_strdup_printf ("%s/%s", g_udev_device_get_sysfs_path (device), attr);
57   if (!g_file_get_contents (path, &ret, NULL /* size */, &error))
58     {
59       udisks_warning ("Error reading sysfs attr `%s': %s (%s, %d)",
60                       path, error->message, g_quark_to_string (error->domain), error->code);
61       g_clear_error (&error);
62       goto out;
63     }
64 
65   /* remove newline from the attribute */
66   if (ret != NULL)
67     g_strstrip (ret);
68 
69  out:
70   g_free (path);
71   return ret;
72 }
73 
74 gint
read_sysfs_attr_as_int(GUdevDevice * device,const gchar * attr)75 read_sysfs_attr_as_int (GUdevDevice *device,
76                         const gchar *attr)
77 {
78   gint ret = 0;
79   gchar *str = NULL;
80 
81   str = read_sysfs_attr (device, attr);
82   if (str == NULL)
83     goto out;
84 
85   ret = atoi (str);
86   g_free (str);
87 
88  out:
89   return ret;
90 }
91 
92 guint64
read_sysfs_attr_as_uint64(GUdevDevice * device,const gchar * attr)93 read_sysfs_attr_as_uint64 (GUdevDevice *device,
94                            const gchar *attr)
95 {
96   guint64 ret = 0;
97   gchar *str = NULL;
98 
99   str = read_sysfs_attr (device, attr);
100   if (str == NULL)
101     goto out;
102 
103   ret = atoll (str);
104   g_free (str);
105 
106  out:
107   return ret;
108 }
109