1/*
2 * lock-cmd.c -- LOck a working copy path in the repository.
3 *
4 * ====================================================================
5 *    Licensed to the Apache Software Foundation (ASF) under one
6 *    or more contributor license agreements.  See the NOTICE file
7 *    distributed with this work for additional information
8 *    regarding copyright ownership.  The ASF licenses this file
9 *    to you under the Apache License, Version 2.0 (the
10 *    "License"); you may not use this file except in compliance
11 *    with the License.  You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 *    Unless required by applicable law or agreed to in writing,
16 *    software distributed under the License is distributed on an
17 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 *    KIND, either express or implied.  See the License for the
19 *    specific language governing permissions and limitations
20 *    under the License.
21 * ====================================================================
22 */
23
24/* ==================================================================== */
25
26
27
28/*** Includes. ***/
29
30#include "svn_pools.h"
31#include "svn_client.h"
32#include "svn_subst.h"
33#include "svn_path.h"
34#include "svn_error_codes.h"
35#include "svn_error.h"
36#include "svn_cmdline.h"
37#include "cl.h"
38#include "svn_private_config.h"
39
40
41/*** Code. ***/
42
43/* Get a lock comment, allocate it in POOL and store it in *COMMENT. */
44static svn_error_t *
45get_comment(const char **comment, svn_client_ctx_t *ctx,
46            svn_cl__opt_state_t *opt_state, apr_pool_t *pool)
47{
48  svn_string_t *comment_string;
49
50  if (opt_state->filedata)
51    {
52      /* Get it from the -F argument. */
53      if (strlen(opt_state->filedata->data) < opt_state->filedata->len)
54        {
55          /* A message containing a zero byte can't be represented as a C
56             string. */
57          return svn_error_create(SVN_ERR_CL_BAD_LOG_MESSAGE, NULL,
58                                  _("Lock comment contains a zero byte"));
59        }
60      comment_string = svn_string_create(opt_state->filedata->data, pool);
61
62    }
63  else if (opt_state->message)
64    {
65      /* Get if from the -m option. */
66      comment_string = svn_string_create(opt_state->message, pool);
67    }
68  else
69    {
70      *comment = NULL;
71      return SVN_NO_ERROR;
72    }
73
74  /* Translate to UTF8/LF. */
75  SVN_ERR(svn_subst_translate_string2(&comment_string, NULL, NULL,
76                                      comment_string, opt_state->encoding,
77                                      FALSE, pool, pool));
78  *comment = comment_string->data;
79
80  return SVN_NO_ERROR;
81}
82
83/* Baton for notify_lock_handler */
84struct notify_lock_baton_t
85{
86  void *inner_baton;
87  svn_wc_notify_func2_t inner_notify;
88  svn_boolean_t had_failure;
89};
90
91/* Implements svn_wc_notify_func2_t for svn_cl__lock */
92static void
93notify_lock_handler(void *baton,
94                    const svn_wc_notify_t *notify,
95                    apr_pool_t *scratch_pool)
96{
97  struct notify_lock_baton_t *nlb = baton;
98
99  if (notify->action == svn_wc_notify_failed_lock)
100    nlb->had_failure = TRUE;
101
102  if (nlb->inner_notify)
103    nlb->inner_notify(nlb->inner_baton, notify, scratch_pool);
104}
105
106/* This implements the `svn_opt_subcommand_t' interface. */
107svn_error_t *
108svn_cl__lock(apr_getopt_t *os,
109             void *baton,
110             apr_pool_t *pool)
111{
112  svn_cl__opt_state_t *opt_state = ((svn_cl__cmd_baton_t *) baton)->opt_state;
113  svn_client_ctx_t *ctx = ((svn_cl__cmd_baton_t *) baton)->ctx;
114  apr_array_header_t *targets;
115  const char *comment;
116  struct notify_lock_baton_t nlb;
117
118  SVN_ERR(svn_cl__args_to_target_array_print_reserved(&targets, os,
119                                                      opt_state->targets,
120                                                      ctx, FALSE, pool));
121
122  /* We only support locking files, so '.' is not valid. */
123  if (! targets->nelts)
124    return svn_error_create(SVN_ERR_CL_INSUFFICIENT_ARGS, 0, NULL);
125
126  SVN_ERR(svn_cl__assert_homogeneous_target_type(targets));
127
128  /* Get comment. */
129  SVN_ERR(get_comment(&comment, ctx, opt_state, pool));
130
131  SVN_ERR(svn_cl__eat_peg_revisions(&targets, targets, pool));
132
133  nlb.inner_notify = ctx->notify_func2;
134  nlb.inner_baton = ctx->notify_baton2;
135  nlb.had_failure = FALSE;
136
137  ctx->notify_func2 = notify_lock_handler;
138  ctx->notify_baton2 = &nlb;
139
140  SVN_ERR(svn_client_lock(targets, comment, opt_state->force, ctx, pool));
141
142  if (nlb.had_failure)
143    return svn_error_create(SVN_ERR_ILLEGAL_TARGET, NULL,
144                            _("One or more locks could not be obtained"));
145
146  return SVN_NO_ERROR;
147}
148