• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef CRAZY_LINKER_SYSTEM_H
6 #define CRAZY_LINKER_SYSTEM_H
7 
8 #include <errno.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <sys/mman.h>
12 #include <unistd.h>
13 
14 #include "crazy_linker_util.h"  // for String
15 
16 // System abstraction used by the crazy linker.
17 // This is used to make unit testing easier without using tons of
18 // dependency injection in the rest of the code base.
19 //
20 // In a nutshell: in a normal build, this will wrap normal open() / read()
21 // calls. During unit testing, everything is mocked, see
22 // crazy_linker_system_mock.cpp
23 
24 namespace crazy {
25 
26 enum FileOpenMode {
27   FILE_OPEN_READ_ONLY = 0,
28   FILE_OPEN_READ_WRITE,
29   FILE_OPEN_WRITE
30 };
31 
32 // Scoping file descriptor class.
33 class FileDescriptor {
34  public:
35 #ifdef UNIT_TESTS
36 #define kEmptyFD NULL
37   typedef void* HandleType;
38 #else
39 #define kEmptyFD (-1)
40   typedef int HandleType;
41 #endif
42 
FileDescriptor()43   FileDescriptor() : fd_(kEmptyFD) {}
44 
FileDescriptor(const char * path)45   FileDescriptor(const char* path) : fd_(kEmptyFD) { OpenReadOnly(path); }
46 
~FileDescriptor()47   ~FileDescriptor() { Close(); }
48 
IsOk()49   bool IsOk() const { return fd_ != kEmptyFD; }
Get()50   HandleType Get() const { return fd_; }
51   bool OpenReadOnly(const char* path);
52   bool OpenReadWrite(const char* path);
53   int Read(void* buffer, size_t buffer_size);
54   int SeekTo(off_t offset);
55   void* Map(void* address,
56             size_t length,
57             int prot_flags,
58             int flags,
59             off_t offset);
60   void Close();
61 
62  private:
63   HandleType fd_;
64 };
65 
66 // Returns true iff a given file path exists.
67 bool PathExists(const char* path_name);
68 
69 // Returns true iff a given path is a regular file (or link to a regular
70 // file).
71 bool PathIsFile(const char* path_name);
72 
73 // Returns the current directory, as a string.
74 String GetCurrentDirectory();
75 
76 // Returns the value of a given environment variable.
77 const char* GetEnv(const char* var_name);
78 
79 // Returns true iff |lib_name| corresponds to one of the NDK-exposed
80 // system libraries.
81 bool IsSystemLibrary(const char* lib_name);
82 
83 }  // namespace crazy
84 
85 #endif  // CRAZY_LINKER_SYSTEM_H
86