Mercurial > vim
comparison src/fileio.c @ 557:862863033fdd v7.0158
updated for version 7.0158
author | vimboss |
---|---|
date | Wed, 23 Nov 2005 21:25:05 +0000 |
parents | 74c2ea3c3081 |
children | afd8e69dba19 |
comparison
equal
deleted
inserted
replaced
556:f9eaf0a9872d | 557:862863033fdd |
---|---|
3109 * - we can't set the owner/group of the new file | 3109 * - we can't set the owner/group of the new file |
3110 */ | 3110 */ |
3111 if (st_old.st_nlink > 1 | 3111 if (st_old.st_nlink > 1 |
3112 || mch_lstat((char *)fname, &st) < 0 | 3112 || mch_lstat((char *)fname, &st) < 0 |
3113 || st.st_dev != st_old.st_dev | 3113 || st.st_dev != st_old.st_dev |
3114 || st.st_ino != st_old.st_ino) | 3114 || st.st_ino != st_old.st_ino |
3115 # ifndef HAVE_FCHOWN | |
3116 || st.st_uid != st_old.st_uid | |
3117 || st.st_gid != st_old.st_gid | |
3118 # endif | |
3119 ) | |
3115 backup_copy = TRUE; | 3120 backup_copy = TRUE; |
3116 else | 3121 else |
3117 # endif | 3122 # endif |
3118 { | 3123 { |
3119 /* | 3124 /* |
3124 */ | 3129 */ |
3125 STRCPY(IObuff, fname); | 3130 STRCPY(IObuff, fname); |
3126 for (i = 4913; ; i += 123) | 3131 for (i = 4913; ; i += 123) |
3127 { | 3132 { |
3128 sprintf((char *)gettail(IObuff), "%d", i); | 3133 sprintf((char *)gettail(IObuff), "%d", i); |
3129 if (mch_stat((char *)IObuff, &st) < 0) | 3134 if (mch_lstat((char *)IObuff, &st) < 0) |
3130 break; | 3135 break; |
3131 } | 3136 } |
3132 fd = mch_open((char *)IObuff, O_CREAT|O_WRONLY|O_EXCL, perm); | 3137 fd = mch_open((char *)IObuff, |
3133 close(fd); | 3138 O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm); |
3134 if (fd < 0) /* can't write in directory */ | 3139 if (fd < 0) /* can't write in directory */ |
3135 backup_copy = TRUE; | 3140 backup_copy = TRUE; |
3136 else | 3141 else |
3137 { | 3142 { |
3138 # ifdef UNIX | 3143 # ifdef UNIX |
3139 chown((char *)IObuff, st_old.st_uid, st_old.st_gid); | 3144 # ifdef HAVE_FCHOWN |
3140 (void)mch_setperm(IObuff, perm); | 3145 fchown(fd, st_old.st_uid, st_old.st_gid); |
3146 # endif | |
3141 if (mch_stat((char *)IObuff, &st) < 0 | 3147 if (mch_stat((char *)IObuff, &st) < 0 |
3142 || st.st_uid != st_old.st_uid | 3148 || st.st_uid != st_old.st_uid |
3143 || st.st_gid != st_old.st_gid | 3149 || st.st_gid != st_old.st_gid |
3144 || st.st_mode != perm) | 3150 || st.st_mode != perm) |
3145 backup_copy = TRUE; | 3151 backup_copy = TRUE; |
3146 # endif | 3152 # endif |
3147 mch_remove(IObuff); | 3153 mch_remove(IObuff); |
3154 close(fd); | |
3148 } | 3155 } |
3149 } | 3156 } |
3150 } | 3157 } |
3151 | 3158 |
3152 # ifdef UNIX | 3159 # ifdef UNIX |
3336 /* remove old backup, if present */ | 3343 /* remove old backup, if present */ |
3337 mch_remove(backup); | 3344 mch_remove(backup); |
3338 /* Open with O_EXCL to avoid the file being created while | 3345 /* Open with O_EXCL to avoid the file being created while |
3339 * we were sleeping (symlink hacker attack?) */ | 3346 * we were sleeping (symlink hacker attack?) */ |
3340 bfd = mch_open((char *)backup, | 3347 bfd = mch_open((char *)backup, |
3341 O_WRONLY|O_CREAT|O_EXTRA|O_EXCL, perm & 0777); | 3348 O_WRONLY|O_CREAT|O_EXTRA|O_EXCL|O_NOFOLLOW, |
3349 perm & 0777); | |
3342 if (bfd < 0) | 3350 if (bfd < 0) |
3343 { | 3351 { |
3344 vim_free(backup); | 3352 vim_free(backup); |
3345 backup = NULL; | 3353 backup = NULL; |
3346 } | 3354 } |
3355 * Try to set the group of the backup same as the | 3363 * Try to set the group of the backup same as the |
3356 * original file. If this fails, set the protection | 3364 * original file. If this fails, set the protection |
3357 * bits for the group same as the protection bits for | 3365 * bits for the group same as the protection bits for |
3358 * others. | 3366 * others. |
3359 */ | 3367 */ |
3360 if (st_new.st_gid != st_old.st_gid && | 3368 if (st_new.st_gid != st_old.st_gid |
3361 # ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */ | 3369 # ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */ |
3362 fchown(bfd, (uid_t)-1, st_old.st_gid) != 0 | 3370 && fchown(bfd, (uid_t)-1, st_old.st_gid) != 0 |
3363 # else | |
3364 chown((char *)backup, (uid_t)-1, st_old.st_gid) != 0 | |
3365 # endif | 3371 # endif |
3366 ) | 3372 ) |
3367 mch_setperm(backup, | 3373 mch_setperm(backup, |
3368 (perm & 0707) | ((perm & 07) << 3)); | 3374 (perm & 0707) | ((perm & 07) << 3)); |
3369 #endif | 3375 #endif |
4021 errmsg = (char_u *)_("E667: Fsync failed"); | 4027 errmsg = (char_u *)_("E667: Fsync failed"); |
4022 end = 0; | 4028 end = 0; |
4023 } | 4029 } |
4024 #endif | 4030 #endif |
4025 | 4031 |
4032 #ifdef UNIX | |
4033 /* When creating a new file, set its owner/group to that of the original | |
4034 * file. Get the new device and inode number. */ | |
4035 if (backup != NULL && !backup_copy) | |
4036 { | |
4037 # ifdef HAVE_FCHOWN | |
4038 struct stat st; | |
4039 | |
4040 /* don't change the owner when it's already OK, some systems remove | |
4041 * permission or ACL stuff */ | |
4042 if (mch_stat((char *)wfname, &st) < 0 | |
4043 || st.st_uid != st_old.st_uid | |
4044 || st.st_gid != st_old.st_gid) | |
4045 { | |
4046 fchown(fd, st_old.st_uid, st_old.st_gid); | |
4047 if (perm >= 0) /* set permission again, may have changed */ | |
4048 (void)mch_setperm(wfname, perm); | |
4049 } | |
4050 # endif | |
4051 buf_setino(buf); | |
4052 } | |
4053 #endif | |
4054 | |
4026 if (close(fd) != 0) | 4055 if (close(fd) != 0) |
4027 { | 4056 { |
4028 errmsg = (char_u *)_("E512: Close failed"); | 4057 errmsg = (char_u *)_("E512: Close failed"); |
4029 end = 0; | 4058 end = 0; |
4030 } | 4059 } |
4043 #ifdef HAVE_ACL | 4072 #ifdef HAVE_ACL |
4044 /* Probably need to set the ACL before changing the user (can't set the | 4073 /* Probably need to set the ACL before changing the user (can't set the |
4045 * ACL on a file the user doesn't own). */ | 4074 * ACL on a file the user doesn't own). */ |
4046 if (!backup_copy) | 4075 if (!backup_copy) |
4047 mch_set_acl(wfname, acl); | 4076 mch_set_acl(wfname, acl); |
4048 #endif | |
4049 | |
4050 #ifdef UNIX | |
4051 /* When creating a new file, set its owner/group to that of the original | |
4052 * file. Get the new device and inode number. */ | |
4053 if (backup != NULL && !backup_copy) | |
4054 { | |
4055 struct stat st; | |
4056 | |
4057 /* don't change the owner when it's already OK, some systems remove | |
4058 * permission or ACL stuff */ | |
4059 if (mch_stat((char *)wfname, &st) < 0 | |
4060 || st.st_uid != st_old.st_uid | |
4061 || st.st_gid != st_old.st_gid) | |
4062 { | |
4063 chown((char *)wfname, st_old.st_uid, st_old.st_gid); | |
4064 if (perm >= 0) /* set permission again, may have changed */ | |
4065 (void)mch_setperm(wfname, perm); | |
4066 } | |
4067 buf_setino(buf); | |
4068 } | |
4069 #endif | 4077 #endif |
4070 | 4078 |
4071 | 4079 |
4072 #if defined(FEAT_MBYTE) && defined(FEAT_EVAL) | 4080 #if defined(FEAT_MBYTE) && defined(FEAT_EVAL) |
4073 if (wfname != fname) | 4081 if (wfname != fname) |
4286 else | 4294 else |
4287 { | 4295 { |
4288 int empty_fd; | 4296 int empty_fd; |
4289 | 4297 |
4290 if (org == NULL | 4298 if (org == NULL |
4291 || (empty_fd = mch_open(org, O_CREAT | O_EXTRA | O_EXCL, | 4299 || (empty_fd = mch_open(org, |
4300 O_CREAT | O_EXTRA | O_EXCL | O_NOFOLLOW, | |
4292 perm < 0 ? 0666 : (perm & 0777))) < 0) | 4301 perm < 0 ? 0666 : (perm & 0777))) < 0) |
4293 EMSG(_("E206: patchmode: can't touch empty original file")); | 4302 EMSG(_("E206: patchmode: can't touch empty original file")); |
4294 else | 4303 else |
4295 close(empty_fd); | 4304 close(empty_fd); |
4296 } | 4305 } |
5755 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0); | 5764 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0); |
5756 if (fd_in == -1) | 5765 if (fd_in == -1) |
5757 return -1; | 5766 return -1; |
5758 | 5767 |
5759 /* Create the new file with same permissions as the original. */ | 5768 /* Create the new file with same permissions as the original. */ |
5760 fd_out = mch_open((char *)to, O_CREAT|O_EXCL|O_WRONLY|O_EXTRA, (int)perm); | 5769 fd_out = mch_open((char *)to, |
5770 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm); | |
5761 if (fd_out == -1) | 5771 if (fd_out == -1) |
5762 { | 5772 { |
5763 close(fd_in); | 5773 close(fd_in); |
5764 return -1; | 5774 return -1; |
5765 } | 5775 } |
7271 char_u *save_ei; | 7281 char_u *save_ei; |
7272 | 7282 |
7273 save_ei = vim_strsave(p_ei); | 7283 save_ei = vim_strsave(p_ei); |
7274 if (save_ei != NULL) | 7284 if (save_ei != NULL) |
7275 { | 7285 { |
7276 new_ei = vim_strnsave(p_ei, (int)STRLEN(p_ei) + 8); | 7286 new_ei = vim_strnsave(p_ei, (int)(STRLEN(p_ei) + STRLEN(what))); |
7277 if (new_ei != NULL) | 7287 if (new_ei != NULL) |
7278 { | 7288 { |
7279 STRCAT(new_ei, what); | 7289 STRCAT(new_ei, what); |
7280 set_string_option_direct((char_u *)"ei", -1, new_ei, OPT_FREE); | 7290 set_string_option_direct((char_u *)"ei", -1, new_ei, OPT_FREE); |
7281 vim_free(new_ei); | 7291 vim_free(new_ei); |