File: loadpartitions.c

package info (click to toggle)
autopartkit 1.07
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,060 kB
  • ctags: 198
  • sloc: ansic: 2,489; sh: 843; makefile: 103
file content (197 lines) | stat: -rw-r--r-- 4,348 bytes parent folder | download | duplicates (2)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * Load the partition specification from disk.
 *
 * Author: Petter Reinholdtsen <pere@hungry.com>
 * Date:   2002-04-14
 */

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "autopartkit.h"

struct partition_list {
  diskspace_req_t *disk_reqs;
  unsigned int capacity;
  unsigned int count;
};

static int
list_makeroom(struct partition_list *list, unsigned int room)
{
  unsigned int newsize;
  void *mem;
  assert(list);
  if (list->capacity > list->count + room)
  {
    autopartkit_log(4, "Enough room in list %d > %d + %d\n",
	            list->capacity, list->count, room);
    return 0;
  }

  newsize = list->capacity * 2 ;
  if (room > newsize)
    newsize = room;

  mem = realloc(list->disk_reqs, newsize * sizeof(list->disk_reqs[0]));
  if (NULL == mem)
    return -1;
  else
  {
    list->capacity = newsize;
    list->disk_reqs = mem;
    return 0;
  }
}

static int
list_add_entry(struct partition_list *list, char *mountpoint, 
	       char *fstype, int minsize, int maxsize, int ondisk)
{
  if (0 != list_makeroom(list, 1))
    return -1;

  if (0 != mountpoint)
    mountpoint = strdup(mountpoint);

  if (0 != fstype)
    fstype = strdup(fstype);

  list->disk_reqs[list->count].mountpoint = mountpoint;
  list->disk_reqs[list->count].fstype = fstype;
  list->disk_reqs[list->count].minsize = minsize;
  list->disk_reqs[list->count].maxsize = maxsize;
  list->disk_reqs[list->count].ondisk = ondisk;
  (list->count)++;

  return 0;
}

static int
list_init(struct partition_list *list)
{
  assert(list);
  list->disk_reqs = NULL;
  list->capacity = 0;
  list->count = 0;
  return 0;
}

static void
remove_junk(char *buf, unsigned int buflength)
{
  /* Terminate on comment char */
  unsigned int i;
  for (i=0; i < buflength; ++i)
    if ('#' == buf[i] || '\n' == buf[i])
      buf[i] = '\0';
}

static int
add_partition(struct partition_list *list, char *line)
{
  char mountpoint[1024];
  char fstype[1024];
  char minsize[1024];
  char maxsize[1024];
  int ondisk;
  int minsize_mb, maxsize_mb;

  if (!line || ! *line) /* Ignore empty lines */
    return -1;

  assert(list);

  autopartkit_log(3, "Adding '%s'\n", line);

  if (4 != sscanf(line, "%s %s %s %s ", mountpoint, fstype, minsize, maxsize))
  {
      autopartkit_log(3, "Failed to parse line.\n");
      return -1; /* error */
  }

  autopartkit_log(2, "Fetched partition info %s %s %s %s\n",
                  mountpoint, fstype, minsize, maxsize);
  if (maxsize != 0)
      ondisk = 1;
  else
      ondisk = 0;

  /* round minsize and maxsize to closest megabyte */
  minsize_mb = (int)floor(evaluate(minsize) + 0.5);
  maxsize_mb = (int)floor(evaluate(maxsize) + 0.5);
 
  return list_add_entry(list, mountpoint, fstype, minsize_mb, maxsize_mb, ondisk);
}

diskspace_req_t *
load_partitions(const char *filename)
{
  struct partition_list list;
  FILE *fp;
  char buf[1024];

  memset(buf, 0, sizeof(buf));

  list_init(&list);

  fp = fopen(filename, "r");
  if (NULL == fp)
  {
    autopartkit_error(0, "Failed to open table file '%s'\n", filename);
    return NULL;
  }
  autopartkit_log(2, "Loading table file '%s'\n", filename);
  while ( ! feof(fp) )
  {
    if (NULL != fgets(buf, sizeof(buf), fp))
    {
      remove_junk(buf, sizeof(buf));
      add_partition(&list, buf);
    }
  }

  fclose(fp);

  /* Terminate list */
  list_add_entry(&list, NULL, NULL, -1, -1, 0);

  return list.disk_reqs; /* Transfering resposibility for this memory block */
}

void
free_partition_list(diskspace_req_t *list)
{
  int i;
  for (i=0; list && list[i].mountpoint; i++)
  {
    if (list[i].mountpoint)
      free(list[i].mountpoint);
    if (list[i].fstype)
      free(list[i].fstype);
  }
  free(list);
}

/*
 * There are multiple ways to get available RAM in the machine. /proc/meminfo
 * (and thus also /usr/bin/free) gives a number that is too small, and grokking
 * things out of dmesg is quite ugly. Stat'ing /proc/kcore is easy and reports
 * a number that is much better then /proc/meminfo, although still a tad too
 * low.
 */
double
get_ram_size(void)
{
  struct stat buf;
  if (stat("/proc/kcore", &buf) == -1)
    autopartkit_error(0, "Failed to stat /proc/kcore\n");

  return ((double)(buf.st_size) / (double)(MEGABYTE));
}