Coverage Report

Created: 2025-11-11 06:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/brpc/src/butil/files/file_watcher.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
// Date: 2010/05/29
19
20
// Watch timestamp of a file
21
22
#ifndef BUTIL_FILES_FILE_WATCHER_H
23
#define BUTIL_FILES_FILE_WATCHER_H
24
25
#include <stdint.h>                                 // int64_t
26
#include <string>                                   // std::string
27
28
// Example:
29
//   FileWatcher fw;
30
//   fw.init("to_be_watched_file");
31
//   ....
32
//   if (fw.check_and_consume() > 0) {
33
//       // the file is created or updated 
34
//       ......
35
//   }
36
37
namespace butil {
38
class FileWatcher {
39
public:
40
    enum Change {
41
        DELETED = -1,
42
        UNCHANGED = 0,
43
        UPDATED = 1,
44
        CREATED = 2,
45
    };
46
47
    typedef int64_t Timestamp;
48
    
49
    FileWatcher();
50
51
    // Watch file at `file_path', must be called before calling other methods.
52
    // Returns 0 on success, -1 otherwise.
53
    int init(const char* file_path);
54
    // Let check_and_consume returns CREATE when file_path already exists.
55
    int init_from_not_exist(const char* file_path);
56
57
    // Check and consume change of the watched file. Write `last_timestamp'
58
    // if it's not NULL.
59
    // Returns:
60
    //   CREATE    the file is created since last call to this method.
61
    //   UPDATED   the file is modified since last call.
62
    //   UNCHANGED the file has no change since last call.
63
    //   DELETED   the file was deleted since last call.
64
    // Note: If the file is updated too frequently, this method may return 
65
    // UNCHANGED due to precision of stat(2) and the file system. If the file
66
    // is created and deleted too frequently, the event may not be detected.
67
    Change check_and_consume(Timestamp* last_timestamp = NULL);
68
69
    // Set internal timestamp. User can use this method to make
70
    // check_and_consume() replay the change.
71
    void restore(Timestamp timestamp);
72
    
73
    // Get path of watched file
74
0
    const char* filepath() const { return _file_path.c_str(); }
75
76
private:
77
    Change check(Timestamp* new_timestamp) const;
78
79
    std::string _file_path;
80
    Timestamp _last_ts;    
81
};
82
}  // namespace butil
83
84
#endif  // BUTIL_FILES_FILE_WATCHER_H