summaryrefslogtreecommitdiffstats
path: root/src/org.gluster.storage.management.gateway.scripts/src/backend/FsTabUtils.py
blob: 0bb328db72c31c8a896a85931f22e055fa4e7d1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/python
#  Copyright (C) 2011 Gluster, Inc. <http://www.gluster.com>
#  This file is part of Gluster Management Gateway (GlusterMG).
#
#  GlusterMG is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published
#  by the Free Software Foundation; either version 3 of the License,
#  or (at your option) any later version.
#
#  GlusterMG is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see
#  <http://www.gnu.org/licenses/>.
#

import os
import sys
p1 = os.path.abspath(os.path.dirname(sys.argv[0]))
p2 = "%s/common" % os.path.dirname(p1)
if not p1 in sys.path:
    sys.path.append(p1)
if not p2 in sys.path:
    sys.path.append(p2)
import Utils
import Globals

def readFsTab(fsTabFile=Globals.FSTAB_FILE):
    lines = Utils.readFile(fsTabFile)

    fsTabEntryList = []
    for line in lines:
        tokens = line.strip().split()
        if not tokens or tokens[0].startswith('#'):
            continue
        fsTabEntry = {}
        fsTabEntry["Device"] = None
        fsTabEntry["MountPoint"] = None
        fsTabEntry["FsType"] = None
        fsTabEntry["Options"] = None
        fsTabEntry["DumpOption"] = 0
        fsTabEntry["fsckOrder"] = 0
        try:
            fsTabEntry["Device"] = tokens[0]
            fsTabEntry["MountPoint"] = tokens[1]
            fsTabEntry["FsType"] = tokens[2]
            fsTabEntry["Options"] = tokens[3]
            fsTabEntry["DumpOption"] = tokens[4]
            fsTabEntry["fsckOrder"] = tokens[5]
        except IndexError, e:
            pass
        if fsTabEntry["Device"] and fsTabEntry["MountPoint"] and fsTabEntry["FsType"] and fsTabEntry["Options"]:
            fsTabEntryList.append(fsTabEntry)
    return fsTabEntryList

def writeFsTab(fsTabEntryList, fsTabFile=Globals.FSTAB_FILE):
    try:
        fsTabfp = open(fsTabFile, "w")
        for fsTabEntry in fsTabEntryList:
            fsTabfp.write("%s\t%s\t%s\t%s\t%s\t%s\n" %
                          (fsTabEntry["Device"], fsTabEntry["MountPoint"],
                           fsTabEntry["FsType"], fsTabEntry["Options"],
                           fsTabEntry["DumpOption"], fsTabEntry["fsckOrder"]))
        fsTabfp.close()
    except IOError, e:
        log("writeFsTab(): " + str(e))
        return False
    return True

def addFsTabEntry(fsTabEntry, fsTabFile=Globals.FSTAB_FILE):
    try:
        fsTabfp = open(fsTabFile, "a")
        fsTabfp.write("%s\t%s\t%s\t%s\t%s\t%s\n" %
                      (fsTabEntry["Device"], fsTabEntry["MountPoint"],
                       fsTabEntry["FsType"], fsTabEntry["Options"],
                       fsTabEntry["DumpOption"], fsTabEntry["fsckOrder"]))
        fsTabfp.close()
    except IOError, e:
        log("addFsTabEntry(): " + str(e))
        return False
    return True

def removeFsTabEntry(fsTabEntry, fsTabFile=Globals.FSTAB_FILE):
    fsTabEntryList = readFsTab(fsTabFile)
    if not fsTabEntryList:
        return False

    try:
        fsTabEntryList.remove(fsTabEntry)
    except ValueError, e:
        return False

    return writeFsTab(fsTabEntryList, fsTabFile)