File: cowbuilder_util.c

package info (click to toggle)
cowdancer 0.73
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 752 kB
  • ctags: 269
  • sloc: ansic: 3,822; sh: 481; makefile: 191; cpp: 6
file content (53 lines) | stat: -rw-r--r-- 1,393 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
#include <mntent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "cowbuilder_util.h"

/**
 * @return 0 if not mounted, 1 if something under the mountpoint is mounted.
 */
int check_mountpoint(const char* mountpoint) {
  /* Check if a subdirectory under buildplace is still mounted, which may happen when there's a stale bind mount or whatnot. */
  FILE *mtab = NULL;
  struct mntent * part = NULL;
  if ((mtab = setmntent("/etc/mtab", "r")) != NULL)
    {
      while ((part = getmntent(mtab) ) != NULL)
        {
          if ( part->mnt_fsname != NULL )
            {
              if (strstr(part->mnt_dir, mountpoint) == part->mnt_dir)
                {
                  printf("E: Something (%s) is still mounted under %s; unmount and remove %s manually\n",
			 part->mnt_dir, mountpoint, mountpoint);
                  endmntent(mtab);
                  return 1;
                }
            }
        }
      endmntent(mtab);
    }
  return 0;
}

/**
 * Remove double slash "//" from pc->buildplace and use "/"
 * dest needs to be large enough to hold buildplace string.
 */
void canonicalize_doubleslash(const char* buildplace,
			      char* dest) {
  char prev=0;
  int i, j;
  for (i = 0, j = 0; buildplace[i]; ++i)
    {
      if (prev == '/' && buildplace[i] == '/')
	{
	  continue;
	}
      prev = dest[j++] = buildplace[i];
    }
  dest[j] = '\0';
}