TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
fsutils.h
1 /*
2  * Copyright (c) 2017-present, Facebook, Inc.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #pragma once
9 
10 #include <chrono>
11 #include <string>
12 #include <vector>
13 
14 namespace common {
15 
16 /**
17  * Utility functions for interacting with the file system.
18  *
19  * These are a few simple syscall wrappers for file system interaction. The
20  * design here follows a few simple rules:
21  * - Short names that closely resemble well-known *nix shell commands, e.g.
22  * "rm -rf" becomes rmrf(), there's cd() etc.
23  * - Simple interfaces in favor of micro-optimization. For example, there might
24  * be a future ls() call that returns a vector of strings rather than a file
25  * system iterator.
26  * - Syscall errors result in exceptions (unless otherwise noted in the function
27  * documentation), so make sure to catch them if necessary (i.e. in core bot
28  * code).
29  */
30 namespace fsutils {
31 
32 #ifdef WITHOUT_POSIX
33 char constexpr kPathSep = '\\';
34 #else
35 char constexpr kPathSep = '/';
36 #endif
37 
38 /// Returns the current working directory
39 std::string pwd();
40 
41 /// Equivalent to basename(1)
42 std::string basename(
43  std::string const& path,
44  std::string const& ext = std::string());
45 
46 /// Equivalent to dirname(1)
47 std::string dirname(std::string const& path);
48 
49 /// Locates an executable on the system
50 std::string which(std::string const& executable);
51 
52 /// Change working directory
53 void cd(std::string const& path);
54 
55 /// Checks if a filesystem entry the given path exists.
56 /// If modeMask is non-zero, this function also checks if it applies to the
57 /// entry's permission bits.
58 bool exists(std::string const& path, int modeMask = 0);
59 
60 /// Checks if the given path is a directory.
61 /// If modeMask is non-zero, this function also checks if it applies to the
62 /// directory's permission bits.
63 bool isdir(std::string const& path, int modeMask = 0);
64 
65 /// Creates a directory at the given path.
66 /// If required, intermediate directories will be created.
67 void mkdir(std::string const& path, int mode = 0777);
68 
69 /// Creates a directory at a suitable temporary location and returns its name.
70 /// Note: If tmpdir is not specified, OSX will always use /tmp because of the
71 /// long directory name sometimes causes problems...
72 std::string mktempd(
73  std::string const& prefix = "tmp",
74  std::string const& tmpdir = "");
75 
76 /// Creates a file at a suitable temporary location and returns its name.
77 /// Note: OSX will always use /tmp because of the long directory name sometimes
78 /// causes problems...
79 std::string mktemp(
80  std::string const& prefix = "cherrypi.tmp",
81  std::string const& tmpdir = "");
82 
83 /// Update file access and modification times.
84 /// This is a simplified version of touch(1) that changes both access and
85 /// modification time to "now".
86 void touch(std::string const& path);
87 
88 /// Recursively remove a file system entry.
89 /// Roughly corresponds to rm -rf and thus also silently swallows any errors.
90 void rmrf(std::string const& path);
91 
92 /// moves a file system entry.
93 /// Roughly corresponds to mv source dest
94 void mv(std::string const& source, std::string const& dest);
95 
96 /// Find files matching a pattern (non-recursively).
97 std::vector<std::string> find(
98  std::string const& path,
99  std::string const& pattern);
100 
101 /// Find files matching a pattern (recursively).
102 std::vector<std::string> findr(
103  std::string const& path,
104  std::string const& pattern);
105 
106 /// File globbing
107 std::vector<std::string> glob(std::string const& pattern);
108 
109 /// Get the size of the file in bytes
110 size_t size(std::string const& path);
111 
112 /// Get the last modification time of the file
113 std::chrono::system_clock::time_point mtime(std::string const& path);
114 
115 /// Reads data from a given path and splits it into separate lines.
116 std::vector<std::string> readLines(std::string const& path);
117 
118 /// writes data to a given path in different lines
119 void writeLines(std::string const& path, std::vector<std::string>);
120 
121 /// Reads data from a given path and splits it into separate lines.
122 /// This version will return a given partition of all lines. Specifically, it
123 /// returns every kth line for which `(k % numPartitions) == partition`.
124 std::vector<std::string>
125 readLinesPartition(std::string const& path, int partition, int numPartitions);
126 
127 std::vector<unsigned char> md5(std::string const& path);
128 
129 } // namespace fsutils
130 } // namespace common
void mv(std::string const &src, std::string const &dest)
moves a file system entry.
Definition: fsutils.cpp:462
void rmrf(std::string const &path)
Recursively remove a file system entry.
Definition: fsutils.cpp:310
std::vector< std::string > readLinesPartition(std::string const &path, int partition, int numPartitions)
Reads data from a given path and splits it into separate lines.
Definition: fsutils.cpp:504
size_t size(std::string const &path)
Get the size of the file in bytes.
Definition: fsutils.cpp:475
std::chrono::system_clock::time_point mtime(std::string const &path)
Get the last modification time of the file.
Definition: fsutils.cpp:483
void touch(std::string const &path)
Update file access and modification times.
Definition: fsutils.cpp:296
void cd(std::string const &path)
Change working directory.
Definition: fsutils.cpp:230
char constexpr kPathSep
Definition: fsutils.h:35
std::string which(std::string const &executable)
Locates an executable on the system.
Definition: fsutils.cpp:192
bool isdir(std::string const &path, int modeMask)
Checks if the given path is a directory.
Definition: fsutils.cpp:248
void writeLines(std::string const &path, std::vector< std::string > data)
writes data to a given path in different lines
Definition: fsutils.cpp:491
std::string mktemp(std::string const &prefix, std::string const &tmpLoc)
Creates a file at a suitable temporary location and returns its name.
Definition: fsutils.cpp:273
std::string dirname(std::string const &path)
Equivalent to dirname(1)
Definition: fsutils.cpp:423
General utilities.
Definition: assert.cpp:7
void mkdir(std::string const &path, int mode)
Creates a directory at the given path.
Definition: fsutils.cpp:452
std::vector< std::string > findr(std::string const &path, std::string const &pattern)
Find files matching a pattern (recursively).
Definition: fsutils.cpp:344
std::string basename(std::string const &path, std::string const &ext)
Equivalent to basename(1)
Definition: fsutils.cpp:402
std::vector< std::string > readLines(std::string const &path)
Reads data from a given path and splits it into separate lines.
Definition: fsutils.cpp:499
std::string mktempd(std::string const &prefix, std::string const &tmpdir)
Creates a directory at a suitable temporary location and returns its name.
Definition: fsutils.cpp:262
std::vector< unsigned char > md5(std::string const &path)
Definition: fsutils.cpp:381
std::vector< std::string > glob(std::string const &pattern)
File globbing.
Definition: fsutils.cpp:365
Utility functions for interacting with the file system.
Definition: fsutils.cpp:37
std::string pwd()
Returns the current working directory.
Definition: fsutils.cpp:182
std::vector< std::string > find(std::string const &path, std::string const &pattern)
Find files matching a pattern (non-recursively).
Definition: fsutils.cpp:328
bool exists(std::string const &path, int modeMask)
Checks if a filesystem entry the given path exists.
Definition: fsutils.cpp:236