include/osquery/filesystem.h contains utilities for accessing the filesystem.

    Consider the following example for reading a file from the filesystem:

    1. #include <iostream>
    2. #include <string>
    3. #include <osquery/filesystem.h>
    4. const std::string kPath = "/foo/bar.txt"
    5. int main(int argc, char* argv[]) {
    6. auto s = osquery::pathExists(kPath);
    7. if (s.ok()) {
    8. std::string content;
    9. s = osquery::readFile(kPath, content);
    10. if (s.ok()) {
    11. std::cout << "Contents of " << kPath << ":\n";
    12. std::cout << content;
    13. } else {
    14. std::cerr << "Error reading file: " << s.toString();
    15. return s.code();
    16. }
    17. } else {
    18. std::cerr << "The path doesn't exist\n";
    19. return 1;
    20. }
    21. return 0;
    22. }

    To internalize the main API, consider the same example without error checking:

    1. #include <iostream>
    2. #include <string>
    3. #include <osquery/filesystem.h>
    4. int main(int argc, char* argv[]) {
    5. std::string content;
    6. osquery::readFile("/foo/bar.txt", content);
    7. std::cout << "Contents of " << "/foo/bar.txt" << ":\n" << content;
    8. return 0;
    9. }