OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "nacl_io/getdents_helper.h" | 5 #include "nacl_io/getdents_helper.h" |
6 | 6 |
7 #include <assert.h> | 7 #include <assert.h> |
8 #include <errno.h> | 8 #include <errno.h> |
9 #include <string.h> | 9 #include <string.h> |
10 | 10 |
11 #include <algorithm> | 11 #include <algorithm> |
12 | 12 |
| 13 #include "nacl_io/log.h" |
| 14 |
13 #include "sdk_util/macros.h" | 15 #include "sdk_util/macros.h" |
14 | 16 |
15 namespace nacl_io { | 17 namespace nacl_io { |
16 | 18 |
17 GetDentsHelper::GetDentsHelper() | 19 GetDentsHelper::GetDentsHelper() |
18 : curdir_ino_(0), parentdir_ino_(0), init_defaults_(false) { | 20 : curdir_ino_(0), parentdir_ino_(0), init_defaults_(false) { |
19 Initialize(); | 21 Initialize(); |
20 } | 22 } |
21 | 23 |
22 GetDentsHelper::GetDentsHelper(ino_t curdir_ino, ino_t parentdir_ino) | 24 GetDentsHelper::GetDentsHelper(ino_t curdir_ino, ino_t parentdir_ino) |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 entry.d_name[copylen] = 0; | 58 entry.d_name[copylen] = 0; |
57 } | 59 } |
58 | 60 |
59 Error GetDentsHelper::GetDents(size_t offs, | 61 Error GetDentsHelper::GetDents(size_t offs, |
60 dirent* pdir, | 62 dirent* pdir, |
61 size_t size, | 63 size_t size, |
62 int* out_bytes) const { | 64 int* out_bytes) const { |
63 *out_bytes = 0; | 65 *out_bytes = 0; |
64 | 66 |
65 // If the buffer pointer is invalid, fail | 67 // If the buffer pointer is invalid, fail |
66 if (NULL == pdir) | 68 if (NULL == pdir) { |
| 69 LOG_TRACE("dirent pointer is NULL."); |
67 return EINVAL; | 70 return EINVAL; |
| 71 } |
68 | 72 |
69 // If the buffer is too small, fail | 73 // If the buffer is too small, fail |
70 if (size < sizeof(dirent)) | 74 if (size < sizeof(dirent)) { |
| 75 LOG_TRACE("dirent buffer size is too small: %d < %d", size, sizeof(dirent)); |
71 return EINVAL; | 76 return EINVAL; |
| 77 } |
72 | 78 |
73 // Force size to a multiple of dirent | 79 // Force size to a multiple of dirent |
74 size -= size % sizeof(dirent); | 80 size -= size % sizeof(dirent); |
75 | 81 |
76 size_t max = dirents_.size() * sizeof(dirent); | 82 size_t max = dirents_.size() * sizeof(dirent); |
77 if (offs >= max) { | 83 if (offs >= max) { |
78 // OK, trying to read past the end. | 84 // OK, trying to read past the end. |
79 return 0; | 85 return 0; |
80 } | 86 } |
81 | 87 |
82 if (offs + size >= max) | 88 if (offs + size >= max) |
83 size = max - offs; | 89 size = max - offs; |
84 | 90 |
85 memcpy(pdir, reinterpret_cast<const char*>(dirents_.data()) + offs, size); | 91 memcpy(pdir, reinterpret_cast<const char*>(dirents_.data()) + offs, size); |
86 *out_bytes = size; | 92 *out_bytes = size; |
87 return 0; | 93 return 0; |
88 } | 94 } |
89 | 95 |
90 } // namespace nacl_io | 96 } // namespace nacl_io |
OLD | NEW |