source: titan/titan/harddisk.h @ 45259

Last change on this file since 45259 was 45259, checked in by obi, 3 years ago

fix spinner

File size: 35.6 KB
Line 
1#ifndef HARDDISK_H
2#define HARDDISK_H
3
4int checkfilesystemexist(char* filesystem)
5{
6        int ret = 0;
7        char* tmpstr = NULL, *tmpstr1 = NULL;
8
9        tmpstr = readfiletomem("/proc/filesystems", 0);
10
11        tmpstr1 = ostrcat("\t", filesystem, 0, 0);
12        tmpstr1 = ostrcat(tmpstr1, "\n", 0, 0);
13
14        if(ostrstr(tmpstr, tmpstr1) != NULL)
15                ret = 1;
16
17        free(tmpstr); tmpstr = NULL;
18        free(tmpstr1); tmpstr1 = NULL;
19
20        return ret;
21}
22
23char* blocktogb(unsigned long long size)
24{
25        char* buf = NULL;
26
27        buf = malloc(MINMALLOC);
28        if(buf == NULL)
29        {
30                err("no mem");
31                return NULL;
32        }
33        snprintf(buf, MINMALLOC, "%.2f", (float)size / 2 / 1024 / 1024); //size is in 512KB blocks
34        return ostrshrink(buf);
35}
36
37struct hdd* gethdd(char* device)
38{
39        struct hdd *node = hdd;
40
41        while(node != NULL)
42        {
43                if(ostrcmp(node->device, device) == 0)
44                        return node;
45                node = node->next;
46        }
47        debug(100, "hdd not found (device=%s)", device);
48        return NULL;
49}
50
51unsigned long long hddgetsize(char* device, char* partition)
52{
53        char* file = NULL;
54        unsigned long long size = 0;
55
56        file = ostrcat(file , "/sys/block/", 1, 0);
57        file = ostrcat(file , device, 1, 0);
58        if(partition != NULL)
59        {
60                file = ostrcat(file , "/", 1, 0);
61                file = ostrcat(file , partition, 1, 0);
62        }
63        file = ostrcat(file , "/size", 1, 0);
64        size = readsysull(file, 1);
65        debug(80, "file: %s, size: %lu", file, size);
66        free(file); file = NULL;
67
68        return size;
69}
70
71char* hddgetvendor(char* device)
72{
73        char* file = NULL, *vendor = NULL;
74
75        file = ostrcat(file , "/sys/block/", 1, 0);
76        file = ostrcat(file , device, 1, 0);
77        file = ostrcat(file , "/device/vendor", 1, 0);
78        vendor = strstrip(readsys(file, 1));
79        debug(80, "file: %s, vendor: %s", file, vendor);
80        free(file); file = NULL;
81
82        return vendor;
83}
84
85char* hddgetmodel(char* device)
86{
87        char* file = NULL, *model = NULL;
88
89        file = ostrcat(file , "/sys/block/", 1, 0);
90        file = ostrcat(file , device, 1, 0);
91        file = ostrcat(file , "/device/model", 1, 0);
92        model = strstrip(readsys(file, 1));
93        debug(80, "file: %s, model: %s", file, model);
94        free(file); file = NULL;
95
96        return model;
97}
98
99int hddgetremovable(char* device)
100{
101        char* file = NULL;
102        int removable = 0;
103
104        file = ostrcat(file , "/sys/block/", 1, 0);
105        file = ostrcat(file , device, 1, 0);
106        file = ostrcat(file , "/removable", 1, 0);
107        removable = readsysul(file, 1);
108        debug(80, "file: %s, removable: %d", file, removable);
109        free(file); file = NULL;
110
111        return removable;
112}
113
114void screenfilesystem(char* dev)
115{
116        int i, rcret = 0, count = 1;
117        struct skin* screen = getscreen("harddisk_main");
118        struct skin* details = getscreennode(screen, "details");
119        struct skin* listbox = getscreennode(screen, "listbox");
120        struct skin* tmp = NULL;
121        char* tmpstr = NULL, *tmpstr1 = NULL;
122
123        delmarkedscreennodes(screen, 1);
124        listbox->aktpage = -1;
125        listbox->aktline = 1;
126        changetitle(screen, _("Harddisk Format - Filesystem"));
127        if(details != status.skinerr) changetext(details, _("select the format filesystem type for your media drive"));
128               
129        if(status.expertmodus > 9) count = 5;
130
131        for(i = 0; i < count; i++)
132        {
133                if(i == 0 && checkfilesystemexist("ext3") == 0) continue;
134                else if(i == 1 && checkfilesystemexist("ext4") == 0) continue;
135                else if(i == 2 && checkfilesystemexist("ext2") == 0) continue;
136                else if(i == 3 && checkfilesystemexist("jfs") == 0) continue;
137                else if(i == 4 && checkfilesystemexist("vfat") == 0) continue;
138               
139                tmp = addlistbox(screen, listbox, tmp, 1);
140                if(tmp != NULL)
141                {
142                        if(i == 0) tmpstr = "ext3";
143                        else if(i == 1) tmpstr = "ext4";       
144                        else if(i == 2) tmpstr = "ext2";
145                        else if(i == 3) tmpstr = "jfs";
146                        else if(i == 4) tmpstr = "vfat";
147                        tmpstr1 = ostrcat(tmpstr1, getconfig("skinpath", NULL), 1, 0);
148                        tmpstr1 = ostrcat(tmpstr1, "/skin/", 1, 0);
149                        tmpstr1 = ostrcat(tmpstr1, tmpstr, 1, 0);
150                        tmpstr1 = ostrcat(tmpstr1, ".png", 1, 0);
151
152                        if(!file_exist(tmpstr1))
153                        {
154                                free(tmpstr1); tmpstr1 = NULL;
155                                tmpstr1 = ostrcat(tmpstr1, getconfig("skinpath", NULL), 1, 0);
156                                tmpstr1 = ostrcat(tmpstr1, "/skin/default.png", 1, 0);
157                        }
158                        changepic(tmp, tmpstr1);
159                        free(tmpstr1); tmpstr1 = NULL;
160
161                        changename(tmp, tmpstr);
162                        if(i == 0)
163                        {
164                                tmpstr = ostrcat(tmpstr, " (", 0, 0);
165                                tmpstr = ostrcat(tmpstr, _("default"), 1, 0);
166                                tmpstr = ostrcat(tmpstr, ")", 1, 0);
167                                changetext(tmp, tmpstr);
168                                free(tmpstr); tmpstr = NULL;
169                        }
170                        else
171                                changetext(tmp, tmpstr);
172
173                        tmp->textposx = 120;
174                        tmp->height = 50;
175                        tmp->valign = convertxmlentry("middle", 0);
176                        tmp->hspace = 5;
177                }
178        }
179
180        drawscreen(screen, 0, 0);
181        addscreenrc(screen, listbox);
182
183        while (1)
184        {
185                rcret = waitrc(screen, 0, 0);
186
187                if(rcret==getrcconfigint("rcexit", NULL)) break;
188                if(listbox->select != NULL && rcret==getrcconfigint("rcok", NULL))
189                {
190                        hddformat(dev, listbox->select->name);
191                        break;
192                }
193        }
194        delownerrc(screen);
195        delmarkedscreennodes(screen, 1);
196        clearscreen(screen);
197}
198
199void screenconfigurehdd(char* dev)
200{
201        int i, y = 1, rcret = 0, ret = 0;
202        struct skin* screen = getscreen("harddisk_main");
203        struct skin* details = getscreennode(screen, "details");
204        struct skin* listbox = getscreennode(screen, "listbox");
205
206        struct skin* load = getscreen("loading");
207        struct skin* tmp = NULL;
208        char* tmpstr = NULL, *tmpstr1 = NULL, *tmpstr2 = NULL, *path = NULL, *mountpath = NULL, *cmd = NULL;
209        int record = 0, swapextensions = 0, swapdir = 0, backup = 0;
210        delmarkedscreennodes(screen, 1);
211        listbox->aktpage = -1;
212        listbox->aktline = 1;
213        changetitle(screen, _("Harddisk Configure"));
214        if(details != status.skinerr) changetext(details, _("select the function for the specified device"));
215
216        mountpath = getconfig("mountpath", NULL);
217        mountpath = ostrcat(mountpath, "/", 0, 0);
218        mountpath = ostrcat(mountpath, dev, 1, 0);
219        mountpath = ostrcat(mountpath, "/", 1, 0);
220       
221        if(status.expertmodus > 9) y = 4;
222        for(i = 0; i < y; i++)
223        {                       
224                tmp = addlistbox(screen, listbox, tmp, 1);
225                if(tmp != NULL)
226                {
227                        tmp->type = CHOICEBOX;
228                        if(i == 0)
229                        {
230                                tmpstr = _("use medium for record");
231                                tmpstr1 = "record";
232                                path = ostrcat(mountpath, "movie", 0, 0);
233                                if(file_exist(path))
234                                {
235                                        addchoicebox(tmp, "1", _("yes"));                       
236                                        addchoicebox(tmp, "0", _("no"));
237                                        record = 1;
238                                }
239                                else
240                                {
241                                        addchoicebox(tmp, "0", _("no"));
242                                        addchoicebox(tmp, "1", _("yes"));                       
243                                        record = 0;
244                                }
245                                free(path), path = NULL;
246                        }
247                        else if(i == 1)
248                        {
249                                tmpstr = _("use medium for extension");
250                                tmpstr1 = "swapextensions";
251                                path = ostrcat(mountpath, "swapextensions", 0, 0);
252                                if(file_exist(path))
253                                {
254                                        addchoicebox(tmp, "1", _("yes"));                       
255                                        addchoicebox(tmp, "0", _("no"));
256                                        swapextensions = 1;
257                                }
258                                else
259                                {
260                                        addchoicebox(tmp, "0", _("no"));
261                                        addchoicebox(tmp, "1", _("yes"));                       
262                                        swapextensions = 0;
263                                }
264                                free(path), path = NULL;
265                        }
266                        else if(i == 2)
267                        {
268                                tmpstr = _("use medium for swap");
269                                tmpstr1 = "swapdir";
270                                path = ostrcat(mountpath, "swapdir", 0, 0);
271                                if(file_exist(path))
272                                {
273                                        addchoicebox(tmp, "1", _("yes"));                       
274                                        addchoicebox(tmp, "0", _("no"));
275                                        swapdir = 1;
276                                }
277                                else
278                                {
279                                        addchoicebox(tmp, "0", _("no"));
280                                        addchoicebox(tmp, "1", _("yes"));                       
281                                        swapdir = 0;
282                                }
283                                free(path), path = NULL;
284                        }
285                        else if(i == 3)
286                        {
287                                tmpstr = _("use medium for backup");
288                                tmpstr1 = "backup";
289                                path = ostrcat(mountpath, "backup", 0, 0);
290                                if(file_exist(path))
291                                {
292                                        addchoicebox(tmp, "1", _("yes"));                       
293                                        addchoicebox(tmp, "0", _("no"));
294                                        backup = 1;
295                                }
296                                else
297                                {
298                                        addchoicebox(tmp, "0", _("no"));
299                                        addchoicebox(tmp, "1", _("yes"));                       
300                                        backup = 0;
301                                }
302                                free(path), path = NULL;
303                        }
304
305                        tmpstr2 = ostrcat(tmpstr2, getconfig("skinpath", NULL), 1, 0);
306                        tmpstr2 = ostrcat(tmpstr2, "/skin/", 1, 0);
307                        tmpstr2 = ostrcat(tmpstr2, tmpstr1, 1, 0);
308                        tmpstr2 = ostrcat(tmpstr2, ".png", 1, 0);
309
310                        if(!file_exist(tmpstr2))
311                        {
312                                free(tmpstr2); tmpstr2 = NULL;
313                                tmpstr2 = ostrcat(tmpstr2, getconfig("skinpath", NULL), 1, 0);
314                                tmpstr2 = ostrcat(tmpstr2, "/skin/default.png", 1, 0);
315                        }
316                        changepic(tmp, tmpstr2);
317                        free(tmpstr2); tmpstr2 = NULL;
318
319                        changetext(tmp, tmpstr);
320                        changename(tmp, tmpstr1);
321                        tmp->textposx = 120;
322                        tmp->height = 50;
323                        tmp->valign = convertxmlentry("middle", 0);
324                        tmp->hspace = 5;
325                }
326        }
327
328        drawscreen(screen, 0, 0);
329        tmp = listbox->select;
330
331        while (1)
332        {
333                addscreenrc(screen, tmp);
334
335                rcret = waitrc(screen, 0, 0);
336                tmp = listbox->select;
337
338                if(ostrcmp(listbox->select->name, "record") == 0)
339                        record = atoi(listbox->select->ret);
340                if(ostrcmp(listbox->select->name, "swapextensions") == 0)
341                        swapextensions = atoi(listbox->select->ret);
342                if(ostrcmp(listbox->select->name, "swapdir") == 0)
343                        swapdir = atoi(listbox->select->ret);
344                if(ostrcmp(listbox->select->name, "backup") == 0)
345                        backup = atoi(listbox->select->ret);
346
347                if(rcret==getrcconfigint("rcexit",NULL)) break;
348                if(listbox->select != NULL && rcret == getrcconfigint("rcok", NULL))
349                {
350                        if(record == 0)
351                        {
352                                path = ostrcat(mountpath, "movie", 0, 0);
353                                if(file_exist(path))
354                                {
355                                        if(textbox("Message", _("Are you sure, you want to delete the record directory on this drive?"), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 800, 200, 0, 0) == 1)
356                                        {
357                                                drawscreen(load, 0, 0);
358                                                cmd = ostrcat("rm -rf ", path, 0, 0);
359                                                system(cmd);
360                                                free(cmd); cmd = NULL;
361                                                system("hotplug.sh delRecord");
362                                                clearscreen(load);
363                                        }
364                                }
365                                free(path), path = NULL;
366                        }
367                        else if(record == 1)
368                        {
369                                path = ostrcat(mountpath, "movie", 0, 0);
370                                if(!file_exist(path))
371                                {
372                                        drawscreen(load, 0, 0);
373                                        ret = mkdir(path, 0777);                               
374                                        system("hotplug.sh first");
375                                        clearscreen(load);
376                                }
377                                free(path), path = NULL;
378                        }
379
380                        if(swapextensions == 0)
381                        {
382                                path = ostrcat(mountpath, "swapextensions", 0, 0);
383                                if(file_exist(path))
384                                {
385                                        drawscreen(load, 0, 0);
386                                        cmd = ostrcat("rm -rf ", path, 0, 0);
387                                        system(cmd);
388                                        free(cmd); cmd = NULL;
389                                        system("hotplug.sh delSwapextensions");
390                                        clearscreen(load);
391                                }
392                                free(path), path = NULL;
393                        }
394                        else if(swapextensions == 1)
395                        {
396                                path = ostrcat(mountpath, "swapextensions", 0, 0);
397                                if(!file_exist(path))
398                                {
399                                        drawscreen(load, 0, 0);
400                                        ret = mkdir(path, 0777);                               
401                                        system("hotplug.sh first");
402                                        clearscreen(load);
403                                }
404                                free(path), path = NULL;
405                        }
406
407                        if(swapdir == 0)
408                        {
409                                path = ostrcat(mountpath, "swapdir", 0, 0);
410                                if(file_exist(path))
411                                {
412                                        drawscreen(load, 0, 0);
413                                        system("hotplug.sh delSwap");
414                                        cmd = ostrcat("rm -rf ", path, 0, 0);
415                                        system(cmd);
416                                        free(cmd); cmd = NULL;
417                                        clearscreen(load);
418                                }
419                                free(path), path = NULL;
420                        }
421                        else if(swapdir == 1)
422                        {
423                                path = ostrcat(mountpath, "swapdir", 0, 0);
424                                if(!file_exist(path))
425                                {
426                                        drawscreen(load, 0, 0);
427                                        ret = mkdir(path, 0777);                               
428                                        system("hotplug.sh first");
429                                        clearscreen(load);
430                                }
431                                free(path), path = NULL;
432                        }
433
434                        if(backup == 0)
435                        {
436                                path = ostrcat(mountpath, "backup", 0, 0);
437                                if(file_exist(path))
438                                {
439                                        drawscreen(load, 0, 0);
440                                        cmd = ostrcat("rm -rf ", path, 0, 0);
441                                        system(cmd);
442                                        free(cmd); cmd = NULL;
443                                        system("hotplug.sh delBackup");
444                                        clearscreen(load);
445                                }
446                                free(path), path = NULL;
447                        }
448                        else if(backup == 1)
449                        {
450                                path = ostrcat(mountpath, "backup", 0, 0);
451                                if(!file_exist(path))
452                                {
453                                        drawscreen(load, 0, 0);
454                                        ret = mkdir(path, 0777);                               
455                                        system("hotplug.sh first");
456                                        clearscreen(load);
457                                }
458                                free(path), path = NULL;
459                        }
460                        break;
461                }
462        }
463
464        free(mountpath), mountpath = NULL;
465        delownerrc(screen);
466        delmarkedscreennodes(screen, 1);
467        clearscreen(screen);
468}
469
470void screenharddisk(int mode)
471{
472        struct skin* screen = getscreen("harddisk_main");
473        struct skin* listbox = getscreennode(screen, "listbox");
474        struct skin* details = getscreennode(screen, "details");
475        struct skin* tmp = NULL;
476        struct hdd* hddnode = NULL;
477        int tmphangtime = 999999, rcret = 0;
478        char* tmpstr = NULL, *tmpstr1 = NULL, *path = NULL;
479
480        addhddall();
481start:
482        tmp = NULL;
483        hddnode = hdd;
484
485        if(hdd == NULL)
486        {
487                textbox(_("Harddisk"), _("sorry found no harddisk"), _("OK"), getrcconfigint("rcok", NULL), NULL, 0, NULL, 0, NULL, 0, 800, 600, 0, 0);
488                return;
489        }
490
491        status.hangtime = tmphangtime;
492        delmarkedscreennodes(screen, 1);
493        if(mode == 0)
494        {
495                changetitle(screen, _("Harddisk Format"));
496                if(details != status.skinerr) changetext(details, _("select the media drive from Device List for formating"));
497        }
498        else if(mode == 1)
499        {
500                changetitle(screen, _("Harddisk Fsck"));
501                if(details != status.skinerr) changetext(details, _("select the media drive from Device List for fsck"));
502        }
503        else if(mode == 2)
504        {
505                changetitle(screen, _("Harddisk Configure"));
506                if(details != status.skinerr) changetext(details, _("select the media drive from Device List for configuration"));
507        }
508
509        listbox->aktpage = -1;
510        listbox->aktline = 1;
511
512        while(hddnode != NULL)
513        {
514
515                path = getconfig("mountpath", NULL);
516                path = ostrcat(path, "/", 0, 0);
517                path = ostrcat(path, hddnode->device, 1, 0);
518
519                if(mode == 2 && !file_exist(path) && hddnode->partition == 0)
520                {
521                        hddnode = hddnode->next;
522                        free(path); path = NULL;
523                        continue;
524                }
525                free(path); path = NULL;
526
527                tmp = addlistbox(screen, listbox, tmp, 1);
528                if(tmp != NULL)
529                {
530                        if(hddnode->removable == 0)
531                        {
532                                tmpstr = ostrcat(tmpstr, _("HDD "), 1, 0);
533                                tmpstr1 = ostrcat(tmpstr1, getconfig("skinpath", NULL), 1, 0);
534                                tmpstr1 = ostrcat(tmpstr1, "/skin/hdd.png", 1, 0);
535                        }
536                        else
537                        {
538                                tmpstr = ostrcat(tmpstr, _("STICK "), 1, 0);
539                                tmpstr1 = ostrcat(tmpstr1, getconfig("skinpath", NULL), 1, 0);
540                                tmpstr1 = ostrcat(tmpstr1, "/skin/stick.png", 1, 0);
541                        }
542                        if(mode == 2)
543                        {
544                                tmpstr = ostrcat(tmpstr, hddnode->label, 1, 0);
545                                tmpstr = ostrcat(tmpstr, " (", 1, 0);
546                                tmpstr = ostrcat(tmpstr, hddnode->device, 1, 0);
547                                tmpstr = ostrcat(tmpstr, ")", 1, 0);
548
549                                path = getconfig("mountpath", NULL);
550                                path = ostrcat(path, "/", 0, 0);
551                                path = ostrcat(path, hddnode->device, 1, 0);
552                                path = ostrcat(path, "/", 1, 0);
553                                path = ostrcat(path, "swapextensions", 1, 0);
554                                if(file_exist(path) == 1)
555                                {
556                                        tmpstr = ostrcat(tmpstr, " - ", 1, 0);
557                                        tmpstr = ostrcat(tmpstr, _("ext"), 1, 0);
558                                }
559                                free(path); path = NULL;
560
561                                path = getconfig("mountpath", NULL);
562                                path = ostrcat(path, "/", 0, 0);
563                                path = ostrcat(path, hddnode->device, 1, 0);
564                                path = ostrcat(path, "/", 1, 0);
565                                path = ostrcat(path, "swapdir", 1, 0);
566                                if(file_exist(path) == 1)
567                                {
568                                        tmpstr = ostrcat(tmpstr, " - ", 1, 0);
569                                        tmpstr = ostrcat(tmpstr, _("swap"), 1, 0);
570                                }
571                                free(path); path = NULL;
572
573                                path = getconfig("mountpath", NULL);
574                                path = ostrcat(path, "/", 0, 0);
575                                path = ostrcat(path, hddnode->device, 1, 0);
576                                path = ostrcat(path, "/", 1, 0);
577                                path = ostrcat(path, "backup", 1, 0);
578                                if(file_exist(path) == 1)
579                                {
580                                        tmpstr = ostrcat(tmpstr, " - ", 1, 0);
581                                        tmpstr = ostrcat(tmpstr, _("backup"), 1, 0);
582                                }
583                                free(path); path = NULL;
584
585                                path = getconfig("mountpath", NULL);
586                                path = ostrcat(path, "/", 0, 0);
587                                path = ostrcat(path, hddnode->device, 1, 0);
588                                path = ostrcat(path, "/", 1, 0);
589                                path = ostrcat(path, "movie", 1, 0);
590                                if(file_exist(path) == 1)
591                                {
592                                        tmpstr = ostrcat(tmpstr, " - ", 1, 0);
593                                        tmpstr = ostrcat(tmpstr, _("record"), 1, 0);
594                                }
595                                free(path); path = NULL;
596                        }
597                        else
598                        {
599                                tmpstr = ostrcat(tmpstr, "(", 1, 0);
600                                tmpstr = ostrcat(tmpstr, hddnode->device, 1, 0);
601                                tmpstr = ostrcat(tmpstr, "-", 1, 0);
602                                tmpstr = ostrcat(tmpstr, hddnode->label, 1, 0);
603                                tmpstr = ostrcat(tmpstr, "-", 1, 0);
604                                tmpstr = ostrcat(tmpstr, hddnode->filesystem, 1, 0);
605                                tmpstr = ostrcat(tmpstr, ") ", 1, 0);
606                                tmpstr = ostrcat(tmpstr, hddnode->model, 1, 0);
607                                tmpstr = ostrcat(tmpstr, " ", 1, 0);
608                                tmpstr = ostrcat(tmpstr, blocktogb(hddnode->size), 1, 1);
609                                tmpstr = ostrcat(tmpstr, " GB", 1, 0);
610                        }
611                        changetext(tmp, tmpstr);
612                        free(tmpstr); tmpstr = NULL;
613
614                        changename(tmp, hddnode->device);
615                        tmp->textposx = 120;
616                        tmp->height = 50;
617                        tmp->valign = convertxmlentry("middle", 0);
618                        tmp->hspace = 5;
619
620                        if(!file_exist(tmpstr1))
621                        {
622                                free(tmpstr1); tmpstr1 = NULL;
623                                tmpstr1 = ostrcat(tmpstr1, getconfig("skinpath", NULL), 1, 0);
624                                tmpstr1 = ostrcat(tmpstr1, "/skin/default.png", 1, 0);
625                        }
626                        changepic(tmp, tmpstr1);
627                        free(tmpstr1); tmpstr1 = NULL;
628                }
629                hddnode = hddnode->next;
630        }
631
632        drawscreen(screen, 0, 0);
633        addscreenrc(screen, listbox);
634
635        while (1)
636        {
637                rcret = waitrc(screen, 0, 0);
638
639                if(rcret==getrcconfigint("rcexit",NULL)) break;
640                if(listbox->select != NULL && rcret==getrcconfigint("rcok",NULL))
641                {
642                        if(mode == 0)
643                        {
644                                tmpstr = ostrcat(listbox->select->name, NULL, 0, 0);
645                                screenfilesystem(tmpstr);
646                                free(tmpstr); tmpstr = NULL;
647                        }
648                        else if(mode == 1)
649                        {
650                                if(hddfsck(listbox->select->name) == 1)
651                                        continue;
652                        }
653                        else if(mode == 2)
654                        {
655                                tmpstr = ostrcat(listbox->select->name, NULL, 0, 0);
656                                screenconfigurehdd(tmpstr);
657                                free(tmpstr); tmpstr = NULL;
658                                delownerrc(screen);
659                                goto start;
660                        }
661                        break;
662                }
663        }
664
665        delownerrc(screen);
666        delmarkedscreennodes(screen, 1);
667        clearscreen(screen);
668}
669
670void hddformat(char* dev, char* filesystem)
671{
672        int format = 0;
673        int large = 0;
674        int rc = 0;
675        char* cmd = NULL;
676        struct hdd* node = NULL;
677        resettvpic();
678
679#ifdef OEBUILD
680//      status.sec = 0;
681        status.spinnertime = 99999999;
682        status.hangtime = 999999;
683#endif
684
685        node = gethdd(dev);
686        if(node == NULL) return;
687       
688        if(node->size > 4294967295UL)
689        {       
690                if(!file_exist("/var/bin/parted") && !file_exist("/var/swap/bin/parted") && !file_exist("/mnt/swapextensions/bin/parted") && !file_exist("/usr/sbin/parted"))
691                {
692                        textbox(_("Message"), _("HDD > 2TB unsupported.\nYou must first install plugin parted, then you can use this panel."), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 800, 200, 0, 0);
693                        return;
694                }
695                large = 1;
696        }
697
698        if(node->partition == 0)
699        {
700                if(textbox(_("Message"), _("Are you sure you want to remove all Partitions\non this device and create a new Partition 1?\nBox reboots after format"), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 800, 200, 0, 0) == 1)
701                {
702                        cmd = ostrcat("/sbin/parter.sh /dev/" , dev, 0, 0);
703                        cmd = ostrcat(cmd , " create 1 ", 1, 0);
704                        cmd = ostrcat(cmd , filesystem, 1, 0);
705                        if(large == 1)
706                                cmd = ostrcat(cmd , " large", 1, 0);
707#ifdef OEBUILD
708                        if(!file_exist("/mnt/logs"))
709                                 mkdir("/mnt/logs", 777);
710               
711                        if(file_exist("/mnt/logs"))
712                                cmd = ostrcat(cmd, " > /mnt/logs/format_debug.log 2>&1", 1, 0);
713#else
714                        if(file_exist("/var/swap"))
715                        {
716                                if(!file_exist("/var/swap/logs"))
717                                         mkdir("/var/swap/logs", 777);
718                       
719                                if(file_exist("/etc/.beta") && file_exist("/var/swap/logs"))
720                                        cmd = ostrcat(cmd, " > /var/swap/logs/format_debug.log 2>&1", 1, 0);           
721                        }
722                        else if(!file_exist("/etc/.smalbox"))
723                        {
724                                if(!file_exist("/mnt/logs"))
725                                         mkdir("/mnt/logs", 777);
726                       
727                                if(file_exist("/etc/.beta") && file_exist("/mnt/logs"))
728                                        cmd = ostrcat(cmd, " > /mnt/logs/format_debug.log 2>&1", 1, 0);
729                        }
730#endif
731                        debug(80, "fdisk create cmd: %s", cmd);
732                        system(cmd);
733                        format = 2;
734                        free(cmd); cmd = NULL;
735                }
736               
737        }
738        else
739        {
740                if(textbox(_("Message"), _("Are you sure you want to format this Partition?\nBox reboots after format"), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 800, 200, 0, 0) == 1)
741                {
742                        printf("dev: %s\n", dev);
743
744                        char* part = oregex(".*([0-9]{1,2}).*", dev);
745                        printf("part: %s\n", part);
746
747                        char* devname = oregex(".*([a-z]{3}).*", dev);
748                        printf("devname: %s\n", devname);
749
750                        cmd = ostrcat("/sbin/parter.sh /dev/" , devname, 0, 0);
751                        cmd = ostrcat(cmd , " update ", 1, 0);
752                        cmd = ostrcat(cmd , part, 1, 0);
753                        cmd = ostrcat(cmd , " ", 1, 0);
754                        cmd = ostrcat(cmd , filesystem, 1, 0);
755                        free(part), part = NULL;
756                        free(devname), devname = NULL;
757
758                        if(large == 1)
759                                cmd = ostrcat(cmd , " large", 1, 0);
760#ifdef OEBUILD
761                        if(!file_exist("/mnt/logs"))
762                                 mkdir("/mnt/logs", 777);
763               
764                        if(file_exist("/mnt/logs"))
765                                cmd = ostrcat(cmd, " > /mnt/logs/format_debug.log 2>&1", 1, 0);
766#else
767                        if(file_exist("/var/swap"))
768                        {
769                                if(!file_exist("/var/swap/logs"))
770                                         mkdir("/var/swap/logs", 777);
771                       
772                                if(file_exist("/etc/.beta") && file_exist("/var/swap/logs"))
773                                        cmd = ostrcat(cmd, " > /var/swap/logs/format_debug.log 2>&1", 1, 0);           
774                        }
775                        else if(!file_exist("/etc/.smalbox"))
776                        {
777                                if(!file_exist("/mnt/logs"))
778                                         mkdir("/mnt/logs", 777);
779                       
780                                if(file_exist("/etc/.beta") && file_exist("/mnt/logs"))
781                                        cmd = ostrcat(cmd, " > /mnt/logs/format_debug.log 2>&1", 1, 0);
782                        }
783#endif
784                        debug(80, "fdisk update cmd: %s", cmd);
785                        system(cmd);
786                        format = 1;
787                        free(cmd); cmd = NULL;
788                }
789        }
790
791        if(format > 0)
792        {
793                int record = 0, ext = 0, swap = 0, backup = 0;
794               
795                if(textbox(_("Message"), _("use medium for record"), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 800, 200, 0, 0) == 1)
796                        record = 1;
797               
798                if(status.expertmodus > 9)
799                {
800                        if(textbox(_("Message"), _("use medium for extension"), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 800, 200, 0, 0) == 1)
801                                ext = 1;
802                        if(textbox(_("Message"), _("use medium for swap"), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 800, 200, 0, 0) == 1)
803                                swap = 1;
804                        if(textbox(_("Message"), _("use medium for backup"), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 800, 200, 0, 0) == 1)
805                                backup = 1;
806                }
807                       
808#ifndef OEBUILD
809                if(ostrcmp(filesystem, "vfat") == 0)
810                        cmd = ostrcat("/sbin/cmd.sh \"mkfs.fat.gui -F 32\" /dev/" , dev, 0, 0);
811                else if(ostrcmp(filesystem, "jfs") == 0)
812                        cmd = ostrcat("/sbin/cmd.sh \"mkfs.jfs.gui -q\" /dev/" , dev, 0, 0);
813                else if(ostrcmp(filesystem, "ext2") == 0)
814                        cmd = ostrcat("/sbin/cmd.sh mkfs.ext2.gui /dev/" , dev, 0, 0);
815                else if(ostrcmp(filesystem, "ext3") == 0)
816                        cmd = ostrcat("/sbin/cmd.sh \"mkfs.ext3.gui -T largefile -m0 -O dir_index\" /dev/" , dev, 0, 0);
817                else if(ostrcmp(filesystem, "ext4") == 0)
818                        cmd = ostrcat("/sbin/cmd.sh \"mkfs.ext4.gui -T largefile -m0 -O dir_index\" /dev/" , dev, 0, 0);
819#else
820                if(ostrcmp(filesystem, "vfat") == 0)
821                        cmd = ostrcat("/sbin/cmd.sh \"mkfs.fat -F 32\" /dev/" , dev, 0, 0);
822                else if(ostrcmp(filesystem, "jfs") == 0)
823                        cmd = ostrcat("/sbin/cmd.sh \"mkfs.jfs -q\" /dev/" , dev, 0, 0);
824                else if(ostrcmp(filesystem, "ext2") == 0)
825                        cmd = ostrcat("/sbin/cmd.sh mkfs.ext2 /dev/" , dev, 0, 0);
826                else if(ostrcmp(filesystem, "ext3") == 0)
827                        cmd = ostrcat("/sbin/cmd.sh \"mkfs.ext3 -T largefile -N 262144 -F -F -m0 -O ^metadata_csum -O dir_index,sparse_super\" /dev/" , dev, 0, 0);                     
828//                      cmd = ostrcat("/sbin/cmd.sh \"mkfs.ext3 -T largefile -m0 -O dir_index\" /dev/" , dev, 0, 0);
829                else if(ostrcmp(filesystem, "ext4") == 0)
830                        cmd = ostrcat("/sbin/cmd.sh \"mkfs.ext4 -T largefile -N 262144 -F -F -m0 -O ^metadata_csum -O dir_index,sparse_super\" /dev/" , dev, 0, 0);
831//                      cmd = ostrcat("/sbin/cmd.sh \"mkfs.ext4 -T largefile -m0 -O dir_index\" /dev/" , dev, 0, 0);
832#endif
833
834                if(format == 2) cmd = ostrcat(cmd , "1", 1, 0);
835                       
836                if(record == 1) 
837                        cmd = ostrcat(cmd , " 1", 1, 0);
838                else
839                        cmd = ostrcat(cmd , " 0", 1, 0);
840                       
841                if(ext == 1) 
842                        cmd = ostrcat(cmd , " 1", 1, 0);
843                else
844                        cmd = ostrcat(cmd , " 0", 1, 0);
845                       
846                if(swap == 1) 
847                        cmd = ostrcat(cmd , " 1", 1, 0);
848                else
849                        cmd = ostrcat(cmd , " 0", 1, 0);
850                       
851                if(backup == 1) 
852                        cmd = ostrcat(cmd , " 1", 1, 0);
853                else
854                        cmd = ostrcat(cmd , " 0", 1, 0);
855#ifdef OEBUILD
856                if(!file_exist("/mnt/logs"))
857                         mkdir("/mnt/logs", 777);
858       
859                if(file_exist("/mnt/logs"))
860                        cmd = ostrcat(cmd, " >> /mnt/logs/format_debug.log 2>&1", 1, 0);               
861#else
862                if(file_exist("/var/swap"))
863                {
864                        if(!file_exist("/var/swap/logs"))
865                                 mkdir("/var/swap/logs", 777);
866               
867                        if(file_exist("/etc/.beta") && file_exist("/var/swap/logs"))
868                                cmd = ostrcat(cmd, " >> /var/swap/logs/format_debug.log 2>&1", 1, 0);           
869                }
870                else if(!file_exist("/etc/.smalbox"))
871                {
872                        if(!file_exist("/mnt/logs"))
873                                 mkdir("/mnt/logs", 777);
874               
875                        if(file_exist("/etc/.beta") && file_exist("/mnt/logs"))
876                                cmd = ostrcat(cmd, " >> /mnt/logs/format_debug.log 2>&1", 1, 0);
877                }
878#endif
879                debug(80, "format cmd: %s", cmd);
880
881                rc = system(cmd);
882
883#ifdef OEBUILD
884                status.spinnertime = getconfigint("spinnertime", NULL);
885                status.hangtime = getconfigint("hangtime", NULL);
886#endif
887
888                free(cmd); cmd = NULL;
889                if(rc != 0)
890                {
891                        textbox(_("Message"), _("ERROR\npartition could not be created"), _("OK"), getrcconfigint("rcok", NULL), NULL, 0, NULL, 0, NULL, 0, 800, 200, 0, 0);
892                        return;
893                }
894        }
895#ifdef OEBUILD
896        status.spinnertime = getconfigint("spinnertime", NULL);
897        status.hangtime = getconfigint("hangtime", NULL);
898#endif
899}
900
901int hddfsck(char* dev)
902{
903        char* cmd = NULL;
904        struct hdd* node = NULL;
905
906#ifdef OEBUILD
907//      status.sec = 0;
908        status.spinnertime = 99999999;
909        status.hangtime = 999999;
910#endif
911
912        node = gethdd(dev);
913        if(node == NULL) return 1;
914        if(node->filesystem == NULL) return 1;
915        debug(80, "device=%s filesystem=%s", dev, node->filesystem);
916        resettvpic();
917
918        if(node->size > 524288000UL)
919        {       
920                textbox(_("Message"), _("Information: This hard drive size can take a file\nsystem check between 30 minutes and 1.5 hours."), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 800, 200, 0, 0);
921        }
922
923        if(textbox(_("Message"), _("Are you sure you want to check this Partition?\nBox reboots after check"), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 800, 200, 0, 0) == 1)
924        {
925                if(ostrcmp(node->filesystem, "vfat") == 0)
926                        cmd = ostrcat("/sbin/cmd.sh \"fsck.fat.gui -a -v\" /dev/" , dev, 0, 0);
927                else if(ostrcmp(node->filesystem, "jfs") == 0)
928                        cmd = ostrcat("/sbin/cmd.sh \"fsck.jfs.gui -f -p\" /dev/" , dev, 0, 0);
929                else if(ostrcmp(node->filesystem, "ext2") == 0)
930                        cmd = ostrcat("/sbin/cmd.sh \"fsck.ext2.gui -f -p\" /dev/" , dev, 0, 0);
931                else if(ostrcmp(node->filesystem, "ext3") == 0)
932                        cmd = ostrcat("/sbin/cmd.sh \"fsck.ext3.gui -f -p\" /dev/" , dev, 0, 0);
933                else if(ostrcmp(node->filesystem, "ext4") == 0)
934                        cmd = ostrcat("/sbin/cmd.sh \"fsck.ext4.gui -f -p\" /dev/" , dev, 0, 0);
935#ifdef OEBUILD
936                if(!file_exist("/mnt/logs"))
937                         mkdir("/mnt/logs", 777);
938       
939                if(file_exist("/mnt/logs"))
940                        cmd = ostrcat(cmd, " > /mnt/logs/fsck_debug.log 2>&1", 1, 0);
941#else
942                if(file_exist("/var/swap"))
943                {
944                        if(!file_exist("/var/swap/logs"))
945                                 mkdir("/var/swap/logs", 777);
946               
947                        if(file_exist("/etc/.beta") && file_exist("/var/swap/logs"))
948                                cmd = ostrcat(cmd, " > /var/swap/logs/fsck_debug.log 2>&1", 1, 0);             
949                }
950                else if(!file_exist("/etc/.smalbox"))
951                {
952                        if(!file_exist("/mnt/logs"))
953                                 mkdir("/mnt/logs", 777);
954               
955                        if(file_exist("/etc/.beta") && file_exist("/mnt/logs"))
956                                cmd = ostrcat(cmd, " > /mnt/logs/fsck_debug.log 2>&1", 1, 0);
957                }
958#endif         
959                debug(80, "fsck cmd: %s", cmd);
960                system(cmd);
961                free(cmd); cmd = NULL;
962        }
963
964#ifdef OEBUILD
965        status.spinnertime = getconfigint("spinnertime", NULL);
966        status.hangtime = getconfigint("hangtime", NULL);
967#endif
968
969        return 0;
970}
971
972void screenharddisksleep()
973{
974        char* sleepWert = NULL;
975        int rcret = 0;
976        struct skin* sleep_config = getscreen("harddisk_main");
977        struct skin* details = getscreennode(sleep_config, "details");
978        struct skin* listbox = getscreennode(sleep_config, "listbox");
979        struct skin* node = NULL;
980        struct skin* tmp = NULL;
981       
982        /*int m_with = sleep_config->width;
983        int m_height = sleep_config->height;
984        int m_prozwidth = sleep_config->prozwidth;
985        int m_prozheight= sleep_config->prozheight;
986       
987        sleep_config->prozheight = YES;
988        sleep_config->prozwidth = YES;
989        sleep_config->width = 40;
990        sleep_config->height = 30;*/
991
992        delmarkedscreennodes(sleep_config, 1);
993        listbox->aktpage = -1;
994        listbox->aktline = 1;
995        changetitle(sleep_config, _("Harddisk Sleep"));
996        if(details != status.skinerr) changetext(details, _("set here the default sleeptime for all your media drives"));
997       
998        node = addlistbox(sleep_config, listbox, node, 1);
999        if(node != NULL)
1000        {
1001                node->type = CHOICEBOX;
1002                changetext(node, _("Time to sleep")); changename(node, "timetosleep");
1003                addchoicebox(node, "0", _("off"));
1004                addchoicebox(node, "300", _("5 min")); addchoicebox(node, "600", _("10 min"));
1005                addchoicebox(node, "900", _("15 min")); addchoicebox(node, "3600", _("60 min"));
1006       
1007                sleepWert = getconfig("timetosleep", NULL);
1008                if(sleepWert == NULL)
1009                        setchoiceboxselection(node, "0");
1010                else
1011                        setchoiceboxselection(node, sleepWert);
1012        }
1013               
1014        drawscreen(sleep_config, 0, 0);
1015        tmp = listbox->select;
1016       
1017        while(1)
1018        {
1019                addscreenrc(sleep_config, tmp);
1020                rcret = waitrc(sleep_config, 0, 0);
1021                tmp = listbox->select;
1022
1023                if(rcret == getrcconfigint("rcexit", NULL)) break;
1024
1025                if(listbox->select != NULL && listbox->select->ret != NULL && rcret == getrcconfigint("rcok", NULL))
1026                {
1027                        addconfig("timetosleep", listbox->select->ret);
1028                        //settimetosleep(atoi(listbox->select->ret));
1029                        break;
1030                }
1031        }
1032
1033        /*sleep_config->width = m_with;
1034        sleep_config->height = m_height;
1035        sleep_config->prozheight = m_prozheight;
1036        sleep_config->prozwidth = m_prozwidth;*/
1037        delmarkedscreennodes(sleep_config, 1);
1038        delownerrc(sleep_config);
1039        clearscreen(sleep_config);
1040}
1041
1042//flag 0: lock
1043//flag 1: no lock
1044void delhdd(char* device, int flag)
1045{
1046        if(flag == 0) m_lock(&status.hddmutex, 13);
1047        struct hdd *node = hdd, *prev = hdd;
1048       
1049        while(node != NULL)
1050        {
1051                if(ostrcmp(node->device, device) == 0)
1052                {
1053                        if(node == hdd)
1054                        {
1055                                hdd = node->next;
1056                                if(hdd != NULL)
1057                                        hdd->prev = NULL;
1058                        }
1059                        else
1060                        {
1061                                prev->next = node->next;
1062                                if(node->next != NULL)
1063                                        node->next->prev = prev;
1064                        }
1065
1066                        free(node->device); node->device = NULL;
1067                        free(node->vendor); node->vendor = NULL;
1068                        free(node->model); node->model = NULL;
1069                        free(node->label); node->label = NULL;
1070                        free(node->uuid); node->uuid = NULL;
1071                        free(node->filesystem); node->filesystem = NULL;
1072       
1073                        free(node);
1074                        node = NULL;
1075                        break;
1076                }
1077
1078                prev = node;
1079                node = node->next;
1080        }
1081        if(flag == 0) m_unlock(&status.hddmutex, 13);
1082}
1083
1084//flag 0: lock
1085//flag 1: no lock
1086struct hdd* addhdd(char* device, int partition, unsigned long long size, int removable, char* vendor, char *model, char* label, char* filesystem, char* uuid, int timetosleep, struct hdd* last, int flag)
1087{
1088        if(flag == 0) m_lock(&status.hddmutex, 13);
1089        struct hdd *newnode = NULL, *prev = NULL, *node = hdd;
1090
1091        newnode = (struct hdd*)malloc(sizeof(struct hdd));     
1092        if(newnode == NULL)
1093        {
1094                err("no memory");
1095                if(flag == 0) m_unlock(&status.hddmutex, 13);
1096                return NULL;
1097        }
1098        memset(newnode, 0, sizeof(struct hdd));
1099
1100        newnode->device = device;
1101        newnode->partition = partition;
1102        newnode->size = size;
1103        newnode->removable = removable;
1104        newnode->vendor = vendor;
1105        newnode->model = model;
1106        newnode->label = label;
1107        newnode->uuid = uuid;
1108        newnode->filesystem = filesystem;
1109        newnode->read = 0;
1110        newnode->write = 0;
1111        newnode->sleeptime = timetosleep;
1112        newnode->notchanged = 0;
1113
1114        debug(80, "add hdd %s", device);
1115       
1116        if(last == NULL)
1117        {
1118                while(node != NULL && strcoll(newnode->device, node->device) > 0)
1119                {
1120                        prev = node;
1121                        node = node->next;
1122                }
1123        }
1124        else
1125        {
1126                prev = last;
1127                node = last->next;
1128        }
1129
1130        if(prev == NULL)
1131        {
1132                hdd = newnode;
1133        }
1134        else
1135        {
1136                prev->next = newnode;
1137                newnode->prev = prev;
1138        }
1139        newnode->next = node;
1140       
1141        //show message for new, not configured devices
1142        //check only partition 1
1143        if(status.standby == 0 && newnode->partition == 1 && ostrstr(newnode->device, "1") != NULL)
1144        {
1145                char* tmpstr = NULL, *backup = NULL, *movie = NULL, *swapextensions = NULL, *swapfile = NULL, *checkfile = NULL;
1146                int newdev = 1, ret = 0;
1147               
1148                tmpstr = ostrcat("/autofs/", newnode->device, 0, 0);
1149                checkfile = ostrcat(tmpstr, "/.titandev", 0, 0);
1150                if(file_exist(checkfile) == 0)
1151                {
1152                        backup = ostrcat(tmpstr, "/backup", 0, 0);
1153                        movie = ostrcat(tmpstr, "/movie", 0, 0);
1154                        swapextensions = ostrcat(tmpstr, "/swapextensions", 0, 0);
1155                        swapfile = ostrcat(tmpstr, "/swapfile", 0, 0);
1156                       
1157                        if(file_exist(movie) == 1 || file_exist(swapextensions)== 1 || file_exist(backup) == 1 || file_exist(swapfile) == 1)
1158                                newdev = 0;
1159                               
1160                        ret = writesys(checkfile, "titan", 1);
1161                        if(ret == 0)
1162                        {
1163                                sync();
1164                                if(newdev == 1)
1165                                        textbox("Message", _("Found new Stick/HDD.\nYou can configure it in Harddisk Menu."), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 800, 200, 15, 0);
1166                        }
1167                }
1168                free(tmpstr); tmpstr = NULL;
1169                free(backup); backup = NULL;
1170                free(movie); movie = NULL;
1171                free(swapextensions); swapextensions = NULL;
1172                free(swapfile); swapfile = NULL;                               
1173                free(checkfile); checkfile = NULL;                             
1174        }
1175       
1176        if(flag == 0) m_unlock(&status.hddmutex, 13);
1177        return newnode;
1178}
1179
1180//flag = 0: mutex lock
1181//flag = 1: no mutex lock
1182void freehdd(int flag)
1183{
1184        if(flag == 0) m_lock(&status.hddmutex, 13);
1185        struct hdd *node = hdd, *prev = hdd;
1186
1187        while(node != NULL)
1188        {
1189                prev = node;
1190                node = node->next;
1191                if(prev != NULL)
1192                        delhdd(prev->device, 1);
1193        }
1194        if(flag == 0) m_unlock(&status.hddmutex, 13);
1195}
1196
1197int addhddall()
1198{
1199        m_lock(&status.hddmutex, 13);
1200        FILE* fd = NULL;
1201        FILE* fd2 = NULL;
1202        char* fileline = NULL, *dev = NULL, *pos = NULL, *part = NULL;
1203        char* tmpstr = NULL;
1204        char* tmpstr2 = NULL;
1205        int partition = 0;
1206        int timetosleep = 0;
1207        struct hdd *node = hdd;
1208        struct hdd *nodedev = hdd;
1209        int w1, w2, w3, w4, w5;
1210               
1211        fd = fopen("/proc/partitions", "r");
1212        if(fd == NULL)
1213        {
1214                err("open /proc/partitions");
1215                m_unlock(&status.hddmutex, 13);
1216                return 1;
1217        }
1218
1219        fileline = malloc(MINMALLOC);
1220        if(fileline == NULL)
1221        {
1222                err("no mem");
1223                m_unlock(&status.hddmutex, 13);
1224                return 1;
1225        }
1226
1227        while(fgets(fileline, MINMALLOC, fd) != NULL)
1228        {
1229                pos = ostrstr(fileline, "sd");
1230                if(pos != NULL)
1231                {
1232                        partition = 0;
1233                        part = NULL;
1234                        pos = string_newline(pos);
1235                        if(strlen(pos) == 3)
1236                        {
1237                                free(dev); dev = NULL;
1238                                dev = ostrcat(pos, NULL, 0, 0);
1239                                if(getconfig("timetosleep", NULL) == NULL)
1240                                        timetosleep = 0;
1241                                else 
1242                                        timetosleep = atoi(getconfig("timetosleep", NULL));
1243                        }
1244                        if(strlen(pos) == 4)
1245                        {
1246                                part = pos;
1247                                partition = atoi(&pos[3]);
1248                        }
1249                        tmpstr = ostrcat(tmpstr, "#", 1, 0);
1250                        tmpstr = ostrcat(tmpstr, pos, 1, 0);
1251                        //if(gethdd(pos) == NULL)
1252                        nodedev = gethdd(pos);
1253                        if(nodedev == NULL) 
1254                                nodedev = addhdd(ostrcat(pos, NULL, 0, 0), partition, hddgetsize(dev, part), hddgetremovable(dev), hddgetvendor(dev), hddgetmodel(dev), get_label(part), get_filesystem(part), get_uuid(part), timetosleep, NULL, 1);
1255                       
1256                        //HDD SleepTimer start
1257                        if(nodedev != NULL) {
1258                                if(nodedev->sleeptime != timetosleep) {
1259                                        nodedev->read = 0;
1260                                        nodedev->write = 0;
1261                                        nodedev->sleeptime = timetosleep;
1262                                        nodedev->notchanged = 0;
1263                                }
1264                                if(strlen(pos) == 3 && timetosleep > 0) {
1265                                        free(tmpstr2);tmpstr2=NULL;
1266                                        tmpstr2 = ostrcat(tmpstr2, "/sys/block/", 1, 0);
1267                                        tmpstr2 = ostrcat(tmpstr2, nodedev->device, 1, 0);
1268                                        tmpstr2 = ostrcat(tmpstr2, "/stat", 1, 0);
1269                                        fd2 = fopen(tmpstr2, "r");
1270                                        if(fd2 != NULL) {
1271                                                fscanf(fd2, "%d%d%d%d%d", &w1,&w2,&w3,&w4,&w5);
1272                                                fclose(fd2);
1273                                                fd2 = NULL;
1274                                                if((nodedev->read != w1) || (nodedev->write != w5)) {
1275                                                        nodedev->read = w1;
1276                                                        nodedev->write = w5;
1277                                                        nodedev->notchanged = 0;
1278                                                } else {
1279                                                        if (nodedev->notchanged < nodedev->sleeptime) {
1280                                                                nodedev->notchanged = nodedev->notchanged + (status.addhddall->delay / 1000);
1281                                                                if (nodedev->notchanged >= nodedev->sleeptime) {
1282                                                                        free(tmpstr2);tmpstr2=NULL;
1283#ifdef MIPSEL
1284                                                                        tmpstr2 = ostrcat(tmpstr2, "sdparm -C stop /dev/", 1, 0);
1285#else
1286                                                                        tmpstr2 = ostrcat(tmpstr2, "hd-idle -t ", 1, 0);
1287#endif
1288                                                                        tmpstr2 = ostrcat(tmpstr2, nodedev->device, 1, 0);
1289                                                                        printf("cmd: %s\n", tmpstr2);
1290                                                                        system(tmpstr2);
1291                                                                }
1292                                                        }
1293                                                }
1294                                        }
1295                                }
1296                        }
1297                        //HDD SleepTimer end
1298               
1299                }
1300        }
1301
1302        //check for removed devs
1303        while(node != NULL && tmpstr != NULL)
1304        {
1305                if(ostrstr(tmpstr, node->device) == NULL)
1306                {
1307                        debug(80, "remove %s", node->device);
1308                        delhdd(node->device, 1);
1309                }
1310                node = node->next;
1311        }
1312
1313        if(node != NULL && tmpstr == NULL)
1314        {
1315                debug(80, "remove all");
1316                freehdd(1);
1317        }
1318
1319        free(tmpstr);
1320        free(tmpstr2);
1321        free(dev);
1322        free(fileline);
1323        fclose(fd);
1324        m_unlock(&status.hddmutex, 13);
1325        return 0;
1326}
1327
1328#endif
Note: See TracBrowser for help on using the repository browser.