Prompt whether to remove FILENAME, if required via a combination of the options specified by X and/or file attributes. If the file may be removed, return RM_OK or RM_USER_ACCEPTED, the latter if the user was prompted and accepted. If the user declines to remove the file, return RM_USER_DECLINED. If not ignoring missing files and we cannot lstat FILENAME, then return RM_ERROR.
| 186 | Use and update *DIR_STATUS as needed, via the conventions of |
| 187 | get_dir_status. */ |
| 188 | static enum RM_status |
| 189 | prompt (FTS const *fts, FTSENT const *ent, bool is_dir, |
| 190 | struct rm_options const *x, enum Prompt_action mode, |
| 191 | int *dir_status) |
| 192 | { |
| 193 | int fd_cwd = fts->fts_cwd_fd; |
| 194 | char const *full_name = ent->fts_path; |
| 195 | char const *filename = ent->fts_accpath; |
| 196 | struct stat st; |
| 197 | struct stat *sbuf = &st; |
| 198 | cache_stat_init (sbuf); |
| 199 | |
| 200 | int dirent_type = is_dir ? DT_DIR : DT_UNKNOWN; |
| 201 | int write_protected = 0; |
| 202 | |
| 203 | /* When nonzero, this indicates that we failed to remove a child entry, |
| 204 | either because the user declined an interactive prompt, or due to |
| 205 | some other failure, like permissions. */ |
| 206 | if (ent->fts_number) |
| 207 | return RM_USER_DECLINED; |
| 208 | |
| 209 | if (x->interactive == RMI_NEVER) |
| 210 | return RM_OK; |
| 211 | |
| 212 | int wp_errno = 0; |
| 213 | if (!x->ignore_missing_files |
| 214 | && (x->interactive == RMI_ALWAYS || x->stdin_tty) |
| 215 | && dirent_type != DT_LNK) |
| 216 | { |
| 217 | write_protected = write_protected_non_symlink (fd_cwd, filename, sbuf); |
| 218 | wp_errno = errno; |
| 219 | } |
| 220 | |
| 221 | if (write_protected || x->interactive == RMI_ALWAYS) |
| 222 | { |
| 223 | if (0 <= write_protected && dirent_type == DT_UNKNOWN) |
| 224 | { |
| 225 | if (cache_fstatat (fd_cwd, filename, sbuf, AT_SYMLINK_NOFOLLOW) == 0) |
| 226 | { |
| 227 | if (S_ISLNK (sbuf->st_mode)) |
| 228 | dirent_type = DT_LNK; |
| 229 | else if (S_ISDIR (sbuf->st_mode)) |
| 230 | dirent_type = DT_DIR; |
| 231 | /* Otherwise it doesn't matter, so leave it DT_UNKNOWN. */ |
| 232 | } |
| 233 | else |
| 234 | { |
| 235 | /* This happens, e.g., with 'rm '''. */ |
| 236 | write_protected = -1; |
| 237 | wp_errno = errno; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | if (0 <= write_protected) |
| 242 | switch (dirent_type) |
| 243 | { |
| 244 | case DT_LNK: |
| 245 | /* Using permissions doesn't make sense for symlinks. */ |
no test coverage detected