8bae9fc298516738f3e896aa4c31edfa7350f893
[ccan] / ccan / tdb2 / tdb.c
1 #include "private.h"
2 #include <ccan/tdb2/tdb2.h>
3 #include <ccan/build_assert/build_assert.h>
4 #include <ccan/likely/likely.h>
5 #include <assert.h>
6
7 /* The null return. */
8 struct tdb_data tdb_null = { .dptr = NULL, .dsize = 0 };
9
10 /* all contexts, to ensure no double-opens (fcntl locks don't nest!) */
11 static struct tdb_context *tdbs = NULL;
12
13 PRINTF_FMT(4, 5) static void
14 null_log_fn(struct tdb_context *tdb,
15             enum tdb_debug_level level, void *priv,
16             const char *fmt, ...)
17 {
18 }
19
20 static bool tdb_already_open(dev_t device, ino_t ino)
21 {
22         struct tdb_context *i;
23         
24         for (i = tdbs; i; i = i->next) {
25                 if (i->device == device && i->inode == ino) {
26                         return true;
27                 }
28         }
29
30         return false;
31 }
32
33 static uint64_t random_number(struct tdb_context *tdb)
34 {
35         int fd;
36         uint64_t ret = 0;
37         struct timeval now;
38
39         fd = open("/dev/urandom", O_RDONLY);
40         if (fd >= 0) {
41                 if (tdb_read_all(fd, &ret, sizeof(ret))) {
42                         tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
43                                  "tdb_open: random from /dev/urandom\n");
44                         close(fd);
45                         return ret;
46                 }
47                 close(fd);
48         }
49         /* FIXME: Untested!  Based on Wikipedia protocol description! */
50         fd = open("/dev/egd-pool", O_RDWR);
51         if (fd >= 0) {
52                 /* Command is 1, next byte is size we want to read. */
53                 char cmd[2] = { 1, sizeof(uint64_t) };
54                 if (write(fd, cmd, sizeof(cmd)) == sizeof(cmd)) {
55                         char reply[1 + sizeof(uint64_t)];
56                         int r = read(fd, reply, sizeof(reply));
57                         if (r > 1) {
58                                 tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
59                                          "tdb_open: %u random bytes from"
60                                          " /dev/egd-pool\n", r-1);
61                                 /* Copy at least some bytes. */
62                                 memcpy(&ret, reply+1, r - 1);
63                                 if (reply[0] == sizeof(uint64_t)
64                                     && r == sizeof(reply)) {
65                                         close(fd);
66                                         return ret;
67                                 }
68                         }
69                 }
70                 close(fd);
71         }
72
73         /* Fallback: pid and time. */
74         gettimeofday(&now, NULL);
75         ret = getpid() * 100132289ULL + now.tv_sec * 1000000ULL + now.tv_usec;
76         tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
77                  "tdb_open: random from getpid and time\n");
78         return ret;
79 }
80
81 struct new_database {
82         struct tdb_header hdr;
83         struct tdb_freelist flist;
84 };
85
86 /* initialise a new database */
87 static int tdb_new_database(struct tdb_context *tdb,
88                             struct tdb_attribute_seed *seed,
89                             struct tdb_header *hdr)
90 {
91         /* We make it up in memory, then write it out if not internal */
92         struct new_database newdb;
93         unsigned int magic_len;
94
95         /* Fill in the header */
96         newdb.hdr.version = TDB_VERSION;
97         if (seed)
98                 newdb.hdr.hash_seed = seed->seed;
99         else
100                 newdb.hdr.hash_seed = random_number(tdb);
101         newdb.hdr.hash_test = TDB_HASH_MAGIC;
102         newdb.hdr.hash_test = tdb->khash(&newdb.hdr.hash_test,
103                                          sizeof(newdb.hdr.hash_test),
104                                          newdb.hdr.hash_seed,
105                                          tdb->hash_priv);
106         newdb.hdr.recovery = 0;
107         memset(newdb.hdr.reserved, 0, sizeof(newdb.hdr.reserved));
108         /* Initial hashes are empty. */
109         memset(newdb.hdr.hashtable, 0, sizeof(newdb.hdr.hashtable));
110
111         /* Free is empty. */
112         newdb.hdr.free_list = offsetof(struct new_database, flist);
113         memset(&newdb.flist, 0, sizeof(newdb.flist));
114         set_header(NULL, &newdb.flist.hdr, 0,
115                    sizeof(newdb.flist) - sizeof(newdb.flist.hdr),
116                    sizeof(newdb.flist) - sizeof(newdb.flist.hdr), 1);
117
118         /* Magic food */
119         memset(newdb.hdr.magic_food, 0, sizeof(newdb.hdr.magic_food));
120         strcpy(newdb.hdr.magic_food, TDB_MAGIC_FOOD);
121
122         /* This creates an endian-converted database, as if read from disk */
123         magic_len = sizeof(newdb.hdr.magic_food);
124         tdb_convert(tdb,
125                     (char *)&newdb.hdr + magic_len, sizeof(newdb) - magic_len);
126
127         *hdr = newdb.hdr;
128
129         if (tdb->flags & TDB_INTERNAL) {
130                 tdb->map_size = sizeof(newdb);
131                 tdb->map_ptr = malloc(tdb->map_size);
132                 if (!tdb->map_ptr) {
133                         tdb->ecode = TDB_ERR_OOM;
134                         return -1;
135                 }
136                 memcpy(tdb->map_ptr, &newdb, tdb->map_size);
137                 return 0;
138         }
139         if (lseek(tdb->fd, 0, SEEK_SET) == -1)
140                 return -1;
141
142         if (ftruncate(tdb->fd, 0) == -1)
143                 return -1;
144
145         if (!tdb_pwrite_all(tdb->fd, &newdb, sizeof(newdb), 0)) {
146                 tdb->ecode = TDB_ERR_IO;
147                 return -1;
148         }
149         return 0;
150 }
151
152 struct tdb_context *tdb_open(const char *name, int tdb_flags,
153                              int open_flags, mode_t mode,
154                              union tdb_attribute *attr)
155 {
156         struct tdb_context *tdb;
157         struct stat st;
158         int save_errno;
159         uint64_t hash_test;
160         unsigned v;
161         struct tdb_header hdr;
162         struct tdb_attribute_seed *seed = NULL;
163
164         tdb = malloc(sizeof(*tdb));
165         if (!tdb) {
166                 /* Can't log this */
167                 errno = ENOMEM;
168                 goto fail;
169         }
170         tdb->name = NULL;
171         tdb->map_ptr = NULL;
172         tdb->direct_access = 0;
173         tdb->fd = -1;
174         tdb->map_size = sizeof(struct tdb_header);
175         tdb->ecode = TDB_SUCCESS;
176         tdb->flags = tdb_flags;
177         tdb->log = null_log_fn;
178         tdb->log_priv = NULL;
179         tdb->transaction = NULL;
180         tdb_hash_init(tdb);
181         tdb_io_init(tdb);
182         tdb_lock_init(tdb);
183
184         while (attr) {
185                 switch (attr->base.attr) {
186                 case TDB_ATTRIBUTE_LOG:
187                         tdb->log = attr->log.log_fn;
188                         tdb->log_priv = attr->log.log_private;
189                         break;
190                 case TDB_ATTRIBUTE_HASH:
191                         tdb->khash = attr->hash.hash_fn;
192                         tdb->hash_priv = attr->hash.hash_private;
193                         break;
194                 case TDB_ATTRIBUTE_SEED:
195                         seed = &attr->seed;
196                         break;
197                 default:
198                         tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
199                                  "tdb_open: unknown attribute type %u\n",
200                                  attr->base.attr);
201                         errno = EINVAL;
202                         goto fail;
203                 }
204                 attr = attr->base.next;
205         }
206
207         if ((open_flags & O_ACCMODE) == O_WRONLY) {
208                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
209                          "tdb_open: can't open tdb %s write-only\n", name);
210                 errno = EINVAL;
211                 goto fail;
212         }
213
214         if ((open_flags & O_ACCMODE) == O_RDONLY) {
215                 tdb->read_only = true;
216                 /* read only databases don't do locking */
217                 tdb->flags |= TDB_NOLOCK;
218                 tdb->mmap_flags = PROT_READ;
219         } else {
220                 tdb->read_only = false;
221                 tdb->mmap_flags = PROT_READ | PROT_WRITE;
222         }
223
224         /* internal databases don't need any of the rest. */
225         if (tdb->flags & TDB_INTERNAL) {
226                 tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
227                 if (tdb_new_database(tdb, seed, &hdr) != 0) {
228                         tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
229                                  "tdb_open: tdb_new_database failed!");
230                         goto fail;
231                 }
232                 tdb_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed));
233                 tdb->hash_seed = hdr.hash_seed;
234                 tdb_flist_init(tdb);
235                 return tdb;
236         }
237
238         if ((tdb->fd = open(name, open_flags, mode)) == -1) {
239                 tdb->log(tdb, TDB_DEBUG_WARNING, tdb->log_priv,
240                          "tdb_open: could not open file %s: %s\n",
241                          name, strerror(errno));
242                 goto fail;      /* errno set by open(2) */
243         }
244
245         /* on exec, don't inherit the fd */
246         v = fcntl(tdb->fd, F_GETFD, 0);
247         fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC);
248
249         /* ensure there is only one process initialising at once */
250         if (tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK) == -1) {
251                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
252                          "tdb_open: failed to get open lock on %s: %s\n",
253                          name, strerror(errno));
254                 goto fail;      /* errno set by tdb_brlock */
255         }
256
257         if (!tdb_pread_all(tdb->fd, &hdr, sizeof(hdr), 0)
258             || strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
259                 if (!(open_flags & O_CREAT)
260                     || tdb_new_database(tdb, seed, &hdr) == -1) {
261                         if (errno == 0) {
262                                 errno = EIO; /* ie bad format or something */
263                         }
264                         goto fail;
265                 }
266         } else if (hdr.version != TDB_VERSION) {
267                 if (hdr.version == bswap_64(TDB_VERSION))
268                         tdb->flags |= TDB_CONVERT;
269                 else {
270                         /* wrong version */
271                         tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
272                                  "tdb_open: %s is unknown version 0x%llx\n",
273                                  name, (long long)hdr.version);
274                         errno = EIO;
275                         goto fail;
276                 }
277         }
278
279         tdb_convert(tdb, &hdr, sizeof(hdr));
280         tdb->hash_seed = hdr.hash_seed;
281         hash_test = TDB_HASH_MAGIC;
282         hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test));
283         if (hdr.hash_test != hash_test) {
284                 /* wrong hash variant */
285                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
286                          "tdb_open: %s uses a different hash function\n",
287                          name);
288                 errno = EIO;
289                 goto fail;
290         }
291
292         if (fstat(tdb->fd, &st) == -1)
293                 goto fail;
294
295         /* Is it already in the open list?  If so, fail. */
296         if (tdb_already_open(st.st_dev, st.st_ino)) {
297                 /* FIXME */
298                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
299                          "tdb_open: %s (%d,%d) is already open in this process\n",
300                          name, (int)st.st_dev, (int)st.st_ino);
301                 errno = EBUSY;
302                 goto fail;
303         }
304
305         tdb->name = strdup(name);
306         if (!tdb->name) {
307                 errno = ENOMEM;
308                 goto fail;
309         }
310
311         tdb->device = st.st_dev;
312         tdb->inode = st.st_ino;
313         tdb_unlock_open(tdb);
314
315         /* This make sure we have current map_size and mmap. */
316         tdb->methods->oob(tdb, tdb->map_size + 1, true);
317
318         /* Now it's fully formed, recover if necessary. */
319         if (tdb_needs_recovery(tdb) && tdb_lock_and_recover(tdb) == -1) {
320                 errno = EIO;
321                 goto fail;
322         }
323
324         if (tdb_flist_init(tdb) == -1)
325                 goto fail;
326
327         tdb->next = tdbs;
328         tdbs = tdb;
329         return tdb;
330
331  fail:
332         save_errno = errno;
333
334         if (!tdb)
335                 return NULL;
336
337 #ifdef TDB_TRACE
338         close(tdb->tracefd);
339 #endif
340         if (tdb->map_ptr) {
341                 if (tdb->flags & TDB_INTERNAL) {
342                         free(tdb->map_ptr);
343                 } else
344                         tdb_munmap(tdb);
345         }
346         free((char *)tdb->name);
347         if (tdb->fd != -1)
348                 if (close(tdb->fd) != 0)
349                         tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
350                                  "tdb_open: failed to close tdb->fd"
351                                  " on error!\n");
352         free(tdb);
353         errno = save_errno;
354         return NULL;
355 }
356
357 /* FIXME: modify, don't rewrite! */
358 static int update_rec_hdr(struct tdb_context *tdb,
359                           tdb_off_t off,
360                           tdb_len_t keylen,
361                           tdb_len_t datalen,
362                           struct tdb_used_record *rec,
363                           uint64_t h)
364 {
365         uint64_t dataroom = rec_data_length(rec) + rec_extra_padding(rec);
366
367         if (set_header(tdb, rec, keylen, datalen, keylen + dataroom, h))
368                 return -1;
369
370         return tdb_write_convert(tdb, off, rec, sizeof(*rec));
371 }
372
373 /* Returns -1 on error, 0 on OK */
374 static int replace_data(struct tdb_context *tdb,
375                         struct hash_info *h,
376                         struct tdb_data key, struct tdb_data dbuf,
377                         tdb_off_t old_off, tdb_len_t old_room,
378                         bool growing)
379 {
380         tdb_off_t new_off;
381
382         /* Allocate a new record. */
383         new_off = alloc(tdb, key.dsize, dbuf.dsize, h->h, growing);
384         if (unlikely(new_off == TDB_OFF_ERR))
385                 return -1;
386
387         /* We didn't like the existing one: remove it. */
388         if (old_off) {
389                 add_free_record(tdb, old_off,
390                                 sizeof(struct tdb_used_record)
391                                 + key.dsize + old_room);
392                 if (replace_in_hash(tdb, h, new_off) == -1)
393                         return -1;
394         } else {
395                 if (add_to_hash(tdb, h, new_off) == -1)
396                         return -1;
397         }
398
399         new_off += sizeof(struct tdb_used_record);
400         if (tdb->methods->write(tdb, new_off, key.dptr, key.dsize) == -1)
401                 return -1;
402
403         new_off += key.dsize;
404         if (tdb->methods->write(tdb, new_off, dbuf.dptr, dbuf.dsize) == -1)
405                 return -1;
406
407         /* FIXME: tdb_increment_seqnum(tdb); */
408         return 0;
409 }
410
411 int tdb_store(struct tdb_context *tdb,
412               struct tdb_data key, struct tdb_data dbuf, int flag)
413 {
414         struct hash_info h;
415         tdb_off_t off;
416         tdb_len_t old_room = 0;
417         struct tdb_used_record rec;
418         int ret;
419
420         off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
421         if (unlikely(off == TDB_OFF_ERR))
422                 return -1;
423
424         /* Now we have lock on this hash bucket. */
425         if (flag == TDB_INSERT) {
426                 if (off) {
427                         tdb->ecode = TDB_ERR_EXISTS;
428                         goto fail;
429                 }
430         } else {
431                 if (off) {
432                         old_room = rec_data_length(&rec)
433                                 + rec_extra_padding(&rec);
434                         if (old_room >= dbuf.dsize) {
435                                 /* Can modify in-place.  Easy! */
436                                 if (update_rec_hdr(tdb, off,
437                                                    key.dsize, dbuf.dsize,
438                                                    &rec, h.h))
439                                         goto fail;
440                                 if (tdb->methods->write(tdb, off + sizeof(rec)
441                                                         + key.dsize,
442                                                         dbuf.dptr, dbuf.dsize))
443                                         goto fail;
444                                 tdb_unlock_hashes(tdb, h.hlock_start,
445                                                   h.hlock_range, F_WRLCK);
446                                 return 0;
447                         }
448                         /* FIXME: See if right record is free? */
449                 } else {
450                         if (flag == TDB_MODIFY) {
451                                 /* if the record doesn't exist and we
452                                    are in TDB_MODIFY mode then we should fail
453                                    the store */
454                                 tdb->ecode = TDB_ERR_NOEXIST;
455                                 goto fail;
456                         }
457                 }
458         }
459
460         /* If we didn't use the old record, this implies we're growing. */
461         ret = replace_data(tdb, &h, key, dbuf, off, old_room, off != 0);
462         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
463         return ret;
464
465 fail:
466         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
467         return -1;
468 }
469
470 int tdb_append(struct tdb_context *tdb,
471                struct tdb_data key, struct tdb_data dbuf)
472 {
473         struct hash_info h;
474         tdb_off_t off;
475         struct tdb_used_record rec;
476         tdb_len_t old_room = 0, old_dlen;
477         unsigned char *newdata;
478         struct tdb_data new_dbuf;
479         int ret;
480
481         off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
482         if (unlikely(off == TDB_OFF_ERR))
483                 return -1;
484
485         if (off) {
486                 old_dlen = rec_data_length(&rec);
487                 old_room = old_dlen + rec_extra_padding(&rec);
488
489                 /* Fast path: can append in place. */
490                 if (rec_extra_padding(&rec) >= dbuf.dsize) {
491                         if (update_rec_hdr(tdb, off, key.dsize,
492                                            old_dlen + dbuf.dsize, &rec, h.h))
493                                 goto fail;
494
495                         off += sizeof(rec) + key.dsize + old_dlen;
496                         if (tdb->methods->write(tdb, off, dbuf.dptr,
497                                                 dbuf.dsize) == -1)
498                                 goto fail;
499
500                         /* FIXME: tdb_increment_seqnum(tdb); */
501                         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range,
502                                           F_WRLCK);
503                         return 0;
504                 }
505                 /* FIXME: Check right record free? */
506
507                 /* Slow path. */
508                 newdata = malloc(key.dsize + old_dlen + dbuf.dsize);
509                 if (!newdata) {
510                         tdb->ecode = TDB_ERR_OOM;
511                         tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
512                                  "tdb_append: cannot allocate %llu bytes!\n",
513                                  (long long)key.dsize + old_dlen + dbuf.dsize);
514                         goto fail;
515                 }
516                 if (tdb->methods->read(tdb, off + sizeof(rec) + key.dsize,
517                                        newdata, old_dlen) != 0) {
518                         free(newdata);
519                         goto fail;
520                 }
521                 memcpy(newdata + old_dlen, dbuf.dptr, dbuf.dsize);
522                 new_dbuf.dptr = newdata;
523                 new_dbuf.dsize = old_dlen + dbuf.dsize;
524         } else {
525                 newdata = NULL;
526                 new_dbuf = dbuf;
527         }
528
529         /* If they're using tdb_append(), it implies they're growing record. */
530         ret = replace_data(tdb, &h, key, new_dbuf, off, old_room, true);
531         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
532         free(newdata);
533
534         return ret;
535
536 fail:
537         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
538         return -1;
539 }
540
541 struct tdb_data tdb_fetch(struct tdb_context *tdb, struct tdb_data key)
542 {
543         tdb_off_t off;
544         struct tdb_used_record rec;
545         struct hash_info h;
546         struct tdb_data ret;
547
548         off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
549         if (unlikely(off == TDB_OFF_ERR))
550                 return tdb_null;
551
552         if (!off) {
553                 tdb->ecode = TDB_ERR_NOEXIST;
554                 ret = tdb_null;
555         } else {
556                 ret.dsize = rec_data_length(&rec);
557                 ret.dptr = tdb_alloc_read(tdb, off + sizeof(rec) + key.dsize,
558                                           ret.dsize);
559         }
560
561         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
562         return ret;
563 }
564
565 int tdb_delete(struct tdb_context *tdb, struct tdb_data key)
566 {
567         tdb_off_t off;
568         struct tdb_used_record rec;
569         struct hash_info h;
570
571         off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
572         if (unlikely(off == TDB_OFF_ERR))
573                 return -1;
574
575         if (!off) {
576                 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
577                 tdb->ecode = TDB_ERR_NOEXIST;
578                 return -1;
579         }
580
581         if (delete_from_hash(tdb, &h) == -1)
582                 goto unlock_err;
583
584         /* Free the deleted entry. */
585         if (add_free_record(tdb, off,
586                             sizeof(struct tdb_used_record)
587                             + rec_key_length(&rec)
588                             + rec_data_length(&rec)
589                             + rec_extra_padding(&rec)) != 0)
590                 goto unlock_err;
591
592         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
593         return 0;
594
595 unlock_err:
596         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
597         return -1;
598 }
599
600 int tdb_close(struct tdb_context *tdb)
601 {
602         struct tdb_context **i;
603         int ret = 0;
604
605         tdb_trace(tdb, "tdb_close");
606
607         if (tdb->transaction) {
608                 tdb_transaction_cancel(tdb);
609         }
610
611         if (tdb->map_ptr) {
612                 if (tdb->flags & TDB_INTERNAL)
613                         free(tdb->map_ptr);
614                 else
615                         tdb_munmap(tdb);
616         }
617         free((char *)tdb->name);
618         if (tdb->fd != -1) {
619                 ret = close(tdb->fd);
620                 tdb->fd = -1;
621         }
622         free(tdb->lockrecs);
623
624         /* Remove from contexts list */
625         for (i = &tdbs; *i; i = &(*i)->next) {
626                 if (*i == tdb) {
627                         *i = tdb->next;
628                         break;
629                 }
630         }
631
632 #ifdef TDB_TRACE
633         close(tdb->tracefd);
634 #endif
635         free(tdb);
636
637         return ret;
638 }
639
640 enum TDB_ERROR tdb_error(struct tdb_context *tdb)
641 {
642         return tdb->ecode;
643 }
644
645 const char *tdb_errorstr(struct tdb_context *tdb)
646 {
647         /* Gcc warns if you miss a case in the switch, so use that. */
648         switch (tdb->ecode) {
649         case TDB_SUCCESS: return "Success";
650         case TDB_ERR_CORRUPT: return "Corrupt database";
651         case TDB_ERR_IO: return "IO Error";
652         case TDB_ERR_LOCK: return "Locking error";
653         case TDB_ERR_OOM: return "Out of memory";
654         case TDB_ERR_EXISTS: return "Record exists";
655         case TDB_ERR_NESTING: return "Transaction already started";
656         case TDB_ERR_EINVAL: return "Invalid parameter";
657         case TDB_ERR_NOEXIST: return "Record does not exist";
658         case TDB_ERR_RDONLY: return "write not permitted";
659         }
660         return "Invalid error code";
661 }