Welcome to mirror list, hosted at ThFree Co, Russian Federation.

umount.cc « utils « winsup - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c37e57ad0c9537008f8b8e653c6366e9bdf484cc (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
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
/* umount.cc

   Copyright 1996, 1998, 1999 Cygnus Solutions.

This file is part of Cygwin.

This software is a copyrighted work licensed under the terms of the
Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
details. */

#include <stdio.h>
#include <string.h>
#include <sys/mount.h>
#include <mntent.h>

static void remove_all_mounts ();
static void remove_all_automounts ();
static void remove_all_user_mounts ();
static void remove_all_system_mounts ();

static const char *progname;

static void
usage (void)
{
  fprintf (stderr, "Usage %s [-s] <posixpath>\n", progname);
  fprintf (stderr, "-s = remove mount point from system-wide registry location\n");
  fprintf (stderr, "\n");
  fprintf (stderr, "--remove-all-mounts = remove all mounts\n");
  fprintf (stderr, "--remove-auto-mounts = remove all automatically mounted mounts\n");
  fprintf (stderr, "--remove-user-mounts = remove all mounts in the current user mount registry area, including auto mounts\n");
  fprintf (stderr, "--remove-system-mounts = Remove all mounts in the system-wide mount registry area\n");
  exit (1);
}

int
main (int argc, char **argv)
{
  int i;
  int flags = 0;
  progname = argv[0];

  if (argc == 1)
    usage ();

  for (i = 1; i < argc; ++i)
    {
      if (argv[i][0] != '-')
        break;

      if (strcmp (argv[i], "-s") == 0)
	{
	  flags |= MOUNT_SYSTEM;
	}
      else if (strcmp (argv[i], "--remove-all-mounts") == 0)
	{
	  remove_all_mounts ();
	  exit (0);
	}
      else if (strcmp (argv[i], "--remove-user-mounts") == 0)
	{
	  remove_all_user_mounts ();
	  exit (0);
	}
      else if (strcmp (argv[i], "--remove-system-mounts") == 0)
	{
	  remove_all_system_mounts ();
	  exit (0);
	}
      else if (strcmp (argv[i], "--remove-auto-mounts") == 0)
	{
	  remove_all_automounts ();
	  exit (0);
	}
      else
	usage ();
    }

  if ((i + 1) != argc)
    usage ();

  if (cygwin_umount (argv[i], flags) != 0)
    {
      perror ("umount");
      exit (1);
    }

  return 0;
}

/* remove_all_mounts: Unmount all mounts. */
static void
remove_all_mounts ()
{
  remove_all_user_mounts ();
  remove_all_system_mounts ();
}

/* remove_all_automounts: Unmount all automounts. */
static void
remove_all_automounts ()
{
  FILE *m = setmntent ("/-not-used-", "r");
  struct mntent *p;

  while ((p = getmntent (m)) != NULL)
    {
      /* Remove the mount if it's an automount. */
      if (strcmp (p->mnt_type, "user,auto") == 0)
	{
	  cygwin_umount (p->mnt_dir, 0);
	  /* We've modified the table so we need to start over. */
	  endmntent (m);
	  m = setmntent ("/-not-used-", "r");
	}
      else if (strcmp (p->mnt_type, "system,auto") == 0)
	{
	  cygwin_umount (p->mnt_dir, MOUNT_SYSTEM);
	  /* We've modified the table so we need to start over. */
	  endmntent (m);
	  m = setmntent ("/-not-used-", "r");
	}
    }

  endmntent (m);
}

/* remove_all_user_mounts: Unmount all user mounts. */
static void
remove_all_user_mounts ()
{
  FILE *m = setmntent ("/-not-used-", "r");
  struct mntent *p;
  int err;

  while ((p = getmntent (m)) != NULL)
    {
      /* Remove the mount if it's a user mount. */
      if (strncmp (p->mnt_type, "user", 4) == 0)
	{
	  err = cygwin_umount (p->mnt_dir, 0);

	  /* We've modified the table so we need to start over. */
	  endmntent (m);
	  m = setmntent ("/-not-used-", "r");
	}
    }

  endmntent (m);
}

/* remove_all_system_mounts: Unmount all system mounts. */
static void
remove_all_system_mounts ()
{
  FILE *m = setmntent ("/-not-used-", "r");
  struct mntent *p;

  while ((p = getmntent (m)) != NULL)
    {
      /* Remove the mount if it's a system mount. */
      if (strncmp (p->mnt_type, "system", 6) == 0)
	{
	  cygwin_umount (p->mnt_dir, MOUNT_SYSTEM);

	  /* We've modified the table so we need to start over. */
	  endmntent (m);
	  m = setmntent ("/-not-used-", "r");
	}
    }

  endmntent (m);
}