Initial commit to Gerrit
[profile/ivi/quota.git] / quotacheck_v1.c
1 /*
2  *
3  *      Checking routines for old VFS quota format
4  *
5  */
6
7 #include "config.h"
8
9 #include <stdio.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <errno.h>
13
14 #include "pot.h"
15 #include "common.h"
16 #include "quotaio.h"
17 #include "quotaio_v1.h"
18 #include "quotacheck.h"
19
20 /* Load all other dquot structures */
21 static void load_dquots(char *filename, int fd, int type)
22 {
23         struct v1_disk_dqblk ddqblk;
24         struct util_dqblk *udq;
25         struct dquot *dquot;
26         int err;
27         qid_t id = 0;
28
29         lseek(fd, 0, SEEK_SET);
30         while ((err = read(fd, &ddqblk, sizeof(ddqblk)))) {
31                 if (err < 0)
32                         die(1, _("Cannot read entry for id %u from quotafile %s: %s\n"), (uint) id,
33                             filename, strerror(errno));
34                 if (err != sizeof(ddqblk)) {
35                         errstr(_("Entry for id %u is truncated.\n"),
36                                 (uint) id);
37                         break;
38                 }
39                 if (ddqblk.dqb_bhardlimit == 0
40                         && ddqblk.dqb_bsoftlimit == 0
41                         && ddqblk.dqb_ihardlimit == 0
42                         && ddqblk.dqb_isoftlimit == 0) {
43                         id++;
44                         continue;
45                 }
46                 dquot = add_dquot(id, type);
47                 udq = &dquot->dq_dqb;
48                 udq->dqb_bhardlimit = ddqblk.dqb_bhardlimit;
49                 udq->dqb_bsoftlimit = ddqblk.dqb_bsoftlimit;
50                 udq->dqb_ihardlimit = ddqblk.dqb_ihardlimit;
51                 udq->dqb_isoftlimit = ddqblk.dqb_isoftlimit;
52                 udq->dqb_btime = ddqblk.dqb_btime;
53                 udq->dqb_itime = ddqblk.dqb_itime;
54                 id++;
55         }
56 }
57
58 /* Load first structure - get grace times */
59 static int check_info(char *filename, int fd, int type)
60 {
61         struct v1_disk_dqblk ddqblk;
62         int err;
63
64         debug(FL_DEBUG, _("Loading first quota entry with grace times.\n"));
65         lseek(fd, 0, SEEK_SET);
66         err = read(fd, &ddqblk, sizeof(ddqblk));
67         if (err < 0)
68                 die(1, _("Cannot read first entry from quotafile %s: %s\n"), filename,
69                     strerror(errno));
70         if (err != sizeof(ddqblk)) {
71                 errstr(
72                         _("WARNING - Quotafile %s was probably truncated. Cannot save quota settings...\n"),
73                         filename);
74                 return -1;
75         }
76         old_info[type].dqi_bgrace = ddqblk.dqb_btime;
77         old_info[type].dqi_igrace = ddqblk.dqb_itime;
78         debug(FL_DEBUG, _("First entry loaded.\n"));
79         return 0;
80 }
81
82 int v1_buffer_file(char *filename, int fd, int type)
83 {
84         old_info[type].dqi_bgrace = MAX_DQ_TIME;
85         old_info[type].dqi_igrace = MAX_IQ_TIME;
86         if (flags & FL_NEWFILE)
87                 return 0;
88         if (check_info(filename, fd, type) < 0)
89                 return 0;
90         load_dquots(filename, fd, type);
91         return 0;
92 }