emqx_utils_fs_SUITE.erl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2023 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_utils_fs_SUITE).
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -include_lib("common_test/include/ct.hrl").
  20. -include_lib("eunit/include/eunit.hrl").
  21. -include_lib("kernel/include/file.hrl").
  22. all() ->
  23. emqx_common_test_helpers:all(?MODULE).
  24. init_per_suite(Config) ->
  25. Root = ?config(data_dir, Config),
  26. D1 = filename:join([Root, "nonempty", "d1/"]),
  27. D2 = filename:join([Root, "nonempty", "d2/"]),
  28. F1 = filename:join([D1, "1"]),
  29. F2 = filename:join([D1, "2"]),
  30. DeepDir = filename:join([Root, "nonempty", "d2", "deep", "down/"]),
  31. DeepFile = filename:join([DeepDir, "here"]),
  32. Files = [F1, F2, DeepFile],
  33. lists:foreach(fun filelib:ensure_dir/1, Files),
  34. D1LinkMutrec = filename:join([D1, "mutrec"]),
  35. D2LinkMutrec = filename:join([D2, "deep", "mutrec"]),
  36. lists:foreach(fun(File) -> file:write_file(File, <<"">>, [write]) end, Files),
  37. chmod_file(D1, 8#00777),
  38. chmod_file(DeepFile, 8#00600),
  39. make_symlink(DeepDir, D1LinkMutrec),
  40. %% can't file:make_link("../../d1", D2Mutrec) on mac, return {error, eperm}
  41. make_symlink("../../d1", D2LinkMutrec),
  42. {ok, D2MutrecInfo} = file:read_link_info(D2LinkMutrec),
  43. ct:pal("~ts 's file_info is ~p~n", [D2LinkMutrec, D2MutrecInfo]),
  44. Config.
  45. end_per_suite(Config) ->
  46. Root = ?config(data_dir, Config),
  47. ok = file:del_dir_r(filename:join([Root, "nonempty"])),
  48. ok.
  49. %%
  50. t_traverse_dir(Config) ->
  51. Dir = ?config(data_dir, Config),
  52. Traversal = lists:sort(emqx_utils_fs:traverse_dir(fun cons_fileinfo/3, [], Dir)),
  53. ?assertMatch(
  54. [
  55. {"nonempty/d1/1", #file_info{type = regular}},
  56. {"nonempty/d1/2", #file_info{type = regular}},
  57. {"nonempty/d1/mutrec", #file_info{type = symlink, mode = ARWX}},
  58. {"nonempty/d2/deep/down/here", #file_info{type = regular, mode = ORW}},
  59. {"nonempty/d2/deep/mutrec", #file_info{type = symlink, mode = ARWX}}
  60. ] when
  61. ((ORW band 8#00600 =:= 8#00600) and
  62. (ARWX band 8#00777 =:= 8#00777)),
  63. [{string:prefix(Filename, Dir), Info} || {Filename, Info} <- Traversal]
  64. ).
  65. t_traverse_symlink(Config) ->
  66. Dir = filename:join([?config(data_dir, Config), "nonempty", "d1", "mutrec"]),
  67. ?assertMatch(
  68. [{Dir, #file_info{type = symlink}}],
  69. emqx_utils_fs:traverse_dir(fun cons_fileinfo/3, [], Dir)
  70. ).
  71. t_traverse_symlink_subdir(Config) ->
  72. Dir = filename:join([?config(data_dir, Config), "nonempty", "d2", "deep", "mutrec", "."]),
  73. Traversal = lists:sort(emqx_utils_fs:traverse_dir(fun cons_fileinfo/3, [], Dir)),
  74. ?assertMatch(
  75. [
  76. {"nonempty/d2/deep/mutrec/1", #file_info{type = regular}},
  77. {"nonempty/d2/deep/mutrec/2", #file_info{type = regular}},
  78. {"nonempty/d2/deep/mutrec/mutrec", #file_info{type = symlink}}
  79. ],
  80. [
  81. {string:prefix(Filename, ?config(data_dir, Config)), Info}
  82. || {Filename, Info} <- Traversal
  83. ]
  84. ).
  85. t_traverse_empty(Config) ->
  86. Dir = filename:join(?config(data_dir, Config), "empty"),
  87. _ = file:make_dir(Dir),
  88. ?assertEqual(
  89. [],
  90. emqx_utils_fs:traverse_dir(fun cons_fileinfo/3, [], Dir)
  91. ).
  92. t_traverse_nonexisting(_) ->
  93. ?assertEqual(
  94. [{"this/should/not/exist", {error, enoent}}],
  95. emqx_utils_fs:traverse_dir(fun cons_fileinfo/3, [], "this/should/not/exist")
  96. ).
  97. cons_fileinfo(Filename, Info, Acc) ->
  98. [{Filename, Info} | Acc].
  99. %%
  100. t_canonicalize_empty(_) ->
  101. ?assertEqual(
  102. "",
  103. emqx_utils_fs:canonicalize(<<>>)
  104. ).
  105. t_canonicalize_relative(_) ->
  106. ?assertEqual(
  107. "rel",
  108. emqx_utils_fs:canonicalize(<<"rel/">>)
  109. ).
  110. t_canonicalize_trailing_slash(_) ->
  111. ?assertEqual(
  112. "/usr/local",
  113. emqx_utils_fs:canonicalize("/usr/local/")
  114. ).
  115. t_canonicalize_double_slashes(_) ->
  116. ?assertEqual(
  117. "/usr/local/.",
  118. emqx_utils_fs:canonicalize("//usr//local//.//")
  119. ).
  120. t_canonicalize_non_utf8(_) ->
  121. ?assertError(
  122. badarg,
  123. emqx_utils_fs:canonicalize(<<128, 128, 128>>)
  124. ).
  125. chmod_file(File, Mode) ->
  126. {ok, FileInfo} = file:read_file_info(File),
  127. ok = file:write_file_info(File, FileInfo#file_info{mode = Mode}).
  128. make_symlink(FileOrDir, NewLink) ->
  129. _ = file:delete(NewLink),
  130. ok = file:make_symlink(FileOrDir, NewLink).