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/* This implements the `svn_opt_subcommand_t' interface. */
84svn_error_t *
85svn_cl__lock(apr_getopt_t *os,
86             void *baton,
87             apr_pool_t *pool)
88{
89  svn_cl__opt_state_t *opt_state = ((svn_cl__cmd_baton_t *) baton)->opt_state;
90  svn_client_ctx_t *ctx = ((svn_cl__cmd_baton_t *) baton)->ctx;
91  apr_array_header_t *targets;
92  const char *comment;
93
94  SVN_ERR(svn_cl__args_to_target_array_print_reserved(&targets, os,
95                                                      opt_state->targets,
96                                                      ctx, FALSE, pool));
97
98  /* We only support locking files, so '.' is not valid. */
99  if (! targets->nelts)
100    return svn_error_create(SVN_ERR_CL_INSUFFICIENT_ARGS, 0, NULL);
101
102  SVN_ERR(svn_cl__assert_homogeneous_target_type(targets));
103
104  /* Get comment. */
105  SVN_ERR(get_comment(&comment, ctx, opt_state, pool));
106
107  SVN_ERR(svn_cl__eat_peg_revisions(&targets, targets, pool));
108
109  return svn_client_lock(targets, comment, opt_state->force, ctx, pool);
110}
111