about summary refs log tree commit diff
path: root/nixpkgs/pkgs/applications/version-management/cvs/getcwd-chroot.patch
blob: 3f827a1e6981efaecfbc8ea8d185e2b765c3d61b (plain) (blame)
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
Fix Gnulib's getcwd in chroots.
From Debian bug #456164, http://bugs.debian.org/456164 .

--- cvs-1.12.13.orig/debian/patches/20_readdir_errno
+++ cvs-1.12.13/debian/patches/20_readdir_errno
@@ -0,0 +1,121 @@
+# From Gnulib:
+#   http://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=commitdiff;h=0b78641d85af3b72e3b9d94cb7b94e45f3c08ee5
+# We don't need this directly, but it's required so that 21_getcwd_chroot
+# applies cleanly.
+#
+# 2005-10-29  Paul Eggert  <eggert@cs.ucla.edu>
+#
+# 	* getcwd.c (__getcwd): Don't assume that system calls after readdir
+# 	leave errno alone.  Problem reported by Dmitry V. Levin.
+
+--- cvs-1.12.13-old/lib/getcwd.c
++++ cvs-1.12.13/lib/getcwd.c
+@@ -201,6 +201,8 @@ __getcwd (char *buf, size_t size)
+       ino_t dotino;
+       bool mount_point;
+       int parent_status;
++      size_t dirroom;
++      size_t namlen;
+ 
+       /* Look at the parent directory.  */
+ #ifdef AT_FDCWD
+@@ -241,11 +243,20 @@ __getcwd (char *buf, size_t size)
+ 	goto lose;
+       dotlist[dotlen++] = '/';
+ #endif
+-      /* Clear errno to distinguish EOF from error if readdir returns
+-	 NULL.  */
+-      __set_errno (0);
+-      while ((d = __readdir (dirstream)) != NULL)
++      for (;;)
+ 	{
++	  /* Clear errno to distinguish EOF from error if readdir returns
++	     NULL.  */
++	  __set_errno (0);
++	  d = __readdir (dirstream);
++	  if (d == NULL)
++	    {
++	      if (errno == 0)
++		/* EOF on dirstream, which means that the current directory
++		   has been removed.  */
++		__set_errno (ENOENT);
++	      goto lose;
++	    }
+ 	  if (d->d_name[0] == '.' &&
+ 	      (d->d_name[1] == '\0' ||
+ 	       (d->d_name[1] == '.' && d->d_name[2] == '\0')))
+@@ -303,48 +314,38 @@ __getcwd (char *buf, size_t size)
+ 		break;
+ 	    }
+ 	}
+-      if (d == NULL)
+-	{
+-	  if (errno == 0)
+-	    /* EOF on dirstream, which means that the current directory
+-	       has been removed.  */
+-	    __set_errno (ENOENT);
+-	  goto lose;
+-	}
+-      else
+-	{
+-	  size_t dirroom = dirp - dir;
+-	  size_t namlen = _D_EXACT_NAMLEN (d);
+ 
+-	  if (dirroom <= namlen)
++      dirroom = dirp - dir;
++      namlen = _D_EXACT_NAMLEN (d);
++
++      if (dirroom <= namlen)
++	{
++	  if (size != 0)
+ 	    {
+-	      if (size != 0)
+-		{
+-		  __set_errno (ERANGE);
+-		  goto lose;
+-		}
+-	      else
+-		{
+-		  char *tmp;
+-		  size_t oldsize = allocated;
++	      __set_errno (ERANGE);
++	      goto lose;
++	    }
++	  else
++	    {
++	      char *tmp;
++	      size_t oldsize = allocated;
+ 
+-		  allocated += MAX (allocated, namlen);
+-		  if (allocated < oldsize
+-		      || ! (tmp = realloc (dir, allocated)))
+-		    goto memory_exhausted;
++	      allocated += MAX (allocated, namlen);
++	      if (allocated < oldsize
++		  || ! (tmp = realloc (dir, allocated)))
++		goto memory_exhausted;
+ 
+-		  /* Move current contents up to the end of the buffer.
+-		     This is guaranteed to be non-overlapping.  */
+-		  dirp = memcpy (tmp + allocated - (oldsize - dirroom),
+-				 tmp + dirroom,
+-				 oldsize - dirroom);
+-		  dir = tmp;
+-		}
++	      /* Move current contents up to the end of the buffer.
++		 This is guaranteed to be non-overlapping.  */
++	      dirp = memcpy (tmp + allocated - (oldsize - dirroom),
++			     tmp + dirroom,
++			     oldsize - dirroom);
++	      dir = tmp;
+ 	    }
+-	  dirp -= namlen;
+-	  memcpy (dirp, d->d_name, namlen);
+-	  *--dirp = '/';
+ 	}
++      dirp -= namlen;
++      memcpy (dirp, d->d_name, namlen);
++      *--dirp = '/';
+ 
+       thisdev = dotdev;
+       thisino = dotino;
--- cvs-1.12.13.orig/debian/patches/21_getcwd_chroot
+++ cvs-1.12.13/debian/patches/21_getcwd_chroot
@@ -0,0 +1,172 @@
+# From Gnulib:
+#  http://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=commitdiff;h=79c0a43808d9ca85acd04600149fc1a9b75bd1b9
+#
+# 2006-07-03  Paul Eggert  <eggert@cs.ucla.edu>
+#
+# 	Merge from coreutils.
+#
+# 	2006-03-19  Jim Meyering  <jim@meyering.net>
+#
+# 	Work even in a chroot where d_ino values for entries in "/"
+# 	don't match the stat.st_ino values for the same names.
+# 	* getcwd.c (__getcwd): When no d_ino value matches the target inode
+# 	number, iterate through all entries again, using lstat instead.
+# 	Reported by Kenshi Muto in http://bugs.debian.org/355810, and by
+# 	Zouhir Hafidi in https://bugzilla.redhat.com/bugzilla/190656.
+#
+# 	* getcwd.c (__getcwd): Clarify a comment.
+# 	Use memcpy in place of a call to strcpy.
+
+--- cvs-1.12.13-old/lib/getcwd.c
++++ cvs-1.12.13/lib/getcwd.c
+@@ -211,6 +211,7 @@ __getcwd (char *buf, size_t size)
+       int parent_status;
+       size_t dirroom;
+       size_t namlen;
++      bool use_d_ino = true;
+ 
+       /* Look at the parent directory.  */
+ #ifdef AT_FDCWD
+@@ -257,11 +258,26 @@ __getcwd (char *buf, size_t size)
+ 	     NULL.  */
+ 	  __set_errno (0);
+ 	  d = __readdir (dirstream);
++
++	  /* When we've iterated through all directory entries without finding
++	     one with a matching d_ino, rewind the stream and consider each
++	     name again, but this time, using lstat.  This is necessary in a
++	     chroot on at least one system (glibc-2.3.6 + linux 2.6.12), where
++	     .., ../.., ../../.., etc. all had the same device number, yet the
++	     d_ino values for entries in / did not match those obtained
++	     via lstat.  */
++	  if (d == NULL && errno == 0 && use_d_ino)
++	    {
++	      use_d_ino = false;
++	      rewinddir (dirstream);
++	      d = __readdir (dirstream);
++	    }
++
+ 	  if (d == NULL)
+ 	    {
+ 	      if (errno == 0)
+-		/* EOF on dirstream, which means that the current directory
+-		   has been removed.  */
++		/* EOF on dirstream, which can mean e.g., that the current
++		   directory has been removed.  */
+ 		__set_errno (ENOENT);
+ 	      goto lose;
+ 	    }
+@@ -269,58 +285,65 @@ __getcwd (char *buf, size_t size)
+ 	      (d->d_name[1] == '\0' ||
+ 	       (d->d_name[1] == '.' && d->d_name[2] == '\0')))
+ 	    continue;
+-	  if (MATCHING_INO (d, thisino) || mount_point)
++
++	  if (use_d_ino)
+ 	    {
+-	      int entry_status;
++	      bool match = (MATCHING_INO (d, thisino) || mount_point);
++	      if (! match)
++		continue;
++	    }
++
++	  {
++	    int entry_status;
+ #ifdef AT_FDCWD
+-	      entry_status = fstatat (fd, d->d_name, &st, AT_SYMLINK_NOFOLLOW);
++	    entry_status = fstatat (fd, d->d_name, &st, AT_SYMLINK_NOFOLLOW);
+ #else
+-	      /* Compute size needed for this file name, or for the file
+-		 name ".." in the same directory, whichever is larger.
+-	         Room for ".." might be needed the next time through
+-		 the outer loop.  */
+-	      size_t name_alloc = _D_ALLOC_NAMLEN (d);
+-	      size_t filesize = dotlen + MAX (sizeof "..", name_alloc);
+-
+-	      if (filesize < dotlen)
+-		goto memory_exhausted;
+-
+-	      if (dotsize < filesize)
+-		{
+-		  /* My, what a deep directory tree you have, Grandma.  */
+-		  size_t newsize = MAX (filesize, dotsize * 2);
+-		  size_t i;
+-		  if (newsize < dotsize)
+-		    goto memory_exhausted;
+-		  if (dotlist != dots)
+-		    free (dotlist);
+-		  dotlist = malloc (newsize);
+-		  if (dotlist == NULL)
+-		    goto lose;
+-		  dotsize = newsize;
+-
+-		  i = 0;
+-		  do
+-		    {
+-		      dotlist[i++] = '.';
+-		      dotlist[i++] = '.';
+-		      dotlist[i++] = '/';
+-		    }
+-		  while (i < dotlen);
+-		}
+-
+-	      strcpy (dotlist + dotlen, d->d_name);
+-	      entry_status = __lstat (dotlist, &st);
++	    /* Compute size needed for this file name, or for the file
++	       name ".." in the same directory, whichever is larger.
++	       Room for ".." might be needed the next time through
++	       the outer loop.  */
++	    size_t name_alloc = _D_ALLOC_NAMLEN (d);
++	    size_t filesize = dotlen + MAX (sizeof "..", name_alloc);
++
++	    if (filesize < dotlen)
++	      goto memory_exhausted;
++
++	    if (dotsize < filesize)
++	      {
++		/* My, what a deep directory tree you have, Grandma.  */
++		size_t newsize = MAX (filesize, dotsize * 2);
++		size_t i;
++		if (newsize < dotsize)
++		  goto memory_exhausted;
++		if (dotlist != dots)
++		  free (dotlist);
++		dotlist = malloc (newsize);
++		if (dotlist == NULL)
++		  goto lose;
++		dotsize = newsize;
++
++		i = 0;
++		do
++		  {
++		    dotlist[i++] = '.';
++		    dotlist[i++] = '.';
++		    dotlist[i++] = '/';
++		  }
++		while (i < dotlen);
++	      }
++
++	    memcpy (dotlist + dotlen, d->d_name, _D_ALLOC_NAMLEN (d));
++	    entry_status = __lstat (dotlist, &st);
+ #endif
+-	      /* We don't fail here if we cannot stat() a directory entry.
+-		 This can happen when (network) file systems fail.  If this
+-		 entry is in fact the one we are looking for we will find
+-		 out soon as we reach the end of the directory without
+-		 having found anything.  */
+-	      if (entry_status == 0 && S_ISDIR (st.st_mode)
+-		  && st.st_dev == thisdev && st.st_ino == thisino)
+-		break;
+-	    }
++	    /* We don't fail here if we cannot stat() a directory entry.
++	       This can happen when (network) file systems fail.  If this
++	       entry is in fact the one we are looking for we will find
++	       out soon as we reach the end of the directory without
++	       having found anything.  */
++	    if (entry_status == 0 && S_ISDIR (st.st_mode)
++		&& st.st_dev == thisdev && st.st_ino == thisino)
++	      break;
++	  }
+ 	}
+ 
+       dirroom = dirp - dir;