1 /*
2    Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 #ifndef OPENFILES_H
26 #define OPENFILES_H
27 
28 #include <Vector.hpp>
29 
30 #define JAM_FILE_ID 386
31 
32 
33 class OpenFiles
34 {
35 public:
OpenFiles()36    OpenFiles(){ }
37 
38   /* Get a pointer to the file with id */
39   AsyncFile* find(Uint16 id) const;
40   /* Insert file with id */
41   bool insert(AsyncFile* file, Uint16 id);
42   /* Erase file with id */
43   bool erase(Uint16 id);
44   /* Get number of open files */
45   unsigned size();
46 
47   Uint16 getId(unsigned i);
48   AsyncFile* getFile(unsigned i);
49 
50 
51 private:
52 
53   class OpenFileItem {
54   public:
OpenFileItem()55     OpenFileItem():  m_file(NULL), m_id(0){}
56 
57     AsyncFile* m_file;
58     Uint16 m_id;
59   };
60 
61   Vector<OpenFileItem> m_files;
62 };
63 
64 
65 //*****************************************************************************
find(Uint16 id) const66 inline AsyncFile* OpenFiles::find(Uint16 id) const {
67   for (unsigned i = 0; i < m_files.size(); i++){
68     if (m_files[i].m_id == id){
69       return m_files[i].m_file;
70     }
71   }
72   return NULL;
73 }
74 
75 //*****************************************************************************
erase(Uint16 id)76 inline bool OpenFiles::erase(Uint16 id){
77   for (unsigned i = 0; i < m_files.size(); i++){
78     if (m_files[i].m_id == id){
79       m_files.erase(i);
80       return true;
81     }
82   }
83   // Item was not found in list
84   return false;
85 }
86 
87 
88 //*****************************************************************************
insert(AsyncFile * file,Uint16 id)89 inline bool OpenFiles::insert(AsyncFile* file, Uint16 id){
90   // Check if file has already been opened
91   for (unsigned i = 0; i < m_files.size(); i++){
92     if(m_files[i].m_file == NULL)
93       continue;
94 
95     if(strcmp(m_files[i].m_file->theFileName.c_str(),
96 	      file->theFileName.c_str()) == 0)
97     {
98       BaseString names;
99       names.assfmt("open: >%s< existing: >%s<",
100 		   file->theFileName.c_str(),
101 		   m_files[i].m_file->theFileName.c_str());
102       ERROR_SET(fatal, NDBD_EXIT_AFS_ALREADY_OPEN, names.c_str(),
103 		"OpenFiles::insert()");
104     }
105   }
106 
107   // Insert the file into vector
108   OpenFileItem openFile;
109   openFile.m_id = id;
110   openFile.m_file = file;
111   m_files.push_back(openFile);
112 
113   return true;
114 }
115 
116 //*****************************************************************************
getId(unsigned i)117 inline Uint16 OpenFiles::getId(unsigned i){
118   return m_files[i].m_id;
119 }
120 
121 //*****************************************************************************
getFile(unsigned i)122 inline AsyncFile* OpenFiles::getFile(unsigned i){
123   return m_files[i].m_file;
124 }
125 
126 //*****************************************************************************
size()127 inline unsigned OpenFiles::size(){
128   return m_files.size();
129 }
130 
131 
132 #undef JAM_FILE_ID
133 
134 #endif
135