summaryrefslogtreecommitdiff
path: root/trans/remap.c
blob: 5afbaa02f99f50902c2fa556430df93299199b8d (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
/* remap -- a translator for changing paths
   Copyright (C) 2013 Free Software Foundation, Inc.

   This program 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 2, or (at
   your option) any later version.

   This program 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, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#include <hurd/trivfs.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <argp.h>
#include <error.h>
#include <string.h>

#include <version.h>

int trivfs_fstype = FSTYPE_MISC;
int trivfs_fsid = 0;
int trivfs_support_read = 0;
int trivfs_support_write = 0;
int trivfs_support_exec = 0;
int trivfs_allow_open = 0;

void
trivfs_modify_stat (struct trivfs_protid *cred, struct stat *st)
{
  /* Don't care */  
}

error_t
trivfs_goaway (struct trivfs_control *cntl, int flags)
{
  exit (0);
}

struct remap
{
  char *from;
  char *to;
  struct remap *next;
};

static struct remap *remaps;

error_t
trivfs_S_dir_lookup (struct trivfs_protid *diruser,
		     mach_port_t reply, mach_msg_type_name_t reply_type,
		     char *filename,
		     int flags,
		     mode_t mode,
		     retry_type *do_retry,
		     char *retry_name,
		     mach_port_t *retry_port,
		     mach_msg_type_name_t *retry_port_type)
{
  struct remap *remap;

  if (!diruser)
    return EOPNOTSUPP;

  for (remap = remaps; remap; remap = remap->next)
    /* FIXME: should match just prefix of filename too */
    if (!strcmp (remap->from, filename))
      {
#ifdef DEBUG
	fprintf (stderr,"replacing %s with %s\n", remap->from, remap->to);
	fflush (stderr);
#endif
	filename = remap->to;
	break;
      }

  *do_retry = FS_RETRY_REAUTH;
  *retry_port = getcrdir ();
  *retry_port_type = MACH_MSG_TYPE_COPY_SEND;
  strcpy (retry_name, filename);

  return 0;
}

static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
  static char *remap_from;

  switch (key)
  {
    case ARGP_KEY_ARG:
      if (arg[0] != '/')
	{
	  argp_error (state, "remap only works with absolute paths: %s",
		      arg);
	  return EINVAL;
	}

      /* Skip heading slashes */
      while (arg[0] == '/')
        arg++;

      if (!remap_from)
	/* First of a pair */
	remap_from = strdup (arg);
      else
	{
	  /* Second of a pair */
	  struct remap *remap = malloc (sizeof (*remap));
	  remap->from = remap_from;
	  remap->to = strdup (arg);
	  remap->next = remaps;
#ifdef DEBUG
	  fprintf (stderr, "adding remap %s->%s\n", remap->from, remap->to);
#endif
	  remaps = remap;
	  remap_from = NULL;
	}

      break;
  }
  return 0;
}

const char *argp_program_version = STANDARD_HURD_VERSION (fakeroot);

int
main (int argc, char **argv)
{
  error_t err;
  mach_port_t bootstrap;

  struct argp argp = { NULL, parse_opt, "[ FROM1 TO1 [ FROM2 TO2 [ ... ] ] ]", "\
A translator for remapping directories.\v\
This translator is to be used as a chroot, within which paths point to the\
same files as the original root, except a given set of paths, which are\
remapped to given paths." };

  argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, 0);

  task_get_bootstrap_port (mach_task_self (), &bootstrap);
  struct trivfs_control *fsys;

  err = trivfs_startup (bootstrap, 0, 0, 0, 0, 0, &fsys);
  if (err)
    error (1, err, "trivfs_startup failed");
  ports_manage_port_operations_one_thread (fsys->pi.bucket, trivfs_demuxer, 0);

  /*NOTREACHED*/
  return 0;
}