1251881Speter/*
2251881Speter * lock-cmd.c -- LOck a working copy path in the repository.
3251881Speter *
4251881Speter * ====================================================================
5251881Speter *    Licensed to the Apache Software Foundation (ASF) under one
6251881Speter *    or more contributor license agreements.  See the NOTICE file
7251881Speter *    distributed with this work for additional information
8251881Speter *    regarding copyright ownership.  The ASF licenses this file
9251881Speter *    to you under the Apache License, Version 2.0 (the
10251881Speter *    "License"); you may not use this file except in compliance
11251881Speter *    with the License.  You may obtain a copy of the License at
12251881Speter *
13251881Speter *      http://www.apache.org/licenses/LICENSE-2.0
14251881Speter *
15251881Speter *    Unless required by applicable law or agreed to in writing,
16251881Speter *    software distributed under the License is distributed on an
17251881Speter *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18251881Speter *    KIND, either express or implied.  See the License for the
19251881Speter *    specific language governing permissions and limitations
20251881Speter *    under the License.
21251881Speter * ====================================================================
22251881Speter */
23251881Speter
24251881Speter/* ==================================================================== */
25251881Speter
26251881Speter
27251881Speter
28251881Speter/*** Includes. ***/
29251881Speter
30251881Speter#include "svn_pools.h"
31251881Speter#include "svn_client.h"
32251881Speter#include "svn_subst.h"
33251881Speter#include "svn_path.h"
34251881Speter#include "svn_error_codes.h"
35251881Speter#include "svn_error.h"
36251881Speter#include "svn_cmdline.h"
37251881Speter#include "cl.h"
38251881Speter#include "svn_private_config.h"
39251881Speter
40251881Speter
41251881Speter/*** Code. ***/
42251881Speter
43251881Speter/* Get a lock comment, allocate it in POOL and store it in *COMMENT. */
44251881Speterstatic svn_error_t *
45251881Speterget_comment(const char **comment, svn_client_ctx_t *ctx,
46251881Speter            svn_cl__opt_state_t *opt_state, apr_pool_t *pool)
47251881Speter{
48251881Speter  svn_string_t *comment_string;
49251881Speter
50251881Speter  if (opt_state->filedata)
51251881Speter    {
52251881Speter      /* Get it from the -F argument. */
53251881Speter      if (strlen(opt_state->filedata->data) < opt_state->filedata->len)
54251881Speter        {
55251881Speter          /* A message containing a zero byte can't be represented as a C
56251881Speter             string. */
57251881Speter          return svn_error_create(SVN_ERR_CL_BAD_LOG_MESSAGE, NULL,
58251881Speter                                  _("Lock comment contains a zero byte"));
59251881Speter        }
60251881Speter      comment_string = svn_string_create(opt_state->filedata->data, pool);
61251881Speter
62251881Speter    }
63251881Speter  else if (opt_state->message)
64251881Speter    {
65251881Speter      /* Get if from the -m option. */
66251881Speter      comment_string = svn_string_create(opt_state->message, pool);
67251881Speter    }
68251881Speter  else
69251881Speter    {
70251881Speter      *comment = NULL;
71251881Speter      return SVN_NO_ERROR;
72251881Speter    }
73251881Speter
74251881Speter  /* Translate to UTF8/LF. */
75251881Speter  SVN_ERR(svn_subst_translate_string2(&comment_string, NULL, NULL,
76251881Speter                                      comment_string, opt_state->encoding,
77251881Speter                                      FALSE, pool, pool));
78251881Speter  *comment = comment_string->data;
79251881Speter
80251881Speter  return SVN_NO_ERROR;
81251881Speter}
82251881Speter
83299742Sdim/* Baton for notify_lock_handler */
84299742Sdimstruct notify_lock_baton_t
85299742Sdim{
86299742Sdim  void *inner_baton;
87299742Sdim  svn_wc_notify_func2_t inner_notify;
88299742Sdim  svn_boolean_t had_failure;
89299742Sdim};
90299742Sdim
91299742Sdim/* Implements svn_wc_notify_func2_t for svn_cl__lock */
92299742Sdimstatic void
93299742Sdimnotify_lock_handler(void *baton,
94299742Sdim                    const svn_wc_notify_t *notify,
95299742Sdim                    apr_pool_t *scratch_pool)
96299742Sdim{
97299742Sdim  struct notify_lock_baton_t *nlb = baton;
98299742Sdim
99299742Sdim  if (notify->action == svn_wc_notify_failed_lock)
100299742Sdim    nlb->had_failure = TRUE;
101299742Sdim
102299742Sdim  if (nlb->inner_notify)
103299742Sdim    nlb->inner_notify(nlb->inner_baton, notify, scratch_pool);
104299742Sdim}
105299742Sdim
106251881Speter/* This implements the `svn_opt_subcommand_t' interface. */
107251881Spetersvn_error_t *
108251881Spetersvn_cl__lock(apr_getopt_t *os,
109251881Speter             void *baton,
110251881Speter             apr_pool_t *pool)
111251881Speter{
112251881Speter  svn_cl__opt_state_t *opt_state = ((svn_cl__cmd_baton_t *) baton)->opt_state;
113251881Speter  svn_client_ctx_t *ctx = ((svn_cl__cmd_baton_t *) baton)->ctx;
114251881Speter  apr_array_header_t *targets;
115251881Speter  const char *comment;
116299742Sdim  struct notify_lock_baton_t nlb;
117251881Speter
118251881Speter  SVN_ERR(svn_cl__args_to_target_array_print_reserved(&targets, os,
119251881Speter                                                      opt_state->targets,
120251881Speter                                                      ctx, FALSE, pool));
121251881Speter
122251881Speter  /* We only support locking files, so '.' is not valid. */
123251881Speter  if (! targets->nelts)
124251881Speter    return svn_error_create(SVN_ERR_CL_INSUFFICIENT_ARGS, 0, NULL);
125251881Speter
126251881Speter  SVN_ERR(svn_cl__assert_homogeneous_target_type(targets));
127251881Speter
128251881Speter  /* Get comment. */
129251881Speter  SVN_ERR(get_comment(&comment, ctx, opt_state, pool));
130251881Speter
131251881Speter  SVN_ERR(svn_cl__eat_peg_revisions(&targets, targets, pool));
132251881Speter
133299742Sdim  nlb.inner_notify = ctx->notify_func2;
134299742Sdim  nlb.inner_baton = ctx->notify_baton2;
135299742Sdim  nlb.had_failure = FALSE;
136299742Sdim
137299742Sdim  ctx->notify_func2 = notify_lock_handler;
138299742Sdim  ctx->notify_baton2 = &nlb;
139299742Sdim
140299742Sdim  SVN_ERR(svn_client_lock(targets, comment, opt_state->force, ctx, pool));
141299742Sdim
142299742Sdim  if (nlb.had_failure)
143299742Sdim    return svn_error_create(SVN_ERR_ILLEGAL_TARGET, NULL,
144299742Sdim                            _("One or more locks could not be obtained"));
145299742Sdim
146299742Sdim  return SVN_NO_ERROR;
147251881Speter}
148