YourDefrag  SVN
inc/JkDefragLib.h
Go to the documentation of this file.
00001 /*
00002 
00003   The JkDefragDll library.
00004 
00005 
00006 
00007   This program is free software; you can redistribute it and/or modify
00008   it under the terms of the GNU General Public License as published by
00009   the Free Software Foundation; either version 2 of the License, or
00010   (at your option) any later version.
00011 
00012   This program is distributed in the hope that it will be useful,
00013   but WITHOUT ANY WARRANTY; without even the implied warranty of
00014   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015   GNU Lesser General Public License for more details.
00016 
00017   For the full text of the license see the "License lgpl.txt" file.
00018 
00019 
00020 
00021   Jeroen C. Kessels
00022   Internet Engineer
00023   http://www.kessels.com/
00024 
00025 */
00026 
00027 
00028 
00029 /* Include guard */
00030 #ifndef JKDEFRAGLIB
00031 #define JKDEFRAGLIB
00032 
00033 
00034 
00035 /* Make functions accessible in the DLL. */
00036 extern "C" {
00037 
00038 
00039 
00040 #define VERSIONTEXT L"JkDefrag v3.36"
00041 
00042 #define NO     0
00043 #define YES    1
00044 
00045 #define VIRTUALFRAGMENT 18446744073709551615          /* _UI64_MAX - 1 */
00046 
00047 /* The three running states. */
00048 #define RUNNING  0
00049 #define STOPPING 1
00050 #define STOPPED  2
00051 
00052 /* The colors used by the defragger. */
00053 #define COLOREMPTY        0     /* Empty diskspace. */
00054 #define COLORALLOCATED    1     /* Used diskspace / system files. */
00055 #define COLORUNFRAGMENTED 2     /* Unfragmented files. */
00056 #define COLORUNMOVABLE    3     /* Unmovable files. */
00057 #define COLORFRAGMENTED   4     /* Fragmented files. */
00058 #define COLORBUSY         5     /* Busy color. */
00059 #define COLORMFT          6     /* MFT reserved zones. */
00060 #define COLORSPACEHOG     7     /* Spacehogs. */
00061 
00062 /* List in memory of the fragments of a file. */
00063 struct FragmentListStruct {
00064   ULONG64 Lcn;                /* Logical cluster number, location on disk. */
00065   ULONG64 NextVcn;             /* Virtual cluster number of next fragment. */
00066   struct FragmentListStruct *Next;
00067   };
00068 
00069 /* List in memory of all the files on disk, sorted by LCN (Logical
00070    Cluster Number). */
00071 struct ItemStruct {
00072   struct ItemStruct *Parent;                               /* Parent item. */
00073   struct ItemStruct *Smaller;                        /* Next smaller item. */
00074   struct ItemStruct *Bigger;                          /* Next bigger item. */
00075   WCHAR     *LongFilename;                               /* Long filename. */
00076   WCHAR     *LongPath;               /* Full path on disk, long filenames. */
00077   WCHAR     *ShortFilename;                   /* Short filename (8.3 DOS). */
00078   WCHAR     *ShortPath;             /* Full path on disk, short filenames. */
00079   ULONG64   Bytes;                               /* Total number of bytes. */
00080   ULONG64   Clusters;                         /* Total number of clusters. */
00081   ULONG64   CreationTime;                           /* 1 second = 10000000 */
00082   ULONG64   MftChangeTime;
00083   ULONG64   LastAccessTime;
00084   struct FragmentListStruct *Fragments;              /* List of fragments. */
00085   ULONG64   ParentInode;      /* The Inode number of the parent directory. */
00086   struct ItemStruct *ParentDirectory;
00087   BOOL      Directory;                           /* YES: it's a directory. */
00088   BOOL      Unmovable;               /* YES: file can't/couldn't be moved. */
00089   BOOL      Exclude;        /* YES: file is not to be defragged/optimized. */
00090   BOOL      SpaceHog;             /* YES: file to be moved to end of disk. */
00091   };
00092 
00093 enum DiskType {
00094   UnknownType = 0,
00095   NTFS        = 1,
00096   FAT12       = 12,
00097   FAT16       = 16,
00098   FAT32       = 32
00099   };
00100 
00101 /* Information about a disk volume. */
00102 struct DiskStruct {
00103   HANDLE    VolumeHandle;
00104   WCHAR     *MountPoint;          /* Example: "c:" */
00105   WCHAR     *MountPointSlash;     /* Example: "c:\" */
00106   WCHAR     VolumeName[52];       /* Example: "\\?\Volume{08439462-3004-11da-bbca-806d6172696f}" */
00107   WCHAR     VolumeNameSlash[52];  /* Example: "\\?\Volume{08439462-3004-11da-bbca-806d6172696f}\" */
00108   DiskType Type;
00109   ULONG64   MftLockedClusters;    /* Number of clusters at begin of MFT that cannot be moved. */
00110   };
00111 
00112 /* List of clusters used by the MFT. */
00113 struct ExcludesStruct {
00114   ULONG64 Start;
00115   ULONG64 End;
00116   };
00117 
00118 /* The big data struct that holds all the defragger's variables for a
00119    single thread. */
00120 struct DefragDataStruct {
00121   int Phase;                             /* The current Phase (1...3). */
00122   int Zone;                    /* The current Zone (0..2) for Phase 3. */
00123   int *Running;                /* If not RUNNING then stop defragging. */
00124   int *RedrawScreen;                      /* 0:no, 1:request, 2: busy. */
00125   BOOL UseLastAccessTime;     /* If TRUE then use LastAccessTime for SpaceHogs. */
00126   int CannotMoveDirs;      /* If bigger than 20 then do not move dirs. */
00127 
00128   WCHAR *IncludeMask;                            /* Example: "c:\t1\*" */
00129   struct DiskStruct Disk;
00130 
00131   double FreeSpace;           /* Percentage of total disk size 0..100. */
00132 
00133   /* Tree in memory with information about all the files. */
00134   struct ItemStruct *ItemTree;
00135   int BalanceCount;
00136   WCHAR **Excludes;                       /* Array with exclude masks. */
00137   BOOL UseDefaultSpaceHogs;       /* TRUE: use the built-in SpaceHogs. */
00138   WCHAR **SpaceHogs;                     /* Array with SpaceHog masks. */
00139   ULONG64 Zones[4];                       /* Begin (LCN) of the zones. */
00140 
00141   /* List of clusters reserved for the MFT. */
00142   struct ExcludesStruct MftExcludes[3];
00143 
00144   /* Counters filled before Phase 1. */
00145   ULONG64 TotalClusters;           /* Size of the volume, in clusters. */
00146   ULONG64 BytesPerCluster;             /* Number of bytes per cluster. */
00147 
00148   /* Counters updated before/after every Phase. */
00149   ULONG64 CountFreeClusters;               /* Number of free clusters. */
00150   ULONG64 CountGaps;                                /* Number of gaps. */
00151   ULONG64 BiggestGap;             /* Size of biggest gap, in clusters. */
00152   ULONG64 CountGapsLess16; /* Number of gaps smaller than 16 clusters. */
00153   ULONG64 CountClustersLess16;  /* Number of clusters in gaps that are smaller than 16 clusters. */
00154 
00155   /* Counters updated after every Phase, but not before Phase 1 (analyze). */
00156   ULONG64 CountDirectories;      /* Number of analysed subdirectories. */
00157   ULONG64 CountAllFiles;                  /* Number of analysed files. */
00158   ULONG64 CountFragmentedItems;         /* Number of fragmented files. */
00159   ULONG64 CountAllBytes;                   /* Bytes in analysed files. */
00160   ULONG64 CountFragmentedBytes;          /* Bytes in fragmented files. */
00161   ULONG64 CountAllClusters;             /* Clusters in analysed files. */
00162   ULONG64 CountFragmentedClusters;    /* Clusters in fragmented files. */
00163   double AverageDistance;           /* Between end and begin of files. */
00164 
00165   /* Counters used to calculate the percentage of work done. */
00166   ULONG64 PhaseTodo;           /* Number of items to do in this Phase. */
00167   ULONG64 PhaseDone;    /* Number of items already done in this Phase. */
00168 
00169   /* Variables used to throttle the speed. */
00170   int Speed;                          /* Speed as a percentage 1..100. */
00171   LONG64 StartTime;
00172   LONG64 RunningTime;
00173   LONG64 LastCheckpoint;
00174 
00175   /* The callback subroutines. */
00176   void (__cdecl *ShowStatus)(struct DefragDataStruct *Data);
00177   void (__cdecl *ShowMove)(struct ItemStruct *Item, ULONG64 Clusters,
00178     ULONG64 FromLcn, ULONG64 ToLcn, ULONG64 FromVcn);
00179   void (__cdecl *ShowAnalyze)(struct DefragDataStruct *Data,
00180     struct ItemStruct *Item);
00181   void (__cdecl *ShowDebug)(int Level, struct ItemStruct *Item, WCHAR *Message, ...);
00182   void (__cdecl *DrawCluster)(struct DefragDataStruct *Data, ULONG64 ClusterStart,
00183     ULONG64 ClusterEnd, int Color);
00184   void (__cdecl *ClearScreen)(WCHAR *Format, ...);
00185 
00186   /* The array with error messages. */
00187   WCHAR **DebugMsg;
00188   };
00189 
00190 
00191 
00192 
00193 
00194 /* Run the defragger/optimizer. The parameters:
00195    Path:         The name of a disk, mountpoint, directory, or file. It may contain
00196                  wildcards '*' and '?'. If Path is empty or NULL then defrag all the
00197                  mounted, writable, fixed disks on the computer. Some examples:
00198                    c:
00199                    c:\xyz
00200                    c:\xyz\*.txt
00201                    \\?\Volume{08439462-3004-11da-bbca-806d6172696f}
00202    Mode:         0 = Analyze only, do not defragment and do not optimize.
00203                  1 = Analyze and fixup, do not optimize.
00204                  2 = Analyze, fixup, and fast optimization (default).
00205                  3 = Deprecated. Analyze, fixup, and full optimization.
00206                  4 = Analyze and force together.
00207                  5 = Analyze and move to end of disk.
00208                  6 = Analyze and sort files by name.
00209                  7 = Analyze and sort files by size (smallest first).
00210                  8 = Analyze and sort files by last access (newest first).
00211                  9 = Analyze and sort files by last change (oldest first).
00212                  10 = Analyze and sort files by creation time (oldest first).
00213    Speed:        Percentage 0...100 of the normal speed. The defragger will slow down
00214                  by inserting sleep periods so that the wall time is 100% and the
00215                  actual processing time is this percentage. Specify 100 (or zero) to
00216                  run at maximum speed.
00217    FreeSpace:    Percentage 0...100 of the total volume space that must be kept
00218                  free after the MFT and directories.
00219    Excludes:     Array of strings. Each string contains a mask, last string must be
00220                  NULL. If an item (disk, file, directory) matches one of the strings
00221                  in this array then it will be ignored (skipped). Specify NULL to
00222                  disable this feature.
00223    SpaceHogs:    Array of strings. Each string contains a mask, last string must be
00224                  NULL. If an item (file, directory) matches one of the strings in
00225                  this array then it will be marked as a space hog and moved to the end
00226                  of the disk. A build-in list of spacehogs will be added to this list,
00227                  except if one of the strings in the array is "DisableDefaults".
00228    Running:      Pointer to an integer. It is used by the StopJkDefrag() subroutine
00229                  to stop the defragger. If the pointer is NULL then this feature is
00230                  disabled.
00231    RedrawScreen: Pointer to an integer. It can be used by other threads to signal the
00232                  defragger that it must redraw the screen, for example when the window
00233                  is resized. If the pointer is NULL then this feature is disabled.
00234    ShowStatus:   Callback subroutine. Is called just before the defragger starts a
00235                  new Phase, and when it finishes a volume. Specify NULL if the callback
00236                  is not needed.
00237    ShowMove:     Callback subroutine. Is called whenever an item (file, directory) is
00238                  moved on disk. Specify NULL if the callback is not needed.
00239    ShowAnalyze:  Callback subroutine. Is called for every file during analysis.
00240                  This subroutine is called one last time with Item=NULL when analysis
00241                  has finished. Specify NULL if the callback is not needed.
00242    ShowDebug:    Callback subroutine. Is called for every message to show. Specify NULL
00243                  if the callback is not needed.
00244    DrawCluster:  Callback subroutine. Is called to paint a fragment on the screen in
00245                  a color. There are 7 colors, see the .h file. Specify NULL if the
00246                  callback is not needed.
00247    ClearScreen:  Callback subroutine. Is called when the defragger wants to clear the
00248                  diskmap on the screen. Specify NULL if the callback is not needed.
00249    DebugMsg:     Array of textmessages, used when the defragger wants to show a debug
00250                  message. Specify NULL to use the internal default array of english text
00251                  messages.
00252    */
00253 __declspec(dllexport) void RunJkDefrag(WCHAR *Path, int Mode, int Speed,
00254   double FreeSpace, WCHAR **Excludes, WCHAR **SpaceHogs, int *Running,
00255   int *RedrawScreen,
00256   void (__cdecl *ShowStatus)(struct DefragDataStruct *Data),
00257   void (__cdecl *ShowMove)(struct ItemStruct *Item, ULONG64 Clusters,
00258     ULONG64 FromLcn, ULONG64 ToLcn, ULONG64 FromVcn),
00259   void (__cdecl *ShowAnalyze)(struct DefragDataStruct *Data, struct ItemStruct *Item),
00260   void (__cdecl *ShowDebug)(int Level, struct ItemStruct *Item, WCHAR *Message, ...),
00261   void (__cdecl *DrawCluster)(struct DefragDataStruct *Data, ULONG64 ClusterStart,
00262     ULONG64 ClusterEnd, int Color),
00263   void (__cdecl *ClearScreen)(WCHAR *Format, ...),
00264   WCHAR **DebugMsg);
00265 
00266 
00267 
00268 
00269 /* Stop the defragger. Wait for a maximum of TimeOut milliseconds for the
00270    defragger to stop. If TimeOut is zero then wait indefinitely. If TimeOut is
00271    negative then immediately return without waiting.
00272    Note: The "Running" variable must be the same as what was given to the
00273    RunJkDefrag() subroutine. */
00274 __declspec(dllexport) void StopJkDefrag(int *Running, int TimeOut);
00275 
00276 
00277 
00278 
00279 /* Other exported functions that might be useful in programs that use JkDefrag. */
00280 __declspec(dllexport) char *stristr(char *Haystack, char *Needle);
00281 __declspec(dllexport) WCHAR *stristrW(WCHAR *Haystack, WCHAR *Needle);
00282 __declspec(dllexport) void SystemErrorStr(DWORD ErrorCode, WCHAR *Out, size_t Width);
00283 __declspec(dllexport) void ShowHex(struct DefragDataStruct *Data, BYTE *Buffer, ULONG64 Count);
00284 __declspec(dllexport) int MatchMask(WCHAR *String, WCHAR *Mask);
00285 __declspec(dllexport) WCHAR **AddArrayString(WCHAR **Array, WCHAR *NewString);
00286 __declspec(dllexport) WCHAR *GetShortPath(struct DefragDataStruct *Data, struct ItemStruct *Item);
00287 __declspec(dllexport) WCHAR *GetLongPath(struct DefragDataStruct *Data, struct ItemStruct *Item);
00288 __declspec(dllexport) void SlowDown(struct DefragDataStruct *Data);
00289 __declspec(dllexport) ULONG64 GetItemLcn(struct ItemStruct *Item);
00290 __declspec(dllexport) struct ItemStruct *TreeSmallest(struct ItemStruct *Top);
00291 __declspec(dllexport) struct ItemStruct *TreeBiggest(struct ItemStruct *Top);
00292 __declspec(dllexport) struct ItemStruct *TreeFirst(struct ItemStruct *Top, int Direction);
00293 __declspec(dllexport) struct ItemStruct *TreePrev(struct ItemStruct *Here);
00294 __declspec(dllexport) struct ItemStruct *TreeNext(struct ItemStruct *Here);
00295 __declspec(dllexport) struct ItemStruct *TreeNextPrev(struct ItemStruct *Here, int Direction);
00296 __declspec(dllexport) void TreeInsert(struct DefragDataStruct *Data, struct ItemStruct *New);
00297 __declspec(dllexport) void TreeDetach(struct DefragDataStruct *Data, struct ItemStruct *Item);
00298 __declspec(dllexport) void DeleteItemTree(struct ItemStruct *Top);
00299 __declspec(dllexport) int FragmentCount(struct ItemStruct *Item);
00300 __declspec(dllexport) int IsFragmented(struct ItemStruct *Item, ULONG64 Offset, ULONG64 Size);
00301 __declspec(dllexport) void ColorizeItem(struct DefragDataStruct *Data,
00302   struct ItemStruct *Item, ULONG64 BusyOffset, ULONG64 BusySize, int UnDraw);
00303 __declspec(dllexport) void ShowDiskmap(struct DefragDataStruct *Data);
00304 __declspec(dllexport) void CallShowStatus(struct DefragDataStruct *Data, int Phase, int Zone);
00305 
00306 
00307 
00308 
00309 } /* extern "C" */
00310 
00311 
00312 
00313 #endif    /* Include guard */
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines