* Convert a quota file from one format to another. */
| 514 | * Convert a quota file from one format to another. |
| 515 | */ |
| 516 | int |
| 517 | quota_convert(struct quotafile *qf, int wordsize) |
| 518 | { |
| 519 | struct quotafile *newqf; |
| 520 | struct dqhdr64 dqh; |
| 521 | struct dqblk dqblk; |
| 522 | struct group *grp; |
| 523 | int serrno, maxid, id, fd; |
| 524 | |
| 525 | /* |
| 526 | * Quotas must not be active and quotafile must be open |
| 527 | * for reading and writing. |
| 528 | */ |
| 529 | if ((qf->accmode & O_RDWR) != O_RDWR || qf->fd == -1) { |
| 530 | errno = EBADF; |
| 531 | return (-1); |
| 532 | } |
| 533 | if ((wordsize != 32 && wordsize != 64) || |
| 534 | wordsize == qf->wordsize) { |
| 535 | errno = EINVAL; |
| 536 | return (-1); |
| 537 | } |
| 538 | maxid = quota_maxid(qf); |
| 539 | if ((newqf = calloc(1, sizeof(*qf))) == NULL) { |
| 540 | errno = ENOMEM; |
| 541 | return (-1); |
| 542 | } |
| 543 | *newqf = *qf; |
| 544 | snprintf(newqf->qfname, MAXPATHLEN + 1, "%s_%d.orig", qf->qfname, |
| 545 | qf->wordsize); |
| 546 | if (rename(qf->qfname, newqf->qfname) < 0) { |
| 547 | free(newqf); |
| 548 | return (-1); |
| 549 | } |
| 550 | if ((newqf->fd = open(qf->qfname, O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, |
| 551 | 0)) < 0) { |
| 552 | serrno = errno; |
| 553 | goto error; |
| 554 | } |
| 555 | newqf->wordsize = wordsize; |
| 556 | if (wordsize == 64) { |
| 557 | memset(&dqh, 0, sizeof(dqh)); |
| 558 | memcpy(dqh.dqh_magic, Q_DQHDR64_MAGIC, sizeof(dqh.dqh_magic)); |
| 559 | dqh.dqh_version = htobe32(Q_DQHDR64_VERSION); |
| 560 | dqh.dqh_hdrlen = htobe32(sizeof(struct dqhdr64)); |
| 561 | dqh.dqh_reclen = htobe32(sizeof(struct dqblk64)); |
| 562 | if (write(newqf->fd, &dqh, sizeof(dqh)) != sizeof(dqh)) { |
| 563 | serrno = errno; |
| 564 | goto error; |
| 565 | } |
| 566 | } |
| 567 | grp = getgrnam(QUOTAGROUP); |
| 568 | fchown(newqf->fd, 0, grp ? grp->gr_gid : 0); |
| 569 | fchmod(newqf->fd, 0640); |
| 570 | for (id = 0; id <= maxid; id++) { |
| 571 | if ((quota_read(qf, &dqblk, id)) < 0) |
| 572 | break; |
| 573 | switch (newqf->wordsize) { |
nothing calls this directly
no test coverage detected