本文整理汇总了C++中Try::bytes方法的典型用法代码示例。如果您正苦于以下问题:C++ Try::bytes方法的具体用法?C++ Try::bytes怎么用?C++ Try::bytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Try
的用法示例。
在下文中一共展示了Try::bytes方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Error
static Try<Nothing> doIncreasePageCache(const vector<string>& tokens)
{
const Bytes UNIT = Megabytes(1);
if (tokens.size() < 2) {
return Error("Expect at least one argument");
}
Try<Bytes> size = Bytes::parse(tokens[1]);
if (size.isError()) {
return Error("The first argument '" + tokens[1] + "' is not a byte size");
}
// TODO(chzhcn): Currently, we assume the current working directory
// is a temporary directory and will be cleaned up when the test
// finishes. Since the child process will inherit the current
// working directory from the parent process, that means the test
// that uses this helper probably needs to inherit from
// TemporaryDirectoryTest. Consider relaxing this constraint.
Try<string> path = os::mktemp(path::join(os::getcwd(), "XXXXXX"));
if (path.isError()) {
return Error("Failed to create a temporary file: " + path.error());
}
Try<int> fd = os::open(path.get(), O_WRONLY);
if (fd.isError()) {
return Error("Failed to open file: " + fd.error());
}
// NOTE: We are doing round-down here to calculate the number of
// writes to do.
for (uint64_t i = 0; i < size->bytes() / UNIT.bytes(); i++) {
// Write UNIT size to disk at a time. The content isn't important.
Try<Nothing> write = os::write(fd.get(), string(UNIT.bytes(), 'a'));
if (write.isError()) {
os::close(fd.get());
return Error("Failed to write file: " + write.error());
}
// Use fsync to make sure data is written to disk.
Try<Nothing> fsync = os::fsync(fd.get());
if (fsync.isError()) {
os::close(fd.get());
return Error("Failed to fsync: " + fsync.error());
}
}
os::close(fd.get());
return Nothing();
}