example_tc_file.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include "util/tc_file.h"
  17. #include <errno.h>
  18. #include <iterator>
  19. #include <sys/time.h>
  20. using namespace tars;
  21. /*
  22. int removeFile(const string &sFullFileName, bool bRecursive)
  23. {
  24. cout << sFullFileName << endl;
  25. string path = TC_File::simplifyDirectory(sFullFileName);
  26. struct stat f_stat;
  27. if (lstat(path.c_str(), &f_stat) == -1)
  28. {
  29. return -1;
  30. }
  31. if(S_ISDIR(f_stat.st_mode))
  32. {
  33. if(bRecursive)
  34. {
  35. vector<string> files;
  36. TC_File::listDirectory(path, files, true, false);
  37. for(size_t i = 0; i < files.size(); i++)
  38. {
  39. removeFile(files[i], bRecursive);
  40. }
  41. if(path != "/")
  42. {
  43. if(::rmdir(path.c_str()) == -1)
  44. {
  45. return -1;
  46. }
  47. return 0;
  48. }
  49. }
  50. else
  51. {
  52. if(::rmdir(path.c_str()) == -1)
  53. {
  54. return -1;
  55. }
  56. }
  57. }
  58. else
  59. {
  60. if(::remove(path.c_str()) == -1)
  61. {
  62. return -1;
  63. }
  64. }
  65. return 0;
  66. }
  67. */
  68. struct Out
  69. {
  70. void operator()(const string &n)
  71. {
  72. cout << n << endl;
  73. }
  74. };
  75. int main(int argc, char *argv[])
  76. {
  77. try
  78. {
  79. // cout << TC_File::getFileSize("./test_tc_file.cpp") << endl;
  80. // cout << TC_File::isFileExist("./test_tc_file.cpp", S_IFDIR) << endl;
  81. // cout << TC_File::makeDir("test") << endl;
  82. // cout << TC_File::simplifyDirectory("/.") << endl;
  83. // cout << TC_File::simplifyDirectory("/./ab/tt//t///t//../tt/") << endl;
  84. // TC_File::removeFile("./", true);
  85. // vector<string> v;
  86. // TC_File::listDirectory("/home/base.l", v, true);
  87. // for_each(v.begin(), v.end(), Out());
  88. TC_File::copyFile("/data/shared/Tars/web/", "/home/tars/", true);
  89. // TC_File::removeFile("/home/base.l", false);
  90. }
  91. catch(exception &ex)
  92. {
  93. cout << ex.what() << endl;
  94. }
  95. return 0;
  96. }