summaryrefslogtreecommitdiffstats
path: root/src/dokan/options.cc
blob: 4c968e13af5d6c0fe45d5fa7275ba6f46a5f4a11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 * Copyright (C) 2021 SUSE LINUX GmbH
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
*/
#include <regex>

#include "include/compat.h"
#include "include/cephfs/libcephfs.h"

#include "ceph_dokan.h"
#include "utils.h"

#include "common/ceph_argparse.h"
#include "common/config.h"
#include "common/win32/wstring.h"

#include "global/global_init.h"

void print_usage() {
  const char* usage_str = R"(
Usage: ceph-dokan.exe -l <mountpoint>
                      map -l <mountpoint>    Map a CephFS filesystem
                      unmap -l <mountpoint>  Unmap a CephFS filesystem

Map options:
  -l [ --mountpoint ] arg     mountpoint (path or drive letter) (e.g -l x)
  -x [ --root-path ] arg      mount a Ceph filesystem subdirectory

  --operation-timeout arg     Dokan operation timeout. Default: 120s.

  --debug                     enable debug output
  --dokan-stderr              enable stderr Dokan logging

  --read-only                 read-only mount
  -o [ --win-mount-mgr]       use the Windows mount manager
  --current-session-only      expose the mount only to the current user session
  --removable                 use a removable drive
  --win-vol-name arg          The Windows volume name. Default: Ceph - <fs_name>.
  --win-vol-serial arg        The Windows volume serial number. Default: <fs_id>.
  --max-path-len              The value of the maximum path length. Default: 256.
  --file-mode                 The access mode to be used when creating files.
  --dir-mode                  The access mode to be used when creating directories.

Unmap options:
  -l [ --mountpoint ] arg     mountpoint (path or drive letter) (e.g -l x).
                              It has to be the exact same mountpoint that was
                              used when the mapping was created.

Common Options:
)";

  std::cout << usage_str;
  generic_client_usage();
}


int parse_args(
  std::vector<const char*>& args,
  std::ostream *err_msg,
  Command *command, Config *cfg)
{
  if (args.empty()) {
    std::cout << "ceph-dokan: -h or --help for usage" << std::endl;
    return -EINVAL;
  }

  std::string conf_file_list;
  std::string cluster;
  CephInitParameters iparams = ceph_argparse_early_args(
    args, CEPH_ENTITY_TYPE_CLIENT, &cluster, &conf_file_list);

  ConfigProxy config{false};
  config->name = iparams.name;
  config->cluster = cluster;
  if (!conf_file_list.empty()) {
    config.parse_config_files(conf_file_list.c_str(), nullptr, 0);
  } else {
    config.parse_config_files(nullptr, nullptr, 0);
  }
  config.parse_env(CEPH_ENTITY_TYPE_CLIENT);
  config.parse_argv(args);

  std::vector<const char*>::iterator i;
  std::ostringstream err;
  std::string mountpoint;
  std::string win_vol_name;
  std::string win_vol_serial;
  std::string max_path_len;
  std::string file_mode;
  std::string dir_mode;

  int thread_count;

  for (i = args.begin(); i != args.end(); ) {
    if (ceph_argparse_flag(args, i, "-h", "--help", (char*)NULL)) {
      *command = Command::Help;
      return 0;
    } else if (ceph_argparse_flag(args, i, "-v", "--version", (char*)NULL)) {
      *command = Command::Version;
    } else if (ceph_argparse_witharg(args, i, &mountpoint,
                                     "--mountpoint", "-l", (char *)NULL)) {
      cfg->mountpoint = to_wstring(mountpoint);
    } else if (ceph_argparse_witharg(args, i, &cfg->root_path,
                                     "--root-path", "-x", (char *)NULL)) {
    } else if (ceph_argparse_flag(args, i, "--debug", (char *)NULL)) {
      cfg->debug = true;
    } else if (ceph_argparse_flag(args, i, "--dokan-stderr", (char *)NULL)) {
      cfg->dokan_stderr = true;
    } else if (ceph_argparse_flag(args, i, "--read-only", (char *)NULL)) {
      cfg->readonly = true;
    } else if (ceph_argparse_flag(args, i, "--removable", (char *)NULL)) {
      cfg->removable = true;
    } else if (ceph_argparse_flag(args, i, "--win-mount-mgr", "-o", (char *)NULL)) {
      cfg->use_win_mount_mgr = true;
    } else if (ceph_argparse_witharg(args, i, &win_vol_name,
                                     "--win-vol-name", (char *)NULL)) {
      cfg->win_vol_name = to_wstring(win_vol_name);
    } else if (ceph_argparse_witharg(args, i, &win_vol_serial,
                                     "--win-vol-serial", (char *)NULL)) {
      cfg->win_vol_serial = std::stoul(win_vol_serial);
    } else if (ceph_argparse_witharg(args, i, &max_path_len,
                                     "--max-path-len", (char*)NULL)) {
      unsigned long max_path_length = std::stoul(max_path_len);

      if (max_path_length > 32767) {
        *err_msg << "ceph-dokan: maximum path length should not "
                 << "exceed " << 32767;
        return -EINVAL;
      }

      if (max_path_length < 256) {
        *err_msg << "ceph-dokan: maximum path length should not "
                 << "have a value lower than 256";
        return -EINVAL;
      }

      cfg->max_path_len = max_path_length;
    } else if (ceph_argparse_witharg(args, i, &file_mode, "--file-mode", (char *)NULL)) {
      mode_t mode = strtol(file_mode.c_str(), NULL, 8);
      if (!std::regex_match(file_mode, std::regex("^[0-7]{3}$"))
          || mode < 01 || mode > 0777) {
        *err_msg << "ceph-dokan: invalid file access mode";
        return -EINVAL;
      }
      cfg->file_mode = mode;
    } else if (ceph_argparse_witharg(args, i, &dir_mode, "--dir-mode", (char *)NULL)) {
      mode_t mode = strtol(dir_mode.c_str(), NULL, 8);
      if (!std::regex_match(dir_mode, std::regex("^[0-7]{3}$"))
          || mode < 01 || mode > 0777) {
        *err_msg << "ceph-dokan: invalid directory access mode";
        return -EINVAL;
      }
      cfg->dir_mode = mode;
    } else if (ceph_argparse_flag(args, i, "--current-session-only", (char *)NULL)) {
      cfg->current_session_only = true;
    } else if (ceph_argparse_witharg(args, i, &thread_count,
                                     err, "--thread-count", "-t", (char *)NULL)) {
      std::cerr << "ceph-dokan: the thread count parameter is not supported by Dokany v2 "
                << "and has been deprecated." << std::endl;
    } else if (ceph_argparse_witharg(args, i, (int*)&cfg->operation_timeout,
                                     err, "--operation-timeout", (char *)NULL)) {
      if (!err.str().empty()) {
        *err_msg << "ceph-dokan: " << err.str();
        return -EINVAL;
      }
      if (cfg->operation_timeout < 0) {
        *err_msg << "ceph-dokan: Invalid argument for operation-timeout";
        return -EINVAL;
      }
    } else {
      ++i;
    }
  }

  if (cfg->use_win_mount_mgr && cfg->current_session_only) {
    *err_msg << "ceph-dokan: The mount manager always mounts the drive "
             << "for all user sessions.";
    return -EINVAL;
  }

  Command cmd = Command::None;
  if (args.begin() != args.end()) {
    if (strcmp(*args.begin(), "help") == 0) {
      cmd = Command::Help;
    } else if (strcmp(*args.begin(), "version") == 0) {
      cmd = Command::Version;
    } else if (strcmp(*args.begin(), "map") == 0) {
      cmd = Command::Map;
    } else if (strcmp(*args.begin(), "unmap") == 0) {
      cmd = Command::Unmap;
    } else {
      *err_msg << "ceph-dokan: unknown command: " <<  *args.begin();
      return -EINVAL;
    }
    args.erase(args.begin());
  }
  if (cmd == Command::None) {
    // The default command.
    cmd = Command::Map;
  }

  switch (cmd) {
    case Command::Map:
    case Command::Unmap:
      if (cfg->mountpoint.empty()) {
        *err_msg << "ceph-dokan: missing mountpoint.";
        return -EINVAL;
      }
      break;
    default:
      break;
  }

  if (args.begin() != args.end()) {
    *err_msg << "ceph-dokan: unknown args: " << *args.begin();
    return -EINVAL;
  }

  *command = cmd;
  return 0;
}

int set_dokan_options(Config *cfg, PDOKAN_OPTIONS dokan_options) {
  ZeroMemory(dokan_options, sizeof(DOKAN_OPTIONS));
  dokan_options->Version = DOKAN_VERSION;
  dokan_options->MountPoint = cfg->mountpoint.c_str();
  dokan_options->Timeout = cfg->operation_timeout * 1000;

  if (cfg->removable)
    dokan_options->Options |= DOKAN_OPTION_REMOVABLE;
  if (cfg->use_win_mount_mgr)
    dokan_options->Options |= DOKAN_OPTION_MOUNT_MANAGER;
  if (cfg->current_session_only)
    dokan_options->Options |= DOKAN_OPTION_CURRENT_SESSION;
  if (cfg->readonly)
    dokan_options->Options |= DOKAN_OPTION_WRITE_PROTECT;
  if (cfg->debug)
    dokan_options->Options |= DOKAN_OPTION_DEBUG;
  if (cfg->dokan_stderr)
    dokan_options->Options |= DOKAN_OPTION_STDERR;

  return 0;
}