SdBaseFile.cpp

Thu, 07 Jul 2016 12:23:34 +0200

author
mbayer
date
Thu, 07 Jul 2016 12:23:34 +0200
changeset 2
b373b0288715
parent 0
2c8ba1964db7
permissions
-rw-r--r--

added missing sanguino files

0
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1 /* Arduino SdFat Library
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
2 * Copyright (C) 2009 by William Greiman
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
3 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
4 * This file is part of the Arduino SdFat Library
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
5 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
6 * This Library is free software: you can redistribute it and/or modify
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
7 * it under the terms of the GNU General Public License as published by
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
8 * the Free Software Foundation, either version 3 of the License, or
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
9 * (at your option) any later version.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
10 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
11 * This Library is distributed in the hope that it will be useful,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
14 * GNU General Public License for more details.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
15 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
16 * You should have received a copy of the GNU General Public License
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
17 * along with the Arduino SdFat Library. If not, see
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
18 * <http://www.gnu.org/licenses/>.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
19 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
20
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
21 #include "Marlin.h"
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
22 #ifdef SDSUPPORT
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
23
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
24 #include "SdBaseFile.h"
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
25 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
26 // pointer to cwd directory
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
27 SdBaseFile* SdBaseFile::cwd_ = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
28 // callback function for date/time
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
29 void (*SdBaseFile::dateTime_)(uint16_t* date, uint16_t* time) = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
30 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
31 // add a cluster to a file
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
32 bool SdBaseFile::addCluster() {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
33 if (!vol_->allocContiguous(1, &curCluster_)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
34
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
35 // if first cluster of file link to directory entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
36 if (firstCluster_ == 0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
37 firstCluster_ = curCluster_;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
38 flags_ |= F_FILE_DIR_DIRTY;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
39 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
40 return true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
41
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
42 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
43 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
44 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
45 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
46 // Add a cluster to a directory file and zero the cluster.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
47 // return with first block of cluster in the cache
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
48 bool SdBaseFile::addDirCluster() {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
49 uint32_t block;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
50 // max folder size
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
51 if (fileSize_/sizeof(dir_t) >= 0XFFFF) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
52
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
53 if (!addCluster()) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
54 if (!vol_->cacheFlush()) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
55
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
56 block = vol_->clusterStartBlock(curCluster_);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
57
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
58 // set cache to first block of cluster
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
59 vol_->cacheSetBlockNumber(block, true);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
60
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
61 // zero first block of cluster
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
62 memset(vol_->cacheBuffer_.data, 0, 512);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
63
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
64 // zero rest of cluster
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
65 for (uint8_t i = 1; i < vol_->blocksPerCluster_; i++) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
66 if (!vol_->writeBlock(block + i, vol_->cacheBuffer_.data)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
67 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
68 // Increase directory file size by cluster size
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
69 fileSize_ += 512UL << vol_->clusterSizeShift_;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
70 return true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
71
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
72 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
73 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
74 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
75 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
76 // cache a file's directory entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
77 // return pointer to cached entry or null for failure
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
78 dir_t* SdBaseFile::cacheDirEntry(uint8_t action) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
79 if (!vol_->cacheRawBlock(dirBlock_, action)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
80 return vol_->cache()->dir + dirIndex_;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
81
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
82 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
83 return 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
84 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
85 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
86 /** Close a file and force cached data and directory information
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
87 * to be written to the storage device.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
88 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
89 * \return The value one, true, is returned for success and
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
90 * the value zero, false, is returned for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
91 * Reasons for failure include no file is open or an I/O error.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
92 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
93 bool SdBaseFile::close() {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
94 bool rtn = sync();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
95 type_ = FAT_FILE_TYPE_CLOSED;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
96 return rtn;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
97 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
98 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
99 /** Check for contiguous file and return its raw block range.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
100 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
101 * \param[out] bgnBlock the first block address for the file.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
102 * \param[out] endBlock the last block address for the file.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
103 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
104 * \return The value one, true, is returned for success and
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
105 * the value zero, false, is returned for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
106 * Reasons for failure include file is not contiguous, file has zero length
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
107 * or an I/O error occurred.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
108 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
109 bool SdBaseFile::contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
110 // error if no blocks
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
111 if (firstCluster_ == 0) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
112
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
113 for (uint32_t c = firstCluster_; ; c++) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
114 uint32_t next;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
115 if (!vol_->fatGet(c, &next)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
116
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
117 // check for contiguous
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
118 if (next != (c + 1)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
119 // error if not end of chain
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
120 if (!vol_->isEOC(next)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
121 *bgnBlock = vol_->clusterStartBlock(firstCluster_);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
122 *endBlock = vol_->clusterStartBlock(c)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
123 + vol_->blocksPerCluster_ - 1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
124 return true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
125 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
126 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
127
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
128 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
129 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
130 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
131 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
132 /** Create and open a new contiguous file of a specified size.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
133 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
134 * \note This function only supports short DOS 8.3 names.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
135 * See open() for more information.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
136 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
137 * \param[in] dirFile The directory where the file will be created.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
138 * \param[in] path A path with a valid DOS 8.3 file name.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
139 * \param[in] size The desired file size.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
140 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
141 * \return The value one, true, is returned for success and
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
142 * the value zero, false, is returned for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
143 * Reasons for failure include \a path contains
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
144 * an invalid DOS 8.3 file name, the FAT volume has not been initialized,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
145 * a file is already open, the file already exists, the root
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
146 * directory is full or an I/O error.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
147 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
148 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
149 bool SdBaseFile::createContiguous(SdBaseFile* dirFile,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
150 const char* path, uint32_t size) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
151 uint32_t count;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
152 // don't allow zero length file
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
153 if (size == 0) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
154 if (!open(dirFile, path, O_CREAT | O_EXCL | O_RDWR)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
155
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
156 // calculate number of clusters needed
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
157 count = ((size - 1) >> (vol_->clusterSizeShift_ + 9)) + 1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
158
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
159 // allocate clusters
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
160 if (!vol_->allocContiguous(count, &firstCluster_)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
161 remove();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
162 goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
163 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
164 fileSize_ = size;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
165
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
166 // insure sync() will update dir entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
167 flags_ |= F_FILE_DIR_DIRTY;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
168
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
169 return sync();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
170
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
171 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
172 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
173 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
174 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
175 /** Return a file's directory entry.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
176 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
177 * \param[out] dir Location for return of the file's directory entry.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
178 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
179 * \return The value one, true, is returned for success and
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
180 * the value zero, false, is returned for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
181 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
182 bool SdBaseFile::dirEntry(dir_t* dir) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
183 dir_t* p;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
184 // make sure fields on SD are correct
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
185 if (!sync()) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
186
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
187 // read entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
188 p = cacheDirEntry(SdVolume::CACHE_FOR_READ);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
189 if (!p) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
190
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
191 // copy to caller's struct
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
192 memcpy(dir, p, sizeof(dir_t));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
193 return true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
194
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
195 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
196 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
197 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
198 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
199 /** Format the name field of \a dir into the 13 byte array
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
200 * \a name in standard 8.3 short name format.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
201 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
202 * \param[in] dir The directory structure containing the name.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
203 * \param[out] name A 13 byte char array for the formatted name.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
204 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
205 void SdBaseFile::dirName(const dir_t& dir, char* name) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
206 uint8_t j = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
207 for (uint8_t i = 0; i < 11; i++) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
208 if (dir.name[i] == ' ')continue;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
209 if (i == 8) name[j++] = '.';
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
210 name[j++] = dir.name[i];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
211 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
212 name[j] = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
213 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
214 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
215 /** Test for the existence of a file in a directory
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
216 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
217 * \param[in] name Name of the file to be tested for.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
218 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
219 * The calling instance must be an open directory file.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
220 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
221 * dirFile.exists("TOFIND.TXT") searches for "TOFIND.TXT" in the directory
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
222 * dirFile.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
223 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
224 * \return true if the file exists else false.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
225 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
226 bool SdBaseFile::exists(const char* name) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
227 SdBaseFile file;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
228 return file.open(this, name, O_READ);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
229 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
230 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
231 /**
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
232 * Get a string from a file.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
233 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
234 * fgets() reads bytes from a file into the array pointed to by \a str, until
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
235 * \a num - 1 bytes are read, or a delimiter is read and transferred to \a str,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
236 * or end-of-file is encountered. The string is then terminated
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
237 * with a null byte.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
238 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
239 * fgets() deletes CR, '\\r', from the string. This insures only a '\\n'
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
240 * terminates the string for Windows text files which use CRLF for newline.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
241 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
242 * \param[out] str Pointer to the array where the string is stored.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
243 * \param[in] num Maximum number of characters to be read
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
244 * (including the final null byte). Usually the length
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
245 * of the array \a str is used.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
246 * \param[in] delim Optional set of delimiters. The default is "\n".
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
247 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
248 * \return For success fgets() returns the length of the string in \a str.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
249 * If no data is read, fgets() returns zero for EOF or -1 if an error occurred.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
250 **/
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
251 int16_t SdBaseFile::fgets(char* str, int16_t num, char* delim) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
252 char ch;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
253 int16_t n = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
254 int16_t r = -1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
255 while ((n + 1) < num && (r = read(&ch, 1)) == 1) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
256 // delete CR
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
257 if (ch == '\r') continue;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
258 str[n++] = ch;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
259 if (!delim) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
260 if (ch == '\n') break;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
261 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
262 if (strchr(delim, ch)) break;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
263 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
264 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
265 if (r < 0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
266 // read error
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
267 return -1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
268 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
269 str[n] = '\0';
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
270 return n;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
271 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
272 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
273 /** Get a file's name
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
274 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
275 * \param[out] name An array of 13 characters for the file's name.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
276 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
277 * \return The value one, true, is returned for success and
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
278 * the value zero, false, is returned for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
279 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
280 bool SdBaseFile::getFilename(char* name) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
281 if (!isOpen()) return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
282
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
283 if (isRoot()) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
284 name[0] = '/';
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
285 name[1] = '\0';
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
286 return true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
287 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
288 // cache entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
289 dir_t* p = cacheDirEntry(SdVolume::CACHE_FOR_READ);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
290 if (!p) return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
291
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
292 // format name
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
293 dirName(*p, name);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
294 return true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
295 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
296 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
297 void SdBaseFile::getpos(fpos_t* pos) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
298 pos->position = curPosition_;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
299 pos->cluster = curCluster_;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
300 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
301
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
302 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
303 /** List directory contents.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
304 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
305 * \param[in] pr Print stream for list.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
306 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
307 * \param[in] flags The inclusive OR of
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
308 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
309 * LS_DATE - %Print file modification date
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
310 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
311 * LS_SIZE - %Print file size.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
312 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
313 * LS_R - Recursive list of subdirectories.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
314 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
315 * \param[in] indent Amount of space before file name. Used for recursive
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
316 * list to indicate subdirectory level.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
317 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
318 void SdBaseFile::ls(uint8_t flags, uint8_t indent) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
319 rewind();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
320 int8_t status;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
321 while ((status = lsPrintNext( flags, indent))) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
322 if (status > 1 && (flags & LS_R)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
323 uint16_t index = curPosition()/32 - 1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
324 SdBaseFile s;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
325 if (s.open(this, index, O_READ)) s.ls( flags, indent + 2);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
326 seekSet(32 * (index + 1));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
327 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
328 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
329 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
330 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
331 // saves 32 bytes on stack for ls recursion
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
332 // return 0 - EOF, 1 - normal file, or 2 - directory
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
333 int8_t SdBaseFile::lsPrintNext( uint8_t flags, uint8_t indent) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
334 dir_t dir;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
335 uint8_t w = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
336
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
337 while (1) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
338 if (read(&dir, sizeof(dir)) != sizeof(dir)) return 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
339 if (dir.name[0] == DIR_NAME_FREE) return 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
340
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
341 // skip deleted entry and entries for . and ..
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
342 if (dir.name[0] != DIR_NAME_DELETED && dir.name[0] != '.'
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
343 && DIR_IS_FILE_OR_SUBDIR(&dir)) break;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
344 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
345 // indent for dir level
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
346 for (uint8_t i = 0; i < indent; i++) MYSERIAL.write(' ');
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
347
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
348 // print name
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
349 for (uint8_t i = 0; i < 11; i++) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
350 if (dir.name[i] == ' ')continue;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
351 if (i == 8) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
352 MYSERIAL.write('.');
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
353 w++;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
354 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
355 MYSERIAL.write(dir.name[i]);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
356 w++;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
357 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
358 if (DIR_IS_SUBDIR(&dir)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
359 MYSERIAL.write('/');
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
360 w++;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
361 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
362 if (flags & (LS_DATE | LS_SIZE)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
363 while (w++ < 14) MYSERIAL.write(' ');
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
364 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
365 // print modify date/time if requested
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
366 if (flags & LS_DATE) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
367 MYSERIAL.write(' ');
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
368 printFatDate( dir.lastWriteDate);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
369 MYSERIAL.write(' ');
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
370 printFatTime( dir.lastWriteTime);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
371 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
372 // print size if requested
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
373 if (!DIR_IS_SUBDIR(&dir) && (flags & LS_SIZE)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
374 MYSERIAL.write(' ');
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
375 MYSERIAL.print(dir.fileSize);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
376 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
377 MYSERIAL.println();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
378 return DIR_IS_FILE(&dir) ? 1 : 2;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
379 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
380 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
381 // format directory name field from a 8.3 name string
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
382 bool SdBaseFile::make83Name(const char* str, uint8_t* name, const char** ptr) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
383 uint8_t c;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
384 uint8_t n = 7; // max index for part before dot
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
385 uint8_t i = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
386 // blank fill name and extension
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
387 while (i < 11) name[i++] = ' ';
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
388 i = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
389 while (*str != '\0' && *str != '/') {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
390 c = *str++;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
391 if (c == '.') {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
392 if (n == 10) goto fail; // only one dot allowed
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
393 n = 10; // max index for full 8.3 name
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
394 i = 8; // place for extension
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
395 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
396 // illegal FAT characters
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
397 PGM_P p = PSTR("|<>^+=?/[];,*\"\\");
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
398 uint8_t b;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
399 while ((b = pgm_read_byte(p++))) if (b == c) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
400 // check size and only allow ASCII printable characters
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
401 if (i > n || c < 0X21 || c > 0X7E)goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
402 // only upper case allowed in 8.3 names - convert lower to upper
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
403 name[i++] = c < 'a' || c > 'z' ? c : c + ('A' - 'a');
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
404 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
405 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
406 *ptr = str;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
407 // must have a file name, extension is optional
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
408 return name[0] != ' ';
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
409
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
410 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
411 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
412 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
413 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
414 /** Make a new directory.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
415 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
416 * \param[in] parent An open SdFat instance for the directory that will contain
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
417 * the new directory.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
418 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
419 * \param[in] path A path with a valid 8.3 DOS name for the new directory.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
420 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
421 * \param[in] pFlag Create missing parent directories if true.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
422 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
423 * \return The value one, true, is returned for success and
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
424 * the value zero, false, is returned for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
425 * Reasons for failure include this file is already open, \a parent is not a
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
426 * directory, \a path is invalid or already exists in \a parent.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
427 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
428 bool SdBaseFile::mkdir(SdBaseFile* parent, const char* path, bool pFlag) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
429 uint8_t dname[11];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
430 SdBaseFile dir1, dir2;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
431 SdBaseFile* sub = &dir1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
432 SdBaseFile* start = parent;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
433
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
434 if (!parent || isOpen()) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
435
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
436 if (*path == '/') {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
437 while (*path == '/') path++;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
438 if (!parent->isRoot()) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
439 if (!dir2.openRoot(parent->vol_)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
440 parent = &dir2;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
441 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
442 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
443 while (1) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
444 if (!make83Name(path, dname, &path)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
445 while (*path == '/') path++;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
446 if (!*path) break;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
447 if (!sub->open(parent, dname, O_READ)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
448 if (!pFlag || !sub->mkdir(parent, dname)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
449 goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
450 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
451 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
452 if (parent != start) parent->close();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
453 parent = sub;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
454 sub = parent != &dir1 ? &dir1 : &dir2;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
455 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
456 return mkdir(parent, dname);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
457
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
458 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
459 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
460 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
461 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
462 bool SdBaseFile::mkdir(SdBaseFile* parent, const uint8_t dname[11]) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
463 uint32_t block;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
464 dir_t d;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
465 dir_t* p;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
466
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
467 if (!parent->isDir()) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
468
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
469 // create a normal file
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
470 if (!open(parent, dname, O_CREAT | O_EXCL | O_RDWR)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
471
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
472 // convert file to directory
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
473 flags_ = O_READ;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
474 type_ = FAT_FILE_TYPE_SUBDIR;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
475
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
476 // allocate and zero first cluster
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
477 if (!addDirCluster())goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
478
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
479 // force entry to SD
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
480 if (!sync()) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
481
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
482 // cache entry - should already be in cache due to sync() call
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
483 p = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
484 if (!p) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
485
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
486 // change directory entry attribute
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
487 p->attributes = DIR_ATT_DIRECTORY;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
488
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
489 // make entry for '.'
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
490 memcpy(&d, p, sizeof(d));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
491 d.name[0] = '.';
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
492 for (uint8_t i = 1; i < 11; i++) d.name[i] = ' ';
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
493
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
494 // cache block for '.' and '..'
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
495 block = vol_->clusterStartBlock(firstCluster_);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
496 if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_WRITE)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
497
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
498 // copy '.' to block
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
499 memcpy(&vol_->cache()->dir[0], &d, sizeof(d));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
500
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
501 // make entry for '..'
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
502 d.name[1] = '.';
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
503 if (parent->isRoot()) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
504 d.firstClusterLow = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
505 d.firstClusterHigh = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
506 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
507 d.firstClusterLow = parent->firstCluster_ & 0XFFFF;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
508 d.firstClusterHigh = parent->firstCluster_ >> 16;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
509 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
510 // copy '..' to block
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
511 memcpy(&vol_->cache()->dir[1], &d, sizeof(d));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
512
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
513 // write first block
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
514 return vol_->cacheFlush();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
515
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
516 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
517 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
518 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
519 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
520 /** Open a file in the current working directory.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
521 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
522 * \param[in] path A path with a valid 8.3 DOS name for a file to be opened.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
523 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
524 * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
525 * OR of open flags. see SdBaseFile::open(SdBaseFile*, const char*, uint8_t).
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
526 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
527 * \return The value one, true, is returned for success and
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
528 * the value zero, false, is returned for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
529 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
530 bool SdBaseFile::open(const char* path, uint8_t oflag) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
531 return open(cwd_, path, oflag);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
532 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
533 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
534 /** Open a file or directory by name.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
535 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
536 * \param[in] dirFile An open SdFat instance for the directory containing the
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
537 * file to be opened.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
538 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
539 * \param[in] path A path with a valid 8.3 DOS name for a file to be opened.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
540 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
541 * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
542 * OR of flags from the following list
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
543 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
544 * O_READ - Open for reading.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
545 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
546 * O_RDONLY - Same as O_READ.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
547 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
548 * O_WRITE - Open for writing.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
549 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
550 * O_WRONLY - Same as O_WRITE.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
551 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
552 * O_RDWR - Open for reading and writing.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
553 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
554 * O_APPEND - If set, the file offset shall be set to the end of the
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
555 * file prior to each write.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
556 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
557 * O_AT_END - Set the initial position at the end of the file.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
558 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
559 * O_CREAT - If the file exists, this flag has no effect except as noted
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
560 * under O_EXCL below. Otherwise, the file shall be created
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
561 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
562 * O_EXCL - If O_CREAT and O_EXCL are set, open() shall fail if the file exists.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
563 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
564 * O_SYNC - Call sync() after each write. This flag should not be used with
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
565 * write(uint8_t), write_P(PGM_P), writeln_P(PGM_P), or the Arduino Print class.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
566 * These functions do character at a time writes so sync() will be called
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
567 * after each byte.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
568 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
569 * O_TRUNC - If the file exists and is a regular file, and the file is
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
570 * successfully opened and is not read only, its length shall be truncated to 0.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
571 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
572 * WARNING: A given file must not be opened by more than one SdBaseFile object
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
573 * of file corruption may occur.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
574 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
575 * \note Directory files must be opened read only. Write and truncation is
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
576 * not allowed for directory files.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
577 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
578 * \return The value one, true, is returned for success and
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
579 * the value zero, false, is returned for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
580 * Reasons for failure include this file is already open, \a dirFile is not
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
581 * a directory, \a path is invalid, the file does not exist
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
582 * or can't be opened in the access mode specified by oflag.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
583 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
584 bool SdBaseFile::open(SdBaseFile* dirFile, const char* path, uint8_t oflag) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
585 uint8_t dname[11];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
586 SdBaseFile dir1, dir2;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
587 SdBaseFile *parent = dirFile;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
588 SdBaseFile *sub = &dir1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
589
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
590 if (!dirFile) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
591
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
592 // error if already open
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
593 if (isOpen()) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
594
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
595 if (*path == '/') {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
596 while (*path == '/') path++;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
597 if (!dirFile->isRoot()) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
598 if (!dir2.openRoot(dirFile->vol_)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
599 parent = &dir2;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
600 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
601 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
602 while (1) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
603 if (!make83Name(path, dname, &path)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
604 while (*path == '/') path++;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
605 if (!*path) break;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
606 if (!sub->open(parent, dname, O_READ)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
607 if (parent != dirFile) parent->close();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
608 parent = sub;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
609 sub = parent != &dir1 ? &dir1 : &dir2;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
610 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
611 return open(parent, dname, oflag);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
612
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
613 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
614 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
615 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
616 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
617 // open with filename in dname
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
618 bool SdBaseFile::open(SdBaseFile* dirFile,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
619 const uint8_t dname[11], uint8_t oflag) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
620 bool emptyFound = false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
621 bool fileFound = false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
622 uint8_t index;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
623 dir_t* p;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
624
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
625 vol_ = dirFile->vol_;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
626
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
627 dirFile->rewind();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
628 // search for file
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
629
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
630 while (dirFile->curPosition_ < dirFile->fileSize_) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
631 index = 0XF & (dirFile->curPosition_ >> 5);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
632 p = dirFile->readDirCache();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
633 if (!p) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
634
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
635 if (p->name[0] == DIR_NAME_FREE || p->name[0] == DIR_NAME_DELETED) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
636 // remember first empty slot
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
637 if (!emptyFound) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
638 dirBlock_ = dirFile->vol_->cacheBlockNumber();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
639 dirIndex_ = index;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
640 emptyFound = true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
641 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
642 // done if no entries follow
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
643 if (p->name[0] == DIR_NAME_FREE) break;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
644 } else if (!memcmp(dname, p->name, 11)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
645 fileFound = true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
646 break;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
647 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
648 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
649 if (fileFound) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
650 // don't open existing file if O_EXCL
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
651 if (oflag & O_EXCL) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
652 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
653 // don't create unless O_CREAT and O_WRITE
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
654 if (!(oflag & O_CREAT) || !(oflag & O_WRITE)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
655 if (emptyFound) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
656 index = dirIndex_;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
657 p = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
658 if (!p) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
659 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
660 if (dirFile->type_ == FAT_FILE_TYPE_ROOT_FIXED) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
661
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
662 // add and zero cluster for dirFile - first cluster is in cache for write
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
663 if (!dirFile->addDirCluster()) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
664
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
665 // use first entry in cluster
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
666 p = dirFile->vol_->cache()->dir;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
667 index = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
668 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
669 // initialize as empty file
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
670 memset(p, 0, sizeof(dir_t));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
671 memcpy(p->name, dname, 11);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
672
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
673 // set timestamps
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
674 if (dateTime_) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
675 // call user date/time function
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
676 dateTime_(&p->creationDate, &p->creationTime);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
677 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
678 // use default date/time
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
679 p->creationDate = FAT_DEFAULT_DATE;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
680 p->creationTime = FAT_DEFAULT_TIME;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
681 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
682 p->lastAccessDate = p->creationDate;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
683 p->lastWriteDate = p->creationDate;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
684 p->lastWriteTime = p->creationTime;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
685
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
686 // write entry to SD
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
687 if (!dirFile->vol_->cacheFlush()) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
688 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
689 // open entry in cache
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
690 return openCachedEntry(index, oflag);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
691
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
692 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
693 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
694 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
695 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
696 /** Open a file by index.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
697 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
698 * \param[in] dirFile An open SdFat instance for the directory.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
699 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
700 * \param[in] index The \a index of the directory entry for the file to be
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
701 * opened. The value for \a index is (directory file position)/32.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
702 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
703 * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
704 * OR of flags O_READ, O_WRITE, O_TRUNC, and O_SYNC.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
705 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
706 * See open() by path for definition of flags.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
707 * \return true for success or false for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
708 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
709 bool SdBaseFile::open(SdBaseFile* dirFile, uint16_t index, uint8_t oflag) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
710 dir_t* p;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
711
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
712 vol_ = dirFile->vol_;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
713
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
714 // error if already open
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
715 if (isOpen() || !dirFile) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
716
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
717 // don't open existing file if O_EXCL - user call error
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
718 if (oflag & O_EXCL) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
719
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
720 // seek to location of entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
721 if (!dirFile->seekSet(32 * index)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
722
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
723 // read entry into cache
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
724 p = dirFile->readDirCache();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
725 if (!p) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
726
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
727 // error if empty slot or '.' or '..'
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
728 if (p->name[0] == DIR_NAME_FREE ||
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
729 p->name[0] == DIR_NAME_DELETED || p->name[0] == '.') {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
730 goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
731 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
732 // open cached entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
733 return openCachedEntry(index & 0XF, oflag);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
734
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
735 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
736 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
737 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
738 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
739 // open a cached directory entry. Assumes vol_ is initialized
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
740 bool SdBaseFile::openCachedEntry(uint8_t dirIndex, uint8_t oflag) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
741 // location of entry in cache
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
742 dir_t* p = &vol_->cache()->dir[dirIndex];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
743
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
744 // write or truncate is an error for a directory or read-only file
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
745 if (p->attributes & (DIR_ATT_READ_ONLY | DIR_ATT_DIRECTORY)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
746 if (oflag & (O_WRITE | O_TRUNC)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
747 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
748 // remember location of directory entry on SD
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
749 dirBlock_ = vol_->cacheBlockNumber();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
750 dirIndex_ = dirIndex;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
751
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
752 // copy first cluster number for directory fields
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
753 firstCluster_ = (uint32_t)p->firstClusterHigh << 16;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
754 firstCluster_ |= p->firstClusterLow;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
755
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
756 // make sure it is a normal file or subdirectory
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
757 if (DIR_IS_FILE(p)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
758 fileSize_ = p->fileSize;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
759 type_ = FAT_FILE_TYPE_NORMAL;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
760 } else if (DIR_IS_SUBDIR(p)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
761 if (!vol_->chainSize(firstCluster_, &fileSize_)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
762 type_ = FAT_FILE_TYPE_SUBDIR;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
763 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
764 goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
765 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
766 // save open flags for read/write
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
767 flags_ = oflag & F_OFLAG;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
768
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
769 // set to start of file
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
770 curCluster_ = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
771 curPosition_ = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
772 if ((oflag & O_TRUNC) && !truncate(0)) return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
773 return oflag & O_AT_END ? seekEnd(0) : true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
774
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
775 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
776 type_ = FAT_FILE_TYPE_CLOSED;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
777 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
778 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
779 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
780 /** Open the next file or subdirectory in a directory.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
781 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
782 * \param[in] dirFile An open SdFat instance for the directory containing the
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
783 * file to be opened.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
784 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
785 * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
786 * OR of flags O_READ, O_WRITE, O_TRUNC, and O_SYNC.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
787 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
788 * See open() by path for definition of flags.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
789 * \return true for success or false for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
790 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
791 bool SdBaseFile::openNext(SdBaseFile* dirFile, uint8_t oflag) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
792 dir_t* p;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
793 uint8_t index;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
794
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
795 if (!dirFile) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
796
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
797 // error if already open
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
798 if (isOpen()) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
799
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
800 vol_ = dirFile->vol_;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
801
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
802 while (1) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
803 index = 0XF & (dirFile->curPosition_ >> 5);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
804
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
805 // read entry into cache
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
806 p = dirFile->readDirCache();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
807 if (!p) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
808
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
809 // done if last entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
810 if (p->name[0] == DIR_NAME_FREE) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
811
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
812 // skip empty slot or '.' or '..'
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
813 if (p->name[0] == DIR_NAME_DELETED || p->name[0] == '.') {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
814 continue;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
815 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
816 // must be file or dir
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
817 if (DIR_IS_FILE_OR_SUBDIR(p)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
818 return openCachedEntry(index, oflag);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
819 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
820 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
821
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
822 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
823 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
824 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
825 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
826 /** Open a directory's parent directory.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
827 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
828 * \param[in] dir Parent of this directory will be opened. Must not be root.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
829 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
830 * \return The value one, true, is returned for success and
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
831 * the value zero, false, is returned for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
832 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
833 bool SdBaseFile::openParent(SdBaseFile* dir) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
834 dir_t entry;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
835 dir_t* p;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
836 SdBaseFile file;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
837 uint32_t c;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
838 uint32_t cluster;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
839 uint32_t lbn;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
840 // error if already open or dir is root or dir is not a directory
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
841 if (isOpen() || !dir || dir->isRoot() || !dir->isDir()) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
842 vol_ = dir->vol_;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
843 // position to '..'
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
844 if (!dir->seekSet(32)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
845 // read '..' entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
846 if (dir->read(&entry, sizeof(entry)) != 32) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
847 // verify it is '..'
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
848 if (entry.name[0] != '.' || entry.name[1] != '.') goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
849 // start cluster for '..'
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
850 cluster = entry.firstClusterLow;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
851 cluster |= (uint32_t)entry.firstClusterHigh << 16;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
852 if (cluster == 0) return openRoot(vol_);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
853 // start block for '..'
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
854 lbn = vol_->clusterStartBlock(cluster);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
855 // first block of parent dir
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
856 if (!vol_->cacheRawBlock(lbn, SdVolume::CACHE_FOR_READ)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
857 goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
858 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
859 p = &vol_->cacheBuffer_.dir[1];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
860 // verify name for '../..'
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
861 if (p->name[0] != '.' || p->name[1] != '.') goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
862 // '..' is pointer to first cluster of parent. open '../..' to find parent
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
863 if (p->firstClusterHigh == 0 && p->firstClusterLow == 0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
864 if (!file.openRoot(dir->volume())) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
865 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
866 if (!file.openCachedEntry(1, O_READ)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
867 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
868 // search for parent in '../..'
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
869 do {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
870 if (file.readDir(&entry) != 32) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
871 c = entry.firstClusterLow;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
872 c |= (uint32_t)entry.firstClusterHigh << 16;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
873 } while (c != cluster);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
874 // open parent
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
875 return open(&file, file.curPosition()/32 - 1, O_READ);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
876
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
877 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
878 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
879 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
880 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
881 /** Open a volume's root directory.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
882 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
883 * \param[in] vol The FAT volume containing the root directory to be opened.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
884 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
885 * \return The value one, true, is returned for success and
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
886 * the value zero, false, is returned for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
887 * Reasons for failure include the file is already open, the FAT volume has
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
888 * not been initialized or it a FAT12 volume.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
889 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
890 bool SdBaseFile::openRoot(SdVolume* vol) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
891 // error if file is already open
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
892 if (isOpen()) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
893
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
894 if (vol->fatType() == 16 || (FAT12_SUPPORT && vol->fatType() == 12)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
895 type_ = FAT_FILE_TYPE_ROOT_FIXED;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
896 firstCluster_ = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
897 fileSize_ = 32 * vol->rootDirEntryCount();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
898 } else if (vol->fatType() == 32) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
899 type_ = FAT_FILE_TYPE_ROOT32;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
900 firstCluster_ = vol->rootDirStart();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
901 if (!vol->chainSize(firstCluster_, &fileSize_)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
902 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
903 // volume is not initialized, invalid, or FAT12 without support
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
904 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
905 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
906 vol_ = vol;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
907 // read only
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
908 flags_ = O_READ;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
909
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
910 // set to start of file
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
911 curCluster_ = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
912 curPosition_ = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
913
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
914 // root has no directory entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
915 dirBlock_ = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
916 dirIndex_ = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
917 return true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
918
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
919 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
920 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
921 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
922 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
923 /** Return the next available byte without consuming it.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
924 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
925 * \return The byte if no error and not at eof else -1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
926 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
927 int SdBaseFile::peek() {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
928 fpos_t pos;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
929 getpos(&pos);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
930 int c = read();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
931 if (c >= 0) setpos(&pos);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
932 return c;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
933 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
934
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
935 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
936 /** %Print the name field of a directory entry in 8.3 format.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
937 * \param[in] pr Print stream for output.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
938 * \param[in] dir The directory structure containing the name.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
939 * \param[in] width Blank fill name if length is less than \a width.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
940 * \param[in] printSlash Print '/' after directory names if true.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
941 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
942 void SdBaseFile::printDirName(const dir_t& dir,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
943 uint8_t width, bool printSlash) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
944 uint8_t w = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
945 for (uint8_t i = 0; i < 11; i++) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
946 if (dir.name[i] == ' ')continue;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
947 if (i == 8) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
948 MYSERIAL.write('.');
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
949 w++;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
950 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
951 MYSERIAL.write(dir.name[i]);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
952 w++;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
953 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
954 if (DIR_IS_SUBDIR(&dir) && printSlash) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
955 MYSERIAL.write('/');
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
956 w++;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
957 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
958 while (w < width) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
959 MYSERIAL.write(' ');
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
960 w++;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
961 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
962 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
963 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
964 // print uint8_t with width 2
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
965 static void print2u( uint8_t v) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
966 if (v < 10) MYSERIAL.write('0');
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
967 MYSERIAL.print(v, DEC);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
968 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
969 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
970 /** %Print a directory date field to Serial.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
971 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
972 * Format is yyyy-mm-dd.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
973 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
974 * \param[in] fatDate The date field from a directory entry.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
975 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
976
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
977 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
978 /** %Print a directory date field.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
979 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
980 * Format is yyyy-mm-dd.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
981 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
982 * \param[in] pr Print stream for output.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
983 * \param[in] fatDate The date field from a directory entry.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
984 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
985 void SdBaseFile::printFatDate(uint16_t fatDate) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
986 MYSERIAL.print(FAT_YEAR(fatDate));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
987 MYSERIAL.write('-');
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
988 print2u( FAT_MONTH(fatDate));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
989 MYSERIAL.write('-');
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
990 print2u( FAT_DAY(fatDate));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
991 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
992
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
993 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
994 /** %Print a directory time field.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
995 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
996 * Format is hh:mm:ss.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
997 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
998 * \param[in] pr Print stream for output.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
999 * \param[in] fatTime The time field from a directory entry.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1000 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1001 void SdBaseFile::printFatTime( uint16_t fatTime) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1002 print2u( FAT_HOUR(fatTime));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1003 MYSERIAL.write(':');
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1004 print2u( FAT_MINUTE(fatTime));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1005 MYSERIAL.write(':');
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1006 print2u( FAT_SECOND(fatTime));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1007 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1008 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1009 /** Print a file's name to Serial
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1010 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1011 * \return The value one, true, is returned for success and
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1012 * the value zero, false, is returned for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1013 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1014 bool SdBaseFile::printName() {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1015 char name[13];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1016 if (!getFilename(name)) return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1017 MYSERIAL.print(name);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1018 return true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1019 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1020 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1021 /** Read the next byte from a file.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1022 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1023 * \return For success read returns the next byte in the file as an int.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1024 * If an error occurs or end of file is reached -1 is returned.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1025 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1026 int16_t SdBaseFile::read() {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1027 uint8_t b;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1028 return read(&b, 1) == 1 ? b : -1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1029 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1030 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1031 /** Read data from a file starting at the current position.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1032 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1033 * \param[out] buf Pointer to the location that will receive the data.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1034 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1035 * \param[in] nbyte Maximum number of bytes to read.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1036 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1037 * \return For success read() returns the number of bytes read.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1038 * A value less than \a nbyte, including zero, will be returned
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1039 * if end of file is reached.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1040 * If an error occurs, read() returns -1. Possible errors include
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1041 * read() called before a file has been opened, corrupt file system
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1042 * or an I/O error occurred.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1043 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1044 int16_t SdBaseFile::read(void* buf, uint16_t nbyte) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1045 uint8_t* dst = reinterpret_cast<uint8_t*>(buf);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1046 uint16_t offset;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1047 uint16_t toRead;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1048 uint32_t block; // raw device block number
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1049
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1050 // error if not open or write only
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1051 if (!isOpen() || !(flags_ & O_READ)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1052
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1053 // max bytes left in file
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1054 if (nbyte >= (fileSize_ - curPosition_)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1055 nbyte = fileSize_ - curPosition_;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1056 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1057 // amount left to read
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1058 toRead = nbyte;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1059 while (toRead > 0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1060 offset = curPosition_ & 0X1FF; // offset in block
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1061 if (type_ == FAT_FILE_TYPE_ROOT_FIXED) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1062 block = vol_->rootDirStart() + (curPosition_ >> 9);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1063 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1064 uint8_t blockOfCluster = vol_->blockOfCluster(curPosition_);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1065 if (offset == 0 && blockOfCluster == 0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1066 // start of new cluster
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1067 if (curPosition_ == 0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1068 // use first cluster in file
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1069 curCluster_ = firstCluster_;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1070 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1071 // get next cluster from FAT
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1072 if (!vol_->fatGet(curCluster_, &curCluster_)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1073 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1074 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1075 block = vol_->clusterStartBlock(curCluster_) + blockOfCluster;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1076 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1077 uint16_t n = toRead;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1078
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1079 // amount to be read from current block
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1080 if (n > (512 - offset)) n = 512 - offset;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1081
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1082 // no buffering needed if n == 512
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1083 if (n == 512 && block != vol_->cacheBlockNumber()) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1084 if (!vol_->readBlock(block, dst)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1085 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1086 // read block to cache and copy data to caller
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1087 if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_READ)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1088 uint8_t* src = vol_->cache()->data + offset;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1089 memcpy(dst, src, n);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1090 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1091 dst += n;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1092 curPosition_ += n;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1093 toRead -= n;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1094 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1095 return nbyte;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1096
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1097 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1098 return -1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1099 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1100 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1101 /** Read the next directory entry from a directory file.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1102 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1103 * \param[out] dir The dir_t struct that will receive the data.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1104 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1105 * \return For success readDir() returns the number of bytes read.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1106 * A value of zero will be returned if end of file is reached.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1107 * If an error occurs, readDir() returns -1. Possible errors include
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1108 * readDir() called before a directory has been opened, this is not
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1109 * a directory file or an I/O error occurred.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1110 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1111 int8_t SdBaseFile::readDir(dir_t* dir) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1112 int16_t n;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1113 // if not a directory file or miss-positioned return an error
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1114 if (!isDir() || (0X1F & curPosition_)) return -1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1115
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1116 while (1) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1117 n = read(dir, sizeof(dir_t));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1118 if (n != sizeof(dir_t)) return n == 0 ? 0 : -1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1119 // last entry if DIR_NAME_FREE
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1120 if (dir->name[0] == DIR_NAME_FREE) return 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1121 // skip empty entries and entry for . and ..
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1122 if (dir->name[0] == DIR_NAME_DELETED || dir->name[0] == '.') continue;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1123 // return if normal file or subdirectory
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1124 if (DIR_IS_FILE_OR_SUBDIR(dir)) return n;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1125 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1126 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1127 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1128 // Read next directory entry into the cache
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1129 // Assumes file is correctly positioned
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1130 dir_t* SdBaseFile::readDirCache() {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1131 uint8_t i;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1132 // error if not directory
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1133 if (!isDir()) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1134
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1135 // index of entry in cache
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1136 i = (curPosition_ >> 5) & 0XF;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1137
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1138 // use read to locate and cache block
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1139 if (read() < 0) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1140
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1141 // advance to next entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1142 curPosition_ += 31;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1143
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1144 // return pointer to entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1145 return vol_->cache()->dir + i;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1146
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1147 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1148 return 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1149 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1150 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1151 /** Remove a file.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1152 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1153 * The directory entry and all data for the file are deleted.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1154 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1155 * \note This function should not be used to delete the 8.3 version of a
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1156 * file that has a long name. For example if a file has the long name
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1157 * "New Text Document.txt" you should not delete the 8.3 name "NEWTEX~1.TXT".
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1158 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1159 * \return The value one, true, is returned for success and
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1160 * the value zero, false, is returned for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1161 * Reasons for failure include the file read-only, is a directory,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1162 * or an I/O error occurred.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1163 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1164 bool SdBaseFile::remove() {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1165 dir_t* d;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1166 // free any clusters - will fail if read-only or directory
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1167 if (!truncate(0)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1168
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1169 // cache directory entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1170 d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1171 if (!d) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1172
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1173 // mark entry deleted
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1174 d->name[0] = DIR_NAME_DELETED;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1175
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1176 // set this file closed
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1177 type_ = FAT_FILE_TYPE_CLOSED;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1178
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1179 // write entry to SD
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1180 return vol_->cacheFlush();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1181 return true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1182
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1183 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1184 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1185 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1186 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1187 /** Remove a file.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1188 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1189 * The directory entry and all data for the file are deleted.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1190 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1191 * \param[in] dirFile The directory that contains the file.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1192 * \param[in] path Path for the file to be removed.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1193 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1194 * \note This function should not be used to delete the 8.3 version of a
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1195 * file that has a long name. For example if a file has the long name
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1196 * "New Text Document.txt" you should not delete the 8.3 name "NEWTEX~1.TXT".
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1197 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1198 * \return The value one, true, is returned for success and
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1199 * the value zero, false, is returned for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1200 * Reasons for failure include the file is a directory, is read only,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1201 * \a dirFile is not a directory, \a path is not found
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1202 * or an I/O error occurred.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1203 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1204 bool SdBaseFile::remove(SdBaseFile* dirFile, const char* path) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1205 SdBaseFile file;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1206 if (!file.open(dirFile, path, O_WRITE)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1207 return file.remove();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1208
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1209 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1210 // can't set iostate - static function
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1211 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1212 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1213 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1214 /** Rename a file or subdirectory.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1215 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1216 * \param[in] dirFile Directory for the new path.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1217 * \param[in] newPath New path name for the file/directory.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1218 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1219 * \return The value one, true, is returned for success and
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1220 * the value zero, false, is returned for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1221 * Reasons for failure include \a dirFile is not open or is not a directory
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1222 * file, newPath is invalid or already exists, or an I/O error occurs.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1223 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1224 bool SdBaseFile::rename(SdBaseFile* dirFile, const char* newPath) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1225 dir_t entry;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1226 uint32_t dirCluster = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1227 SdBaseFile file;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1228 dir_t* d;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1229
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1230 // must be an open file or subdirectory
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1231 if (!(isFile() || isSubDir())) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1232
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1233 // can't move file
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1234 if (vol_ != dirFile->vol_) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1235
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1236 // sync() and cache directory entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1237 sync();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1238 d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1239 if (!d) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1240
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1241 // save directory entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1242 memcpy(&entry, d, sizeof(entry));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1243
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1244 // mark entry deleted
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1245 d->name[0] = DIR_NAME_DELETED;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1246
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1247 // make directory entry for new path
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1248 if (isFile()) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1249 if (!file.open(dirFile, newPath, O_CREAT | O_EXCL | O_WRITE)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1250 goto restore;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1251 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1252 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1253 // don't create missing path prefix components
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1254 if (!file.mkdir(dirFile, newPath, false)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1255 goto restore;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1256 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1257 // save cluster containing new dot dot
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1258 dirCluster = file.firstCluster_;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1259 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1260 // change to new directory entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1261 dirBlock_ = file.dirBlock_;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1262 dirIndex_ = file.dirIndex_;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1263
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1264 // mark closed to avoid possible destructor close call
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1265 file.type_ = FAT_FILE_TYPE_CLOSED;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1266
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1267 // cache new directory entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1268 d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1269 if (!d) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1270
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1271 // copy all but name field to new directory entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1272 memcpy(&d->attributes, &entry.attributes, sizeof(entry) - sizeof(d->name));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1273
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1274 // update dot dot if directory
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1275 if (dirCluster) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1276 // get new dot dot
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1277 uint32_t block = vol_->clusterStartBlock(dirCluster);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1278 if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_READ)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1279 memcpy(&entry, &vol_->cache()->dir[1], sizeof(entry));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1280
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1281 // free unused cluster
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1282 if (!vol_->freeChain(dirCluster)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1283
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1284 // store new dot dot
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1285 block = vol_->clusterStartBlock(firstCluster_);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1286 if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_WRITE)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1287 memcpy(&vol_->cache()->dir[1], &entry, sizeof(entry));
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1288 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1289 return vol_->cacheFlush();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1290
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1291 restore:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1292 d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1293 if (!d) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1294 // restore entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1295 d->name[0] = entry.name[0];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1296 vol_->cacheFlush();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1297
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1298 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1299 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1300 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1301 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1302 /** Remove a directory file.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1303 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1304 * The directory file will be removed only if it is empty and is not the
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1305 * root directory. rmdir() follows DOS and Windows and ignores the
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1306 * read-only attribute for the directory.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1307 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1308 * \note This function should not be used to delete the 8.3 version of a
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1309 * directory that has a long name. For example if a directory has the
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1310 * long name "New folder" you should not delete the 8.3 name "NEWFOL~1".
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1311 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1312 * \return The value one, true, is returned for success and
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1313 * the value zero, false, is returned for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1314 * Reasons for failure include the file is not a directory, is the root
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1315 * directory, is not empty, or an I/O error occurred.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1316 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1317 bool SdBaseFile::rmdir() {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1318 // must be open subdirectory
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1319 if (!isSubDir()) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1320
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1321 rewind();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1322
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1323 // make sure directory is empty
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1324 while (curPosition_ < fileSize_) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1325 dir_t* p = readDirCache();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1326 if (!p) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1327 // done if past last used entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1328 if (p->name[0] == DIR_NAME_FREE) break;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1329 // skip empty slot, '.' or '..'
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1330 if (p->name[0] == DIR_NAME_DELETED || p->name[0] == '.') continue;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1331 // error not empty
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1332 if (DIR_IS_FILE_OR_SUBDIR(p)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1333 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1334 // convert empty directory to normal file for remove
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1335 type_ = FAT_FILE_TYPE_NORMAL;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1336 flags_ |= O_WRITE;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1337 return remove();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1338
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1339 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1340 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1341 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1342 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1343 /** Recursively delete a directory and all contained files.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1344 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1345 * This is like the Unix/Linux 'rm -rf *' if called with the root directory
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1346 * hence the name.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1347 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1348 * Warning - This will remove all contents of the directory including
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1349 * subdirectories. The directory will then be removed if it is not root.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1350 * The read-only attribute for files will be ignored.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1351 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1352 * \note This function should not be used to delete the 8.3 version of
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1353 * a directory that has a long name. See remove() and rmdir().
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1354 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1355 * \return The value one, true, is returned for success and
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1356 * the value zero, false, is returned for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1357 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1358 bool SdBaseFile::rmRfStar() {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1359 uint16_t index;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1360 SdBaseFile f;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1361 rewind();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1362 while (curPosition_ < fileSize_) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1363 // remember position
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1364 index = curPosition_/32;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1365
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1366 dir_t* p = readDirCache();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1367 if (!p) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1368
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1369 // done if past last entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1370 if (p->name[0] == DIR_NAME_FREE) break;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1371
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1372 // skip empty slot or '.' or '..'
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1373 if (p->name[0] == DIR_NAME_DELETED || p->name[0] == '.') continue;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1374
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1375 // skip if part of long file name or volume label in root
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1376 if (!DIR_IS_FILE_OR_SUBDIR(p)) continue;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1377
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1378 if (!f.open(this, index, O_READ)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1379 if (f.isSubDir()) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1380 // recursively delete
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1381 if (!f.rmRfStar()) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1382 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1383 // ignore read-only
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1384 f.flags_ |= O_WRITE;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1385 if (!f.remove()) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1386 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1387 // position to next entry if required
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1388 if (curPosition_ != (32*(index + 1))) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1389 if (!seekSet(32*(index + 1))) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1390 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1391 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1392 // don't try to delete root
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1393 if (!isRoot()) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1394 if (!rmdir()) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1395 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1396 return true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1397
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1398 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1399 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1400 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1401 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1402 /** Create a file object and open it in the current working directory.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1403 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1404 * \param[in] path A path with a valid 8.3 DOS name for a file to be opened.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1405 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1406 * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1407 * OR of open flags. see SdBaseFile::open(SdBaseFile*, const char*, uint8_t).
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1408 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1409 SdBaseFile::SdBaseFile(const char* path, uint8_t oflag) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1410 type_ = FAT_FILE_TYPE_CLOSED;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1411 writeError = false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1412 open(path, oflag);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1413 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1414 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1415 /** Sets a file's position.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1416 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1417 * \param[in] pos The new position in bytes from the beginning of the file.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1418 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1419 * \return The value one, true, is returned for success and
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1420 * the value zero, false, is returned for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1421 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1422 bool SdBaseFile::seekSet(uint32_t pos) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1423 uint32_t nCur;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1424 uint32_t nNew;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1425 // error if file not open or seek past end of file
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1426 if (!isOpen() || pos > fileSize_) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1427
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1428 if (type_ == FAT_FILE_TYPE_ROOT_FIXED) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1429 curPosition_ = pos;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1430 goto done;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1431 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1432 if (pos == 0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1433 // set position to start of file
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1434 curCluster_ = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1435 curPosition_ = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1436 goto done;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1437 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1438 // calculate cluster index for cur and new position
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1439 nCur = (curPosition_ - 1) >> (vol_->clusterSizeShift_ + 9);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1440 nNew = (pos - 1) >> (vol_->clusterSizeShift_ + 9);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1441
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1442 if (nNew < nCur || curPosition_ == 0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1443 // must follow chain from first cluster
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1444 curCluster_ = firstCluster_;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1445 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1446 // advance from curPosition
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1447 nNew -= nCur;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1448 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1449 while (nNew--) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1450 if (!vol_->fatGet(curCluster_, &curCluster_)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1451 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1452 curPosition_ = pos;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1453
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1454 done:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1455 return true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1456
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1457 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1458 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1459 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1460 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1461 void SdBaseFile::setpos(fpos_t* pos) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1462 curPosition_ = pos->position;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1463 curCluster_ = pos->cluster;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1464 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1465 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1466 /** The sync() call causes all modified data and directory fields
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1467 * to be written to the storage device.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1468 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1469 * \return The value one, true, is returned for success and
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1470 * the value zero, false, is returned for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1471 * Reasons for failure include a call to sync() before a file has been
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1472 * opened or an I/O error.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1473 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1474 bool SdBaseFile::sync() {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1475 // only allow open files and directories
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1476 if (!isOpen()) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1477
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1478 if (flags_ & F_FILE_DIR_DIRTY) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1479 dir_t* d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1480 // check for deleted by another open file object
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1481 if (!d || d->name[0] == DIR_NAME_DELETED) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1482
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1483 // do not set filesize for dir files
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1484 if (!isDir()) d->fileSize = fileSize_;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1485
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1486 // update first cluster fields
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1487 d->firstClusterLow = firstCluster_ & 0XFFFF;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1488 d->firstClusterHigh = firstCluster_ >> 16;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1489
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1490 // set modify time if user supplied a callback date/time function
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1491 if (dateTime_) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1492 dateTime_(&d->lastWriteDate, &d->lastWriteTime);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1493 d->lastAccessDate = d->lastWriteDate;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1494 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1495 // clear directory dirty
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1496 flags_ &= ~F_FILE_DIR_DIRTY;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1497 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1498 return vol_->cacheFlush();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1499
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1500 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1501 writeError = true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1502 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1503 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1504 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1505 /** Copy a file's timestamps
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1506 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1507 * \param[in] file File to copy timestamps from.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1508 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1509 * \note
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1510 * Modify and access timestamps may be overwritten if a date time callback
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1511 * function has been set by dateTimeCallback().
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1512 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1513 * \return The value one, true, is returned for success and
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1514 * the value zero, false, is returned for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1515 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1516 bool SdBaseFile::timestamp(SdBaseFile* file) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1517 dir_t* d;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1518 dir_t dir;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1519
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1520 // get timestamps
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1521 if (!file->dirEntry(&dir)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1522
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1523 // update directory fields
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1524 if (!sync()) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1525
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1526 d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1527 if (!d) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1528
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1529 // copy timestamps
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1530 d->lastAccessDate = dir.lastAccessDate;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1531 d->creationDate = dir.creationDate;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1532 d->creationTime = dir.creationTime;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1533 d->creationTimeTenths = dir.creationTimeTenths;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1534 d->lastWriteDate = dir.lastWriteDate;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1535 d->lastWriteTime = dir.lastWriteTime;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1536
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1537 // write back entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1538 return vol_->cacheFlush();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1539
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1540 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1541 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1542 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1543 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1544 /** Set a file's timestamps in its directory entry.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1545 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1546 * \param[in] flags Values for \a flags are constructed by a bitwise-inclusive
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1547 * OR of flags from the following list
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1548 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1549 * T_ACCESS - Set the file's last access date.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1550 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1551 * T_CREATE - Set the file's creation date and time.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1552 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1553 * T_WRITE - Set the file's last write/modification date and time.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1554 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1555 * \param[in] year Valid range 1980 - 2107 inclusive.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1556 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1557 * \param[in] month Valid range 1 - 12 inclusive.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1558 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1559 * \param[in] day Valid range 1 - 31 inclusive.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1560 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1561 * \param[in] hour Valid range 0 - 23 inclusive.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1562 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1563 * \param[in] minute Valid range 0 - 59 inclusive.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1564 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1565 * \param[in] second Valid range 0 - 59 inclusive
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1566 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1567 * \note It is possible to set an invalid date since there is no check for
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1568 * the number of days in a month.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1569 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1570 * \note
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1571 * Modify and access timestamps may be overwritten if a date time callback
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1572 * function has been set by dateTimeCallback().
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1573 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1574 * \return The value one, true, is returned for success and
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1575 * the value zero, false, is returned for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1576 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1577 bool SdBaseFile::timestamp(uint8_t flags, uint16_t year, uint8_t month,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1578 uint8_t day, uint8_t hour, uint8_t minute, uint8_t second) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1579 uint16_t dirDate;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1580 uint16_t dirTime;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1581 dir_t* d;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1582
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1583 if (!isOpen()
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1584 || year < 1980
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1585 || year > 2107
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1586 || month < 1
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1587 || month > 12
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1588 || day < 1
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1589 || day > 31
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1590 || hour > 23
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1591 || minute > 59
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1592 || second > 59) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1593 goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1594 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1595 // update directory entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1596 if (!sync()) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1597
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1598 d = cacheDirEntry(SdVolume::CACHE_FOR_WRITE);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1599 if (!d) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1600
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1601 dirDate = FAT_DATE(year, month, day);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1602 dirTime = FAT_TIME(hour, minute, second);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1603 if (flags & T_ACCESS) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1604 d->lastAccessDate = dirDate;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1605 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1606 if (flags & T_CREATE) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1607 d->creationDate = dirDate;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1608 d->creationTime = dirTime;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1609 // seems to be units of 1/100 second not 1/10 as Microsoft states
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1610 d->creationTimeTenths = second & 1 ? 100 : 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1611 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1612 if (flags & T_WRITE) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1613 d->lastWriteDate = dirDate;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1614 d->lastWriteTime = dirTime;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1615 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1616 return vol_->cacheFlush();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1617
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1618 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1619 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1620 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1621 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1622 /** Truncate a file to a specified length. The current file position
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1623 * will be maintained if it is less than or equal to \a length otherwise
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1624 * it will be set to end of file.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1625 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1626 * \param[in] length The desired length for the file.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1627 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1628 * \return The value one, true, is returned for success and
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1629 * the value zero, false, is returned for failure.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1630 * Reasons for failure include file is read only, file is a directory,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1631 * \a length is greater than the current file size or an I/O error occurs.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1632 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1633 bool SdBaseFile::truncate(uint32_t length) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1634 uint32_t newPos;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1635 // error if not a normal file or read-only
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1636 if (!isFile() || !(flags_ & O_WRITE)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1637
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1638 // error if length is greater than current size
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1639 if (length > fileSize_) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1640
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1641 // fileSize and length are zero - nothing to do
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1642 if (fileSize_ == 0) return true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1643
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1644 // remember position for seek after truncation
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1645 newPos = curPosition_ > length ? length : curPosition_;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1646
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1647 // position to last cluster in truncated file
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1648 if (!seekSet(length)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1649
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1650 if (length == 0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1651 // free all clusters
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1652 if (!vol_->freeChain(firstCluster_)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1653 firstCluster_ = 0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1654 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1655 uint32_t toFree;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1656 if (!vol_->fatGet(curCluster_, &toFree)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1657
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1658 if (!vol_->isEOC(toFree)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1659 // free extra clusters
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1660 if (!vol_->freeChain(toFree)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1661
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1662 // current cluster is end of chain
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1663 if (!vol_->fatPutEOC(curCluster_)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1664 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1665 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1666 fileSize_ = length;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1667
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1668 // need to update directory entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1669 flags_ |= F_FILE_DIR_DIRTY;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1670
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1671 if (!sync()) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1672
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1673 // set file to correct position
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1674 return seekSet(newPos);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1675
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1676 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1677 return false;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1678 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1679 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1680 /** Write data to an open file.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1681 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1682 * \note Data is moved to the cache but may not be written to the
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1683 * storage device until sync() is called.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1684 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1685 * \param[in] buf Pointer to the location of the data to be written.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1686 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1687 * \param[in] nbyte Number of bytes to write.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1688 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1689 * \return For success write() returns the number of bytes written, always
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1690 * \a nbyte. If an error occurs, write() returns -1. Possible errors
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1691 * include write() is called before a file has been opened, write is called
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1692 * for a read-only file, device is full, a corrupt file system or an I/O error.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1693 *
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1694 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1695 int16_t SdBaseFile::write(const void* buf, uint16_t nbyte) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1696 // convert void* to uint8_t* - must be before goto statements
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1697 const uint8_t* src = reinterpret_cast<const uint8_t*>(buf);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1698
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1699 // number of bytes left to write - must be before goto statements
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1700 uint16_t nToWrite = nbyte;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1701
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1702 // error if not a normal file or is read-only
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1703 if (!isFile() || !(flags_ & O_WRITE)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1704
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1705 // seek to end of file if append flag
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1706 if ((flags_ & O_APPEND) && curPosition_ != fileSize_) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1707 if (!seekEnd()) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1708 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1709
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1710 while (nToWrite > 0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1711 uint8_t blockOfCluster = vol_->blockOfCluster(curPosition_);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1712 uint16_t blockOffset = curPosition_ & 0X1FF;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1713 if (blockOfCluster == 0 && blockOffset == 0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1714 // start of new cluster
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1715 if (curCluster_ == 0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1716 if (firstCluster_ == 0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1717 // allocate first cluster of file
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1718 if (!addCluster()) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1719 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1720 curCluster_ = firstCluster_;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1721 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1722 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1723 uint32_t next;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1724 if (!vol_->fatGet(curCluster_, &next)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1725 if (vol_->isEOC(next)) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1726 // add cluster if at end of chain
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1727 if (!addCluster()) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1728 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1729 curCluster_ = next;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1730 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1731 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1732 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1733 // max space in block
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1734 uint16_t n = 512 - blockOffset;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1735
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1736 // lesser of space and amount to write
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1737 if (n > nToWrite) n = nToWrite;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1738
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1739 // block for data write
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1740 uint32_t block = vol_->clusterStartBlock(curCluster_) + blockOfCluster;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1741 if (n == 512) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1742 // full block - don't need to use cache
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1743 if (vol_->cacheBlockNumber() == block) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1744 // invalidate cache if block is in cache
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1745 vol_->cacheSetBlockNumber(0XFFFFFFFF, false);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1746 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1747 if (!vol_->writeBlock(block, src)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1748 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1749 if (blockOffset == 0 && curPosition_ >= fileSize_) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1750 // start of new block don't need to read into cache
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1751 if (!vol_->cacheFlush()) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1752 // set cache dirty and SD address of block
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1753 vol_->cacheSetBlockNumber(block, true);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1754 } else {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1755 // rewrite part of block
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1756 if (!vol_->cacheRawBlock(block, SdVolume::CACHE_FOR_WRITE)) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1757 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1758 uint8_t* dst = vol_->cache()->data + blockOffset;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1759 memcpy(dst, src, n);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1760 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1761 curPosition_ += n;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1762 src += n;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1763 nToWrite -= n;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1764 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1765 if (curPosition_ > fileSize_) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1766 // update fileSize and insure sync will update dir entry
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1767 fileSize_ = curPosition_;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1768 flags_ |= F_FILE_DIR_DIRTY;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1769 } else if (dateTime_ && nbyte) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1770 // insure sync will update modified date and time
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1771 flags_ |= F_FILE_DIR_DIRTY;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1772 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1773
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1774 if (flags_ & O_SYNC) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1775 if (!sync()) goto fail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1776 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1777 return nbyte;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1778
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1779 fail:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1780 // return for write error
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1781 writeError = true;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1782 return -1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1783 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1784 //------------------------------------------------------------------------------
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1785 // suppress cpplint warnings with NOLINT comment
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1786 #if ALLOW_DEPRECATED_FUNCTIONS && !defined(DOXYGEN)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1787 void (*SdBaseFile::oldDateTime_)(uint16_t& date, uint16_t& time) = 0; // NOLINT
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1788 #endif // ALLOW_DEPRECATED_FUNCTIONS
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1789
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1790
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1791 #endif

mercurial