目录时间戳的规律

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
# Copyright © 2022, 飞麦 <fitmap@qq.com>, All rights reserved.

# frozen_string_literal: true

# 测试目录各时间戳的规律(通用)
class Dirtime
    def result(newtime, oldtime)
        newtime == oldtime ? '不变' : ''
    end

    def compare(title)
        new_stat = File.stat(@cur_dir_name)
        puts "#{title} 目录创建时刻 #{result(new_stat.ctime, @cur_stat.ctime)}"
        puts "#{title} 目录修改时刻 #{result(new_stat.mtime, @cur_stat.mtime)}"
        puts "#{title} 目录存取时刻 #{result(new_stat.atime, @cur_stat.atime)}"
        @cur_stat = new_stat
    end

    def test(func, title)
        sleep(0.01)
        method(func).call
        compare(title)
    end
end

# 测试目录各时间戳的规律(孙目录级)
class Dirtime
    def create_deep_dir
        Dir.mkdir(@deep_dir_name)
    end

    def create_deep_file
        File.write(@deep_file_name, '~' * 4096)
    end

    def change_deep_dir_name
        File.rename(@deep_dir_name, '壬申猴')
        File.rename('壬申猴', @deep_dir_name)
    end

    def change_deep_dir_attr
        system("attrib.exe +h #{@deep_dir_name}")
        system("attrib.exe -h #{@deep_dir_name}")
    end

    def remove_deep_dir
        Dir.rmdir(@deep_dir_name)
    end

    def change_deep_file_name
        File.rename(@deep_file_name, '癸酉鸡')
        File.rename('癸酉鸡', @deep_file_name)
    end

    def change_deep_file_attr
        system("attrib.exe +h #{@deep_file_name}")
        system("attrib.exe -h #{@deep_file_name}")
    end

    def change_deep_file_text
        File.write(@deep_file_name, '&' * 4096)
    end

    def remove_deep_file
        File.delete(@deep_file_name)
    end

    # 测试孙目录
    def check_deep
        test(:create_deep_dir, '创建孙目录')
        test(:create_deep_file, '创建孙文件')
        test(:change_deep_dir_name, '改变孙目录名称')
        test(:change_deep_dir_attr, '变更孙目录属性')
        test(:remove_deep_dir, '删除孙目录')
        test(:change_deep_file_name, '改变孙文件名称')
        test(:change_deep_file_attr, '变更孙文件属性')
        test(:change_deep_file_text, '修改孙文件内容')
        test(:remove_deep_file, '删除孙文件')
    end

    def deal_deep
        @deep_dir_name = '庚午马'
        @deep_file_name = '辛未羊'
        Dir.chdir(@sub_dir_name) do
            check_deep
        end
    end
end

# 测试目录各时间戳的规律(子目录级)
class Dirtime
    def create_sub_dir
        Dir.mkdir(@sub_dir_name)
    end

    def create_sub_file
        File.write(@sub_file_name, '~' * 4096)
    end

    def query_sub_dir
        Dir.chdir(@sub_dir_name) do
            Dir.glob('*')
        end
    end

    def change_sub_dir_name
        File.rename(@sub_dir_name, '戊辰龙')
        File.rename('戊辰龙', @sub_dir_name)
    end

    def change_sub_dir_attr
        system("attrib.exe +h #{@sub_dir_name}")
        system("attrib.exe -h #{@sub_dir_name}")
    end

    def remove_sub_dir
        Dir.rmdir(@sub_dir_name)
    end

    def change_sub_file_name
        File.rename(@sub_file_name, '己巳蛇')
        File.rename('己巳蛇', @sub_file_name)
    end

    def change_sub_file_attr
        system("attrib.exe +h #{@sub_file_name}")
        system("attrib.exe -h #{@sub_file_name}")
    end

    def change_sub_file_text
        File.write(@sub_file_name, '&' * 4096)
    end

    def remove_sub_file
        File.delete(@sub_file_name)
    end

    def check_sub
        test(:create_sub_dir, '创建子目录')
        test(:create_sub_file, '创建子文件')
        test(:query_sub_dir, '查询子目录列表')
        test(:change_sub_dir_name, '改变子目录名称')
        test(:change_sub_dir_attr, '变更子目录属性')
        deal_deep
        test(:remove_sub_dir, '删除子目录')
        test(:change_sub_file_name, '改变子文件名称')
        test(:change_sub_file_attr, '变更子文件属性')
        test(:change_sub_file_text, '修改子文件内容')
        test(:remove_sub_file, '删除子文件')
    end

    def deal_sub
        @sub_dir_name = '丙寅虎'
        @sub_file_name = '丁卯兔'
        Dir.chdir(@cur_dir_name) do
            check_sub
        end
    end
end

# 测试目录各时间戳的规律(目录级)
class Dirtime
    def change_cur_name
        File.rename(@cur_dir_name, '乙丑牛')
        File.rename('乙丑牛', @cur_dir_name)
    end

    def change_cur_attr
        system("attrib.exe +h #{@cur_dir_name}")
        system("attrib.exe -h #{@cur_dir_name}")
    end

    def query_cur_dir
        Dir.glob('*')
    end

    def check_cur
        test(:change_cur_name, '改变目录名称')
        test(:change_cur_attr, '变更目录属性')
        test(:query_cur_dir, '查询目录列表')
    end
end

# 测试目录各时间戳的规律
class Dirtime
    def init_cur
        @cur_dir_name = File.join(Dir.home, '甲子鼠')
        Dir.mkdir(@cur_dir_name)
        @cur_stat = File.stat(@cur_dir_name)
        puts '测试目录的时间戳:'
    end

    def recover_current
        Dir.rmdir(@cur_dir_name)
    end

    def main
        Dir.chdir(Dir.home) do
            init_cur
            check_cur
            deal_sub
            recover_current
        end
        puts '总结:'
        puts '当前目录的创建时间戳:创建当前目录时记录,后续操作不再改变。'
        puts '当前目录的修改与存取时间戳:创建、改名、删除子目录或子文件时改变,修改子文件内容及操作孙目录时不变。'
    end
end

$PROGRAM_NAME == __FILE__ && Dirtime.new.main
posted @ 2024-01-25 12:11  飞麦  阅读(14)  评论(0编辑  收藏  举报