File: files.c

package info (click to toggle)
libsynce 0.15-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,752 kB
  • ctags: 1,236
  • sloc: sh: 10,550; ansic: 4,476; makefile: 140
file content (177 lines) | stat: -rw-r--r-- 3,493 bytes parent folder | download | duplicates (4)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* $Id: files.c 2132 2005-07-31 16:45:01Z twogood $ */

/* play with defines to allow snprintf() */
#undef __STRICT_ANSI__
#define _GNU_SOURCE 1
#include "synce.h"
#include "synce_log.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

#define DIRECTORY_NAME                ".synce"
#define DEFAULT_CONNECTION_FILENAME   "active_connection"
#define SCRIPT_DIRECTORY              "scripts"

static char connection_filename[MAX_PATH] = {DEFAULT_CONNECTION_FILENAME};

static bool make_sure_directory_exists(char* directory)
{
	struct stat dir_stat;

	/*
	 * Make sure that this directory exists
	 */

	if (stat(directory, &dir_stat) < 0)
	{
		if (mkdir(directory, 0700) < 0)
		{
			synce_error("Failed to create directory %s", directory);
			return false;
		}
	}

	return true;
}

/**
 * Get path to config files
 */
bool synce_get_directory(char** path)
{
  char buffer[MAX_PATH];
  char *p;
  struct passwd* user = NULL;

  if (!path)
    return false;

  /* if there is a preference for config dir set
     as an environment variable, use it */
  if ((p = getenv ("SYNCE_CONF_DIR")) != NULL) {
    if (make_sure_directory_exists (p)) {
      *path = strdup (p);
      return true;
    }
  }

  /* XXX: not very thread-safe? */
  user = getpwuid(getuid());

  *path = NULL;

  if (!user)
    return false;

  snprintf(buffer, sizeof(buffer), "%s/" DIRECTORY_NAME, user->pw_dir);

  if (!make_sure_directory_exists(buffer))
    return false;

  *path = strdup(buffer);

  return true;
}

/**
  Set the file name used for active connection info
*/
bool synce_set_connection_filename(const char* filename)
{
  bool success = false;
  
  /* disallow file names containing '..' */
  if (filename && !strstr(filename, ".."))
  {
    /* Use snprintf to limit length and assure string is terminated with 0 */
    int n = snprintf(connection_filename, sizeof(connection_filename), "%s", filename);

    /* Return false if file name was too long or some other error occured */
    success = (n >= 0 && n < (int)sizeof(connection_filename));
  }

  if (!success)
    synce_warning("Invalid filename: '%s'", filename);

  return success;
}

/*
   Restore the default filename used for active connection info
 */
bool synce_set_default_connection_filename()
{
  return synce_set_connection_filename(DEFAULT_CONNECTION_FILENAME);
}

/**
 * Get file name for active connection info
 */
bool synce_get_connection_filename(char** filename)
{
	bool success = false;
	char* path = NULL;
	char buffer[MAX_PATH];

	if (!filename)
		goto exit;

	*filename = NULL;
	
	if (!synce_get_directory(&path))
		goto exit;

	snprintf(buffer, sizeof(buffer), "%s/%s" , path, connection_filename);
	*filename = strdup(buffer);

	success = true;

exit:
	if (path)
		free(path);
	return success;
}

bool synce_get_subdirectory(const char* name, char** directory)
{
	bool success = false;
	char* path = NULL;
	char buffer[MAX_PATH];

	if (!name || !directory)
		goto exit;

	if (strchr(name, '/'))	/* prevent bad names */
		goto exit;

	*directory = NULL;
	
	if (!synce_get_directory(&path))
		goto exit;

	snprintf(buffer, sizeof(buffer), "%s/%s", path, name);

	if (!make_sure_directory_exists(buffer))
		goto exit;
	
	*directory = strdup(buffer);

	success = true;

exit:
	if (path)
		free(path);
	return success;
}

bool synce_get_script_directory(char** directory)
{
	return synce_get_subdirectory(SCRIPT_DIRECTORY, directory);
}