| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/coreutils/src/src/find-mount-point.c |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /* find-mount-point.c -- find the root mount point for a file. | - | ||||||||||||
| 2 | Copyright (C) 2010-2018 Free Software Foundation, Inc. | - | ||||||||||||
| 3 | - | |||||||||||||
| 4 | This program is free software: you can redistribute it and/or modify | - | ||||||||||||
| 5 | it under the terms of the GNU General Public License as published by | - | ||||||||||||
| 6 | the Free Software Foundation, either version 3 of the License, or | - | ||||||||||||
| 7 | (at your option) any later version. | - | ||||||||||||
| 8 | - | |||||||||||||
| 9 | This program is distributed in the hope that it will be useful, | - | ||||||||||||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | - | ||||||||||||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | - | ||||||||||||
| 12 | GNU General Public License for more details. | - | ||||||||||||
| 13 | - | |||||||||||||
| 14 | You should have received a copy of the GNU General Public License | - | ||||||||||||
| 15 | along with this program. If not, see <https://www.gnu.org/licenses/>. */ | - | ||||||||||||
| 16 | - | |||||||||||||
| 17 | #include <config.h> | - | ||||||||||||
| 18 | #include <sys/types.h> | - | ||||||||||||
| 19 | - | |||||||||||||
| 20 | #include "system.h" | - | ||||||||||||
| 21 | #include "die.h" | - | ||||||||||||
| 22 | #include "error.h" | - | ||||||||||||
| 23 | #include "save-cwd.h" | - | ||||||||||||
| 24 | #include "xgetcwd.h" | - | ||||||||||||
| 25 | #include "find-mount-point.h" | - | ||||||||||||
| 26 | - | |||||||||||||
| 27 | /* Return the root mountpoint of the file system on which FILE exists, in | - | ||||||||||||
| 28 | malloced storage. FILE_STAT should be the result of stating FILE. | - | ||||||||||||
| 29 | Give a diagnostic and return NULL if unable to determine the mount point. | - | ||||||||||||
| 30 | Exit if unable to restore current working directory. */ | - | ||||||||||||
| 31 | extern char * | - | ||||||||||||
| 32 | find_mount_point (char const *file, struct stat const *file_stat) | - | ||||||||||||
| 33 | { | - | ||||||||||||
| 34 | struct saved_cwd cwd; | - | ||||||||||||
| 35 | struct stat last_stat; | - | ||||||||||||
| 36 | char *mp = NULL; /* The malloc'd mount point. */ | - | ||||||||||||
| 37 | - | |||||||||||||
| 38 | if (save_cwd (&cwd) != 0)
| 0-6 | ||||||||||||
| 39 | { | - | ||||||||||||
| 40 | error (0, errno, _("cannot get current directory")); | - | ||||||||||||
| 41 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
| 42 | } | - | ||||||||||||
| 43 | - | |||||||||||||
| 44 | if (S_ISDIR (file_stat->st_mode))
| 0-6 | ||||||||||||
| 45 | /* FILE is a directory, so just chdir there directly. */ | - | ||||||||||||
| 46 | { | - | ||||||||||||
| 47 | last_stat = *file_stat; | - | ||||||||||||
| 48 | if (chdir (file) < 0)
| 0-6 | ||||||||||||
| 49 | { | - | ||||||||||||
| 50 | error (0, errno, _("cannot change to directory %s"), quoteaf (file)); | - | ||||||||||||
| 51 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
| 52 | } | - | ||||||||||||
| 53 | } executed 6 times by 2 tests: end of blockExecuted by:
| 6 | ||||||||||||
| 54 | else | - | ||||||||||||
| 55 | /* FILE is some other kind of file; use its directory. */ | - | ||||||||||||
| 56 | { | - | ||||||||||||
| 57 | char *xdir = dir_name (file); | - | ||||||||||||
| 58 | char *dir; | - | ||||||||||||
| 59 | ASSIGN_STRDUPA (dir, xdir); | - | ||||||||||||
| 60 | free (xdir); | - | ||||||||||||
| 61 | - | |||||||||||||
| 62 | if (chdir (dir) < 0)
| 0 | ||||||||||||
| 63 | { | - | ||||||||||||
| 64 | error (0, errno, _("cannot change to directory %s"), quoteaf (dir)); | - | ||||||||||||
| 65 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
| 66 | } | - | ||||||||||||
| 67 | - | |||||||||||||
| 68 | if (stat (".", &last_stat) < 0)
| 0 | ||||||||||||
| 69 | { | - | ||||||||||||
| 70 | error (0, errno, _("cannot stat current directory (now %s)"), | - | ||||||||||||
| 71 | quoteaf (dir)); | - | ||||||||||||
| 72 | goto done; never executed: goto done; | 0 | ||||||||||||
| 73 | } | - | ||||||||||||
| 74 | } never executed: end of block | 0 | ||||||||||||
| 75 | - | |||||||||||||
| 76 | /* Now walk up FILE's parents until we find another file system or /, | - | ||||||||||||
| 77 | chdiring as we go. LAST_STAT holds stat information for the last place | - | ||||||||||||
| 78 | we visited. */ | - | ||||||||||||
| 79 | while (true) | - | ||||||||||||
| 80 | { | - | ||||||||||||
| 81 | struct stat st; | - | ||||||||||||
| 82 | if (stat ("..", &st) < 0)
| 0-48 | ||||||||||||
| 83 | { | - | ||||||||||||
| 84 | error (0, errno, _("cannot stat %s"), quoteaf ("..")); | - | ||||||||||||
| 85 | goto done; never executed: goto done; | 0 | ||||||||||||
| 86 | } | - | ||||||||||||
| 87 | if (st.st_dev != last_stat.st_dev || st.st_ino == last_stat.st_ino)
| 0-48 | ||||||||||||
| 88 | /* cwd is the mount point. */ | - | ||||||||||||
| 89 | break; executed 6 times by 2 tests: break;Executed by:
| 6 | ||||||||||||
| 90 | if (chdir ("..") < 0)
| 0-42 | ||||||||||||
| 91 | { | - | ||||||||||||
| 92 | error (0, errno, _("cannot change to directory %s"), quoteaf ("..")); | - | ||||||||||||
| 93 | goto done; never executed: goto done; | 0 | ||||||||||||
| 94 | } | - | ||||||||||||
| 95 | last_stat = st; | - | ||||||||||||
| 96 | } executed 42 times by 2 tests: end of blockExecuted by:
| 42 | ||||||||||||
| 97 | - | |||||||||||||
| 98 | /* Finally reached a mount point, see what it's called. */ | - | ||||||||||||
| 99 | mp = xgetcwd (); | - | ||||||||||||
| 100 | - | |||||||||||||
| 101 | done: code before this statement executed 6 times by 2 tests: done:Executed by:
| 6 | ||||||||||||
| 102 | /* Restore the original cwd. */ | - | ||||||||||||
| 103 | { | - | ||||||||||||
| 104 | int save_errno = errno; | - | ||||||||||||
| 105 | if (restore_cwd (&cwd) != 0)
| 0-6 | ||||||||||||
| 106 | die (EXIT_FAILURE, errno, never executed: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, (*__errno_location ()), dcgettext (((void *)0), \"failed to return to initial working directory\", 5)), assume (false))" ")"); int _gl_dummy; })) ? ((error ( 1 , (*__errno_loca... initial working directory" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , (*__errno_location ()) , dcgettext (((void *)0), "failed to return to initial working directory" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ())))) ; | 0 | ||||||||||||
| 107 | _("failed to return to initial working directory")); never executed: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, (*__errno_location ()), dcgettext (((void *)0), \"failed to return to initial working directory\", 5)), assume (false))" ")"); int _gl_dummy; })) ? ((error ( 1 , (*__errno_loca... initial working directory" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , (*__errno_location ()) , dcgettext (((void *)0), "failed to return to initial working directory" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ())))) ; | 0 | ||||||||||||
| 108 | free_cwd (&cwd); | - | ||||||||||||
| 109 | errno = save_errno; | - | ||||||||||||
| 110 | } | - | ||||||||||||
| 111 | - | |||||||||||||
| 112 | return mp; executed 6 times by 2 tests: return mp;Executed by:
| 6 | ||||||||||||
| 113 | } | - | ||||||||||||
| Source code | Switch to Preprocessed file |