~ubuntu-branches/debian/stretch/alpine/stretch

« back to all changes in this revision

Viewing changes to alpine/osdep/sunquota

  • Committer: Bazaar Package Importer
  • Author(s): Asheesh Laroia
  • Date: 2007-02-17 13:17:42 UTC
  • Revision ID: james.westby@ubuntu.com-20070217131742-99x5c6cpg1pbkdhw
Tags: upstream-0.82+dfsg
ImportĀ upstreamĀ versionĀ 0.82+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
static char *device_name();
 
2
 
 
3
/*----------------------------------------------------------------------
 
4
   Return space left in disk quota on file system which given path is in.
 
5
 
 
6
    Args: path - Path name of file or directory on file system of concern
 
7
          over - pointer to flag that is set if the user is over quota
 
8
 
 
9
 Returns: If *over = 0, the number of bytes free in disk quota as per
 
10
          the soft limit.
 
11
          If *over = 1, the number of bytes *over* quota.
 
12
          -1 is returned on an error looking up quota
 
13
           0 is returned if there is no quota
 
14
 
 
15
BUG:  If there's more than 2.1Gb free this function will break
 
16
  ----*/
 
17
long
 
18
disk_quota(path, over)
 
19
    char *path;
 
20
    int  *over;
 
21
{
 
22
    static int   no_quota = 0;
 
23
    struct stat  statx;
 
24
    struct dqblk quotax;
 
25
    long         q;
 
26
    char        *dname;
 
27
 
 
28
    if(no_quota)
 
29
      return(0L); /* If no quota the first time, then none the second. */
 
30
 
 
31
    dprint(5, (debugfile, "quota_check path: %s\n", path ? path : "?"));
 
32
    if(stat(path, &statx) < 0) {
 
33
        return(-1L);
 
34
    }
 
35
 
 
36
    *over = 0;
 
37
    errno = 0;
 
38
 
 
39
    dname = device_name(statx.st_dev);
 
40
    if(dname == NULL)
 
41
      return(-1L);
 
42
 
 
43
    dprint(7, (debugfile, "Quota check: UID:%d  device: %s\n", 
 
44
           getuid(), dname ? dname : "?"));
 
45
    if(quotactl(Q_GETQUOTA, dname, getuid(), (char *)&quotax) < 0) {
 
46
        dprint(5, (debugfile, "Quota failed : %s\n",
 
47
                   error_description(errno)));
 
48
        return(-1L); /* Something went wrong */
 
49
    }
 
50
 
 
51
    dprint(5,(debugfile,"Quota: bsoftlimit:%d  bhardlimit:%d  curblock:%d\n",
 
52
          quotax.dqb_bsoftlimit, quotax.dqb_bhardlimit, quotax.dqb_curblocks));
 
53
 
 
54
    if(quotax.dqb_bsoftlimit == -1)
 
55
      return(-1L);
 
56
 
 
57
    q = (quotax.dqb_bsoftlimit - quotax.dqb_curblocks) * 512;    
 
58
 
 
59
    if(q < 0) {
 
60
        q = -q;
 
61
        *over = 1;
 
62
    }
 
63
    dprint(5, (debugfile, "disk_quota returning :%d,  over:%d\n", q, *over));
 
64
    return(q);
 
65
}
 
66
 
 
67
 
 
68
/*----------------------------------------------------------------------
 
69
 *              devNumToName
 
70
 *
 
71
 *      This routine is here so that ex can get a device name to check
 
72
 *      disk quotas.  One might wonder, why not use getmntent(), rather
 
73
 *      than read /etc/mtab in this crude way?  The problem with getmntent
 
74
 *      is that it uses stdio, and ex/vi pointedly doesn't.
 
75
 ----*/
 
76
static  char
 
77
*device_name(st_devArg)
 
78
    dev_t st_devArg;
 
79
{
 
80
#ifndef MTABNAME
 
81
#define MTABNAME "/etc/mtab"
 
82
#endif
 
83
    char *mtab;
 
84
    static char devName[48];
 
85
    static char *answer = (char *) 0;
 
86
    struct stat devStat;
 
87
    static dev_t st_dev;
 
88
    int nb, cur, bol;
 
89
    char c;
 
90
    int dname;
 
91
 
 
92
    if (st_devArg == st_dev)
 
93
      return answer;
 
94
 
 
95
    mtab = read_file(MTABNAME);
 
96
    if(mtab == NULL)
 
97
      return((char *)NULL);
 
98
 
 
99
    /* Initialize save data. */
 
100
    st_dev = st_devArg;
 
101
    answer = (char *) 0;
 
102
    nb = strlen(mtab);
 
103
 
 
104
    for (cur=bol=0, dname=1; cur < nb; ++cur) {
 
105
 
 
106
        if (dname && (mtab[cur] <= ' ')) {
 
107
        /*      Space, tab or other such character has been found,
 
108
                presumably marking the end of the device name string. */
 
109
        
 
110
            dname = 0;
 
111
            c = mtab[cur];      /* Save current character. */
 
112
            mtab[cur] = 0;      /* C zero-terminated string. */
 
113
 
 
114
            /*  Get device number, via stat().  If it's the right
 
115
                number, copy the string and return its address. */
 
116
            if (stat (&mtab[bol], &devStat) == 0) {
 
117
                if (devStat.st_rdev == st_dev) {
 
118
                    if ((cur - bol + 1) < sizeof (devName)) {
 
119
                        strncpy (devName, &mtab[bol], sizeof(devName));
 
120
                        devName[sizeof(devName)-1] = '\0';
 
121
                        answer = &devName[0];
 
122
                        return(answer);
 
123
                    }
 
124
                }
 
125
            }
 
126
            mtab[cur] = c;
 
127
        }
 
128
        if (mtab[cur] == '\n') {
 
129
            dname = 1;
 
130
            bol = cur + 1;
 
131
        }
 
132
    }
 
133
    answer = NULL;
 
134
 
 
135
    return(answer);
 
136
}