ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / native_client_sdk / src / libraries / nacl_io / getdents_helper.cc
blob319a575b90da2eebe0dd909ec3bbacab906bdc27
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "nacl_io/getdents_helper.h"
7 #include <assert.h>
8 #include <errno.h>
9 #include <string.h>
11 #include <algorithm>
13 #include "nacl_io/log.h"
14 #include "nacl_io/osinttypes.h"
16 #include "sdk_util/macros.h"
18 namespace nacl_io {
20 GetDentsHelper::GetDentsHelper()
21 : curdir_ino_(0), parentdir_ino_(0), init_defaults_(false) {
22 Initialize();
25 GetDentsHelper::GetDentsHelper(ino_t curdir_ino, ino_t parentdir_ino)
26 : curdir_ino_(curdir_ino),
27 parentdir_ino_(parentdir_ino),
28 init_defaults_(true) {
29 Initialize();
32 void GetDentsHelper::Reset() {
33 dirents_.clear();
34 Initialize();
37 void GetDentsHelper::Initialize() {
38 if (init_defaults_) {
39 // Add the default entries: "." and ".."
40 AddDirent(curdir_ino_, ".", 1);
41 AddDirent(parentdir_ino_, "..", 2);
45 void GetDentsHelper::AddDirent(ino_t ino, const char* name, size_t namelen) {
46 assert(name != NULL);
47 dirents_.push_back(dirent());
48 dirent& entry = dirents_.back();
49 entry.d_ino = ino;
50 entry.d_reclen = sizeof(dirent);
52 if (namelen == 0)
53 namelen = strlen(name);
55 size_t d_name_max = MEMBER_SIZE(dirent, d_name) - 1; // -1 for \0.
56 size_t copylen = std::min(d_name_max, namelen);
57 strncpy(&entry.d_name[0], name, copylen);
58 entry.d_name[copylen] = 0;
61 Error GetDentsHelper::GetDents(size_t offs,
62 dirent* pdir,
63 size_t size,
64 int* out_bytes) const {
65 *out_bytes = 0;
67 // If the buffer pointer is invalid, fail
68 if (NULL == pdir) {
69 LOG_TRACE("dirent pointer is NULL.");
70 return EINVAL;
73 // If the buffer is too small, fail
74 if (size < sizeof(dirent)) {
75 LOG_TRACE("dirent buffer size is too small: %" PRIuS " < %" PRIuS "",
76 size, sizeof(dirent));
77 return EINVAL;
80 // Force size to a multiple of dirent
81 size -= size % sizeof(dirent);
83 size_t max = dirents_.size() * sizeof(dirent);
84 if (offs >= max) {
85 // OK, trying to read past the end.
86 return 0;
89 if (offs + size >= max)
90 size = max - offs;
92 memcpy(pdir, reinterpret_cast<const char*>(dirents_.data()) + offs, size);
93 *out_bytes = size;
94 return 0;
97 } // namespace nacl_io