1 /* $OpenBSD: commit.c,v 1.35 2005/05/30 07:37:01 xsa Exp $ */ 2 /* 3 * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. The name of the author may not be used to endorse or promote products 13 * derived from this software without specific prior written permission. 14 * 15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 16 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 17 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 18 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 24 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/types.h> 28 #include <sys/queue.h> 29 #include <sys/stat.h> 30 31 #include <errno.h> 32 #include <stdio.h> 33 #include <fcntl.h> 34 #include <stdlib.h> 35 #include <unistd.h> 36 #include <string.h> 37 38 #include "cvs.h" 39 #include "log.h" 40 #include "buf.h" 41 #include "proto.h" 42 43 44 static int cvs_commit_init (struct cvs_cmd *, int, char **, int *); 45 static int cvs_commit_prepare (CVSFILE *, void *); 46 static int cvs_commit_file (CVSFILE *, void *); 47 static int cvs_commit_pre_exec(struct cvsroot *); 48 49 struct cvs_cmd cvs_cmd_commit = { 50 CVS_OP_COMMIT, CVS_REQ_CI, "commit", 51 { "ci", "com" }, 52 "Check files into the repository", 53 "[-flR] [-F logfile | -m msg] [-r rev] ...", 54 "F:flm:Rr:", 55 NULL, 56 CF_RECURSE | CF_IGNORE | CF_SORT, 57 cvs_commit_init, 58 cvs_commit_pre_exec, 59 cvs_commit_file, 60 NULL, 61 NULL, 62 NULL, 63 CVS_CMD_ALLOWSPEC | CVS_CMD_SENDARGS2 64 }; 65 66 static char *mfile = NULL; 67 static char *rev = NULL; 68 static char **commit_files = NULL; 69 static int commit_fcount = 0; 70 71 static int 72 cvs_commit_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg) 73 { 74 int ch; 75 76 while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) { 77 switch (ch) { 78 case 'F': 79 mfile = optarg; 80 break; 81 case 'f': 82 /* XXX half-implemented */ 83 cmd->file_flags &= ~CF_RECURSE; 84 break; 85 case 'l': 86 cmd->file_flags &= ~CF_RECURSE; 87 break; 88 case 'm': 89 cvs_msg = strdup(optarg); 90 if (cvs_msg == NULL) { 91 cvs_log(LP_ERRNO, "failed to copy message"); 92 return (CVS_EX_USAGE); 93 } 94 break; 95 case 'R': 96 cmd->file_flags |= CF_RECURSE; 97 break; 98 case 'r': 99 rev = optarg; 100 break; 101 default: 102 return (CVS_EX_USAGE); 103 } 104 } 105 106 if ((cvs_msg != NULL) && (mfile != NULL)) { 107 cvs_log(LP_ERR, "the -F and -m flags are mutually exclusive"); 108 return (CVS_EX_USAGE); 109 } 110 111 if ((mfile != NULL) && (cvs_msg = cvs_logmsg_open(mfile)) == NULL) 112 return (CVS_EX_DATA); 113 114 *arg = optind; 115 116 commit_files = (argv + optind); 117 commit_fcount = (argc - optind); 118 119 return (0); 120 } 121 122 int 123 cvs_commit_pre_exec(struct cvsroot *root) 124 { 125 struct cvs_flist cl; 126 CVSFILE *cfp; 127 CVSFILE *tmp; 128 int flags = CF_RECURSE | CF_IGNORE | CF_SORT; 129 130 SIMPLEQ_INIT(&cl); 131 132 if (commit_fcount != 0) { 133 tmp = cvs_file_getspec(commit_files, commit_fcount, 134 flags, cvs_commit_prepare, &cl); 135 } else { 136 tmp = cvs_file_get(".", flags, cvs_commit_prepare, &cl); 137 } 138 139 if (tmp == NULL) 140 return (CVS_EX_DATA); 141 142 if (SIMPLEQ_EMPTY(&cl)) { 143 cvs_file_free(tmp); 144 return (0); 145 } 146 147 if (cvs_msg == NULL) 148 cvs_msg = cvs_logmsg_get(CVS_FILE_NAME(tmp), 149 NULL, &cl, NULL); 150 151 cvs_file_free(tmp); 152 153 while (!SIMPLEQ_EMPTY(&cl)) { 154 cfp = SIMPLEQ_FIRST(&cl); 155 SIMPLEQ_REMOVE_HEAD(&cl, cf_list); 156 cvs_file_free(cfp); 157 } 158 159 if (cvs_msg == NULL) 160 return (CVS_EX_DATA); 161 162 if (root->cr_method != CVS_METHOD_LOCAL) { 163 if (rev != NULL) { 164 if ((cvs_sendarg(root, "-r", 0) < 0) || 165 (cvs_sendarg(root, rev, 0) < 0)) 166 return (CVS_EX_PROTO); 167 } 168 } 169 170 return (0); 171 } 172 173 /* 174 * cvs_commit_prepare() 175 * 176 * Examine the file <cf> to see if it will be part of the commit, in which 177 * case it gets added to the list passed as second argument. 178 */ 179 int 180 cvs_commit_prepare(CVSFILE *cf, void *arg) 181 { 182 CVSFILE *copy; 183 struct cvs_flist *clp = (struct cvs_flist *)arg; 184 185 if ((cf->cf_type == DT_REG) && ((cf->cf_cvstat == CVS_FST_MODIFIED) || 186 (cf->cf_cvstat == CVS_FST_ADDED) || 187 (cf->cf_cvstat == CVS_FST_REMOVED))) { 188 copy = cvs_file_copy(cf); 189 if (copy == NULL) 190 return (CVS_EX_DATA); 191 192 SIMPLEQ_INSERT_TAIL(clp, copy, cf_list); 193 } 194 195 return (0); 196 } 197 198 199 /* 200 * cvs_commit_file() 201 * 202 * Commit a single file. 203 */ 204 int 205 cvs_commit_file(CVSFILE *cf, void *arg) 206 { 207 int ret, l; 208 char *repo, rcspath[MAXPATHLEN], fpath[MAXPATHLEN]; 209 RCSFILE *rf; 210 struct cvsroot *root; 211 212 ret = 0; 213 rf = NULL; 214 repo = NULL; 215 root = CVS_DIR_ROOT(cf); 216 217 if (cf->cf_type == DT_DIR) { 218 if (root->cr_method != CVS_METHOD_LOCAL) { 219 if (cf->cf_cvstat != CVS_FST_UNKNOWN) { 220 if (cvs_senddir(root, cf) < 0) 221 return (CVS_EX_PROTO); 222 } 223 } 224 225 return (0); 226 } 227 228 cvs_file_getpath(cf, fpath, sizeof(fpath)); 229 230 if (cf->cf_parent != NULL) 231 repo = cf->cf_parent->cf_repo; 232 233 if ((cf->cf_cvstat == CVS_FST_ADDED) || 234 (cf->cf_cvstat == CVS_FST_MODIFIED) || 235 (cf->cf_cvstat == CVS_FST_REMOVED)) { 236 if (root->cr_method != CVS_METHOD_LOCAL) { 237 if (cvs_sendentry(root, cf) < 0) { 238 return (CVS_EX_PROTO); 239 } 240 241 /* if it's removed, don't bother sending a 242 * Modified request together with the file its 243 * contents. 244 */ 245 if (cf->cf_cvstat == CVS_FST_REMOVED) 246 return (0); 247 248 if (cvs_sendreq(root, CVS_REQ_MODIFIED, 249 CVS_FILE_NAME(cf)) < 0) { 250 return (CVS_EX_PROTO); 251 } 252 253 if (cvs_sendfile(root, fpath) < 0) { 254 return (CVS_EX_PROTO); 255 } 256 } 257 } 258 259 l = snprintf(rcspath, sizeof(rcspath), "%s/%s/%s%s", 260 root->cr_dir, repo, fpath, RCS_FILE_EXT); 261 if (l == -1 || l >= (int)sizeof(rcspath)) { 262 errno = ENAMETOOLONG; 263 cvs_log(LP_ERRNO, "%s", rcspath); 264 return (CVS_EX_DATA); 265 } 266 267 return (0); 268 } 269