This source file includes following definitions.
- dentry_exists
- assert_nonexistent
- test_rename
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 static bool
26 dentry_exists (const char *filename)
27 {
28 bool exists = false;
29 DIR *dir = opendir (".");
30
31 ASSERT (dir != NULL);
32 for (;;)
33 {
34 struct dirent *d = readdir (dir);
35 if (d == NULL)
36 break;
37 if (strcmp (d->d_name, filename) == 0)
38 {
39 exists = true;
40 break;
41 }
42 }
43 ASSERT (closedir (dir) == 0);
44 return exists;
45 }
46
47
48
49 static void
50 assert_nonexistent (const char *filename)
51 {
52 struct stat st;
53
54
55 errno = 0;
56 if (stat (filename, &st) == -1)
57 ASSERT (errno == ENOENT);
58 else
59 {
60
61
62
63
64 ASSERT (!dentry_exists (filename));
65
66
67 (void) rmdir (filename);
68 }
69 }
70
71 static int
72 test_rename (int (*func) (char const *, char const *), bool print)
73 {
74
75 struct stat st;
76 int fd = creat (BASE "file", 0600);
77 ASSERT (0 <= fd);
78 ASSERT (write (fd, "hi", 2) == 2);
79 ASSERT (close (fd) == 0);
80 ASSERT (mkdir (BASE "dir", 0700) == 0);
81
82
83
84
85
86
87
88
89 {
90 {
91 errno = 0;
92 ASSERT (func (BASE "missing", BASE "missing") == -1);
93 ASSERT (errno == ENOENT);
94 }
95 {
96 errno = 0;
97 ASSERT (func (BASE "missing/", BASE "missing") == -1);
98 ASSERT (errno == ENOENT);
99 }
100 {
101 errno = 0;
102 ASSERT (func (BASE "missing", BASE "missing/") == -1);
103 ASSERT (errno == ENOENT);
104 }
105 }
106 {
107 {
108 errno = 0;
109 ASSERT (func ("", BASE "missing") == -1);
110 ASSERT (errno == ENOENT);
111 }
112 {
113 errno = 0;
114 ASSERT (func (BASE "file", "") == -1);
115 ASSERT (errno == ENOENT);
116 }
117 {
118 errno = 0;
119 ASSERT (func (BASE "", "") == -1);
120 ASSERT (errno == ENOENT);
121 }
122 }
123
124
125
126 {
127 {
128 errno = 0;
129 ASSERT (func (BASE "file", BASE "file2/") == -1);
130 ASSERT (errno == ENOENT || errno == ENOTDIR);
131 }
132 {
133 errno = 0;
134 ASSERT (func (BASE "file/", BASE "file2") == -1);
135 ASSERT (errno == ENOTDIR);
136 }
137 {
138 errno = 0;
139 ASSERT (stat (BASE "file2", &st) == -1);
140 ASSERT (errno == ENOENT);
141 }
142 }
143 {
144 ASSERT (func (BASE "file", BASE "file2") == 0);
145 errno = 0;
146 ASSERT (stat (BASE "file", &st) == -1);
147 ASSERT (errno == ENOENT);
148 memset (&st, 0, sizeof st);
149 ASSERT (stat (BASE "file2", &st) == 0);
150 ASSERT (st.st_size == 2);
151 }
152
153
154
155
156 {
157 ASSERT (close (creat (BASE "file", 0600)) == 0);
158 errno = 0;
159 ASSERT (func (BASE "file2", BASE "file/") == -1);
160 ASSERT (errno == ENOTDIR);
161 ASSERT (func (BASE "file2", BASE "file") == 0);
162 memset (&st, 0, sizeof st);
163 ASSERT (stat (BASE "file", &st) == 0);
164 ASSERT (st.st_size == 2);
165 errno = 0;
166 ASSERT (stat (BASE "file2", &st) == -1);
167 ASSERT (errno == ENOENT);
168 }
169
170
171
172
173
174
175
176 {
177 {
178 ASSERT (func (BASE "dir", BASE "dir2/") == 0);
179 errno = 0;
180 ASSERT (stat (BASE "dir", &st) == -1);
181 ASSERT (errno == ENOENT);
182 ASSERT (stat (BASE "dir2", &st) == 0);
183 }
184
185
186
187
188 {
189 ASSERT (func (BASE "dir2/", BASE "dir") == 0);
190 ASSERT (stat (BASE "dir", &st) == 0);
191 errno = 0;
192 ASSERT (stat (BASE "dir2", &st) == -1);
193 ASSERT (errno == ENOENT);
194 }
195
196
197
198
199 {
200 ASSERT (func (BASE "dir", BASE "dir2") == 0);
201 errno = 0;
202 ASSERT (stat (BASE "dir", &st) == -1);
203 ASSERT (errno == ENOENT);
204 ASSERT (stat (BASE "dir2", &st) == 0);
205 }
206
207
208
209
210 {
211 ASSERT (mkdir (BASE "dir", 0700) == 0);
212
213
214
215
216
217 ASSERT (func (BASE "dir2", BASE "dir") == 0);
218
219
220
221
222 ASSERT (mkdir (BASE "dir2", 0700) == 0);
223
224
225
226
227
228 ASSERT (func (BASE "dir2", BASE "dir/") == 0);
229
230
231
232
233 ASSERT (mkdir (BASE "dir2", 0700) == 0);
234
235
236
237
238
239 ASSERT (func (BASE "dir2/", BASE "dir") == 0);
240
241
242
243
244 ASSERT (mkdir (BASE "dir2", 0700) == 0);
245 }
246
247
248
249
250
251 {
252 ASSERT (close (creat (BASE "dir/file", 0600)) == 0);
253
254
255
256
257
258
259 {
260 errno = 0;
261 ASSERT (func (BASE "dir2", BASE "dir") == -1);
262 ASSERT (errno == EEXIST || errno == ENOTEMPTY);
263 }
264 {
265 errno = 0;
266 ASSERT (func (BASE "dir2/", BASE "dir") == -1);
267 ASSERT (errno == EEXIST || errno == ENOTEMPTY);
268 }
269 {
270 errno = 0;
271 ASSERT (func (BASE "dir2", BASE "dir/") == -1);
272 ASSERT (errno == EEXIST || errno == ENOTEMPTY);
273 }
274 }
275 {
276 ASSERT (func (BASE "dir", BASE "dir2") == 0);
277 assert_nonexistent (BASE "dir");
278 ASSERT (stat (BASE "dir2/file", &st) == 0);
279
280
281
282
283
284 ASSERT (mkdir (BASE "dir", 0700) == 0);
285
286
287
288
289
290
291 {
292 ASSERT (func (BASE "dir2/", BASE "dir") == 0);
293 ASSERT (stat (BASE "dir/file", &st) == 0);
294 errno = 0;
295 ASSERT (stat (BASE "dir2", &st) == -1);
296 ASSERT (errno == ENOENT);
297 }
298
299
300
301
302
303 ASSERT (mkdir (BASE "dir2", 0700) == 0);
304
305
306
307
308
309
310 {
311 ASSERT (func (BASE "dir", BASE "dir2/") == 0);
312 assert_nonexistent (BASE "dir");
313 ASSERT (stat (BASE "dir2/file", &st) == 0);
314 }
315
316
317
318
319
320 ASSERT (unlink (BASE "dir2/file") == 0);
321 }
322
323
324
325
326 {
327 {
328 errno = 0;
329 ASSERT (func (BASE "dir2", BASE "dir/.") == -1);
330 ASSERT (errno == EINVAL || errno == ENOENT);
331 }
332 ASSERT (mkdir (BASE "dir", 0700) == 0);
333
334
335
336
337
338 {
339 errno = 0;
340 ASSERT (func (BASE "dir2", BASE "dir/.") == -1);
341 ASSERT (errno == EINVAL || errno == EBUSY || errno == EISDIR
342 || errno == ENOTEMPTY || errno == EEXIST
343 || errno == ENOENT );
344 }
345 {
346 errno = 0;
347 ASSERT (func (BASE "dir2/.", BASE "dir") == -1);
348 ASSERT (errno == EINVAL || errno == EBUSY || errno == EEXIST
349 || errno == ENOENT );
350 }
351 ASSERT (rmdir (BASE "dir") == 0);
352
353
354
355
356 {
357 errno = 0;
358 ASSERT (func (BASE "dir2", BASE "dir/.//") == -1);
359 ASSERT (errno == EINVAL || errno == ENOENT);
360 }
361 ASSERT (mkdir (BASE "dir", 0700) == 0);
362
363
364
365
366
367 {
368 errno = 0;
369 ASSERT (func (BASE "dir2", BASE "dir/.//") == -1);
370 ASSERT (errno == EINVAL || errno == EBUSY || errno == EISDIR
371 || errno == ENOTEMPTY || errno == EEXIST
372 || errno == ENOENT );
373 }
374 {
375 errno = 0;
376 ASSERT (func (BASE "dir2/.//", BASE "dir") == -1);
377 ASSERT (errno == EINVAL || errno == EBUSY || errno == EEXIST
378 || errno == ENOENT );
379 }
380 ASSERT (rmdir (BASE "dir2") == 0);
381
382
383
384
385 }
386 {
387 {
388 errno = 0;
389 ASSERT (func (BASE "dir", BASE "dir/sub") == -1);
390 ASSERT (errno == EINVAL || errno == EACCES);
391 }
392 {
393 errno = 0;
394 ASSERT (stat (BASE "dir/sub", &st) == -1);
395 ASSERT (errno == ENOENT);
396 }
397 ASSERT (mkdir (BASE "dir/sub", 0700) == 0);
398
399
400
401
402
403 {
404 errno = 0;
405 ASSERT (func (BASE "dir", BASE "dir/sub") == -1);
406 ASSERT (errno == EINVAL);
407 ASSERT (stat (BASE "dir/sub", &st) == 0);
408 }
409 ASSERT (rmdir (BASE "dir/sub") == 0);
410 }
411 }
412
413
414
415
416
417
418
419 {
420 {
421 {
422 errno = 0;
423 ASSERT (func (BASE "file", BASE "dir") == -1);
424 ASSERT (errno == EISDIR || errno == ENOTDIR);
425 }
426 {
427 errno = 0;
428 ASSERT (func (BASE "file", BASE "dir/") == -1);
429 ASSERT (errno == EISDIR || errno == ENOTDIR);
430 }
431 }
432 {
433 {
434 errno = 0;
435 ASSERT (func (BASE "dir", BASE "file") == -1);
436 ASSERT (errno == ENOTDIR);
437 }
438 {
439 errno = 0;
440 ASSERT (func (BASE "dir/", BASE "file") == -1);
441 ASSERT (errno == ENOTDIR);
442 }
443 }
444 }
445
446
447
448 {
449 ASSERT (func (BASE "file", BASE "file") == 0);
450 memset (&st, 0, sizeof st);
451 ASSERT (stat (BASE "file", &st) == 0);
452 ASSERT (st.st_size == 2);
453 }
454
455
456
457
458 {
459 ASSERT (func (BASE "dir", BASE "dir") == 0);
460 ASSERT (stat (BASE "dir", &st) == 0);
461 }
462
463
464
465
466 ASSERT (close (creat (BASE "dir/file", 0600)) == 0);
467
468
469
470
471
472 {
473 ASSERT (func (BASE "dir", BASE "dir") == 0);
474 }
475 ASSERT (unlink (BASE "dir/file") == 0);
476
477
478
479
480 {
481
482
483
484
485
486
487 int ret = link (BASE "file", BASE "file2");
488 if (!ret)
489 {
490 memset (&st, 0, sizeof st);
491 ASSERT (stat (BASE "file2", &st) == 0);
492 if (st.st_ino && st.st_nlink != 2)
493 {
494 ASSERT (unlink (BASE "file2") == 0);
495 errno = EPERM;
496 ret = -1;
497 }
498 }
499 if (ret == -1)
500 {
501
502
503 switch (errno)
504 {
505 case EPERM:
506 case EOPNOTSUPP:
507 if (print)
508 fputs ("skipping test: "
509 "hard links not supported on this file system\n",
510 stderr);
511 ASSERT (unlink (BASE "file") == 0);
512 ASSERT (rmdir (BASE "dir") == 0);
513 return 77;
514 default:
515 perror ("link");
516 return 1;
517 }
518 }
519 ASSERT (ret == 0);
520 }
521
522
523
524
525
526 {
527 ASSERT (func (BASE "file", BASE "file2") == 0);
528 memset (&st, 0, sizeof st);
529 if (stat (BASE "file", &st) != 0)
530 {
531
532 ASSERT (errno == ENOENT);
533 ASSERT (link (BASE "file2", BASE "file") == 0);
534 ASSERT (stat (BASE "file", &st) == 0);
535 }
536 ASSERT (st.st_size == 2);
537 memset (&st, 0, sizeof st);
538 ASSERT (stat (BASE "file2", &st) == 0);
539 ASSERT (st.st_size == 2);
540 }
541
542
543
544
545
546 ASSERT (unlink (BASE "file2") == 0);
547
548
549
550
551
552
553
554 if (symlink (BASE "file", BASE "link1"))
555 {
556 if (print)
557 fputs ("skipping test: symlinks not supported on this file system\n",
558 stderr);
559 ASSERT (unlink (BASE "file") == 0);
560 ASSERT (rmdir (BASE "dir") == 0);
561 return 77;
562 }
563
564
565
566
567
568 {
569 ASSERT (func (BASE "link1", BASE "link2") == 0);
570 ASSERT (stat (BASE "file", &st) == 0);
571 errno = 0;
572 ASSERT (lstat (BASE "link1", &st) == -1);
573 ASSERT (errno == ENOENT);
574 memset (&st, 0, sizeof st);
575 ASSERT (lstat (BASE "link2", &st) == 0);
576 ASSERT (S_ISLNK (st.st_mode));
577 }
578
579
580
581
582
583 {
584 ASSERT (symlink (BASE "nowhere", BASE "link1") == 0);
585
586
587
588
589
590
591 {
592 ASSERT (func (BASE "link2", BASE "link1") == 0);
593 memset (&st, 0, sizeof st);
594 ASSERT (stat (BASE "link1", &st) == 0);
595 ASSERT (st.st_size == 2);
596 errno = 0;
597 ASSERT (lstat (BASE "link2", &st) == -1);
598 ASSERT (errno == ENOENT);
599 }
600 }
601
602
603
604
605
606 {
607 ASSERT (symlink (BASE "link2", BASE "link2") == 0);
608
609
610
611
612
613
614 {
615 ASSERT (func (BASE "link2", BASE "link2") == 0);
616 }
617 {
618 errno = 0;
619 ASSERT (func (BASE "link2/", BASE "link2") == -1);
620 ASSERT (errno == ELOOP || errno == ENOTDIR);
621 }
622 ASSERT (func (BASE "link2", BASE "link3") == 0);
623
624
625
626
627
628
629 ASSERT (unlink (BASE "link3") == 0);
630 }
631
632
633
634
635
636 {
637 ASSERT (symlink (BASE "nowhere", BASE "link2") == 0);
638
639
640
641
642
643
644 {
645 ASSERT (func (BASE "link2", BASE "link3") == 0);
646 errno = 0;
647 ASSERT (lstat (BASE "link2", &st) == -1);
648 ASSERT (errno == ENOENT);
649 memset (&st, 0, sizeof st);
650 ASSERT (lstat (BASE "link3", &st) == 0);
651 }
652 }
653
654
655
656
657
658
659 {
660 {
661 errno = 0;
662 ASSERT (func (BASE "link3/", BASE "link2") == -1);
663 ASSERT (errno == ENOENT || errno == ENOTDIR);
664 }
665 {
666 errno = 0;
667 ASSERT (func (BASE "link3", BASE "link2/") == -1);
668 ASSERT (errno == ENOENT || errno == ENOTDIR);
669 }
670 {
671 errno = 0;
672 ASSERT (lstat (BASE "link2", &st) == -1);
673 ASSERT (errno == ENOENT);
674 }
675 memset (&st, 0, sizeof st);
676 ASSERT (lstat (BASE "link3", &st) == 0);
677 }
678
679
680
681
682
683
684 {
685 {
686 errno = 0;
687 ASSERT (func (BASE "link1/", BASE "link2") == -1);
688 ASSERT (errno == ENOTDIR);
689 }
690 {
691 errno = 0;
692 ASSERT (func (BASE "link1", BASE "link3/") == -1);
693 ASSERT (errno == ENOENT || errno == ENOTDIR);
694 }
695 }
696
697
698
699
700
701
702
703
704
705 {
706 ASSERT (close (creat (BASE "file2", 0600)) == 0);
707
708
709
710
711
712
713
714 {
715 ASSERT (func (BASE "file2", BASE "link3") == 0);
716 errno = 0;
717 ASSERT (stat (BASE "file2", &st) == -1);
718 ASSERT (errno == ENOENT);
719 memset (&st, 0, sizeof st);
720 ASSERT (lstat (BASE "link3", &st) == 0);
721 ASSERT (S_ISREG (st.st_mode));
722 }
723
724
725
726
727
728
729 ASSERT (unlink (BASE "link3") == 0);
730 }
731
732
733
734
735
736 {
737 ASSERT (symlink (BASE "nowhere", BASE "link2") == 0);
738
739
740
741
742
743
744 ASSERT (close (creat (BASE "file2", 0600)) == 0);
745
746
747
748
749
750
751
752 {
753 ASSERT (func (BASE "link2", BASE "file2") == 0);
754 errno = 0;
755 ASSERT (lstat (BASE "link2", &st) == -1);
756 ASSERT (errno == ENOENT);
757 memset (&st, 0, sizeof st);
758 ASSERT (lstat (BASE "file2", &st) == 0);
759 ASSERT (S_ISLNK (st.st_mode));
760 }
761
762
763
764
765
766
767 ASSERT (unlink (BASE "file2") == 0);
768 }
769
770
771
772
773
774 {
775 {
776 errno = 0;
777 ASSERT (func (BASE "file/", BASE "link1") == -1);
778 ASSERT (errno == ENOTDIR);
779 }
780 {
781 errno = 0;
782 ASSERT (func (BASE "file", BASE "link1/") == -1);
783 ASSERT (errno == ENOTDIR || errno == ENOENT);
784 }
785 {
786 errno = 0;
787 ASSERT (func (BASE "link1/", BASE "file") == -1);
788 ASSERT (errno == ENOTDIR);
789 }
790 {
791 errno = 0;
792 ASSERT (func (BASE "link1", BASE "file/") == -1);
793 ASSERT (errno == ENOTDIR || errno == ENOENT);
794 memset (&st, 0, sizeof st);
795 ASSERT (lstat (BASE "file", &st) == 0);
796 ASSERT (S_ISREG (st.st_mode));
797 memset (&st, 0, sizeof st);
798 ASSERT (lstat (BASE "link1", &st) == 0);
799 ASSERT (S_ISLNK (st.st_mode));
800 }
801 }
802
803
804
805
806
807
808
809
810 {
811 {
812 errno = 0;
813 ASSERT (func (BASE "dir", BASE "link1") == -1);
814 ASSERT (errno == ENOTDIR);
815 }
816 {
817 errno = 0;
818 ASSERT (func (BASE "dir/", BASE "link1") == -1);
819 ASSERT (errno == ENOTDIR);
820 }
821 {
822 errno = 0;
823 ASSERT (func (BASE "dir", BASE "link1/") == -1);
824 ASSERT (errno == ENOTDIR);
825 }
826 }
827 {
828 {
829 errno = 0;
830 ASSERT (func (BASE "link1", BASE "dir") == -1);
831 ASSERT (errno == EISDIR || errno == ENOTDIR);
832 }
833 {
834 errno = 0;
835 ASSERT (func (BASE "link1", BASE "dir/") == -1);
836 ASSERT (errno == EISDIR || errno == ENOTDIR);
837 }
838 {
839 errno = 0;
840 ASSERT (func (BASE "link1/", BASE "dir") == -1);
841 ASSERT (errno == ENOTDIR);
842 memset (&st, 0, sizeof st);
843 ASSERT (lstat (BASE "link1", &st) == 0);
844 ASSERT (S_ISLNK (st.st_mode));
845 memset (&st, 0, sizeof st);
846 ASSERT (lstat (BASE "dir", &st) == 0);
847 ASSERT (S_ISDIR (st.st_mode));
848 }
849 }
850
851
852
853
854
855
856
857
858
859
860
861
862
863 {
864 int result;
865 ASSERT (symlink (BASE "dir2", BASE "link2") == 0);
866
867
868
869
870
871
872 errno = 0;
873 result = func (BASE "dir", BASE "link2/");
874 if (result == 0)
875 {
876
877 errno = 0;
878 ASSERT (lstat (BASE "dir", &st) == -1);
879 ASSERT (errno == ENOENT);
880 memset (&st, 0, sizeof st);
881 ASSERT (lstat (BASE "dir2", &st) == 0);
882 ASSERT (S_ISDIR (st.st_mode));
883 memset (&st, 0, sizeof st);
884 ASSERT (lstat (BASE "link2", &st) == 0);
885 ASSERT (S_ISLNK (st.st_mode));
886
887
888
889
890
891
892 {
893 ASSERT (func (BASE "link2/", BASE "dir") == 0);
894 memset (&st, 0, sizeof st);
895 ASSERT (lstat (BASE "dir", &st) == 0);
896 ASSERT (S_ISDIR (st.st_mode));
897 errno = 0;
898 ASSERT (lstat (BASE "dir2", &st) == -1);
899 ASSERT (errno == ENOENT);
900 memset (&st, 0, sizeof st);
901 ASSERT (lstat (BASE "link2", &st) == 0);
902 ASSERT (S_ISLNK (st.st_mode));
903 }
904 }
905 else
906 {
907
908 ASSERT (result == -1);
909 ASSERT (errno == ENOTDIR);
910 memset (&st, 0, sizeof st);
911 ASSERT (lstat (BASE "dir", &st) == 0);
912 ASSERT (S_ISDIR (st.st_mode));
913 errno = 0;
914 ASSERT (lstat (BASE "dir2", &st) == -1);
915 ASSERT (errno == ENOENT);
916 memset (&st, 0, sizeof st);
917 ASSERT (lstat (BASE "link2", &st) == 0);
918 ASSERT (S_ISLNK (st.st_mode));
919 ASSERT (unlink (BASE "link2") == 0);
920 ASSERT (symlink (BASE "dir", BASE "link2") == 0);
921
922
923
924
925
926
927 errno = 0;
928 result = func (BASE "link2/", BASE "dir");
929 if (result)
930 {
931 ASSERT (result == -1);
932 ASSERT (errno == ENOTDIR || errno == EISDIR);
933 }
934 memset (&st, 0, sizeof st);
935 ASSERT (lstat (BASE "dir", &st) == 0);
936 ASSERT (S_ISDIR (st.st_mode));
937 errno = 0;
938 ASSERT (lstat (BASE "dir2", &st) == -1);
939 ASSERT (errno == ENOENT);
940 memset (&st, 0, sizeof st);
941 ASSERT (lstat (BASE "link2", &st) == 0);
942 ASSERT (S_ISLNK (st.st_mode));
943 }
944 }
945
946
947
948
949
950
951
952
953 ASSERT (unlink (BASE "file") == 0);
954 ASSERT (rmdir (BASE "dir") == 0);
955 ASSERT (unlink (BASE "link1") == 0);
956 ASSERT (unlink (BASE "link2") == 0);
957
958 return 0;
959 }