~ubuntu-branches/ubuntu/precise/netatalk/precise

« back to all changes in this revision

Viewing changes to etc/afpd/mangle.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Rittau
  • Date: 2004-01-19 12:43:49 UTC
  • Revision ID: james.westby@ubuntu.com-20040119124349-es563jbp0hk0ae51
Tags: upstream-1.6.4
ImportĀ upstreamĀ versionĀ 1.6.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 * $Id: mangle.c,v 1.11 2002/10/05 20:13:31 jmarcus Exp $ 
 
3
 *
 
4
 * Copyright (c) 2002. Joe Marcus Clarke (marcus@marcuscom.com)
 
5
 * All Rights Reserved.  See COPYRIGHT.
 
6
 *
 
7
 * mangle, demangle (filename):
 
8
 * mangle or demangle filenames if they are greater than the max allowed
 
9
 * characters for a given version of AFP.
 
10
 */
 
11
 
 
12
#ifdef HAVE_CONFIG_H
 
13
#include "config.h"
 
14
#endif /* HAVE_CONFIG_H */
 
15
 
 
16
#ifdef FILE_MANGLING
 
17
#include "mangle.h"
 
18
 
 
19
char *
 
20
demangle(const struct vol *vol, char *mfilename) {
 
21
        char *filename = NULL;
 
22
        char *ext = NULL;
 
23
        int ext_len = 0;
 
24
        char *mangle;
 
25
 
 
26
        /* Is this really a mangled file? */
 
27
        mangle = strstr(mfilename, MANGLE_CHAR);
 
28
        if (!mangle) {
 
29
            return mfilename;
 
30
        }
 
31
 
 
32
        if ((ext = strrchr(mfilename, '.')) != NULL) {
 
33
            ext_len = strlen(ext);
 
34
        }
 
35
        if (strlen(mangle) != strlen(MANGLE_CHAR) + MANGLE_LENGTH + ext_len) {
 
36
            LOG(log_error, logtype_afpd, "demangle: %s is not long enough to be a mangled filename", mfilename);
 
37
            return mfilename;
 
38
        }
 
39
 
 
40
        filename = cnid_mangle_get(vol->v_db, mfilename);
 
41
 
 
42
        /* No unmangled filename was found. */
 
43
        if (filename == NULL) {
 
44
            LOG(log_error, logtype_afpd, "demangle: Unable to lookup %s in the mangle database", mfilename);
 
45
            return mfilename;
 
46
        }
 
47
 
 
48
        return filename;
 
49
}
 
50
 
 
51
char *
 
52
mangle(const struct vol *vol, char *filename) {
 
53
    char *ext = NULL;
 
54
    char *tf = NULL;
 
55
    char *m = NULL;
 
56
    static char mfilename[MAX_LENGTH + 1];
 
57
    char mangle_suffix[MANGLE_LENGTH + 1];
 
58
    int ext_len = 0;
 
59
    int mangle_suffix_int = 0;
 
60
 
 
61
    /* Do we really need to mangle this filename? */
 
62
    if (strlen(filename) <= MAX_LENGTH) {
 
63
        return filename;
 
64
    }
 
65
 
 
66
    /* First, attmept to locate a file extension. */
 
67
    if ((ext = strrchr(filename, '.')) != NULL) {
 
68
        ext_len = strlen(ext);
 
69
        if (ext_len > MAX_EXT_LENGTH) {
 
70
            /* Do some bounds checking to prevent an extension overflow. */
 
71
            ext_len = MAX_EXT_LENGTH;
 
72
        }
 
73
    }
 
74
 
 
75
    /* Check to see if we already have a mangled filename by this name. */
 
76
    while (1) {
 
77
        m = mfilename;
 
78
        strncpy(m, filename, MAX_LENGTH - strlen(MANGLE_CHAR) - MANGLE_LENGTH - ext_len);
 
79
        m[MAX_LENGTH - strlen(MANGLE_CHAR) - MANGLE_LENGTH - ext_len] = '\0';
 
80
 
 
81
        strcat(m, MANGLE_CHAR);
 
82
        (void)sprintf(mangle_suffix, "%03d", mangle_suffix_int);
 
83
        strcat(m, mangle_suffix);
 
84
 
 
85
        if (ext) {
 
86
                strncat(m, ext, ext_len);
 
87
        }
 
88
 
 
89
        tf = cnid_mangle_get(vol->v_db, m);
 
90
        if (tf == NULL || (strcmp(tf, filename)) == 0) {
 
91
            break;
 
92
        }
 
93
        else {
 
94
            if (++mangle_suffix_int > MAX_MANGLE_SUFFIX_LENGTH) {
 
95
                LOG(log_error, logtype_afpd, "mangle: Failed to find a free mangle suffix; returning original filename");
 
96
                return filename;
 
97
            }
 
98
        }
 
99
    }
 
100
 
 
101
    if (cnid_mangle_add(vol->v_db, m, filename) < 0) {
 
102
        return filename;
 
103
    }
 
104
 
 
105
    return m;
 
106
}
 
107
#endif /* FILE_MANGLING */