Prova pratica 2017.01.17
Jump to navigation
Jump to search
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
struct fileelem {
dev_t dev;
ino_t ino;
struct fileelem *next;
};
int addfile(struct fileelem **head, dev_t dev, ino_t ino) {
if (*head == NULL) {
*head = malloc(sizeof(struct fileelem));
(*head)->dev = dev;
(*head)->ino = ino;
(*head)->next = NULL;
return 1;
} else if ((*head)->dev == dev && (*head)->ino == ino) {
return 0;
} else {
return addfile(&((*head)->next), dev, ino);
}
}
int filt(const struct dirent *elem) {
if (strcmp(elem->d_name,".") == 0 ||
strcmp(elem->d_name,"..") == 0)
return 0;
else {
return 1;
}
}
int recscan(char *path, struct fileelem **head) {
struct dirent **list;
int count = 0;
int n = scandir(path, &list, filt, alphasort);
int i;
for (i = 0; i < n; i++) {
struct stat buf;
char *filepath;
asprintf(&filepath,"%s/%s",path,list[i]->d_name);
stat(filepath, &buf);
switch (buf.st_mode & S_IFMT) {
case S_IFREG:
count += addfile(head, buf.st_dev, buf.st_ino);
printf("file %s %ld %ld ... %d\n",filepath,buf.st_dev,buf.st_ino,count);
break;
case S_IFDIR:
count = count + recscan(filepath, head);
break;
}
free(filepath);
free(list[i]);
}
free(list);
return count;
}
int main(int argc, char *argv[]) {
struct fileelem *head = NULL;
int result;
if (argc > 1)
chdir(argv[1]);
result = recscan(".", &head);
printf("Num of independent files: %d\n", result);
}
Esercizio 3: Python
def merge(): # dati due path di due directory, fa la merge in una terza directory
# 1=pathprimo 2=pathsecondo 3=pathdestinazione
if (len(sys.argv) > 3):
for d, _, fl in os.walk(sys.argv[1]):
for f in fl:
path = os.path.join(d, f)
if (os.path.isfile(path)): # e' un file e non una directory allora copio tutto
shutil.copy2(path, sys.argv[3])
for d, _, fl in os.walk(sys.argv[2]):
for f in fl:
path = os.path.join(d, f)
if (os.path.isfile(path)): # e' un file e non una directory in path2
esistenza = os.path.join(sys.argv[3], f)
if (not os.path.isfile(esistenza)): # se non esiste un file con quel nome
shutil.copy2(path, sys.argv[3])
else:
if (os.stat(path).st_mtime > os.stat(
esistenza).st_mtime): # e' piu recente tempo del file in path2 rispetto a path3
shutil.copy2(path, sys.argv[3])
else:
print("Mancano degli argomenti")