aboutsummaryrefslogtreecommitdiffstats
path: root/android_profile.c
blob: f8b430dfdcbfbdc6cd44b7341fe9ed87897c0dd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* Copyright (C) 2007-2008 The Android Open Source Project
**
** This software is licensed under the terms of the GNU General Public
** License version 2, as published by the Free Software Foundation, and
** may be copied, distributed, and modified under those terms.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
*/
#include "android_profile.h"
#include <string.h>
#include <unistd.h>

/* this environment variable can be set to change the root profiles directory */
#define   ENV_PROFILE_DIR   "ANDROID_PROFILE_DIR"

/* otherwise, we're going to use <config>/EMULATOR_PROFILE_DIR */
#ifdef _WIN32
#  define  EMULATOR_PROFILE_DIR  "EmulatorProfiles"
#else
#  define  EMULATOR_PROFILE_DIR "profiles"
#endif

static char*  profile_dir;

static char*
profile_dir_find( void )
{
    const char*  env  = getenv( ENV_PROFILE_DIR );
    if (env != NULL) {
        int  len = strlen(env);

        if ( access( env, R_OK ) < 0 ) {
            dprint( "%s variable does not point to a valid directory. ignored\n", ENV_PROFILE_DIR );
        }
        else {
            dprint( "using '%s' as profile root directory", env );
            if (len > 0 && env[len-1] == PATH_SEP[0]) {
                len -= 1;
            }
            profile_dir = malloc( len+1 );
            memcpy(profile_dir, env, len);
            profile_dir[len] = 0;
            return profile_dir;
        }
    }

    {
        char  temp[512];
        char*  p = temp;
        char*  end = p + sizeof(temp);
        p = bufprint_config_file( p, end, EMULATOR_PROFILE_DIR );
        if (p >= end) {
            fprintf( stderr, "emulator configuration directory path too long. aborting" );
            exit(1);
        }
        if (p > temp && p[-1] == PATH_SEP[0])
            p[-1] = 0;

        dprint( "using '%s' as profile root directory", temp );
        profile_dir = strdup( temp );
        return profile_dir;
    }
}

int
android_profile_check_name( const char*  profile_name )
{
    static const char*  goodchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-0123456789.";
    int                 len       = strlen(profile_name);
    int                 slen      = strspn(profile_name, goodchars);

    return (len == slen);
}

char*
android_profile_bufprint_path( char*  p, char*  end, const char*  profile_name )
{
    return android_profile_bufprint_file_path( p, end, profile_name, NULL );
}

char*
android_profile_strdup_path( const char*  profile_name )
{
    return android_profile_strdup_file_path( profile_name, NULL );
}

char*
android_profile_bufprint_file_path( char*  p, char*  end, const char*  profile_name, const char*  file_name )
{
    if (profile_dir == NULL)
        profile_dir = profile_dir_find();

    p = bufprint( p, end, "%s", profile_dir );
    if (file_name != NULL) {
        p = bufprint( p, end, PATH_SEP "%s", file_name );
    }
    return p;
}

char*
android_profile_strdup_file_path( const char*  profile_name, const char*  file_name )
{
    int    len1, len;
    char*  result;

    if (profile_dir == NULL)
        profile_dir = profile_dir_find();

    len1 = strlen(profile_dir);
    len  = len1;
    if (file_name) {
        len += 1 + strlen(file_name);
    }

    result = malloc( len+1 );
    memcpy( result, profile_dir, len1 );
    if (file_name) {
        result[len1] = PATH_SEP[0];
        strcpy( result+len1+1, file_name );
    }
    result[len] = 0;
    return  result;
}