Ticket #18824: main.cpp

File main.cpp, 1.4 KB (added by LupusMichaelis, 3 months ago)

Exemple to launch the Tracker and create files in a locked directory

Line 
1#include <stdio.h>
2
3#include <algorithm>
4#include <iostream>
5#include <string>
6#include <vector>
7
8#include <boost/format.hpp>
9
10#include <private/shared/AutoLocker.h>
11#include <Directory.h>
12#include <File.h>
13
14void lock_and_populate(std::string const & dirname, std::vector<std::string> const & files)
15{
16 auto dir = BDirectory{dirname.c_str()};
17 auto ret = dir.InitCheck();
18 if(B_OK != ret)
19 throw (boost::format("BDirectory::InitCheck: %s: %s")
20 % dirname % strerror(ret)).str();
21
22 ret = dir.Lock();
23 if(B_OK != ret)
24 throw (boost::format("Locking: %s: %s")
25 % dirname % strerror(ret)).str();
26
27 std::ranges::for_each(files, [&dir](std::string const & f) {
28 dir.CreateFile(f.c_str(), 0);
29 });
30}
31
32int main(int argc, char *argv[])
33{
34 if(argc < 2)
35 return 1;
36
37 auto dir = std::string{argv[1]};
38
39 auto tracker_command = (boost::format("/boot/system/Tracker %s") % dir.c_str()).str();
40 auto tracker = popen(tracker_command.c_str(), "r");
41 if(NULL == tracker)
42 throw (boost::format("Failed launching Tracker: %s: %s")
43 % dir % strerror(errno)).str();
44 pclose(tracker);
45
46 {
47 // Wait for the Tracker app to exec, read and wait
48 // 1 second doesn't let enough time on my system, you might need to adjust to a
49 // higher value to observe the bug
50 sleep(2);
51 }
52
53 auto files = std::vector<std::string>{argv+2, argv+argc};
54 try
55 {
56 lock_and_populate(dir, files);
57 }
58 catch(std::string const & e)
59 {
60 std::cerr << e << std::endl;
61 }
62
63 return 0;
64}