The Pedigree Project  0.1
Symlink.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "Symlink.h"
21 #include "Filesystem.h"
22 #include "pedigree/kernel/utilities/utility.h"
23 
24 Symlink::Symlink() : File(), m_pCachedSymlink(0)
25 {
26 }
27 
29  const String &name, Time::Timestamp accessedTime,
30  Time::Timestamp modifiedTime, Time::Timestamp creationTime, uintptr_t inode,
31  Filesystem *pFs, size_t size, File *pParent)
32  : File(
33  name, accessedTime, modifiedTime, creationTime, inode, pFs, size,
34  pParent),
35  m_pCachedSymlink(0), m_sTarget()
36 {
37 }
38 
40 {
41 }
42 
43 void Symlink::initialise(bool bForce)
44 {
45  if (m_sTarget.length() && !bForce)
46  return;
47 
48  size_t sz = getSize();
49  if (sz > 0x1000)
50  sz = 0x1000;
51 
52  // Read symlink target.
53  char *pBuffer = new char[sz];
54  read(0ULL, sz, reinterpret_cast<uintptr_t>(pBuffer));
55  pBuffer[sz] = '\0';
56 
57  // Convert to String object, wipe out whitespace.
58  m_sTarget = pBuffer;
59  m_sTarget.rstrip();
60 
61  // Wipe out cached symlink if we're being forced to re-init.
62  if (bForce)
63  m_pCachedSymlink = 0;
64 }
65 
67 {
68  if (m_pCachedSymlink)
69  return m_pCachedSymlink;
70 
71  initialise();
72 
73  m_pCachedSymlink = m_pFilesystem->find(m_sTarget, m_pParent);
74  return m_pCachedSymlink;
75 }
76 
77 int Symlink::followLink(char *pBuffer, size_t bufLen)
78 {
79  initialise();
80 
81  if (m_sTarget.length() < bufLen)
82  bufLen = m_sTarget.length();
83 
84  StringCopyN(pBuffer, static_cast<const char *>(m_sTarget), bufLen);
85 
86  if (bufLen < m_sTarget.length())
87  pBuffer[bufLen] = '\0';
88 
89  return bufLen;
90 }
91 
92 bool Symlink::isBytewise() const
93 {
94  return true;
95 }
Definition: String.h:49
virtual File * find(const StringView &path)
Definition: Filesystem.cc:53
virtual uint64_t read(uint64_t location, uint64_t size, uintptr_t buffer, bool bCanBlock=true) final
Definition: File.cc:116
void rstrip()
Definition: String.cc:501
Definition: File.h:66