Skip to content

Commit

Permalink
cpp: convert ServerPidFile() and ServerPidSymlink() into constants
Browse files Browse the repository at this point in the history
These two functions have no logic inside it, they just return a string, and a
string that does not change, so they might be better expressed as real C/C++
constants. This might as well generate some better (and less) code anyway.

--
Change-Id: I78673c537c17c2665a74c5778e45a4d41a5f7b50
Reviewed-on: https://bazel-review.googlesource.com/#/c/6114
MOS_MIGRATED_REVID=133805201
  • Loading branch information
tfarina authored and laszlocsomor committed Sep 21, 2016
1 parent c07af7a commit 048bbfc
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/main/cpp/blaze.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1000,8 +1000,8 @@ static int GetServerPid(const string &server_dir) {
// TODO(lberki): Remove the readlink() call when there is no chance of an old
// server lingering around. Probably safe after 2016.06.01.
int len;
string pid_file = blaze_util::JoinPath(server_dir, ServerPidFile());
string pid_symlink = blaze_util::JoinPath(server_dir, ServerPidSymlink());
string pid_file = blaze_util::JoinPath(server_dir, kServerPidFile);
string pid_symlink = blaze_util::JoinPath(server_dir, kServerPidSymlink);
len = readlink(pid_symlink.c_str(), buf, sizeof(buf) - 1);
if (len < 0) {
int fd = open(pid_file.c_str(), O_RDONLY);
Expand Down
9 changes: 2 additions & 7 deletions src/main/cpp/blaze_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,8 @@ using std::vector;

namespace blaze {

string ServerPidFile() {
return "server.pid.txt";
}

string ServerPidSymlink() {
return "server.pid";
}
const char kServerPidFile[] = "server.pid.txt";
const char kServerPidSymlink[] = "server.pid";

string GetUserName() {
const char *user = getenv("USER");
Expand Down
5 changes: 2 additions & 3 deletions src/main/cpp/blaze_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ namespace blaze {

using std::string;

string ServerPidFile();

string ServerPidSymlink();
extern const char kServerPidFile[];
extern const char kServerPidSymlink[];

string GetUserName();

Expand Down
2 changes: 1 addition & 1 deletion src/main/cpp/blaze_util_mingw.cc
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ void ExecuteDaemon(const string& exe, const std::vector<string>& args_vector,
CloseHandle(pipe_read);

string pid_string = ToString(processInfo.dwProcessId);
string pid_file = blaze_util::JoinPath(server_dir, ServerPidFile());
string pid_file = blaze_util::JoinPath(server_dir, kServerPidFile);
if (!WriteFile(pid_string, pid_file)) {
// Not a lot we can do if this fails
fprintf(stderr, "Cannot write PID file %s\n", pid_file.c_str());
Expand Down
5 changes: 2 additions & 3 deletions src/main/cpp/blaze_util_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,8 @@ void ExecuteDaemon(const string& exe,

Daemonize(daemon_output);
string pid_string = ToString(getpid());
string pid_file = blaze_util::JoinPath(server_dir, ServerPidFile());
string pid_symlink_file =
blaze_util::JoinPath(server_dir, ServerPidSymlink());
string pid_file = blaze_util::JoinPath(server_dir, kServerPidFile);
string pid_symlink_file = blaze_util::JoinPath(server_dir, kServerPidSymlink);

if (!WriteFile(pid_string, pid_file)) {
// The exit code does not matter because we are already in the daemonized
Expand Down

0 comments on commit 048bbfc

Please sign in to comment.