2
0

isrename.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*%% (c) Copyright 1993, 1994 Hewlett-Packard Company */
  24. /*%% (c) Copyright 1993, 1994 International Business Machines Corp. */
  25. /*%% (c) Copyright 1993, 1994 Sun Microsystems, Inc. */
  26. /*%% (c) Copyright 1993, 1994 Novell, Inc. */
  27. /*%% $XConsortium: isrename.c /main/3 1995/10/23 11:43:48 rswiston $ */
  28. /*
  29. * Copyright (c) 1988 by Sun Microsystems, Inc.
  30. */
  31. /*
  32. * isrename.c
  33. *
  34. * Description:
  35. * Rename an ISAM file.
  36. */
  37. #include "isam_impl.h"
  38. #include <unistd.h>
  39. #include <sys/time.h>
  40. void _removelast();
  41. static void _removelast2();
  42. char *_lastelement();
  43. static void _rename_datfile(), _rename_indfile(), _rename_varfile();
  44. static int _amrename();
  45. /*
  46. * isfd = isrename(oldname, newname)
  47. *
  48. *
  49. * Errors:
  50. * EBADFILE ISAM file is corrupted or it is not an NetISAM file
  51. * EFLOCKED The file is exclusively locked by other process.
  52. * EFNAME Invalid ISAM file name
  53. * EFNAME ISAM file does not exist
  54. * ETOOMANY Too many ISAM file descriptors are in use (128 is the limit)
  55. *
  56. * The following error code is "borrowed" from UNIX:
  57. * EACCES UNIX file system protection denies access to the file:
  58. * - mode is INOUT or OUTPUT and ISAM file is on
  59. * a Read-Only mounted file system
  60. * - UNIX file permissions don't allow access to the file
  61. */
  62. int
  63. isrename(char *oldname, char *newname)
  64. {
  65. Isfd isfd, isfd2;
  66. Fab *fab;
  67. char olddir [MAXPATHLEN];
  68. char newdir [MAXPATHLEN];
  69. char datfname[MAXPATHLEN];
  70. /*
  71. * Check that the odl and new filename are in the same directory.
  72. */
  73. snprintf(olddir, sizeof(olddir), "%s", oldname);
  74. _removelast2(olddir);
  75. snprintf(newdir, sizeof(newdir), "%s", newname);
  76. _removelast2(newdir);
  77. if (strcmp(newdir, olddir) != 0) {
  78. _setiserrno2(EFNAME, 9, 0);
  79. return (ISERROR);
  80. }
  81. /*
  82. * Open the file
  83. */
  84. if ((isfd = isopen(oldname, ISINOUT)) == -1)
  85. return (ISERROR); /* iserrno is set */
  86. /*
  87. * Reject rename if 'newfile' exists.
  88. */
  89. if ((isfd2 = isopen(newname, ISINOUT)) != -1 || iserrno != ENOENT) {
  90. _setiserrno2(EEXIST, 9, 0);
  91. if (isfd2 >= 0) (void)isclose(isfd2);
  92. return (ISERROR); /* iserrno is set */
  93. }
  94. /*
  95. * Get File Access Block.
  96. */
  97. if ((fab = _isfd_find(isfd)) == NULL) {
  98. _isfatal_error("isrename() cannot find FAB");
  99. _setiserrno2(EFATAL,'9','0');
  100. return (ISERROR);
  101. }
  102. if (_amrename(&fab->isfhandle, _lastelement(newname), &fab->errcode)) {
  103. _seterr_errcode(&fab->errcode);
  104. (void)isclose(isfd);
  105. return (ISERROR);
  106. }
  107. _fab_destroy(fab); /* Deallocate Fab object */
  108. _isfd_delete(isfd);
  109. /*
  110. * We must unlink() the .rec file, or isbuild() with the same
  111. * following immediatly would fail because for NFS files the client
  112. * still thinks that the file exists for a few seconds.
  113. */
  114. snprintf(datfname, sizeof(datfname), "%s", oldname);
  115. _makedat_isfname(datfname);
  116. (void)unlink(datfname);
  117. return (ISOK); /* Successful isrename() */
  118. }
  119. /*
  120. * _removelast2(path)
  121. *
  122. * Remove last element of path. E.g. /usr/db/part yields /usr/db.
  123. * Unlike _removelast() that path does not have to start with '/'.
  124. */
  125. Static void
  126. _removelast2(char *path)
  127. {
  128. char *p;
  129. for (p = path + strlen(path); *--p != '/' && p >= path; )
  130. *p = '\0';
  131. }
  132. /*
  133. * _amrename(isfhandle, newname)
  134. *
  135. * _amrename() renames ISAM file
  136. *
  137. * Input params:
  138. * isfhandle Handle of ISAM file
  139. *
  140. * Output params:
  141. * errcode Error code
  142. *
  143. */
  144. static int
  145. _amrename(Bytearray *isfhandle, char *newname, struct errcode *errcode)
  146. {
  147. Fcb *fcb;
  148. char *isfname = _getisfname(isfhandle);
  149. _isam_entryhook();
  150. /*
  151. * Get FCB corresponding to the isfhandle handle.
  152. */
  153. if ((fcb = _openfcb(isfhandle, errcode)) == NULL) {
  154. goto ERROR;
  155. }
  156. /*
  157. * Delete FCB and remove it from FCB cache.
  158. */
  159. (void) _watchfd_decr(_isfcb_nfds(fcb));
  160. _isfcb_close(fcb);
  161. _mngfcb_delete(isfhandle);
  162. /*
  163. * Rename all UNIX files.
  164. */
  165. _rename_datfile(isfname, newname);
  166. _rename_indfile(isfname, newname);
  167. _rename_varfile(isfname, newname);
  168. _isam_exithook();
  169. return (ISOK);
  170. ERROR:
  171. _isam_exithook();
  172. return (ISERROR);
  173. }
  174. /* newname, with no prefix */
  175. Static void
  176. _rename_datfile(char *isfname, char *newname)
  177. {
  178. char namebuf[MAXPATHLEN];
  179. char newbuf[MAXPATHLEN];
  180. char newbuftemp[sizeof(newbuf)];
  181. snprintf(namebuf, sizeof(namebuf), "%s", isfname);
  182. snprintf(newbuf, sizeof(newbuf), "%s", isfname);
  183. /*
  184. * Replace the last element of the old path with newname.
  185. */
  186. _removelast(newbuf);
  187. if (strcmp(newbuf, "/") != 0) {
  188. snprintf(newbuftemp, sizeof(newbuftemp), "%s/", newbuf);
  189. strcpy(newbuf, newbuftemp);
  190. }
  191. snprintf(newbuftemp, sizeof(newbuftemp), "%s%s", newbuf, newname);
  192. strcpy(newbuf, newbuftemp);
  193. _makedat_isfname(namebuf);
  194. _makedat_isfname(newbuf);
  195. (void)rename(namebuf, newbuf);
  196. }
  197. /* newname, with no prefix */
  198. Static void
  199. _rename_indfile(char *isfname, char *newname)
  200. {
  201. char namebuf[MAXPATHLEN];
  202. char newbuf[MAXPATHLEN];
  203. char newbuftemp[MAXPATHLEN];
  204. snprintf(namebuf, sizeof(namebuf), "%s", isfname);
  205. snprintf(newbuf, sizeof(newbuf), "%s", isfname);
  206. /*
  207. * Replace the last element of the old path with newname.
  208. */
  209. _removelast(newbuf);
  210. if (strcmp(newbuf, "/") != 0) {
  211. snprintf(newbuftemp, sizeof(newbuftemp), "%s/", newbuf);
  212. strcpy(newbuf, newbuftemp);
  213. }
  214. snprintf(newbuftemp, sizeof(newbuftemp), "%s%s", newbuf, newname);
  215. strcpy(newbuf, newbuftemp);
  216. _makeind_isfname(namebuf);
  217. _makeind_isfname(newbuf);
  218. (void)rename(namebuf, newbuf);
  219. }
  220. /* newname, with no prefix */
  221. Static void
  222. _rename_varfile(char *isfname, char *newname)
  223. {
  224. char namebuf[MAXPATHLEN];
  225. char newbuf[MAXPATHLEN];
  226. char newbuftemp[MAXPATHLEN];
  227. snprintf(namebuf, sizeof(namebuf), "%s", isfname);
  228. snprintf(newbuf, sizeof(newbuf), "%s", isfname);
  229. /*
  230. * Replace the last element of the old path with newname.
  231. */
  232. _removelast(newbuf);
  233. if (strcmp(newbuf, "/") != 0) {
  234. snprintf(newbuftemp, sizeof(newbuftemp), "%s/", newbuf);
  235. strcpy(newbuf, newbuftemp);
  236. }
  237. snprintf(newbuftemp, sizeof(newbuftemp), "%s%s", newbuf, newname);
  238. strcpy(newbuf, newbuftemp);
  239. _makevar_isfname(namebuf);
  240. _makevar_isfname(newbuf);
  241. (void)rename(namebuf, newbuf);
  242. }