void ShellHandler::cat(string& _return, const string& file) {
  unique_lock<mutex> g(mutex_);
  validateState();

  FdGuard fd(openat(cwd_, file.c_str(), O_RDONLY));
  if (fd < 0) {
    throwErrno("unable to open file");
  }

  // Resize the output string to the expected size,
  // to make it more efficient for larger files.
  struct stat s;
  if (fstat(fd, &s) != 0) {
    throwErrno("unable to stat() file");
  }
  _return.reserve(s.st_size);

  char buf[4096];
  while (true) {
    ssize_t bytesRead = read(fd, buf, sizeof(buf));
    if (bytesRead == 0) {
      break;
    } else if (bytesRead < 0) {
      throwErrno("read failed");
    }

    _return.append(buf, bytesRead);
  }
}
void ShellHandler::listDirectory(vector<StatInfo> &_return,
                                 const string& dir) {
  unique_lock<mutex> g(mutex_);
  validateState();

  FdGuard fd(openat(cwd_, dir.c_str(), O_RDONLY));
  if (fd < 0) {
    throwErrno("failed to open directory");
  }

  DirGuard d(fdopendir(fd));
  if (!d) {
    throwErrno("failed to open directory handle");
  }
  fd.release();

  while (true) {
    errno = 0;
    struct dirent* ent = readdir(d);
    if (ent == nullptr) {
      if (errno != 0) {
        throwErrno("readdir() failed");
      }
      break;
    }

    StatInfo si;
    si.name = ent->d_name;
    _return.push_back(si);
  }
}
Пример #3
0
bool
HibernationManager::setTargetState( HibernatorBase::SLEEP_STATE state )
{
	if ( state == m_target_state ) {
		return true;
	}
	if ( !validateState( state ) ) {
		return false;
	}
	m_target_state = state;
	return true;
}
void ShellHandler::chdir(const string& dir) {
  unique_lock<mutex> g(mutex_);
  validateState();

  FdGuard newCwd(openat(cwd_, dir.c_str(), O_RDONLY));
  if (newCwd < 0) {
    throwErrno("unable to change directory");
  }

  close(cwd_);
  cwd_ = newCwd.release();
}
Пример #5
0
bool
HibernationManager::switchToState ( HibernatorBase::SLEEP_STATE state )
{
	if ( !validateState( state ) ) {
		return false;
	}
	if ( NULL == m_hibernator ) {
		dprintf( D_ALWAYS, "Can't switch to state %s: no hibernator\n",
				 sleepStateToString( state ) );
		return false;
	}
	return m_hibernator->switchToState ( state, m_actual_state, true );
}
void ShellHandler::pwd(string& _return) {
  unique_lock<mutex> g(mutex_);
  validateState();

  // TODO: this only works on linux
  char procPath[1024];
  snprintf(procPath, sizeof(procPath), "/proc/self/fd/%d", cwd_);

  char cwdPath[1024];
  ssize_t numBytes = readlink(procPath, cwdPath, sizeof(cwdPath));
  if (numBytes < 0) {
    throwErrno("failed to determine current working directory");
  }

  _return.assign(cwdPath, numBytes);
}
Пример #7
0
void ComputeShaderInstance::dispatch(const math::Vec3ui &size) {
	bind();
	validateState();
	gl::dispatchCompute(size);
}