Contents

ARST打卡第140周[140/521]

Algorithm

lc1629_按键持续时间最长的键

直接遍历维护按键时间数组

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution {
public:
    char slowestKey(vector<int>& releaseTimes, string keysPressed) {
        int n = releaseTimes.size();
        char ans = keysPressed[0];
        int maxTime = releaseTimes[0];
        for (int i = 1; i < n; i++) {
            char key = keysPressed[i];
            int time = releaseTimes[i] - releaseTimes[i - 1];
            if (time > maxTime || (time == maxTime && key > ans)) {
                ans = key;
                maxTime = time;
            }
        }
        return ans;
    }
};

Review

cephfs-docs

了解一些cephfs的简单概念

Tips

CEPHFS 内部实现(一):概念篇

Share-handle_mdtm源码分析

2022年1月4日19:27:32 网上完全没有找到mdtm的使用方式,然后自己看源码,发现原来是 p_sess->ftp_arg_str的顺序不是文件 时间,而是 时间 文件 (源码面前,了无秘密) 加上之前学习的 str_split_char – 将原来的串(第一个参数)的从第一个分隔符(第三个参数)截断,然后后面的值赋给第二个参数

str_split_char(&s_setting_str, &s_value_str, ‘=’); s_setting_str是 A = C,用 =分隔 - 然后变成 s_setting_str = A, s_value_str = B

可以确信是这个行为

1
2
3
str_split_char(&p_sess->ftp_arg_str, &s_filename_str, ' ');
modtime = vsf_sysutil_parse_time(str_getbuf(&p_sess->ftp_arg_str));
str_copy(&p_sess->ftp_arg_str, &s_filename_str);

前面的这段分析也知道时间支持,202201012022010122334420220101223344.231231 这三种格式支持

1
2
3
4
5
6
7
8
9
if (tunable_mdtm_write && retval != 0 && loc.found &&
    vsf_sysutil_isdigit(str_get_char_at(&p_sess->ftp_arg_str, 0)))
{
if (loc.index == 8 || loc.index == 14 ||
    (loc.index > 15 && str_get_char_at(&p_sess->ftp_arg_str, 14) == '.'))
{
    do_write = 1;
}
}

整体代码

 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
static void
handle_mdtm(struct vsf_session* p_sess)
{
  static struct mystr s_filename_str;
  static struct vsf_sysutil_statbuf* s_p_statbuf;
  int do_write = 0;
  long modtime = 0;
  struct str_locate_result loc = str_locate_char(&p_sess->ftp_arg_str, ' ');
  int retval = str_stat(&p_sess->ftp_arg_str, &s_p_statbuf);
  if (tunable_mdtm_write && retval != 0 && loc.found &&
      vsf_sysutil_isdigit(str_get_char_at(&p_sess->ftp_arg_str, 0)))
  {
    if (loc.index == 8 || loc.index == 14 ||
        (loc.index > 15 && str_get_char_at(&p_sess->ftp_arg_str, 14) == '.'))
    {
      do_write = 1;
    }
  }
  if (do_write != 0)
  {
    str_split_char(&p_sess->ftp_arg_str, &s_filename_str, ' ');
    modtime = vsf_sysutil_parse_time(str_getbuf(&p_sess->ftp_arg_str));
    str_copy(&p_sess->ftp_arg_str, &s_filename_str);
  }
  resolve_tilde(&p_sess->ftp_arg_str, p_sess);
  if (!vsf_access_check_file(&p_sess->ftp_arg_str))
  {
    vsf_cmdio_write(p_sess, FTP_NOPERM, "Permission denied.");
    return;
  }
  if (do_write && tunable_write_enable &&
      (tunable_anon_other_write_enable || !p_sess->is_anonymous))
  {
    retval = str_stat(&p_sess->ftp_arg_str, &s_p_statbuf);
    if (retval != 0 || !vsf_sysutil_statbuf_is_regfile(s_p_statbuf))
    {
      vsf_cmdio_write(p_sess, FTP_FILEFAIL,
                      "Could not set file modification time.");
    }
    else
    {
      retval = vsf_sysutil_setmodtime(
        str_getbuf(&p_sess->ftp_arg_str), modtime, tunable_use_localtime);
      if (retval != 0)
      {
        vsf_cmdio_write(p_sess, FTP_FILEFAIL,
                        "Could not set file modification time.");
      }
      else
      {
        vsf_cmdio_write(p_sess, FTP_MDTMOK,
                        "File modification time set.");
      }
    }
  }
  else
  {
    if (retval != 0 || !vsf_sysutil_statbuf_is_regfile(s_p_statbuf))
    {
      vsf_cmdio_write(p_sess, FTP_FILEFAIL,
                      "Could not get file modification time.");
    }
    else
    {
      static struct mystr s_mdtm_res_str;
      str_alloc_text(&s_mdtm_res_str,
                     vsf_sysutil_statbuf_get_numeric_date(
                       s_p_statbuf, tunable_use_localtime));
      vsf_cmdio_write_str(p_sess, FTP_MDTMOK, &s_mdtm_res_str);
    }
  }
}

可以用ftplib做简单测试

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
def test_mdtm(self):
  '''测试MDTM接口'''
  try:
      mdtm_test_file = 'test_mdtm.file'
      mode_time_str = '20220101223344.5566'
      ## 成功返回为213的状态码,时间戳会忽略小数点后的
      expect_time_str = '213 20220101223344'
      self.__create_remote_file(mdtm_test_file)
      print(self.ftp_client.sendcmd(
          'MDTM {}'.format(mdtm_test_file)))
      print(self.ftp_client.sendcmd(
          'MDTM {} {}'.format(mode_time_str, mdtm_test_file)))
      res = self.ftp_client.sendcmd(
          'MDTM {}'.format(mdtm_test_file))
      print("mdtm result is {}".format(res))
      if res != expect_time_str:
          return (False, None)
  except Exception as ex:
      LOG.error('mdtm error.', exc_info=1)
      return (False, None)

  return (True, None)